From c78659741e77aaf7afc43932189840a83ba49741 Mon Sep 17 00:00:00 2001 From: Matt Magoffin Date: Tue, 4 Feb 2025 09:41:48 +1300 Subject: [PATCH] Code quality improvements surfaced by IntelliJ. --- .../datum/support/AsyncDatumCollector.java | 10 ++--- .../support/BasicDatumStreamsAccessor.java | 4 +- .../central/datum/support/DatumUtils.java | 19 ++------- ...ralLocationDatumMapPropertySerializer.java | 33 +++++++-------- ...GeneralNodeDatumMapPropertySerializer.java | 42 +++++++++---------- 5 files changed, 46 insertions(+), 62 deletions(-) diff --git a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/AsyncDatumCollector.java b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/AsyncDatumCollector.java index ae7891377..f607a91c4 100644 --- a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/AsyncDatumCollector.java +++ b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/AsyncDatumCollector.java @@ -28,6 +28,7 @@ import java.lang.Thread.UncaughtExceptionHandler; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; @@ -99,7 +100,7 @@ public class AsyncDatumCollector implements CacheEntryCreatedListener datumCache, DatumWr this.stats = requireNonNullArgument(stats, "stats"); this.concurrency = DEFAULT_CONCURRENCY; this.shutdownWaitSecs = DEFAULT_SHUTDOWN_WAIT_SECS; - this.listenerConfiguration = new MutableCacheEntryListenerConfiguration( + this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>( new SingletonFactory>(this), null, false, false); setQueueSize(DEFAULT_QUEUE_SIZE); @@ -317,8 +318,7 @@ public Result performPingTest() throws Exception { for ( BasicCount s : BasicCount.values() ) { statMap.put(s.toString(), stats.get(s)); } - if ( datumCache instanceof BufferingDelegatingCache ) { - BufferingDelegatingCache buf = (BufferingDelegatingCache) datumCache; + if ( datumCache instanceof BufferingDelegatingCache buf ) { statMap.put("BufferSize", buf.getInternalSize()); statMap.put("BufferCapacity", buf.getInternalCapacity()); statMap.put("BufferWatermark", buf.getInternalSizeWatermark()); @@ -504,7 +504,7 @@ public void onRemoved( long c = stats.get(BasicCount.BufferRemovals); if ( stats.getLogFrequency() > 0 && ((c % stats.getLogFrequency()) == 0) ) { Set allKeys = StreamSupport.stream(datumCache.spliterator(), false) - .filter(e -> e != null).map(e -> e.getKey()).collect(Collectors.toSet()); + .filter(Objects::nonNull).map(Entry::getKey).collect(Collectors.toSet()); log.trace("Datum cache keys: {}", allKeys); } } diff --git a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/BasicDatumStreamsAccessor.java b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/BasicDatumStreamsAccessor.java index f586d50d2..278ab9e07 100644 --- a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/BasicDatumStreamsAccessor.java +++ b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/BasicDatumStreamsAccessor.java @@ -81,9 +81,7 @@ private Map> sortedDatumStreams() { map.computeIfAbsent(d.getSourceId(), k -> new ArrayList<>(8)).add(d); } for ( List list : map.values() ) { - Collections.sort(list, (l, r) -> { - return r.getTimestamp().compareTo(l.getTimestamp()); - }); + list.sort((l, r) -> r.getTimestamp().compareTo(l.getTimestamp())); } timeSortedDatumBySource = map; } diff --git a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/DatumUtils.java b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/DatumUtils.java index 97dd595a8..25d779739 100644 --- a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/DatumUtils.java +++ b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/DatumUtils.java @@ -23,7 +23,6 @@ package net.solarnetwork.central.datum.support; import java.math.BigDecimal; -import java.util.Iterator; import java.util.Map; import java.util.Set; import org.springframework.util.PathMatcher; @@ -64,8 +63,8 @@ private DatumUtils() { * a default value to use if {@code o} is null or if any * error occurs serializing the object to JSON * @return the JSON string - * @since 1.1 * @see JsonUtils#getJSONString(Object, String) + * @since 1.1 */ public static String getJSONString(final Object o, final String defaultValue) { return JsonUtils.getJSONString(o, defaultValue); @@ -85,8 +84,8 @@ public static String getJSONString(final Object o, final String defaultValue) { * @param clazz * the type of Object to map the JSON into * @return the object - * @since 1.1 * @see JsonUtils#getJSONString(Object, String) + * @since 1.1 */ public static T getObjectFromJSON(final String json, Class clazz) { return JsonUtils.getObjectFromJSON(json, clazz); @@ -115,12 +114,7 @@ public static Set filterNodeSources(Set sources, Pat || !pathMatcher.isPattern(pattern) ) { return sources; } - for ( Iterator itr = sources.iterator(); itr.hasNext(); ) { - NodeSourcePK pk = itr.next(); - if ( !pathMatcher.match(pattern, pk.getSourceId()) ) { - itr.remove(); - } - } + sources.removeIf(pk -> !pathMatcher.match(pattern, pk.getSourceId())); return sources; } @@ -147,12 +141,7 @@ public static Set filterSources(Set sources, PathMatcher pathMat || !pathMatcher.isPattern(pattern) ) { return sources; } - for ( Iterator itr = sources.iterator(); itr.hasNext(); ) { - String source = itr.next(); - if ( !pathMatcher.match(pattern, source) ) { - itr.remove(); - } - } + sources.removeIf(source -> !pathMatcher.match(pattern, source)); return sources; } diff --git a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralLocationDatumMapPropertySerializer.java b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralLocationDatumMapPropertySerializer.java index c0bc5440f..1230d2d9e 100644 --- a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralLocationDatumMapPropertySerializer.java +++ b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralLocationDatumMapPropertySerializer.java @@ -1,21 +1,21 @@ /* ================================================================== * GeneralLocationDatumMapPropertySerializer.java - Oct 17, 2014 2:31:27 PM - * + * * Copyright 2007-2014 SolarNetwork.net Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * ================================================================== */ @@ -35,7 +35,7 @@ * {@link ReportingDatum} API is also supported (those properties will be added * to the output if a {@link GeneralLocationDatum} subclass implements that * interface). - * + * * @author matt * @version 2.0 */ @@ -44,10 +44,9 @@ public class GeneralLocationDatumMapPropertySerializer implements PropertySerial @Override public Object serialize(Object data, String propertyName, Object propertyValue) { GeneralLocationDatum datum = (GeneralLocationDatum) propertyValue; - Map props = new LinkedHashMap(8); + Map props = new LinkedHashMap<>(8); props.put("created", datum.getCreated()); - if ( datum instanceof ReportingDatum ) { - ReportingDatum rd = (ReportingDatum) datum; + if ( datum instanceof ReportingDatum rd ) { props.put("localDate", rd.getLocalDate()); props.put("localTime", rd.getLocalTime()); } diff --git a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralNodeDatumMapPropertySerializer.java b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralNodeDatumMapPropertySerializer.java index 17dead888..3482abbee 100644 --- a/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralNodeDatumMapPropertySerializer.java +++ b/solarnet/datum/src/main/java/net/solarnetwork/central/datum/support/GeneralNodeDatumMapPropertySerializer.java @@ -1,21 +1,21 @@ /* ================================================================== * GeneralNodeDatumMapPropertySerializer.java - Sep 5, 2014 7:12:44 AM - * + * * Copyright 2007-2014 SolarNetwork.net Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * ================================================================== */ @@ -33,17 +33,17 @@ /** * Serialize a {@link GeneralNodeDatum} to a {@code Map}. - * + * *

* The {@link ReportingDatum} API is also supported (those properties will be * added to the output if a {@link GeneralNodeDatum} subclass implements that - * interface). Similarly the {@link ReadingDatum} API is supported and all start - * and final properties will be added to the resulting map, adding the + * interface). Similarly, the {@link ReadingDatum} API is supported and all + * start and final properties will be added to the resulting map, adding the * {@link ReadingDatum#START_PROPERTY_SUFFIX} and * {@link ReadingDatum#FINAL_PROPERTY_SUFFIX} property name suffix as * appropriate. *

- * + * * @author matt * @version 2.0 */ @@ -52,15 +52,13 @@ public class GeneralNodeDatumMapPropertySerializer implements PropertySerializer @Override public Object serialize(Object data, String propertyName, Object propertyValue) { GeneralNodeDatum datum = (GeneralNodeDatum) propertyValue; - Map props = new LinkedHashMap(8); + Map props = new LinkedHashMap<>(8); props.put("created", datum.getCreated()); - if ( datum instanceof ReportingDatum ) { - ReportingDatum rd = (ReportingDatum) datum; + if ( datum instanceof ReportingDatum rd ) { props.put("localDate", rd.getLocalDate()); props.put("localTime", rd.getLocalTime()); } - if ( datum instanceof ReadingDatum ) { - ReadingDatum rd = (ReadingDatum) datum; + if ( datum instanceof ReadingDatum rd ) { Map dataStart = rd.getSampleDataStart(); if ( dataStart != null ) { for ( Map.Entry me : dataStart.entrySet() ) {