Upgrading to use jdk 17
This commit is contained in:
@@ -15,11 +15,13 @@ import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.locusworks.common.immutables.Pair;
|
||||
import net.locusworks.common.immutables.Unit;
|
||||
|
||||
/**
|
||||
* Properties manager class to help load and read properties
|
||||
* @author Isaac Parenteau
|
||||
* @version 1.0.0
|
||||
* @date 02/15/2018
|
||||
* @version 2.0.0
|
||||
* @date 09/17/2023
|
||||
*/
|
||||
public class PropertiesManager {
|
||||
/**
|
||||
@@ -88,7 +90,8 @@ public class PropertiesManager {
|
||||
* @return a map containing the results of the values added
|
||||
*/
|
||||
public static Map<String, String> addConfiguration(Properties to, Properties from) {
|
||||
Map<String, String> results = from.entrySet()
|
||||
|
||||
return from.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> !to.containsKey(entry.getKey()))
|
||||
.map(entry -> {
|
||||
@@ -97,9 +100,7 @@ public class PropertiesManager {
|
||||
to.put(key, value);
|
||||
return new Pair<String, String>(key, value);
|
||||
})
|
||||
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2()));
|
||||
|
||||
return results;
|
||||
.collect(Collectors.toMap(Unit::getValue1, Pair::getValue2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,19 +110,17 @@ public class PropertiesManager {
|
||||
* @return a map containing the results of the values removed
|
||||
*/
|
||||
public static Map<String, String> removeConfiguration(Properties from, Properties comparedTo) {
|
||||
Map<String, String> results = from.keySet()
|
||||
|
||||
return from.keySet()
|
||||
.stream()
|
||||
.filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties
|
||||
.map(key -> new Pair<String, String>(String.valueOf(key), String.valueOf(from.get(key))))
|
||||
.collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered
|
||||
.toList() //Create a list of paired items (key value) of the items that were filtered
|
||||
.stream()
|
||||
.map(pair -> { //remove those pairs from the from properties
|
||||
.peek(pair -> { //remove those pairs from the from properties
|
||||
from.remove(pair.getValue1());
|
||||
return pair;
|
||||
})
|
||||
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed
|
||||
|
||||
return results;
|
||||
.collect(Collectors.toMap(Unit::getValue1, Pair::getValue2));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -7,10 +7,7 @@ import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.*;
|
||||
import java.util.Base64;
|
||||
|
||||
import net.locusworks.common.utils.RandomString;
|
||||
@@ -21,7 +18,7 @@ import static net.locusworks.common.Charsets.UTF_8;
|
||||
/**
|
||||
* AES encryption/decryption class
|
||||
* This class will encrypt/decrypt data. The encryption key is never known.
|
||||
* Instead it is is generated by the provided seed. As long as the seed stays the same
|
||||
* Instead, it is generated by the provided seed. As long as the seed stays the same
|
||||
* the key will remain the same and the encryption/decryption will work. This
|
||||
* provides and added security.
|
||||
* @author Isaac Parenteau
|
||||
@@ -62,7 +59,6 @@ public class AES {
|
||||
* @param seed Seed to initialize SecureRandom with
|
||||
* @return SecureRandom object
|
||||
* @throws NoSuchAlgorithmException thrown when algorithm can't be used
|
||||
* @throws NoSuchProviderException thrown when the provider cant be found
|
||||
*/
|
||||
private static SecureRandom getSecureRandom(String seed) throws NoSuchAlgorithmException {
|
||||
SecureRandom sr = SecureRandom.getInstance(ALGORITHM);
|
||||
@@ -73,7 +69,7 @@ public class AES {
|
||||
/**
|
||||
* Initialize the aes engine
|
||||
* @param key secret key to use
|
||||
* @param sr
|
||||
* @param sr the secure random class
|
||||
*/
|
||||
private void init(final byte[] key, SecureRandom sr) {
|
||||
try {
|
||||
@@ -81,8 +77,7 @@ public class AES {
|
||||
this.secretKeySpec = new SecretKeySpec(key, ENCRYPTION_TYPE);
|
||||
this.ivParamSpec = new IvParameterSpec(RandomString.getBytes(16, sr));
|
||||
} catch (Exception ex) {
|
||||
System.err.println(ex);
|
||||
throw new IllegalArgumentException("Unable to initalize encryption:", ex);
|
||||
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +117,7 @@ public class AES {
|
||||
}
|
||||
}
|
||||
|
||||
public AES setSeed(String seed) {
|
||||
public AES withSeed(String seed) {
|
||||
if (this.seed == null || !this.seed.equals(seed)) {
|
||||
initSecureKey(seed);
|
||||
}
|
||||
@@ -144,9 +139,7 @@ public class AES {
|
||||
}
|
||||
|
||||
public static AES createInstance(String seed) {
|
||||
AES aes = new AES();
|
||||
aes.setSeed(seed);
|
||||
return aes;
|
||||
return new AES().withSeed(seed);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws NoSuchAlgorithmException {
|
||||
|
@@ -1,13 +1,14 @@
|
||||
package net.locusworks.common.crypto;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.security.PrivateKey;
|
||||
|
||||
import net.locusworks.common.Charsets;
|
||||
|
||||
public class AESKey implements PrivateKey {
|
||||
|
||||
private static final long serialVersionUID = -8452357427706386362L;
|
||||
private String seed;
|
||||
@Serial private static final long serialVersionUID = -8452357427706386362L;
|
||||
private final String seed;
|
||||
|
||||
public AESKey(String seed) {
|
||||
this.seed = seed;
|
||||
|
@@ -114,7 +114,7 @@ public class KeyFile implements AutoCloseable {
|
||||
dosh.writeInt(item.length);
|
||||
dosh.write(item);
|
||||
}));
|
||||
data = String.format("ssh-rsa", dosh.base64Encoded(), this.description);
|
||||
data = String.format("ssh-rsa %s %s", dosh.base64Encoded(), this.description);
|
||||
IOUtils.writeStringToFile(fileName, data);
|
||||
}
|
||||
break;
|
||||
@@ -186,9 +186,9 @@ public class KeyFile implements AutoCloseable {
|
||||
}
|
||||
|
||||
private static class KeySpecHelper {
|
||||
private KeySpec keySpec;
|
||||
private boolean isPrivate;
|
||||
private EncryptionType encryptionType;
|
||||
private final KeySpec keySpec;
|
||||
private final boolean isPrivate;
|
||||
private final EncryptionType encryptionType;
|
||||
|
||||
public KeySpecHelper(KeySpec keySpec, boolean isPrivate, EncryptionType encryptionType) {
|
||||
super();
|
||||
|
@@ -37,8 +37,7 @@ public class SSHEncodedKeySpec extends EncodedKeySpec {
|
||||
checkArguments(SSH_MARKER.equals(marker), "Looking for marker %s but received %s", SSH_MARKER, marker);
|
||||
BigInteger publicExponent = new BigInteger(readLengthFirst(stream));
|
||||
BigInteger modulus = new BigInteger(readLengthFirst(stream));
|
||||
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
|
||||
return keySpec;
|
||||
return new RSAPublicKeySpec(modulus, publicExponent);
|
||||
} catch (Exception ex) {
|
||||
throw new InvalidKeySpecException(ex);
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package net.locusworks.common.exceptions;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/***
|
||||
* Custom exception class for the patch repository
|
||||
* @author Isaac Parenteau
|
||||
@@ -8,7 +10,7 @@ package net.locusworks.common.exceptions;
|
||||
public class ApplicationException extends Exception {
|
||||
private final Integer code;
|
||||
boolean success = false;
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
public static ApplicationException egregiousServer() {
|
||||
return new ApplicationException(9001, "Something went wrong. Please see logs for details");
|
||||
|
@@ -48,10 +48,8 @@ public class Triplet<V1, V2, V3> extends Pair<V1, V2> {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof Triplet)) return false;
|
||||
|
||||
Triplet<?, ?, ?> otherTriplet = (Triplet<?, ?, ?>)other;
|
||||
|
||||
if (!(other instanceof Triplet<?, ?, ?> otherTriplet)) return false;
|
||||
|
||||
return super.equals(otherTriplet) && this.getValue3().equals(otherTriplet.getValue3());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,6 @@ import java.util.Iterator;
|
||||
public interface AutoCloseableIterator<T> extends Iterator<T>, AutoCloseable {
|
||||
|
||||
@Override
|
||||
public void close();
|
||||
void close();
|
||||
|
||||
}
|
||||
|
@@ -172,7 +172,7 @@ public class IOUtils {
|
||||
*/
|
||||
public static void copy(final InputStream input, final Writer output, final Charset inputEncoding)
|
||||
throws IOException {
|
||||
final InputStreamReader in = new InputStreamReader(input, inputEncoding.toString());
|
||||
final InputStreamReader in = new InputStreamReader(input, inputEncoding);
|
||||
copy(in, output);
|
||||
}
|
||||
|
||||
|
@@ -20,7 +20,7 @@ import com.google.gson.GsonBuilder;
|
||||
*/
|
||||
public class ObjectMapperHelper {
|
||||
|
||||
private static ObjectMapper mapper;
|
||||
private static final ObjectMapper mapper;
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
@@ -145,4 +145,4 @@ public class ObjectMapperHelper {
|
||||
return new ObjectMapperListResults<>(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,11 @@
|
||||
package net.locusworks.common.properties;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ImmutableProperties extends Properties {
|
||||
|
||||
private static final long serialVersionUID = 65942088008978137L;
|
||||
@Serial private static final long serialVersionUID = 65942088008978137L;
|
||||
|
||||
public ImmutableProperties() {
|
||||
super();
|
||||
@@ -13,8 +14,8 @@ public class ImmutableProperties extends Properties {
|
||||
public ImmutableProperties(Properties props) {
|
||||
super();
|
||||
if (props == null || props.isEmpty()) return;
|
||||
|
||||
props.entrySet().forEach(item -> this.put(item.getKey(), item.getValue()));
|
||||
|
||||
this.putAll(props);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -24,15 +24,7 @@ package net.locusworks.common.properties;
|
||||
*
|
||||
*
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.*;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
@@ -123,7 +115,7 @@ public class OrderedProperties extends LinkedHashMap<Object,Object> {
|
||||
/**
|
||||
* use serialVersionUID from JDK 1.1.X for interoperability
|
||||
*/
|
||||
private static final long serialVersionUID = 4112578634023874840L;
|
||||
@Serial private static final long serialVersionUID = 4112578634023874840L;
|
||||
|
||||
/**
|
||||
* A property list that contains default values for any keys not
|
||||
|
@@ -8,7 +8,7 @@ import java.util.Base64;
|
||||
|
||||
import net.locusworks.common.Charsets;
|
||||
|
||||
public class DataOutputStreamHelper extends DataOutputStream implements AutoCloseable{
|
||||
public class DataOutputStreamHelper extends DataOutputStream implements AutoCloseable {
|
||||
|
||||
public DataOutputStreamHelper() {
|
||||
this(new ByteArrayOutputStream());
|
||||
@@ -43,7 +43,7 @@ public class DataOutputStreamHelper extends DataOutputStream implements AutoClos
|
||||
try {
|
||||
super.out.close();
|
||||
super.out = null;
|
||||
} catch (Exception ex) {}
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serial;
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
@@ -29,7 +30,7 @@ public class DateTimeStampSerializer extends StdSerializer<Date> {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -4753139740916300831L;
|
||||
@Serial private static final long serialVersionUID = -4753139740916300831L;
|
||||
|
||||
public DateTimeStampSerializer() {
|
||||
this(null);
|
||||
|
@@ -9,51 +9,51 @@ import static net.locusworks.common.utils.Checks.checkArguments;
|
||||
import static net.locusworks.common.utils.Checks.checkNotNull;
|
||||
|
||||
public class Splitter {
|
||||
|
||||
|
||||
private String splitSeq;
|
||||
private boolean omitEmptyStrings = false;
|
||||
private int partition;
|
||||
private int limit;
|
||||
|
||||
|
||||
private static Splitter splitter;
|
||||
|
||||
|
||||
private Splitter(String seq) {
|
||||
this.splitSeq = seq;
|
||||
}
|
||||
|
||||
|
||||
private Splitter(int partition) {
|
||||
this.partition = partition;
|
||||
}
|
||||
|
||||
|
||||
public Splitter omitEmptyStrings() {
|
||||
this.omitEmptyStrings = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Splitter withLimit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public MapSplitter withKeyValueSeparator(String separator) {
|
||||
checkArguments(!Utils.isEmptyString(separator), "Key value separator cannot be empty or null");
|
||||
return new MapSplitter(this, separator);
|
||||
}
|
||||
|
||||
|
||||
public String[] splitToArray(String sentence) {
|
||||
List<String> list = split(sentence);
|
||||
return list.toArray(new String[list.size()]);
|
||||
return list.toArray(new String[0]);
|
||||
}
|
||||
|
||||
|
||||
public List<String> split(String sentence) {
|
||||
checkArguments(!Utils.isEmptyString(sentence), "provided value is null or empty");
|
||||
List<String> list = new ArrayList<>();
|
||||
|
||||
|
||||
if (!Utils.isEmptyString(splitSeq))
|
||||
populateForTrimmer(sentence, list);
|
||||
else
|
||||
else
|
||||
populateForFixedWidth(sentence, list);
|
||||
|
||||
|
||||
return limit > 0 ? list.subList(0, limit) : list;
|
||||
}
|
||||
|
||||
@@ -66,64 +66,67 @@ public class Splitter {
|
||||
|
||||
private void populateForTrimmer(String sentence, List<String> list) {
|
||||
for (String s : sentence.split(splitSeq)) {
|
||||
if (s == null || (omitEmptyStrings && s.trim().isEmpty())) continue;
|
||||
if (s == null || (omitEmptyStrings && s.trim().isEmpty()))
|
||||
continue;
|
||||
list.add(s.trim());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Splitter fixedLengthSplit(int partition) {
|
||||
checkArguments(partition > 0, "Partition has to be greater than 0");
|
||||
splitter = new Splitter(partition);
|
||||
return splitter;
|
||||
}
|
||||
|
||||
|
||||
public static Splitter on(String split) {
|
||||
checkNotNull(split, "Split value provided was null");
|
||||
splitter = new Splitter(split);
|
||||
return splitter;
|
||||
}
|
||||
|
||||
|
||||
public static Splitter onNewLine() {
|
||||
return on("\\r?\\n");
|
||||
}
|
||||
|
||||
|
||||
public static Splitter onSpace() {
|
||||
return on(" ");
|
||||
}
|
||||
|
||||
|
||||
public static class MapSplitter {
|
||||
|
||||
private Splitter splitter;
|
||||
|
||||
private final Splitter splitter;
|
||||
private String separator;
|
||||
private boolean skipInvalid = false;
|
||||
|
||||
|
||||
private MapSplitter(Splitter splitter, String separator) {
|
||||
checkNotNull(splitter, "Splitter cannot be null");
|
||||
checkArguments(!Utils.isEmptyString(separator), "Key value separator cannot be empty or null");
|
||||
checkArguments(!Utils.isEmptyString(separator),
|
||||
"Key value separator cannot be empty or null");
|
||||
this.splitter = splitter;
|
||||
this.separator = separator;
|
||||
}
|
||||
|
||||
|
||||
public MapSplitter skipInvalidKeyValues() {
|
||||
this.skipInvalid = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Map<String, String> split(String sentence) {
|
||||
checkArguments(!Utils.isEmptyString(sentence), "provided value is null or empty");
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
|
||||
|
||||
for (String s : splitter.split(sentence)) {
|
||||
String[] keyValue = s.split(separator);
|
||||
try {
|
||||
checkArguments(keyValue.length == 2, "invalid length found for key value mapping");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
if (!skipInvalid) throw ex;
|
||||
if (!skipInvalid)
|
||||
throw ex;
|
||||
continue;
|
||||
}
|
||||
map.put(keyValue[0], keyValue[1]);
|
||||
}
|
||||
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package net.locusworks.common.utils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -47,7 +48,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
E firstVal = values[0];
|
||||
boolean equal = true;
|
||||
boolean equal = !or;
|
||||
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
if (or)
|
||||
@@ -100,7 +101,7 @@ public class Utils {
|
||||
*
|
||||
* @param <K> the type parameter
|
||||
* @param <V> the type parameter
|
||||
* @param mapClass The map class to map to i.e HashMap, TreeMap etc
|
||||
* @param mapClass The map class to map to i.e. HashMap, TreeMap etc
|
||||
* @param mapKey the map key
|
||||
* @param mapValue the map value
|
||||
* @param data The data to place in the map
|
||||
@@ -109,9 +110,9 @@ public class Utils {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <K, V> Map<K, V> buildMap(Class<?> mapClass, Class<K> mapKey, Class<V> mapValue, Object... data) {
|
||||
Map<K, V> results = new LinkedHashMap<K, V>();
|
||||
Map<K, V> results;
|
||||
try {
|
||||
results = (Map<K, V>) mapClass.newInstance();
|
||||
results = (Map<K, V>) mapClass.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Unable to create instance of " + mapClass.getSimpleName());
|
||||
}
|
||||
@@ -121,7 +122,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
Object key = null;
|
||||
Integer step = -1;
|
||||
int step = -1;
|
||||
|
||||
for (Object value : data) {
|
||||
switch(++step % 2) {
|
||||
@@ -131,7 +132,7 @@ public class Utils {
|
||||
}
|
||||
if (value instanceof Class) {
|
||||
try {
|
||||
value = ((Class<?>)value).newInstance();
|
||||
value = ((Class<?>)value).getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Unable to create new instance of " + value);
|
||||
}
|
||||
@@ -150,7 +151,7 @@ public class Utils {
|
||||
results.put((K) key, (V) value);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalAccessError("Caculation for step was not a multiple of two. This shouldn't have happened");
|
||||
throw new IllegalAccessError("Calculation for step was not a multiple of two. This shouldn't have happened");
|
||||
}
|
||||
}
|
||||
return results;
|
||||
@@ -166,10 +167,10 @@ public class Utils {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <V> Set<V> buildSet(Class<?> setClass, Class<V> setValue, Object... data) {
|
||||
Set<V> results = null;
|
||||
Set<V> results;
|
||||
|
||||
try {
|
||||
results = (Set<V>) setClass.newInstance();
|
||||
results = (Set<V>) setClass.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Unable to create instance of " + setClass.getSimpleName());
|
||||
}
|
||||
@@ -192,21 +193,22 @@ public class Utils {
|
||||
* @return - Returns a HashMap using the data.
|
||||
*/
|
||||
public static HashMap<String, String> buildStringHashMap(String... data) {
|
||||
return new HashMap<String, String>(buildMap(HashMap.class, String.class, String.class, (Object[]) data));
|
||||
return new HashMap<>(buildMap(HashMap.class, String.class, String.class, (Object[]) data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone list list.
|
||||
* Clone list.
|
||||
*
|
||||
* @param <E> the type parameter
|
||||
* @param list the list
|
||||
*
|
||||
* @return the list
|
||||
* @throws IllegalAccessException throw when an object in the list cannot be accessed through reflection
|
||||
* @throws InstantiationException thrown when an boject in the list cannot be instantiated through reflection
|
||||
* @throws InstantiationException thrown when an object in the list cannot be instantiated through reflection
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E> List<E> cloneList(List<E> list) throws InstantiationException, IllegalAccessException {
|
||||
public static <E> List<E> cloneList(List<E> list)
|
||||
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
List<E> clone = new ArrayList<>();
|
||||
for (E item : list) {
|
||||
clone.add((E) cloneObject(item));
|
||||
@@ -221,8 +223,9 @@ public class Utils {
|
||||
* @throws IllegalAccessException thrown when the filed cannot be access
|
||||
* @throws InstantiationException Thrown when the object cannot be instantiated
|
||||
*/
|
||||
public static Object cloneObject(Object obj) throws InstantiationException, IllegalAccessException {
|
||||
Object clone = obj.getClass().newInstance();
|
||||
public static Object cloneObject(Object obj)
|
||||
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||
Object clone = obj.getClass().getDeclaredConstructor().newInstance();
|
||||
for (Field field : obj.getClass().getDeclaredFields()) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
@@ -241,9 +244,7 @@ public class Utils {
|
||||
field.set(clone, cloneObject(field.get(obj)));
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
continue;
|
||||
}
|
||||
} catch (NullPointerException ignored) { }
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
@@ -329,7 +330,7 @@ public class Utils {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E, K> Set<E> extractFieldToSet(String fieldName, Iterable<K> list) {
|
||||
Set<E> newSet = new LinkedHashSet<>();
|
||||
Field field = null;
|
||||
Field field;
|
||||
for (K item : Utils.safeList(list)) {
|
||||
try {
|
||||
field = getField(item.getClass(), fieldName);
|
||||
@@ -358,9 +359,9 @@ public class Utils {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E> List<E> filterList(String fieldName, Object filter, Iterable<E> list) {
|
||||
List<E> newList = new ArrayList<E>();
|
||||
List<E> newList = new ArrayList<>();
|
||||
|
||||
Field field = null;
|
||||
Field field;
|
||||
|
||||
for (E item : safeList(list)) {
|
||||
try {
|
||||
@@ -384,7 +385,6 @@ public class Utils {
|
||||
|
||||
/**
|
||||
* Find a value within the list.
|
||||
*
|
||||
* Public method of type E called "findValue".
|
||||
* The method takes a parameter of type E called "itemToFind" and a String parameter called "fieldName" and a parameter of type List called "list".
|
||||
* The method loops through each item in the list and tries to get the class name and return it as an item.
|
||||
@@ -394,7 +394,7 @@ public class Utils {
|
||||
* @param list - The list to search
|
||||
* @param <E> - The expected class of item to be found
|
||||
* @param <K> - The expected class of the Iterable list
|
||||
* @return - Returns returns the value of the specified field or null if there is no value.
|
||||
* @return - Returns the value of the specified field or null if there is no value.
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "unlikely-arg-type" })
|
||||
@@ -422,11 +422,10 @@ public class Utils {
|
||||
|
||||
/**
|
||||
* Find values list.
|
||||
*
|
||||
* Public method of type List called "findValues".
|
||||
* The method takes a E type parameter called "valueToFind" and a String parameter called "fieldName" and a List parameter called "list".
|
||||
* The method takes an E type parameter called "valueToFind" and a String parameter called "fieldName" and a List parameter called "list".
|
||||
* The method creates a new array list called "tmpList".
|
||||
* The method then iterates through the list and gets all of the classes as items.
|
||||
* The method then iterates through the list and gets all the classes as items.
|
||||
* If it can't find the fields, it will throw an illegal argument exception.
|
||||
* @param valueToFind - The value to find
|
||||
* @param fieldName - The field name
|
||||
@@ -461,7 +460,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* A replacement for String.format. Allows for to many parameters or to few
|
||||
* A replacement for String.format. Allows for to many parameters or too few
|
||||
* Replaces {} in the string in order.
|
||||
*
|
||||
* @param message the message
|
||||
@@ -506,8 +505,8 @@ public class Utils {
|
||||
|
||||
/**
|
||||
* Use reflection to get the field values
|
||||
* @param clazz
|
||||
* @param fieldName
|
||||
* @param clazz the class to find the field
|
||||
* @param fieldName the field name
|
||||
* @return field
|
||||
*/
|
||||
private static Field getField(Class<?> clazz, String fieldName) {
|
||||
@@ -551,7 +550,7 @@ public class Utils {
|
||||
* @return map value
|
||||
*/
|
||||
public static <T, K> T getMapValue(Map<K, T> map, K key, T defaultValue) {
|
||||
return map.containsKey(key) ? map.get(key) : defaultValue;
|
||||
return map.getOrDefault(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -574,7 +573,7 @@ public class Utils {
|
||||
* For collections checks to see if they are null or empty
|
||||
* for all others just checks if they are null
|
||||
* @param <V> the expected class type of the object
|
||||
* @param value The value to check
|
||||
* @param values The value to check
|
||||
* @return true if the value is not valid, false otherwise
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -588,7 +587,7 @@ public class Utils {
|
||||
* For collections checks to see if they are not null and not empty
|
||||
* for all others just checks if they are not null
|
||||
* @param <V> the expected class type of the object
|
||||
* @param value The value to check
|
||||
* @param values The value to check
|
||||
* @return true if the value is valid, false otherwise
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -635,7 +634,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* List to set set.
|
||||
* List to set.
|
||||
*
|
||||
* @param <E> the type parameter
|
||||
* @param valueSet the value set
|
||||
@@ -648,13 +647,12 @@ public class Utils {
|
||||
|
||||
/**
|
||||
* Creates an array from a collection
|
||||
*
|
||||
* Public method of type array called "makeArray".
|
||||
* The method takes a Collection object parameter called "collection" and a Class object parameter called "clazz".
|
||||
* The method first checks to see if the Collection object is null. If so, it returns null.
|
||||
* The method then creates an array called "results" and populates it with a new instance of "clazz" and the size of the collection.
|
||||
* The method then creates a Int variable called "index" and sets the value to zero.
|
||||
* The method then iterates through the collection and adds all of the items to the results array.
|
||||
* The method then creates an Int variable called "index" and sets the value to zero.
|
||||
* The method then iterates through the collection and adds all the items to the results array.
|
||||
* The method then returns the results array.
|
||||
* @param <E> - The type parameter
|
||||
* @param collection - The collection
|
||||
@@ -683,7 +681,7 @@ public class Utils {
|
||||
* The method takes a Collection object parameter called "collection".
|
||||
* The method checks to see if the Collection Object is null. If so, it returns null.
|
||||
* Otherwise, it creates an ArrayList object called "list".
|
||||
* It then iterates through the Collection Object and adds all of the collection items to that list.
|
||||
* It then iterates through the Collection Object and adds all the collection items to that list.
|
||||
* The method then returns the list.
|
||||
* @param <E> - The type parameter
|
||||
* @param collection - The collection
|
||||
@@ -693,11 +691,7 @@ public class Utils {
|
||||
if (collection == null) {
|
||||
return null;
|
||||
}
|
||||
List<E> list = new ArrayList<>();
|
||||
for (E item : collection) {
|
||||
list.add(item);
|
||||
}
|
||||
return list;
|
||||
return new ArrayList<>(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -716,13 +710,7 @@ public class Utils {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<E> newList = new ArrayList<>();
|
||||
|
||||
for (E item : array) {
|
||||
newList.add(item);
|
||||
}
|
||||
|
||||
return newList;
|
||||
return List.of(array);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -752,11 +740,11 @@ public class Utils {
|
||||
* Public method of type Set called "makeSet".
|
||||
* The method takes an Iterable object parameter called "collection".
|
||||
* The method first creates a new HashSet called "set".
|
||||
* The method then iterates through the collection and adds all of the items from it to the HashSet.
|
||||
* The method then iterates through the collection and adds all the items from it to the HashSet.
|
||||
* The method then returns the HashSet.
|
||||
* @param <E> - The type parameter
|
||||
* @param collection - The iterable object name.
|
||||
* @return - Returns the HashSet with all of the items of the collection inside of it.
|
||||
* @return - Returns the HashSet with all the items of the collection inside of it.
|
||||
*/
|
||||
public static <E> Set<E> makeSet(Iterable<E> collection) {
|
||||
Set<E> set = new HashSet<>();
|
||||
@@ -767,7 +755,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Map to list list.
|
||||
* Map to list.
|
||||
*
|
||||
* @param <K> the type parameter
|
||||
* @param <V> the type parameter
|
||||
@@ -776,17 +764,12 @@ public class Utils {
|
||||
* @return the list
|
||||
*/
|
||||
public static <K, V> List<V> mapToList(Map<K, V> map) {
|
||||
List<V> list = new ArrayList<>();
|
||||
|
||||
for (V key : map.values()) {
|
||||
list.add(key);
|
||||
}
|
||||
|
||||
return list;
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Map to set set.
|
||||
* Map to set.
|
||||
*
|
||||
* @param <K> the type parameter
|
||||
* @param <V> the type parameter
|
||||
@@ -795,13 +778,8 @@ public class Utils {
|
||||
* @return the set
|
||||
*/
|
||||
public static <K, V> Set<V> mapToSet(Map<K, V> map) {
|
||||
Set<V> list = new HashSet<>();
|
||||
|
||||
for (V key : map.values()) {
|
||||
list.add(key);
|
||||
}
|
||||
|
||||
return list;
|
||||
return new HashSet<>(map.values());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -833,7 +811,7 @@ public class Utils {
|
||||
*/
|
||||
public static <E> Collection<E> safeList(Collection<E> list) {
|
||||
if (!validateValue(list)) {
|
||||
return new ArrayList<E>();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -864,7 +842,7 @@ public class Utils {
|
||||
*/
|
||||
public static <E> Iterable<E> safeList(Iterable<E> list) {
|
||||
if (!validateValue(list)) {
|
||||
return new ArrayList<E>();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -879,7 +857,7 @@ public class Utils {
|
||||
*/
|
||||
public static <E> List<E> safeList(List<E> list) {
|
||||
if (!validateValue(list)) {
|
||||
return new ArrayList<E>();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -892,17 +870,17 @@ public class Utils {
|
||||
*/
|
||||
public static <E> Set<E> safeSet(Set<E> set) {
|
||||
if (set == null) {
|
||||
return new HashSet<E>();
|
||||
return new HashSet<>();
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a string is not blank or null.
|
||||
* if its not blank it will return the string
|
||||
* if it's not blank it will return the string
|
||||
* otherwise it will return an empty string
|
||||
* @param string The string to check
|
||||
* @return the passed in string if its not null either empty string
|
||||
* @return the passed in string if it's not null either empty string
|
||||
*/
|
||||
public static String safeString(String string) {
|
||||
return isEmptyString(string) ? "" : string;
|
||||
@@ -917,18 +895,14 @@ public class Utils {
|
||||
* @return the to list
|
||||
*/
|
||||
public static <E> List<E> setToList(Set<E> valueSet) {
|
||||
List<E> list = new ArrayList<>();
|
||||
for (E value : valueSet) {
|
||||
list.add(value);
|
||||
}
|
||||
return list;
|
||||
return new ArrayList<>(valueSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a String into an integer without exception
|
||||
* @param value The string value to convert
|
||||
* @param defaultValue The default value to return if the string cant be converted
|
||||
* @return the integer representation of the passed in string or the default value if an exception occured
|
||||
* @return the integer representation of the passed in string or the default value if an exception occurred
|
||||
*/
|
||||
public static Integer toInteger(String value, Integer defaultValue) {
|
||||
try {
|
||||
@@ -952,7 +926,7 @@ public class Utils {
|
||||
}
|
||||
|
||||
public static <E> List<E> toList(Iterable<E> iterable) {
|
||||
List<E> list = new ArrayList<E>();
|
||||
List<E> list = new ArrayList<>();
|
||||
for (E item : iterable) {
|
||||
list.add(item);
|
||||
}
|
||||
@@ -969,17 +943,13 @@ public class Utils {
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <E> Set<E> toSet(E... values) {
|
||||
Set<E> set = new LinkedHashSet<>();
|
||||
for (E value : values) {
|
||||
set.add(value);
|
||||
}
|
||||
|
||||
return set;
|
||||
return new LinkedHashSet<>(Arrays.asList(values));
|
||||
}
|
||||
|
||||
public static List<Byte> toByteList(byte[] bytes) {
|
||||
return IntStream.range(0, bytes.length)
|
||||
.mapToObj(index -> new Byte(bytes[index]))
|
||||
.mapToObj(index -> bytes[index])
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -1008,7 +978,7 @@ public class Utils {
|
||||
Optional<E> test = StreamUtils.asStream(iterable)
|
||||
.filter(item -> count.getAndIncrement() == index)
|
||||
.findFirst();
|
||||
return test.isPresent() ? test.get() : null;
|
||||
return test.orElse(null);
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> handleExceptionWrapper(ThrowingConsumer<T, E> consumer) {
|
||||
@@ -1026,13 +996,13 @@ public class Utils {
|
||||
* The method takes an Object parameter called "value".
|
||||
* The method loops through and checks to see if value is empty.
|
||||
* If so, it will return that the value is not null and that it is not empty.
|
||||
* Otherwise it will check to see the value is an instance of Map.
|
||||
* Otherwise, it will check to see the value is an instance of Map.
|
||||
* If so, it will return that the value is not null and that it is not empty.
|
||||
* Otherwise it will check to see if value is an instance of a String.
|
||||
* Otherwise, it will check to see if value is an instance of a String.
|
||||
* If so, it will return that the value is not null and that it is not empty.
|
||||
* Otherwise it will check to see if value is an instance of a Collection Object.
|
||||
* If so, it will return that the value is not null and if the value.size is greater than zero.
|
||||
* Otherwise it will just return that the value is not null and not empty.
|
||||
* Otherwise, it will check to see if value is an instance of a Collection Object.
|
||||
* If so, it will return that the value is not null and if the value size is greater than zero.
|
||||
* Otherwise, it will just return that the value is not null and not empty.
|
||||
* @param <V> - The type parameter
|
||||
* @param value - Value to validate
|
||||
* @return - Returns true if it is valid; false otherwise
|
||||
@@ -1047,25 +1017,21 @@ public class Utils {
|
||||
} else if (value instanceof Map){
|
||||
return !((Map)value).isEmpty();
|
||||
} else if (value instanceof Boolean) {
|
||||
return ((Boolean)value) == true;
|
||||
return ((Boolean) value);
|
||||
}
|
||||
else if (!(value instanceof String)) {
|
||||
return value != null;
|
||||
}
|
||||
|
||||
return !value.toString().trim().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates all given values.
|
||||
*
|
||||
* Public method of type Boolean called "validateValues".
|
||||
* The method takes multiple V type objects called "objectToValidate".
|
||||
* The method creates a boolean type variable called "valid" and sets it to true.
|
||||
* The method then loops through all the objects and validates each value in the object.
|
||||
* The method sets valid equal to the result of true and false.
|
||||
* If the method cannot validate the object values, it will throw an exception. If so, it returns false.
|
||||
* Otherwise, the method will return the variable called "valid'.
|
||||
* Otherwise, the method will return the variable called "valid".
|
||||
* @param <V> - The type parameter
|
||||
* @param objectsToValidate - Objects to validate
|
||||
* @return - Returns true if all objects are valid false otherwise.
|
||||
@@ -1084,7 +1050,7 @@ public class Utils {
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <V> boolean validateValues(boolean or, V... objectsToValidate) {
|
||||
boolean valid = true;
|
||||
boolean valid = !or;
|
||||
try {
|
||||
for (Object obj : objectsToValidate) {
|
||||
if (or && validateValue(obj)) {
|
||||
@@ -1103,14 +1069,14 @@ public class Utils {
|
||||
public static boolean isJUnitRunning() {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
Optional<String> junit = StreamUtils.asStream(stackTrace)
|
||||
.map(element -> element.getClassName())
|
||||
.map(StackTraceElement::getClassName)
|
||||
.filter(className -> className.startsWith("org.junit."))
|
||||
.findFirst();
|
||||
return junit.isPresent();
|
||||
}
|
||||
|
||||
private static Set<Class<?>> getWrapperTypes() {
|
||||
Set<Class<?>> ret = new HashSet<Class<?>>();
|
||||
Set<Class<?>> ret = new HashSet<>();
|
||||
ret.add(Boolean.class);
|
||||
ret.add(Character.class);
|
||||
ret.add(Byte.class);
|
||||
@@ -1122,4 +1088,4 @@ public class Utils {
|
||||
ret.add(String.class);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user