[Minor] Code cleanup

This commit is contained in:
Robert von Burg 2016-02-10 20:28:11 +01:00
parent 3336cfda1b
commit c947260853
13 changed files with 79 additions and 86 deletions

View File

@ -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;
}

View File

@ -77,7 +77,7 @@ public class SimpleMessageArchive implements IoMessageArchive {
@Override
public synchronized List<IoMessage> getBy(String connectionId) {
List<IoMessage> all = new ArrayList<IoMessage>();
List<IoMessage> 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<IoMessage> getBy(String connectionId, CommandKey key) {
List<IoMessage> all = new ArrayList<IoMessage>();
List<IoMessage> all = new ArrayList<>();
for (IoMessage msg : this.messageArchive) {
if (msg.getConnectionId().equals(connectionId) && msg.getKey().equals(key))
all.add(msg);

View File

@ -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;

View File

@ -125,14 +125,10 @@ public class Version implements Comparable<Version> {
* 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<Version> {
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<Version> {
* 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<Version> {
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;
}

View File

@ -89,7 +89,7 @@ public class Paging<T> {
*/
public static <T> Paging<T> asPage(List<T> list, int pageSize, int page) {
Paging<T> paging = new Paging<T>(pageSize, page);
Paging<T> paging = new Paging<>(pageSize, page);
paging.nrOfElements = list.size();
if (paging.pageSize <= 0 || paging.pageToReturn <= 0) {

View File

@ -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();
}

View File

@ -378,7 +378,7 @@ public class FileHelper {
File file = files.get(i);
// get list of parents for this file
List<File> parents = new ArrayList<File>();
List<File> 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<File> parentsNext = new ArrayList<File>();
List<File> parentsNext = new ArrayList<>();
File parentNext = fileNext.getParentFile();
while (parentNext != null) {
parentsNext.add(parentNext);

View File

@ -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");
}

View File

@ -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");
}
/**

View File

@ -88,8 +88,8 @@ public class ObjectFilter {
* Default constructor initializing the filter
*/
public ObjectFilter() {
this.cache = new HashMap<Object, ObjectCache>();
this.keySet = new HashSet<String>();
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<Object> getAdded(String key) {
List<Object> addedObjects = new LinkedList<Object>();
List<Object> addedObjects = new LinkedList<>();
Collection<ObjectCache> 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 <V extends Object> List<V> getAdded(Class<V> clazz, String key) {
List<V> addedObjects = new LinkedList<V>();
List<V> addedObjects = new LinkedList<>();
Collection<ObjectCache> 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<Object> getUpdated(String key) {
List<Object> updatedObjects = new LinkedList<Object>();
List<Object> updatedObjects = new LinkedList<>();
Collection<ObjectCache> 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 <V extends Object> List<V> getUpdated(Class<V> clazz, String key) {
List<V> updatedObjects = new LinkedList<V>();
List<V> updatedObjects = new LinkedList<>();
Collection<ObjectCache> 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<Object> getRemoved(String key) {
List<Object> removedObjects = new LinkedList<Object>();
List<Object> removedObjects = new LinkedList<>();
Collection<ObjectCache> 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 <V extends Object> List<V> getRemoved(Class<V> clazz, String key) {
List<V> removedObjects = new LinkedList<V>();
List<V> removedObjects = new LinkedList<>();
Collection<ObjectCache> 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 <V extends Object> List<V> getAll(Class<V> clazz, String key) {
List<V> objects = new LinkedList<V>();
List<V> objects = new LinkedList<>();
Collection<ObjectCache> 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 <V extends Object> List<V> getAll(Class<V> clazz) {
List<V> objects = new LinkedList<V>();
List<V> objects = new LinkedList<>();
Collection<ObjectCache> 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<Object> getAll(String key) {
List<Object> allObjects = new LinkedList<Object>();
List<Object> allObjects = new LinkedList<>();
Collection<ObjectCache> 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<ObjectCache> getCache(String key) {
List<ObjectCache> allCache = new LinkedList<ObjectCache>();
List<ObjectCache> allCache = new LinkedList<>();
Collection<ObjectCache> allObjs = this.cache.values();
for (ObjectCache objectCache : allObjs) {
if (objectCache.getKey().equals(key)) {

View File

@ -33,7 +33,7 @@ public class DefaultedHashMapTest {
@Before
public void setUp() {
this.map = new DefaultedHashMap<String, String>("foobar");
this.map = new DefaultedHashMap<>("foobar");
this.map.put("foo", "foofoo");
}

View File

@ -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) {

View File

@ -39,7 +39,7 @@ public class GenerateReverseBaseEncodingAlphabets {
public static String generateReverseAlphabet(String name, byte[] alphabet) {
Map<Byte, Byte> valueToIndex = new HashMap<Byte, Byte>();
Map<Byte, Byte> valueToIndex = new HashMap<>();
for (byte i = 0; i < alphabet.length; i++) {
Byte value = Byte.valueOf(i);
Byte key = Byte.valueOf(alphabet[value]);