diff --git a/src/main/java/com/aerospike/mapper/tools/AeroMapper.java b/src/main/java/com/aerospike/mapper/tools/AeroMapper.java index dfdf7cb..97d7819 100644 --- a/src/main/java/com/aerospike/mapper/tools/AeroMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/AeroMapper.java @@ -1,29 +1,7 @@ package com.aerospike.mapper.tools; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.Function; - -import javax.validation.constraints.NotNull; - -import org.apache.commons.lang3.StringUtils; - -import com.aerospike.client.AerospikeException; -import com.aerospike.client.Bin; -import com.aerospike.client.IAerospikeClient; -import com.aerospike.client.Key; -import com.aerospike.client.Record; -import com.aerospike.client.Value; -import com.aerospike.client.policy.BatchPolicy; -import com.aerospike.client.policy.Policy; -import com.aerospike.client.policy.QueryPolicy; -import com.aerospike.client.policy.RecordExistsAction; -import com.aerospike.client.policy.ScanPolicy; -import com.aerospike.client.policy.WritePolicy; +import com.aerospike.client.*; +import com.aerospike.client.policy.*; import com.aerospike.client.query.RecordSet; import com.aerospike.client.query.Statement; import com.aerospike.mapper.tools.ClassCache.PolicyType; @@ -32,18 +10,26 @@ import com.aerospike.mapper.tools.TypeUtils.AnnotatedType; import com.aerospike.mapper.tools.configuration.ClassConfig; import com.aerospike.mapper.tools.configuration.Configuration; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.apache.commons.lang3.StringUtils; + +import javax.validation.constraints.NotNull; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Function; public class AeroMapper { - IAerospikeClient mClient; + final IAerospikeClient mClient; public static class Builder { - private AeroMapper mapper; + private final AeroMapper mapper; private List> classesToPreload = null; public Builder(IAerospikeClient client) { @@ -52,9 +38,8 @@ public Builder(IAerospikeClient client) { } /** - * Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation - * - * @param converter + * Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation. + * @param converter The custom converter * @return this object */ public Builder addConverter(Object converter) { @@ -72,22 +57,22 @@ public Builder preLoadClass(Class clazz) { return this; } - public Builder withConfigurationFile(File file) throws JsonParseException, JsonMappingException, IOException { + public Builder withConfigurationFile(File file) throws IOException { return this.withConfigurationFile(file, false); } - public Builder withConfigurationFile(File file, boolean allowsInvalid) throws JsonParseException, JsonMappingException, IOException { + public Builder withConfigurationFile(File file, boolean allowsInvalid) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); Configuration configuration = objectMapper.readValue(file, Configuration.class); this.loadConfiguration(configuration, allowsInvalid); return this; } - public Builder withConfiguration(String configurationYaml) throws JsonMappingException, JsonProcessingException { + public Builder withConfiguration(String configurationYaml) throws JsonProcessingException { return this.withConfiguration(configurationYaml, false); } - public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonMappingException, JsonProcessingException { + public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); Configuration configuration = objectMapper.readValue(configurationYaml, Configuration.class); this.loadConfiguration(configuration, allowsInvalid); @@ -105,7 +90,7 @@ private void loadConfiguration(@NotNull Configuration configuration, boolean all try { Class.forName(config.getClassName()); } catch (ClassNotFoundException e) { - throw new AerospikeException("Canot find a class with name " + name); + throw new AerospikeException("Cannot find a class with name " + name); } } } @@ -224,8 +209,8 @@ private void save(WritePolicy writePolicy, @NotNull T object, RecordExistsAc /** * Translate a Java object to an Aerospike format object. Note that this could potentially have performance issues as * the type information of the passed object must be determined on every call. - * @param obj - * @return + * @param obj A given Java object. + * @return An Aerospike format object. */ public Object translateToAerospike(Object obj) { if (obj == null) { @@ -238,8 +223,8 @@ public Object translateToAerospike(Object obj) { /** * Translate an Aerospike object to a Java object. Note that this could potentially have performance issues as * the type information of the passed object must be determined on every call. - * @param obj - * @return + * @param obj A given Java object. + * @return An Aerospike format object. */ @SuppressWarnings("unchecked") public T translateFromAerospike(@NotNull Object obj, @NotNull Class expectedClazz) { @@ -259,8 +244,8 @@ public T translateFromAerospike(@NotNull Object obj, @NotNull Class expec * * Not that no transactionality is implied by this method -- if any of the save methods fail, the exception will be * thrown without trying the other objects, nor attempting to roll back previously saved objects - * @param object - * @throws AerospikeException + * @param objects One or two objects to save. + * @throws AerospikeException an AerospikeException will be thrown in case of an error. */ public void save(@NotNull Object ... objects) throws AerospikeException { for (Object thisObject : objects) { @@ -271,8 +256,8 @@ public void save(@NotNull Object ... objects) throws AerospikeException { /** * Save an object in the database. This method will perform a REPLACE on the existing record so any existing * data will be overwritten by the data in the passed object - * @param object - * @throws AerospikeException + * @param object The object to save. + * @throws AerospikeException an AerospikeException will be thrown in case of an error. */ public void save(@NotNull Object object, String ...binNames) throws AerospikeException { save(null, object, RecordExistsAction.REPLACE, binNames); @@ -281,19 +266,19 @@ public void save(@NotNull Object object, String ...binNames) throws AerospikeExc /** * Save an object in the database with the given WritePolicy. This write policy will override any other set writePolicy so * is effectively an upsert operation - * @param object - * @throws AerospikeException + * @param writePolicy The write policy for the save operation. + * @param object The object to save. + * @throws AerospikeException an AerospikeException will be thrown in case of an error. */ public void save(@NotNull WritePolicy writePolicy, @NotNull Object object, String ...binNames) throws AerospikeException { save(writePolicy, object, null, binNames); } - /** * Updates the object in the database, merging the record with the existing record. This uses the RecordExistsAction * of UPDATE. If bins are specified, only bins with the passed names will be updated (or all of them if null is passed) - * @param object - * @throws AerospikeException + * @param object The object to update. + * @throws AerospikeException an AerospikeException will be thrown in case of an error. */ public void update(@NotNull Object object, String ... binNames) throws AerospikeException { save(null, object, RecordExistsAction.UPDATE, binNames); @@ -351,7 +336,7 @@ public T read(@NotNull Class clazz, @NotNull Object userKey) throws Aeros } /** - * This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userkey) instead + * This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userKey) instead */ public T read(@NotNull Class clazz, @NotNull Object userKey, boolean resolveDependencies) throws AerospikeException { ClassCacheEntry entry = getEntryAndValidateNamespace(clazz); @@ -360,7 +345,7 @@ public T read(@NotNull Class clazz, @NotNull Object userKey, boolean reso return read(null, clazz, key, entry, resolveDependencies); } - private T read(Policy readPolicy, @NotNull Class clazz, @NotNull Key key, @NotNull ClassCacheEntry entry, boolean resolveDepenencies) { + private T read(Policy readPolicy, @NotNull Class clazz, @NotNull Key key, @NotNull ClassCacheEntry entry, boolean resolveDependencies) { if (readPolicy == null) { readPolicy = entry.getReadPolicy(); } @@ -371,8 +356,7 @@ private T read(Policy readPolicy, @NotNull Class clazz, @NotNull Key key, } else { try { ThreadLocalKeySaver.save(key); - T result = convertToObject(clazz, record, entry, resolveDepenencies); - return result; + return convertToObject(clazz, record, entry, resolveDependencies); } catch (ReflectiveOperationException e) { throw new AerospikeException(e); } @@ -413,7 +397,7 @@ public T[] read(BatchPolicy batchPolicy, @NotNull Class clazz, @NotNull O keys[i] = new Key(entry.getNamespace(), set, Value.get(entry.translateKeyToAerospikeKey(userKeys[i]))); } } - + return this.readBatch(batchPolicy, clazz, keys, entry); } @@ -495,13 +479,13 @@ public boolean delete(WritePolicy writePolicy, @NotNull Object object) throws Ae * * These operation can all be done without having the full set of transactions * @param the type of the elements in the list. - * @param object - * @param binName - * @param elementClazz - * @return + * @param object The object that will use as a base for the virtual list. + * @param binName The Aerospike bin name. + * @param elementClazz The class of the elements in the list. + * @return A virtual list. */ public VirtualList asBackedList(@NotNull Object object, @NotNull String binName, Class elementClazz) { - return new VirtualList(this, object, binName, elementClazz); + return new VirtualList<>(this, object, binName, elementClazz); } /** @@ -521,13 +505,14 @@ public VirtualList asBackedList(@NotNull Object object, @NotNull String b * * These operation can all be done without having the full set of transactions * @param the type of the elements in the list. - * @param object - * @param binName - * @param elementClazz - * @return + * @param owningClazz Used for the definitions of how to map the list elements. + * @param key The key to map the object to the database. + * @param binName The Aerospike bin name. + * @param elementClazz The class of the elements in the list. + * @return A virtual list. */ public VirtualList asBackedList(@NotNull Class owningClazz, @NotNull Object key, @NotNull String binName, Class elementClazz) { - return new VirtualList(this, owningClazz, key, binName, elementClazz); + return new VirtualList<>(this, owningClazz, key, binName, elementClazz); } public void find(@NotNull Class clazz, Function function) throws AerospikeException { @@ -565,11 +550,10 @@ public void find(@NotNull Class clazz, Function function) thr /** * Given a record loaded from Aerospike and a class type, attempt to convert the record to * an instance of the passed class. - * @param - * @param clazz - * @param record - * @return - * @throws ReflectiveOperationException + * @param clazz The class type to convert the Aerospike record to. + * @param record The Aerospike record to convert. + * @return A virtual list. + * @throws AerospikeException an AerospikeException will be thrown in case of an encountering a ReflectiveOperationException. */ public T convertToObject(Class clazz, Record record) { try { diff --git a/src/main/java/com/aerospike/mapper/tools/ClassCache.java b/src/main/java/com/aerospike/mapper/tools/ClassCache.java index dac24e0..59e5383 100644 --- a/src/main/java/com/aerospike/mapper/tools/ClassCache.java +++ b/src/main/java/com/aerospike/mapper/tools/ClassCache.java @@ -16,7 +16,7 @@ import com.aerospike.mapper.tools.configuration.Configuration; public class ClassCache { - private static ClassCache instance = new ClassCache(); + private static final ClassCache instance = new ClassCache(); public static ClassCache getInstance() { return instance; @@ -30,12 +30,12 @@ enum PolicyType { QUERY } - private Map, ClassCacheEntry> cacheMap = new HashMap<>(); - private Map classesConfig = new HashMap<>(); - private Map defaultPolicies = new HashMap<>(); - private Map storedNameToCacheEntry = new HashMap<>(); - private Map, Policy>> childrenPolicies = new HashMap<>(); - private Map, Policy>> specificPolicies = new HashMap<>(); + private final Map, ClassCacheEntry> cacheMap = new HashMap<>(); + private final Map classesConfig = new HashMap<>(); + private final Map defaultPolicies = new HashMap<>(); + private final Map storedNameToCacheEntry = new HashMap<>(); + private final Map, Policy>> childrenPolicies = new HashMap<>(); + private final Map, Policy>> specificPolicies = new HashMap<>(); private ClassCache() { for (PolicyType thisType : PolicyType.values()) { @@ -51,12 +51,12 @@ public ClassCacheEntry loadClass(@NotNull Class clazz, AeroMapper mapp ClassCacheEntry entry = cacheMap.get(clazz); if (entry == null) { try { - entry = new ClassCacheEntry(clazz, mapper, getClassConfig(clazz), - determinePolicy(clazz, PolicyType.READ), - (WritePolicy)determinePolicy(clazz, PolicyType.WRITE), - (BatchPolicy)determinePolicy(clazz, PolicyType.BATCH), - (QueryPolicy)determinePolicy(clazz, PolicyType.QUERY), - (ScanPolicy)determinePolicy(clazz, PolicyType.SCAN)); + entry = new ClassCacheEntry<>(clazz, mapper, getClassConfig(clazz), + determinePolicy(clazz, PolicyType.READ), + (WritePolicy) determinePolicy(clazz, PolicyType.WRITE), + (BatchPolicy) determinePolicy(clazz, PolicyType.BATCH), + (QueryPolicy) determinePolicy(clazz, PolicyType.QUERY), + (ScanPolicy) determinePolicy(clazz, PolicyType.SCAN)); } catch (IllegalArgumentException iae) { return null; @@ -114,7 +114,7 @@ private Policy determinePolicy(@NotNull Class clazz, @NotNull PolicyType poli if (result != null) { return result; } - // Otherwise, iterate up class heirarchy looking for the policy. + // Otherwise, iterate up class hierarchy looking for the policy. Class thisClass = clazz; while (thisClass != null) { Policy aPolicy = childrenPolicies.get(policyType).get(thisClass); diff --git a/src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java b/src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java index 1867799..0a0af03 100644 --- a/src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java +++ b/src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java @@ -1,49 +1,24 @@ package com.aerospike.mapper.tools; -import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Parameter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -import javax.validation.constraints.NotNull; - -import org.apache.commons.lang3.StringUtils; - import com.aerospike.client.AerospikeException; import com.aerospike.client.Bin; -import com.aerospike.client.Key; import com.aerospike.client.Record; -import com.aerospike.client.Value; import com.aerospike.client.cdt.MapOrder; -import com.aerospike.client.policy.BatchPolicy; -import com.aerospike.client.policy.Policy; -import com.aerospike.client.policy.QueryPolicy; -import com.aerospike.client.policy.ScanPolicy; -import com.aerospike.client.policy.WritePolicy; -import com.aerospike.mapper.annotations.AerospikeBin; -import com.aerospike.mapper.annotations.AerospikeConstructor; -import com.aerospike.mapper.annotations.AerospikeExclude; -import com.aerospike.mapper.annotations.AerospikeGetter; -import com.aerospike.mapper.annotations.AerospikeKey; -import com.aerospike.mapper.annotations.AerospikeOrdinal; -import com.aerospike.mapper.annotations.AerospikeRecord; -import com.aerospike.mapper.annotations.AerospikeSetter; -import com.aerospike.mapper.annotations.ParamFrom; -import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObject; -import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObjectSetter; +import com.aerospike.client.policy.*; +import com.aerospike.mapper.annotations.*; import com.aerospike.mapper.tools.TypeUtils.AnnotatedType; import com.aerospike.mapper.tools.configuration.BinConfig; import com.aerospike.mapper.tools.configuration.ClassConfig; import com.aerospike.mapper.tools.configuration.KeyConfig; +import org.apache.commons.lang3.StringUtils; + +import javax.validation.constraints.NotNull; +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.*; public class ClassCacheEntry { @@ -505,7 +480,7 @@ private void loadFieldsFromClass(Class clazz, boolean mapAll, ClassConfig con isKey = true; } - if (thisField.isAnnotationPresent(AerospikeExclude.class) || (thisBin != null && thisBin.isExcluded() != null && thisBin.isExcluded().booleanValue())) { + if (thisField.isAnnotationPresent(AerospikeExclude.class) || (thisBin != null && thisBin.isExcluded() != null && thisBin.isExcluded())) { // This field should be excluded from being stored in the database. Even keys must be stored continue; } @@ -634,7 +609,7 @@ public Bin[] getBins(Object instance, boolean allowNullBins, String[] binNames) Object javaValue = value.get(instance); Object aerospikeValue = value.getTypeMapper().toAerospikeFormat(javaValue); if (aerospikeValue != null || allowNullBins) { - if (aerospikeValue != null && aerospikeValue instanceof TreeMap) { + if (aerospikeValue instanceof TreeMap) { TreeMap treeMap = (TreeMap)aerospikeValue; bins[index++] = new Bin(name, new ArrayList(treeMap.entrySet()), MapOrder.KEY_ORDERED); } @@ -689,7 +664,7 @@ private void addDataFromValueName(String name, Object instance, ClassCacheEntry< } private boolean isKeyField(String name) { - return name != null && keyName != null && keyName.equals(name); + return keyName != null && keyName.equals(name); } public List getList(Object instance, boolean skipKey, boolean needsType) { @@ -868,7 +843,7 @@ public T constructAndHydrate(List list, boolean skipKey) { // If the object saved in the list was a subclass of the declared type, it must have the type name as the last element of the list. // Note that there is a performance implication of using subclasses. Object obj = list.get(endIndex-1); - if (obj != null && (obj instanceof String) && ((String)obj).startsWith(TYPE_PREFIX)) { + if ((obj instanceof String) && ((String) obj).startsWith(TYPE_PREFIX)) { String className = ((String)obj).substring(TYPE_PREFIX.length()); thisClass = ClassCache.getInstance().getCacheEntryFromStoredName(className); if (thisClass == null) { @@ -885,7 +860,7 @@ public T constructAndHydrate(List list, boolean skipKey) { Object lastValue = list.get(endIndex-1); int recordVersion = 1; if ((lastValue instanceof String) && (((String)lastValue).startsWith(VERSION_PREFIX))) { - recordVersion = Integer.valueOf(((String)lastValue).substring(2)); + recordVersion = Integer.parseInt(((String)lastValue).substring(2)); endIndex--; } int objectVersion = thisClass.version; @@ -934,7 +909,7 @@ public void hydrateFromList(List list, Object instance, boolean skipKey) Object lastValue = list.get(endIndex-1); int recordVersion = 1; if ((lastValue instanceof String) && (((String)lastValue).startsWith(VERSION_PREFIX))) { - recordVersion = Integer.valueOf(((String)lastValue).substring(2)); + recordVersion = Integer.parseInt(((String)lastValue).substring(2)); endIndex--; } int objectVersion = thisClass.version; diff --git a/src/main/java/com/aerospike/mapper/tools/DeferredObjectLoader.java b/src/main/java/com/aerospike/mapper/tools/DeferredObjectLoader.java index c7ce283..6704144 100644 --- a/src/main/java/com/aerospike/mapper/tools/DeferredObjectLoader.java +++ b/src/main/java/com/aerospike/mapper/tools/DeferredObjectLoader.java @@ -4,8 +4,8 @@ import java.util.List; public class DeferredObjectLoader { - public static interface DeferredSetter { - public void setValue(Object object); + public interface DeferredSetter { + void setValue(Object object); } public static class DeferredObject { @@ -47,12 +47,7 @@ public DeferredObject getObject() { } - private static ThreadLocal> threadLocalLoader = new ThreadLocal>() { - @Override - public List initialValue() { - return new ArrayList(); - } - }; + private static final ThreadLocal> threadLocalLoader = ThreadLocal.withInitial(ArrayList::new); public static void save(DeferredObjectSetter object) { threadLocalLoader.get().add(object); @@ -72,7 +67,7 @@ public static void add(DeferredObjectSetter deferredSetter) { public static List getAndClear() { List localArray = threadLocalLoader.get(); - List setters = new ArrayList(localArray); + List setters = new ArrayList<>(localArray); localArray.clear(); return setters; } diff --git a/src/main/java/com/aerospike/mapper/tools/DeferredOperation.java b/src/main/java/com/aerospike/mapper/tools/DeferredOperation.java index 66acab2..6af484b 100644 --- a/src/main/java/com/aerospike/mapper/tools/DeferredOperation.java +++ b/src/main/java/com/aerospike/mapper/tools/DeferredOperation.java @@ -3,7 +3,7 @@ import com.aerospike.client.Operation; public interface DeferredOperation { - public Operation getOperation(OperationParameters operationParams); - public ResultsUnpacker[] getUnpackers(OperationParameters operationParams); - public boolean isGetOperation(); + Operation getOperation(OperationParameters operationParams); + ResultsUnpacker[] getUnpackers(OperationParameters operationParams); + boolean isGetOperation(); } diff --git a/src/main/java/com/aerospike/mapper/tools/GenericTypeMapper.java b/src/main/java/com/aerospike/mapper/tools/GenericTypeMapper.java index dc00528..ae9e48e 100644 --- a/src/main/java/com/aerospike/mapper/tools/GenericTypeMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/GenericTypeMapper.java @@ -9,7 +9,7 @@ public class GenericTypeMapper extends TypeMapper { private Class mappedClass; - private Object converter; + private final Object converter; private Method toAerospike; private Method fromAerospike; public GenericTypeMapper(Object converter) { @@ -78,7 +78,7 @@ public Class validateAndGetClass() { if (!this.toAerospike.getParameters()[0].getType().equals(this.fromAerospike.getReturnType())) { throw new AerospikeException(String.format("@FromAerospike method on Converter class %s returns %s, but the @ToAerospike method takes %s. These should be the same class", - this.converter.getClass().getSimpleName(), this.fromAerospike.getReturnType().getClass().getSimpleName(), this.toAerospike.getParameters()[0].getType().getSimpleName())); + this.converter.getClass().getSimpleName(), this.fromAerospike.getReturnType().getSimpleName(), this.toAerospike.getParameters()[0].getType().getSimpleName())); } if (!this.fromAerospike.getParameters()[0].getType().equals(this.toAerospike.getReturnType())) { throw new AerospikeException(String.format("@ToAerospike method on Converter class %s returns %s, but the @FromAerospike method takes %s. These should be the same class", diff --git a/src/main/java/com/aerospike/mapper/tools/OperationParameters.java b/src/main/java/com/aerospike/mapper/tools/OperationParameters.java index 21508a8..4ceb79c 100644 --- a/src/main/java/com/aerospike/mapper/tools/OperationParameters.java +++ b/src/main/java/com/aerospike/mapper/tools/OperationParameters.java @@ -5,13 +5,16 @@ public class OperationParameters { public OperationParameters() { } + public OperationParameters(ReturnType needsResultOfType) { super(); this.needsResultOfType = needsResultOfType; } + public ReturnType getNeedsResultOfType() { return needsResultOfType; } + public void setNeedsResultOfType(ReturnType needsResultOfType) { this.needsResultOfType = needsResultOfType; } diff --git a/src/main/java/com/aerospike/mapper/tools/PropertyDefinition.java b/src/main/java/com/aerospike/mapper/tools/PropertyDefinition.java index a25f2b0..5c89925 100644 --- a/src/main/java/com/aerospike/mapper/tools/PropertyDefinition.java +++ b/src/main/java/com/aerospike/mapper/tools/PropertyDefinition.java @@ -13,7 +13,7 @@ public class PropertyDefinition { - public static enum SetterParamType { + public enum SetterParamType { NONE, KEY, VALUE @@ -24,7 +24,7 @@ public static enum SetterParamType { private String name; private Class clazz; private TypeMapper typeMapper; - private AeroMapper mapper; + private final AeroMapper mapper; private SetterParamType setterParamType = SetterParamType.NONE; public PropertyDefinition(String name, AeroMapper mapper) { @@ -51,7 +51,6 @@ public SetterParamType getSetterParamType() { /** * Get the type of this property. The getter and setter must agree on the property and this method * is only valid after the validate method has been called. - * @return */ public Class getType() { return this.clazz; diff --git a/src/main/java/com/aerospike/mapper/tools/ResultsUnpacker.java b/src/main/java/com/aerospike/mapper/tools/ResultsUnpacker.java index 22aaf88..5e5b367 100644 --- a/src/main/java/com/aerospike/mapper/tools/ResultsUnpacker.java +++ b/src/main/java/com/aerospike/mapper/tools/ResultsUnpacker.java @@ -5,9 +5,9 @@ import java.util.function.Function; public interface ResultsUnpacker { - public Object unpack(Object object); + Object unpack(Object object); - public static class ListUnpacker implements ResultsUnpacker { + class ListUnpacker implements ResultsUnpacker { private ListUnpacker() { } @Override @@ -23,7 +23,7 @@ public Object unpack(Object object) { public final static ListUnpacker instance = new ListUnpacker(); } - public static class IdentityUnpacker implements ResultsUnpacker { + class IdentityUnpacker implements ResultsUnpacker { private IdentityUnpacker() { } @Override @@ -33,7 +33,7 @@ public Object unpack(Object object) { public final static IdentityUnpacker instance = new IdentityUnpacker(); } - public static class ElementUnpacker implements ResultsUnpacker { + class ElementUnpacker implements ResultsUnpacker { Function function; public ElementUnpacker(Function itemMapper) { this.function = itemMapper; @@ -43,7 +43,8 @@ public Object unpack(Object object) { return function.apply(object); } } - public static class ArrayUnpacker implements ResultsUnpacker { + + class ArrayUnpacker implements ResultsUnpacker { Function function; public ArrayUnpacker(Function itemMapper) { this.function = itemMapper; @@ -62,4 +63,4 @@ public Object unpack(Object object) { return results; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/aerospike/mapper/tools/TypeMapper.java b/src/main/java/com/aerospike/mapper/tools/TypeMapper.java index ba73507..d5be671 100644 --- a/src/main/java/com/aerospike/mapper/tools/TypeMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/TypeMapper.java @@ -6,9 +6,6 @@ public abstract class TypeMapper { /** * Some types need to know if they're mapped to the correct class. If they do, they can override this method to glean that information - * @param value - * @param isExpectedType - * @return */ public Object toAerospikeFormat(Object value, boolean isUnknownType, boolean isSubclassOfKnownType) { return toAerospikeFormat(value); diff --git a/src/main/java/com/aerospike/mapper/tools/TypeUtils.java b/src/main/java/com/aerospike/mapper/tools/TypeUtils.java index 77e74f9..af5bb21 100644 --- a/src/main/java/com/aerospike/mapper/tools/TypeUtils.java +++ b/src/main/java/com/aerospike/mapper/tools/TypeUtils.java @@ -38,7 +38,7 @@ import com.aerospike.mapper.tools.mappers.ShortMapper; public class TypeUtils { - private static Map, TypeMapper> mappers = new HashMap<>(); + private static final Map, TypeMapper> mappers = new HashMap<>(); public static class AnnotatedType { @@ -101,9 +101,9 @@ public T getAnnotation(Class clazz) { * This method adds a new type mapper into the system. This type mapper will replace any other mappers * registered for the same class. If there was another mapper for the same type already registered, * the old mapper will be replaced with this mapper and the old mapper returned. - * @param clazz - * @param mapper - * @return + * @param clazz The class to register for the new type mapper. + * @param mapper The new type mapper to create. + * @return Return existing mapper registered for the requested class, null in case there isn't one. */ static TypeMapper addTypeMapper(Class clazz, TypeMapper mapper) { TypeMapper returnValue = mappers.get(clazz); @@ -238,10 +238,7 @@ else if (List.class.isAssignableFrom(clazz)) { else if (clazz.isAnnotationPresent(AerospikeRecord.class) || ClassCache.getInstance().hasClassConfig(clazz)) { boolean throwError = false; - if (type == null) { - - } - else { + if (type != null) { BinConfig binConfig = type.getBinConfig(); if (binConfig != null && (binConfig.getEmbed() != null || binConfig.getReference() != null)) { // The config parameters take precedence over the annotations. @@ -254,7 +251,7 @@ else if (binConfig.getEmbed() != null) { if (embedType == null) { embedType = EmbedType.MAP; } - boolean saveKey = embedConfig.getSaveKey() == null ? false : true; + boolean saveKey = embedConfig.getSaveKey() != null; boolean skipKey = isForSubType && (embedConfig.getType() == EmbedType.MAP && embedConfig.getElementType() == EmbedType.LIST && (!saveKey)); typeMapper = new ObjectEmbedMapper(clazz, embedType, mapper, skipKey); addToMap = false; diff --git a/src/main/java/com/aerospike/mapper/tools/ValueType.java b/src/main/java/com/aerospike/mapper/tools/ValueType.java index 4515f2d..043438c 100644 --- a/src/main/java/com/aerospike/mapper/tools/ValueType.java +++ b/src/main/java/com/aerospike/mapper/tools/ValueType.java @@ -1,11 +1,5 @@ package com.aerospike.mapper.tools; -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; - -import javax.validation.constraints.NotNull; - import com.aerospike.client.AerospikeException; import com.aerospike.client.Key; import com.aerospike.mapper.annotations.AerospikeVersion; @@ -14,6 +8,10 @@ import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredSetter; import com.aerospike.mapper.tools.TypeUtils.AnnotatedType; +import javax.validation.constraints.NotNull; +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + /** * Implementation of a value, which can be either a method on a class (getter) or a field * @author timfaulkes @@ -53,10 +51,9 @@ public TypeMapper getTypeMapper() { public AnnotatedType getAnnotatedType() { return annotatedType; } - - + public static class FieldValue extends ValueType { - private Field field; + private final Field field; public FieldValue(Field field, TypeMapper typeMapper, AnnotatedType annotatedType) { super(typeMapper, annotatedType); this.field = field; @@ -73,15 +70,12 @@ public Object get(Object obj) throws ReflectiveOperationException { } @Override public void set(final Object obj, final Object value) throws ReflectiveOperationException { - if (value != null && value instanceof DeferredObject) { - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - try { - field.set(obj, object); - } catch (IllegalArgumentException | IllegalAccessException e) { - throw new AerospikeException(String.format("Could not set field %s on %s to %s", field, obj, value)); - } + if (value instanceof DeferredObject) { + DeferredSetter setter = object -> { + try { + field.set(obj, object); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new AerospikeException(String.format("Could not set field %s on %s to %s", field, obj, value)); } }; DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)value); @@ -109,7 +103,7 @@ public String toString() { } public static class MethodValue extends ValueType { - private PropertyDefinition property; + private final PropertyDefinition property; public MethodValue(PropertyDefinition property, TypeMapper typeMapper, AnnotatedType annotatedType) { super(typeMapper, annotatedType); @@ -132,15 +126,12 @@ public void set(final Object obj, final Object value) throws ReflectiveOperation switch (this.property.getSetterParamType()) { case KEY: { final Key key = ThreadLocalKeySaver.get(); - if (value != null && value instanceof DeferredObject) { - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - try { - property.getSetter().invoke(obj, value, key); - } catch (ReflectiveOperationException e) { - throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); - } + if (value instanceof DeferredObject) { + DeferredSetter setter = object -> { + try { + property.getSetter().invoke(obj, value, key); + } catch (ReflectiveOperationException e) { + throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); } }; DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)value); @@ -154,15 +145,12 @@ public void setValue(Object object) { case VALUE: { final Key key = ThreadLocalKeySaver.get(); - if (value != null && value instanceof DeferredObject) { - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - try { - property.getSetter().invoke(obj, value, key.userKey); - } catch (ReflectiveOperationException e) { - throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); - } + if (value instanceof DeferredObject) { + DeferredSetter setter = object -> { + try { + property.getSetter().invoke(obj, value, key.userKey); + } catch (ReflectiveOperationException e) { + throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); } }; DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)value); @@ -175,15 +163,12 @@ public void setValue(Object object) { } default: - if (value != null && value instanceof DeferredObject) { - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - try { - property.getSetter().invoke(obj, value); - } catch (ReflectiveOperationException e) { - throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); - } + if (value instanceof DeferredObject) { + DeferredSetter setter = object -> { + try { + property.getSetter().invoke(obj, value); + } catch (ReflectiveOperationException e) { + throw new AerospikeException(String.format("Could not set field %s on %s to %s", property, obj, value)); } }; DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)value); @@ -210,6 +195,5 @@ public Annotation[] getAnnotations() { public String toString() { return String.format("Value(Method): %s/%s (%s)", this.property.getGetter(), this.property.getSetter(), this.property.getType().getSimpleName()); } - } } diff --git a/src/main/java/com/aerospike/mapper/tools/VirtualList.java b/src/main/java/com/aerospike/mapper/tools/VirtualList.java index 524b599..5ecb80e 100644 --- a/src/main/java/com/aerospike/mapper/tools/VirtualList.java +++ b/src/main/java/com/aerospike/mapper/tools/VirtualList.java @@ -57,7 +57,7 @@ private VirtualList(@NotNull AeroMapper mapper, Object object, Class owningCl if (object != null) { owningClazz = object.getClass(); } - this.owningEntry = (ClassCacheEntry) ClassCache.getInstance().loadClass(owningClazz, mapper); + this.owningEntry = ClassCache.getInstance().loadClass(owningClazz, mapper); Object aerospikeKey; if (key == null) { aerospikeKey = owningEntry.getKey(object); @@ -65,7 +65,7 @@ private VirtualList(@NotNull AeroMapper mapper, Object object, Class owningCl else { aerospikeKey = owningEntry.translateKeyToAerospikeKey(key); } - this.elementEntry = (ClassCacheEntry) ClassCache.getInstance().loadClass(clazz, mapper); + this.elementEntry = ClassCache.getInstance().loadClass(clazz, mapper); this.mapper = mapper; this.binName = binName; this.value = owningEntry.getValueFromBinName(binName); @@ -98,7 +98,7 @@ private VirtualList(@NotNull AeroMapper mapper, Object object, Class owningCl else { throw new AerospikeException(String.format("Bin %s on class %s is not mapped via a listMapper. This is unexpected", binName, clazz.getSimpleName())); } - this.instanceMapper = src -> listMapper.fromAerospikeInstanceFormat(src); + this.instanceMapper = listMapper::fromAerospikeInstanceFormat; } @@ -113,10 +113,10 @@ public VirtualList changeKey(Object newKey) { } public class MultiOperation { - private VirtualList virtualList; - private List interactions; + private final VirtualList virtualList; + private final List interactions; private int indexToReturn = -1; - private WritePolicy writePolicy; + private final WritePolicy writePolicy; private MultiOperation(@NotNull VirtualList virtualList, @NotNull WritePolicy writePolicy) { this.virtualList = virtualList; @@ -129,22 +129,27 @@ public MultiOperation append(E item) { this.interactions.add(new Interactor(virtualList.getAppendOperation(aerospikeItem))); return this; } + public MultiOperation removeByKey(Object key) { this.interactions.add(getRemoveKeyInteractor(key)); return this; } + public MultiOperation removeByKeyRange(Object startKey, Object endKey) { this.interactions.add(getRemoveKeyRangeInteractor(startKey, endKey)); return this; } + public MultiOperation removeByValueRange(Object startKey, Object endKey) { this.interactions.add(getRemoveValueRangeInteractor(startKey, endKey)); return this; } + public MultiOperation getByValueRange(Object startKey, Object endKey) { this.interactions.add(getGetByValueRangeInteractor(startKey, endKey)); return this; } + public MultiOperation getByKeyRange(Object startKey, Object endKey) { this.interactions.add(getGetByKeyRangeInteractor(startKey, endKey)); return this; @@ -179,16 +184,17 @@ else if (this.indexToReturn >= 0) { } /** - * Finish the multi operation and process it. - * @return + * Finish the multi operation and process it. + * @return The multi operation result. */ public Object end() { return end(null); } /** - * Finish the multi operation and process it. - * @return + * Finish the multi operation and process it. + * @param resultType The return type for the result. + * @return The multi operation result with the given result type. */ public T end(Class resultType) { if (this.interactions.isEmpty()) { @@ -255,7 +261,7 @@ public MultiOperation beginMulti(WritePolicy writePolicy) { writePolicy = new WritePolicy(owningEntry.getWritePolicy()); writePolicy.recordExistsAction = RecordExistsAction.UPDATE; } - return new MultiOperation(this, writePolicy); + return new MultiOperation<>(this, writePolicy); } @@ -281,16 +287,27 @@ public List getByValueRange(WritePolicy writePolicy, Object startValue, Objec *

* If the list is mapped to a LIST in Aerospike however, the start and end range represent values to be removed from the list. *

- * The result of the method is a list of the records which have been removed from the database if returnResults is true, null otherwise. - * @param startKey - * @param endKey - * @param returnResults - * @return + * @param startKey Start key of the range to remove. + * @param endKey End key of the range to remove. + * @param returnResultsOfType Type to return. + * @return A list of the records which have been removed from the database if returnResults is true, null otherwise. */ public List removeByValueRange(Object startKey, Object endKey, ReturnType returnResultsOfType) { return this.removeByValueRange(null, startKey, endKey, returnResultsOfType); } - + + /** + * Remove items from the list matching the specified key. If the list is mapped to a MAP in Aerospike, the start key and end key will dictate the range of keys to be removed, + * inclusive of the start, exclusive of the end. + *

+ * If the list is mapped to a LIST in Aerospike however, the start and end range represent values to be removed from the list. + *

+ * @param writePolicy An Aerospike write policy to use for the operate() operation. + * @param startKey Start key of the range to remove. + * @param endKey End key of the range to remove. + * @param returnResultsOfType Type to return. + * @return A list of the records which have been removed from the database if returnResults is true, null otherwise. + */ public List removeByValueRange(WritePolicy writePolicy, Object startKey, Object endKey, ReturnType returnResultsOfType) { if (writePolicy == null) { writePolicy = new WritePolicy(owningEntry.getWritePolicy()); @@ -309,16 +326,27 @@ public List removeByValueRange(WritePolicy writePolicy, Object startKey, Obje *

* If the list is mapped to a LIST in Aerospike however, the start and end range represent values to be removed from the list. *

- * The result of the method is a list of the records which have been removed from the database if returnResults is true, null otherwise. - * @param startKey - * @param endKey - * @param returnResults - * @return + * @param startKey Start key of the range to remove. + * @param endKey End key of the range to remove. + * @param returnResultsOfType Type to return. + * @return The result of the method is a list of the records which have been removed from the database if returnResults is true, null otherwise. */ public List removeByKeyRange(Object startKey, Object endKey, ReturnType returnResultsOfType) { return this.removeByKeyRange(null, startKey, endKey, returnResultsOfType); } - + + /** + * Remove items from the list matching the specified key. If the list is mapped to a MAP in Aerospike, the start key and end key will dictate the range of keys to be removed, + * inclusive of the start, exclusive of the end. + *

+ * If the list is mapped to a LIST in Aerospike however, the start and end range represent values to be removed from the list. + *

+ * @param writePolicy An Aerospike write policy to use for the operate() operation. + * @param startKey Start key of the range to remove. + * @param endKey End key of the range to remove. + * @param returnResultsOfType Type to return. + * @return The result of the method is a list of the records which have been removed from the database if returnResults is true, null otherwise. + */ public List removeByKeyRange(WritePolicy writePolicy, Object startKey, Object endKey, ReturnType returnResultsOfType) { if (writePolicy == null) { writePolicy = new WritePolicy(owningEntry.getWritePolicy()); diff --git a/src/main/java/com/aerospike/mapper/tools/configuration/ClassConfig.java b/src/main/java/com/aerospike/mapper/tools/configuration/ClassConfig.java index 3c3b656..2e789a3 100644 --- a/src/main/java/com/aerospike/mapper/tools/configuration/ClassConfig.java +++ b/src/main/java/com/aerospike/mapper/tools/configuration/ClassConfig.java @@ -19,7 +19,7 @@ public class ClassConfig { private Boolean durableDelete; private KeyConfig key; private String shortName; - private List bins; + private final List bins; public ClassConfig() { bins = new ArrayList<>(); @@ -69,9 +69,6 @@ public List getBins() { } public BinConfig getBinByName(@NotNull String name) { - if (bins == null) { - return null; - } for (BinConfig thisBin : bins) { if (name.equals(thisBin.getName())) { return thisBin; @@ -81,9 +78,6 @@ public BinConfig getBinByName(@NotNull String name) { } public BinConfig getBinByGetterName(@NotNull String getterName) { - if (bins == null) { - return null; - } for (BinConfig thisBin : bins) { if (getterName.equals(thisBin.getGetter())) { return thisBin; @@ -93,9 +87,6 @@ public BinConfig getBinByGetterName(@NotNull String getterName) { } public BinConfig getBinByFieldName(@NotNull String fieldName) { - if (bins == null) { - return null; - } for (BinConfig thisBin : bins) { if (fieldName.equals(thisBin.getField())) { return thisBin; @@ -104,10 +95,8 @@ public BinConfig getBinByFieldName(@NotNull String fieldName) { return null; } public void validate() { - if (this.bins != null) { - for (BinConfig thisBin : bins) { - thisBin.validate(this.className); - } + for (BinConfig thisBin : bins) { + thisBin.validate(this.className); } } } \ No newline at end of file diff --git a/src/main/java/com/aerospike/mapper/tools/configuration/Configuration.java b/src/main/java/com/aerospike/mapper/tools/configuration/Configuration.java index b48973c..b9e3d94 100644 --- a/src/main/java/com/aerospike/mapper/tools/configuration/Configuration.java +++ b/src/main/java/com/aerospike/mapper/tools/configuration/Configuration.java @@ -4,7 +4,7 @@ import java.util.List; public class Configuration { - private List classes; + private final List classes; public Configuration() { this.classes = new ArrayList<>(); diff --git a/src/main/java/com/aerospike/mapper/tools/mappers/ArrayMapper.java b/src/main/java/com/aerospike/mapper/tools/mappers/ArrayMapper.java index 31a5521..4a2fb06 100644 --- a/src/main/java/com/aerospike/mapper/tools/mappers/ArrayMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/mappers/ArrayMapper.java @@ -62,12 +62,7 @@ public Object fromAerospikeFormat(Object value) { } else { final int thisIndex = i; - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - Array.set(result, thisIndex, object); - } - }; + DeferredSetter setter = object -> Array.set(result, thisIndex, object); DeferredObjectLoader.add(new DeferredObjectSetter(setter, (DeferredObject)item)); } } diff --git a/src/main/java/com/aerospike/mapper/tools/mappers/ListMapper.java b/src/main/java/com/aerospike/mapper/tools/mappers/ListMapper.java index 81342ab..d479650 100644 --- a/src/main/java/com/aerospike/mapper/tools/mappers/ListMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/mappers/ListMapper.java @@ -190,12 +190,7 @@ public Object fromAerospikeFormat(Object value) { Object result = thisMapper == null ? obj : thisMapper.fromAerospikeFormat(obj); if (result instanceof DeferredObject) { final int thisIndex = index; - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - results.set(thisIndex, object); - } - }; + DeferredSetter setter = object -> results.set(thisIndex, object); DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)result); DeferredObjectLoader.add(objectSetter); // add a placeholder to maintain the index @@ -217,12 +212,7 @@ public void setValue(Object object) { Object result = this.instanceClassMapper.fromAerospikeFormat(obj); if (result instanceof DeferredObject) { final int thisIndex = index; - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - results.set(thisIndex, object); - } - }; + DeferredSetter setter = object -> results.set(thisIndex, object); DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)result); DeferredObjectLoader.add(objectSetter); // add a placeholder to maintain the index diff --git a/src/main/java/com/aerospike/mapper/tools/mappers/MapMapper.java b/src/main/java/com/aerospike/mapper/tools/mappers/MapMapper.java index 6f400b6..f27a84c 100644 --- a/src/main/java/com/aerospike/mapper/tools/mappers/MapMapper.java +++ b/src/main/java/com/aerospike/mapper/tools/mappers/MapMapper.java @@ -74,17 +74,11 @@ public Object fromAerospikeFormat(Object value) { TypeMapper keyMap = keyMapper != null ? keyMapper : TypeUtils.getMapper(key.getClass(), AnnotatedType.getDefaultAnnotateType(), mapper); TypeMapper itemMap = itemMapper != null ? itemMapper : TypeUtils.getMapper(item.getClass(), AnnotatedType.getDefaultAnnotateType(), mapper); // results.put(keyMap.fromAerospikeFormat(key), itemMap.fromAerospikeFormat(item)); - - + final Object javaKey = keyMap == null ? null : keyMap.fromAerospikeFormat(key); final Object javaItem = itemMap == null ? null : itemMap.fromAerospikeFormat(item); if (javaKey instanceof DeferredObject || javaItem instanceof DeferredObject) { - DeferredSetter setter = new DeferredSetter() { - @Override - public void setValue(Object object) { - results.put(javaKey, object); - } - }; + DeferredSetter setter = object -> results.put(javaKey, object); DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)javaItem); DeferredObjectLoader.add(objectSetter); }