diff --git a/src/main/java/ch/eitchnet/communication/ConnectionMode.java b/src/main/java/ch/eitchnet/communication/ConnectionMode.java index 59d5d89d6..8cf2e2640 100644 --- a/src/main/java/ch/eitchnet/communication/ConnectionMode.java +++ b/src/main/java/ch/eitchnet/communication/ConnectionMode.java @@ -39,6 +39,7 @@ public enum ConnectionMode { * or do any other kind of work */ OFF { + @Override public boolean isSimulation() { return false; } @@ -50,6 +51,7 @@ public enum ConnectionMode { * re-established should an {@link IOException} occur */ ON { + @Override public boolean isSimulation() { return false; } @@ -60,6 +62,7 @@ public enum ConnectionMode { * {@link CommunicationConnection} accepts messages, but silently swallows them, instead of processing them */ SIMULATION { + @Override public boolean isSimulation() { return true; } diff --git a/src/main/java/ch/eitchnet/communication/SimpleMessageArchive.java b/src/main/java/ch/eitchnet/communication/SimpleMessageArchive.java index 27414f3db..00ad303b6 100644 --- a/src/main/java/ch/eitchnet/communication/SimpleMessageArchive.java +++ b/src/main/java/ch/eitchnet/communication/SimpleMessageArchive.java @@ -77,7 +77,7 @@ public class SimpleMessageArchive implements IoMessageArchive { @Override public synchronized List getBy(String connectionId) { - List all = new ArrayList(); + List all = new ArrayList<>(); for (IoMessage msg : this.messageArchive) { if (msg.getConnectionId().equals(connectionId)) all.add(msg); @@ -87,7 +87,7 @@ public class SimpleMessageArchive implements IoMessageArchive { @Override public synchronized List getBy(String connectionId, CommandKey key) { - List all = new ArrayList(); + List all = new ArrayList<>(); for (IoMessage msg : this.messageArchive) { if (msg.getConnectionId().equals(connectionId) && msg.getKey().equals(key)) all.add(msg); diff --git a/src/main/java/ch/eitchnet/db/DbSchemaVersionCheck.java b/src/main/java/ch/eitchnet/db/DbSchemaVersionCheck.java index d621a4c58..8f870fec0 100644 --- a/src/main/java/ch/eitchnet/db/DbSchemaVersionCheck.java +++ b/src/main/java/ch/eitchnet/db/DbSchemaVersionCheck.java @@ -162,9 +162,10 @@ public class DbSchemaVersionCheck { Version currentVersion = null; try (PreparedStatement st = con.prepareStatement(sql)) { st.setString(1, app); - ResultSet rs = st.executeQuery(); - if (rs.next()) - currentVersion = Version.valueOf(rs.getString(2)); + try (ResultSet rs = st.executeQuery()) { + if (rs.next()) + currentVersion = Version.valueOf(rs.getString(2)); + } } return currentVersion; diff --git a/src/main/java/ch/eitchnet/utils/Version.java b/src/main/java/ch/eitchnet/utils/Version.java index e3507bbfb..7b6b93dd6 100644 --- a/src/main/java/ch/eitchnet/utils/Version.java +++ b/src/main/java/ch/eitchnet/utils/Version.java @@ -125,14 +125,10 @@ public class Version implements Comparable { * If the numerical components are negative or the qualifier string is invalid. */ public Version(final int major, final int minor, final int micro, String qualifier, boolean osgiStyle) { - if (qualifier == null) { - qualifier = ""; - } - this.major = major; this.minor = minor; this.micro = micro; - this.qualifier = qualifier; + this.qualifier = qualifier == null ? "" : qualifier; this.versionString = null; validate(); } @@ -155,8 +151,8 @@ public class Version implements Comparable { String qual = StringHelper.EMPTY; try { - StringTokenizer st = new StringTokenizer(version, SEPARATOR + MAVEN_QUALIFIER_SEPARATOR - + OSGI_QUALIFIER_SEPARATOR, true); + StringTokenizer st = new StringTokenizer(version, + SEPARATOR + MAVEN_QUALIFIER_SEPARATOR + OSGI_QUALIFIER_SEPARATOR, true); maj = Integer.parseInt(st.nextToken()); if (st.hasMoreTokens()) { // minor @@ -248,16 +244,14 @@ public class Version implements Comparable { * If {@code version} is improperly formatted. */ public static Version valueOf(String version) { - if (version == null) { + if (version == null) return emptyVersion; - } - version = version.trim(); - if (version.length() == 0) { + String trimmedVersion = version.trim(); + if (trimmedVersion.length() == 0) return emptyVersion; - } - return new Version(version); + return new Version(trimmedVersion); } /** @@ -478,12 +472,11 @@ public class Version implements Comparable { private String createQualifier(boolean withOsgiStyle) { if (this.qualifier.equals(MAVEN_SNAPSHOT_QUALIFIER) || this.qualifier.equals(OSGI_SNAPSHOT_QUALIFIER)) { - if (withOsgiStyle) { + if (withOsgiStyle) return OSGI_SNAPSHOT_QUALIFIER; - } else { - return MAVEN_SNAPSHOT_QUALIFIER; - } + return MAVEN_SNAPSHOT_QUALIFIER; } + return this.qualifier; } diff --git a/src/main/java/ch/eitchnet/utils/collections/Paging.java b/src/main/java/ch/eitchnet/utils/collections/Paging.java index 7031c34e4..dde14cd4f 100644 --- a/src/main/java/ch/eitchnet/utils/collections/Paging.java +++ b/src/main/java/ch/eitchnet/utils/collections/Paging.java @@ -89,7 +89,7 @@ public class Paging { */ public static Paging asPage(List list, int pageSize, int page) { - Paging paging = new Paging(pageSize, page); + Paging paging = new Paging<>(pageSize, page); paging.nrOfElements = list.size(); if (paging.pageSize <= 0 || paging.pageToReturn <= 0) { diff --git a/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java b/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java index 35bb99a6d..a498c20cb 100644 --- a/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java @@ -54,10 +54,8 @@ public class ExceptionHelper { * @return */ public static String getExceptionMessageWithCauses(Throwable t) { - - if (t.getCause() == null) { + if (t.getCause() == null) return getExceptionMessage(t); - } String root = getExceptionMessageWithCauses(t.getCause()); return getExceptionMessage(t) + "\n" + root; @@ -87,10 +85,8 @@ public class ExceptionHelper { * @return a string representation of the given {@link Throwable}'s messages including causes */ public static String formatExceptionMessage(Throwable t) { - - if (t.getCause() == null) { + if (t.getCause() == null) return getExceptionMessage(t); - } String root = formatExceptionMessage(t.getCause()); return getExceptionMessage(t) + "\ncause:\n" + root; @@ -99,12 +95,13 @@ public class ExceptionHelper { /** * Returns the root cause for the given {@link Throwable} * - * @param t + * @param throwable * the {@link Throwable} for which to get the root cause * * @return the root cause of the given {@link Throwable} */ - public static Throwable getRootCause(Throwable t) { + public static Throwable getRootCause(Throwable throwable) { + Throwable t = throwable; while (t.getCause() != null) { t = t.getCause(); } diff --git a/src/main/java/ch/eitchnet/utils/helper/FileHelper.java b/src/main/java/ch/eitchnet/utils/helper/FileHelper.java index 397ed6382..57cf58bc9 100644 --- a/src/main/java/ch/eitchnet/utils/helper/FileHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/FileHelper.java @@ -378,7 +378,7 @@ public class FileHelper { File file = files.get(i); // get list of parents for this file - List parents = new ArrayList(); + List parents = new ArrayList<>(); File parent = file.getParentFile(); while (parent != null) { parents.add(parent); @@ -389,7 +389,7 @@ public class FileHelper { // and now the same for the next file File fileNext = files.get(i + 1); - List parentsNext = new ArrayList(); + List parentsNext = new ArrayList<>(); File parentNext = fileNext.getParentFile(); while (parentNext != null) { parentsNext.add(parentNext); diff --git a/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Duration.java b/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Duration.java index 61788c737..61e63f94d 100644 --- a/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Duration.java +++ b/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Duration.java @@ -54,9 +54,13 @@ public class ISO8601Duration implements DurationFormat { * @author gattom */ public enum TimeDuration { - SECOND(1000, 'S'), MINUTE(60 * SECOND.duration(), 'M'), HOUR(60 * MINUTE.duration(), 'H'), DAY(24 * HOUR - .duration(), 'D'), WEEK(7 * DAY.duration(), 'W'), MONTH(30 * DAY.duration(), 'M'), YEAR(12 * MONTH - .duration(), 'Y'); + SECOND(1000, 'S'), + MINUTE(60 * SECOND.duration(), 'M'), + HOUR(60 * MINUTE.duration(), 'H'), + DAY(24 * HOUR.duration(), 'D'), + WEEK(7 * DAY.duration(), 'W'), + MONTH(30 * DAY.duration(), 'M'), + YEAR(12 * MONTH.duration(), 'Y'); final long millis; final char isoChar; @@ -74,17 +78,15 @@ public class ISO8601Duration implements DurationFormat { char duration = isostring.charAt(unitIndex); switch (duration) { case 'S': - if (isostring.substring(0, unitIndex).contains("T")) { + if (isostring.substring(0, unitIndex).contains("T")) return SECOND; - } else - throw new NumberFormatException(duration - + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1S)"); + throw new NumberFormatException( + duration + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1S)"); case 'H': - if (isostring.substring(0, unitIndex).contains("T")) { + if (isostring.substring(0, unitIndex).contains("T")) return HOUR; - } else - throw new NumberFormatException(duration - + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1H)"); + throw new NumberFormatException( + duration + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1H)"); case 'D': return DAY; case 'W': @@ -92,11 +94,9 @@ public class ISO8601Duration implements DurationFormat { case 'Y': return YEAR; case 'M': - if (isostring.substring(0, unitIndex).contains("T")) { + if (isostring.substring(0, unitIndex).contains("T")) return MINUTE; - } else { - return MONTH; - } + return MONTH; default: throw new NumberFormatException(duration + " is not a valid unit of time in ISO8601"); } diff --git a/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Worktime.java b/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Worktime.java index 31dde2a1c..e107a7be7 100644 --- a/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Worktime.java +++ b/src/main/java/ch/eitchnet/utils/iso8601/ISO8601Worktime.java @@ -73,17 +73,15 @@ public class ISO8601Worktime implements WorktimeFormat { char duration = isostring.charAt(unitIndex); switch (duration) { case 'S': - if (isostring.substring(0, unitIndex).contains("T")) { + if (isostring.substring(0, unitIndex).contains("T")) return SECOND; - } else - throw new NumberFormatException(duration - + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1S)"); + throw new NumberFormatException( + duration + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1S)"); case 'H': - if (isostring.substring(0, unitIndex).contains("T")) { + if (isostring.substring(0, unitIndex).contains("T")) return HOUR; - } else - throw new NumberFormatException(duration - + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1H)"); + throw new NumberFormatException( + duration + " is not a valid unit of time in ISO8601 without a preceeding T (e.g.: PT1H)"); case 'M': return MINUTE; default: @@ -143,11 +141,9 @@ public class ISO8601Worktime implements WorktimeFormat { } while (newposition < s.length()); return newResult; - - } else { - throw new NumberFormatException(s + " cannot be parsed to ISO 8601 Duration"); } + throw new NumberFormatException(s + " cannot be parsed to ISO 8601 Duration"); } /** diff --git a/src/main/java/ch/eitchnet/utils/objectfilter/ObjectFilter.java b/src/main/java/ch/eitchnet/utils/objectfilter/ObjectFilter.java index b2282e9ca..6ea65d1ac 100644 --- a/src/main/java/ch/eitchnet/utils/objectfilter/ObjectFilter.java +++ b/src/main/java/ch/eitchnet/utils/objectfilter/ObjectFilter.java @@ -88,8 +88,8 @@ public class ObjectFilter { * Default constructor initializing the filter */ public ObjectFilter() { - this.cache = new HashMap(); - this.keySet = new HashSet(); + this.cache = new HashMap<>(); + this.keySet = new HashSet<>(); } /** @@ -439,7 +439,7 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be added. */ public List getAdded(String key) { - List addedObjects = new LinkedList(); + List addedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.ADD && (objectCache.getKey().equals(key))) { @@ -460,7 +460,7 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be added. */ public List getAdded(Class clazz, String key) { - List addedObjects = new LinkedList(); + List addedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.ADD && (objectCache.getKey().equals(key))) { @@ -483,7 +483,7 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be updated. */ public List getUpdated(String key) { - List updatedObjects = new LinkedList(); + List updatedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.MODIFY && (objectCache.getKey().equals(key))) { @@ -504,7 +504,7 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be updated. */ public List getUpdated(Class clazz, String key) { - List updatedObjects = new LinkedList(); + List updatedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.MODIFY && (objectCache.getKey().equals(key))) { @@ -527,7 +527,7 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getRemoved(String key) { - List removedObjects = new LinkedList(); + List removedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.REMOVE && (objectCache.getKey().equals(key))) { @@ -548,7 +548,7 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getRemoved(Class clazz, String key) { - List removedObjects = new LinkedList(); + List removedObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getOperation() == Operation.REMOVE && (objectCache.getKey().equals(key))) { @@ -573,7 +573,7 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getAll(Class clazz, String key) { - List objects = new LinkedList(); + List objects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getKey().equals(key)) { @@ -596,7 +596,7 @@ public class ObjectFilter { * @return The list of all objects that of the given class */ public List getAll(Class clazz) { - List objects = new LinkedList(); + List objects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getObject().getClass() == clazz) { @@ -618,7 +618,7 @@ public class ObjectFilter { * @return The list of objects matching the given key. */ public List getAll(String key) { - List allObjects = new LinkedList(); + List allObjects = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getKey().equals(key)) { @@ -638,7 +638,7 @@ public class ObjectFilter { * @return The list of objects matching the given key. */ public List getCache(String key) { - List allCache = new LinkedList(); + List allCache = new LinkedList<>(); Collection allObjs = this.cache.values(); for (ObjectCache objectCache : allObjs) { if (objectCache.getKey().equals(key)) { diff --git a/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java b/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java index ddf7c6569..1048d10e1 100644 --- a/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java +++ b/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java @@ -33,7 +33,7 @@ public class DefaultedHashMapTest { @Before public void setUp() { - this.map = new DefaultedHashMap("foobar"); + this.map = new DefaultedHashMap<>("foobar"); this.map.put("foo", "foofoo"); } diff --git a/src/test/java/ch/eitchnet/utils/helper/AesCryptoHelperTest.java b/src/test/java/ch/eitchnet/utils/helper/AesCryptoHelperTest.java index 8e6162040..31b069146 100644 --- a/src/test/java/ch/eitchnet/utils/helper/AesCryptoHelperTest.java +++ b/src/test/java/ch/eitchnet/utils/helper/AesCryptoHelperTest.java @@ -37,26 +37,27 @@ public class AesCryptoHelperTest { // encrypt data ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream(); - OutputStream outputStream = AesCryptoHelper.wrapEncrypt(password, salt, encryptedOut); - outputStream.write(clearTextBytes); - outputStream.flush(); - outputStream.close(); + try (OutputStream outputStream = AesCryptoHelper.wrapEncrypt(password, salt, encryptedOut)) { + outputStream.write(clearTextBytes); + outputStream.flush(); + } // decrypt data byte[] encryptedBytes = encryptedOut.toByteArray(); ByteArrayInputStream encryptedIn = new ByteArrayInputStream(encryptedBytes); - InputStream inputStream = AesCryptoHelper.wrapDecrypt(password, salt, encryptedIn); + try (InputStream inputStream = AesCryptoHelper.wrapDecrypt(password, salt, encryptedIn)) { - ByteArrayOutputStream decryptedOut = new ByteArrayOutputStream(); - byte[] readBuffer = new byte[64]; - int read = 0; - while ((read = inputStream.read(readBuffer)) != -1) { - decryptedOut.write(readBuffer, 0, read); + ByteArrayOutputStream decryptedOut = new ByteArrayOutputStream(); + byte[] readBuffer = new byte[64]; + int read = 0; + while ((read = inputStream.read(readBuffer)) != -1) { + decryptedOut.write(readBuffer, 0, read); + } + + byte[] decryptedBytes = decryptedOut.toByteArray(); + assertArrayEquals(clearTextBytes, decryptedBytes); } - byte[] decryptedBytes = decryptedOut.toByteArray(); - - assertArrayEquals(clearTextBytes, decryptedBytes); } catch (RuntimeException e) { if (ExceptionHelper.getRootCause(e).getMessage().equals("Illegal key size or default parameters")) logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!"); @@ -75,6 +76,7 @@ public class AesCryptoHelperTest { byte[] decryptedBytes = AesCryptoHelper.decrypt(password, salt, encryptedBytes); assertArrayEquals(clearTextBytes, decryptedBytes); + } catch (RuntimeException e) { if (ExceptionHelper.getRootCause(e).getMessage().equals("Illegal key size or default parameters")) logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!"); @@ -141,6 +143,7 @@ public class AesCryptoHelperTest { String inputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(clearTextFileS))); String doutputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(decryptedFileS))); + assertEquals(inputSha256, doutputSha256); } catch (RuntimeException e) { diff --git a/src/test/java/ch/eitchnet/utils/helper/GenerateReverseBaseEncodingAlphabets.java b/src/test/java/ch/eitchnet/utils/helper/GenerateReverseBaseEncodingAlphabets.java index 5a492a2d7..e013c5659 100644 --- a/src/test/java/ch/eitchnet/utils/helper/GenerateReverseBaseEncodingAlphabets.java +++ b/src/test/java/ch/eitchnet/utils/helper/GenerateReverseBaseEncodingAlphabets.java @@ -39,7 +39,7 @@ public class GenerateReverseBaseEncodingAlphabets { public static String generateReverseAlphabet(String name, byte[] alphabet) { - Map valueToIndex = new HashMap(); + Map valueToIndex = new HashMap<>(); for (byte i = 0; i < alphabet.length; i++) { Byte value = Byte.valueOf(i); Byte key = Byte.valueOf(alphabet[value]);