Skip to content

Commit 10b88ba

Browse files
authored
Tools package cleanup (#12)
* tools package cleanup.
1 parent 5fb453f commit 10b88ba

18 files changed

+202
-271
lines changed

src/main/java/com/aerospike/mapper/tools/AeroMapper.java

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
11
package com.aerospike.mapper.tools;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.lang.reflect.Array;
6-
import java.util.ArrayList;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.function.Function;
10-
11-
import javax.validation.constraints.NotNull;
12-
13-
import org.apache.commons.lang3.StringUtils;
14-
15-
import com.aerospike.client.AerospikeException;
16-
import com.aerospike.client.Bin;
17-
import com.aerospike.client.IAerospikeClient;
18-
import com.aerospike.client.Key;
19-
import com.aerospike.client.Record;
20-
import com.aerospike.client.Value;
21-
import com.aerospike.client.policy.BatchPolicy;
22-
import com.aerospike.client.policy.Policy;
23-
import com.aerospike.client.policy.QueryPolicy;
24-
import com.aerospike.client.policy.RecordExistsAction;
25-
import com.aerospike.client.policy.ScanPolicy;
26-
import com.aerospike.client.policy.WritePolicy;
3+
import com.aerospike.client.*;
4+
import com.aerospike.client.policy.*;
275
import com.aerospike.client.query.RecordSet;
286
import com.aerospike.client.query.Statement;
297
import com.aerospike.mapper.tools.ClassCache.PolicyType;
@@ -32,18 +10,26 @@
3210
import com.aerospike.mapper.tools.TypeUtils.AnnotatedType;
3311
import com.aerospike.mapper.tools.configuration.ClassConfig;
3412
import com.aerospike.mapper.tools.configuration.Configuration;
35-
import com.fasterxml.jackson.core.JsonParseException;
3613
import com.fasterxml.jackson.core.JsonProcessingException;
37-
import com.fasterxml.jackson.databind.JsonMappingException;
3814
import com.fasterxml.jackson.databind.ObjectMapper;
3915
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
16+
import org.apache.commons.lang3.StringUtils;
17+
18+
import javax.validation.constraints.NotNull;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.lang.reflect.Array;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.function.Function;
4026

4127
public class AeroMapper {
4228

43-
IAerospikeClient mClient;
29+
final IAerospikeClient mClient;
4430

4531
public static class Builder {
46-
private AeroMapper mapper;
32+
private final AeroMapper mapper;
4733
private List<Class<?>> classesToPreload = null;
4834

4935
public Builder(IAerospikeClient client) {
@@ -52,9 +38,8 @@ public Builder(IAerospikeClient client) {
5238
}
5339

5440
/**
55-
* Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation
56-
*
57-
* @param converter
41+
* Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation.
42+
* @param converter The custom converter
5843
* @return this object
5944
*/
6045
public Builder addConverter(Object converter) {
@@ -72,22 +57,22 @@ public Builder preLoadClass(Class<?> clazz) {
7257
return this;
7358
}
7459

75-
public Builder withConfigurationFile(File file) throws JsonParseException, JsonMappingException, IOException {
60+
public Builder withConfigurationFile(File file) throws IOException {
7661
return this.withConfigurationFile(file, false);
7762
}
7863

79-
public Builder withConfigurationFile(File file, boolean allowsInvalid) throws JsonParseException, JsonMappingException, IOException {
64+
public Builder withConfigurationFile(File file, boolean allowsInvalid) throws IOException {
8065
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
8166
Configuration configuration = objectMapper.readValue(file, Configuration.class);
8267
this.loadConfiguration(configuration, allowsInvalid);
8368
return this;
8469
}
8570

86-
public Builder withConfiguration(String configurationYaml) throws JsonMappingException, JsonProcessingException {
71+
public Builder withConfiguration(String configurationYaml) throws JsonProcessingException {
8772
return this.withConfiguration(configurationYaml, false);
8873
}
8974

90-
public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonMappingException, JsonProcessingException {
75+
public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonProcessingException {
9176
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
9277
Configuration configuration = objectMapper.readValue(configurationYaml, Configuration.class);
9378
this.loadConfiguration(configuration, allowsInvalid);
@@ -105,7 +90,7 @@ private void loadConfiguration(@NotNull Configuration configuration, boolean all
10590
try {
10691
Class.forName(config.getClassName());
10792
} catch (ClassNotFoundException e) {
108-
throw new AerospikeException("Canot find a class with name " + name);
93+
throw new AerospikeException("Cannot find a class with name " + name);
10994
}
11095
}
11196
}
@@ -224,8 +209,8 @@ private <T> void save(WritePolicy writePolicy, @NotNull T object, RecordExistsAc
224209
/**
225210
* Translate a Java object to an Aerospike format object. Note that this could potentially have performance issues as
226211
* the type information of the passed object must be determined on every call.
227-
* @param obj
228-
* @return
212+
* @param obj A given Java object.
213+
* @return An Aerospike format object.
229214
*/
230215
public Object translateToAerospike(Object obj) {
231216
if (obj == null) {
@@ -238,8 +223,8 @@ public Object translateToAerospike(Object obj) {
238223
/**
239224
* Translate an Aerospike object to a Java object. Note that this could potentially have performance issues as
240225
* the type information of the passed object must be determined on every call.
241-
* @param obj
242-
* @return
226+
* @param obj A given Java object.
227+
* @return An Aerospike format object.
243228
*/
244229
@SuppressWarnings("unchecked")
245230
public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expectedClazz) {
@@ -259,8 +244,8 @@ public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expec
259244
* </pre>
260245
* Not that no transactionality is implied by this method -- if any of the save methods fail, the exception will be
261246
* thrown without trying the other objects, nor attempting to roll back previously saved objects
262-
* @param object
263-
* @throws AerospikeException
247+
* @param objects One or two objects to save.
248+
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
264249
*/
265250
public void save(@NotNull Object ... objects) throws AerospikeException {
266251
for (Object thisObject : objects) {
@@ -271,8 +256,8 @@ public void save(@NotNull Object ... objects) throws AerospikeException {
271256
/**
272257
* Save an object in the database. This method will perform a REPLACE on the existing record so any existing
273258
* data will be overwritten by the data in the passed object
274-
* @param object
275-
* @throws AerospikeException
259+
* @param object The object to save.
260+
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
276261
*/
277262
public void save(@NotNull Object object, String ...binNames) throws AerospikeException {
278263
save(null, object, RecordExistsAction.REPLACE, binNames);
@@ -281,19 +266,19 @@ public void save(@NotNull Object object, String ...binNames) throws AerospikeExc
281266
/**
282267
* Save an object in the database with the given WritePolicy. This write policy will override any other set writePolicy so
283268
* is effectively an upsert operation
284-
* @param object
285-
* @throws AerospikeException
269+
* @param writePolicy The write policy for the save operation.
270+
* @param object The object to save.
271+
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
286272
*/
287273
public void save(@NotNull WritePolicy writePolicy, @NotNull Object object, String ...binNames) throws AerospikeException {
288274
save(writePolicy, object, null, binNames);
289275
}
290276

291-
292277
/**
293278
* Updates the object in the database, merging the record with the existing record. This uses the RecordExistsAction
294279
* of UPDATE. If bins are specified, only bins with the passed names will be updated (or all of them if null is passed)
295-
* @param object
296-
* @throws AerospikeException
280+
* @param object The object to update.
281+
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
297282
*/
298283
public void update(@NotNull Object object, String ... binNames) throws AerospikeException {
299284
save(null, object, RecordExistsAction.UPDATE, binNames);
@@ -351,7 +336,7 @@ public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey) throws Aeros
351336
}
352337

353338
/**
354-
* This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userkey) instead
339+
* This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userKey) instead
355340
*/
356341
public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey, boolean resolveDependencies) throws AerospikeException {
357342
ClassCacheEntry<T> entry = getEntryAndValidateNamespace(clazz);
@@ -360,7 +345,7 @@ public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey, boolean reso
360345
return read(null, clazz, key, entry, resolveDependencies);
361346
}
362347

363-
private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key, @NotNull ClassCacheEntry<T> entry, boolean resolveDepenencies) {
348+
private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key, @NotNull ClassCacheEntry<T> entry, boolean resolveDependencies) {
364349
if (readPolicy == null) {
365350
readPolicy = entry.getReadPolicy();
366351
}
@@ -371,8 +356,7 @@ private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key,
371356
} else {
372357
try {
373358
ThreadLocalKeySaver.save(key);
374-
T result = convertToObject(clazz, record, entry, resolveDepenencies);
375-
return result;
359+
return convertToObject(clazz, record, entry, resolveDependencies);
376360
} catch (ReflectiveOperationException e) {
377361
throw new AerospikeException(e);
378362
}
@@ -413,7 +397,7 @@ public <T> T[] read(BatchPolicy batchPolicy, @NotNull Class<T> clazz, @NotNull O
413397
keys[i] = new Key(entry.getNamespace(), set, Value.get(entry.translateKeyToAerospikeKey(userKeys[i])));
414398
}
415399
}
416-
400+
417401
return this.readBatch(batchPolicy, clazz, keys, entry);
418402
}
419403

@@ -495,13 +479,13 @@ public boolean delete(WritePolicy writePolicy, @NotNull Object object) throws Ae
495479
* </ul>
496480
* These operation can all be done without having the full set of transactions
497481
* @param <T> the type of the elements in the list.
498-
* @param object
499-
* @param binName
500-
* @param elementClazz
501-
* @return
482+
* @param object The object that will use as a base for the virtual list.
483+
* @param binName The Aerospike bin name.
484+
* @param elementClazz The class of the elements in the list.
485+
* @return A virtual list.
502486
*/
503487
public <T> VirtualList<T> asBackedList(@NotNull Object object, @NotNull String binName, Class<T> elementClazz) {
504-
return new VirtualList<T>(this, object, binName, elementClazz);
488+
return new VirtualList<>(this, object, binName, elementClazz);
505489
}
506490

507491
/**
@@ -521,13 +505,14 @@ public <T> VirtualList<T> asBackedList(@NotNull Object object, @NotNull String b
521505
* </ul>
522506
* These operation can all be done without having the full set of transactions
523507
* @param <T> the type of the elements in the list.
524-
* @param object
525-
* @param binName
526-
* @param elementClazz
527-
* @return
508+
* @param owningClazz Used for the definitions of how to map the list elements.
509+
* @param key The key to map the object to the database.
510+
* @param binName The Aerospike bin name.
511+
* @param elementClazz The class of the elements in the list.
512+
* @return A virtual list.
528513
*/
529514
public <T> VirtualList<T> asBackedList(@NotNull Class<?> owningClazz, @NotNull Object key, @NotNull String binName, Class<T> elementClazz) {
530-
return new VirtualList<T>(this, owningClazz, key, binName, elementClazz);
515+
return new VirtualList<>(this, owningClazz, key, binName, elementClazz);
531516
}
532517

533518
public <T> void find(@NotNull Class<T> clazz, Function<T, Boolean> function) throws AerospikeException {
@@ -565,11 +550,10 @@ public <T> void find(@NotNull Class<T> clazz, Function<T, Boolean> function) thr
565550
/**
566551
* Given a record loaded from Aerospike and a class type, attempt to convert the record to
567552
* an instance of the passed class.
568-
* @param <T>
569-
* @param clazz
570-
* @param record
571-
* @return
572-
* @throws ReflectiveOperationException
553+
* @param clazz The class type to convert the Aerospike record to.
554+
* @param record The Aerospike record to convert.
555+
* @return A virtual list.
556+
* @throws AerospikeException an AerospikeException will be thrown in case of an encountering a ReflectiveOperationException.
573557
*/
574558
public <T> T convertToObject(Class<T> clazz, Record record) {
575559
try {

src/main/java/com/aerospike/mapper/tools/ClassCache.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.aerospike.mapper.tools.configuration.Configuration;
1717

1818
public class ClassCache {
19-
private static ClassCache instance = new ClassCache();
19+
private static final ClassCache instance = new ClassCache();
2020

2121
public static ClassCache getInstance() {
2222
return instance;
@@ -30,12 +30,12 @@ enum PolicyType {
3030
QUERY
3131
}
3232

33-
private Map<Class<?>, ClassCacheEntry> cacheMap = new HashMap<>();
34-
private Map<String, ClassConfig> classesConfig = new HashMap<>();
35-
private Map<PolicyType, Policy> defaultPolicies = new HashMap<>();
36-
private Map<String, ClassCacheEntry> storedNameToCacheEntry = new HashMap<>();
37-
private Map<PolicyType, Map<Class<?>, Policy>> childrenPolicies = new HashMap<>();
38-
private Map<PolicyType, Map<Class<?>, Policy>> specificPolicies = new HashMap<>();
33+
private final Map<Class<?>, ClassCacheEntry> cacheMap = new HashMap<>();
34+
private final Map<String, ClassConfig> classesConfig = new HashMap<>();
35+
private final Map<PolicyType, Policy> defaultPolicies = new HashMap<>();
36+
private final Map<String, ClassCacheEntry> storedNameToCacheEntry = new HashMap<>();
37+
private final Map<PolicyType, Map<Class<?>, Policy>> childrenPolicies = new HashMap<>();
38+
private final Map<PolicyType, Map<Class<?>, Policy>> specificPolicies = new HashMap<>();
3939

4040
private ClassCache() {
4141
for (PolicyType thisType : PolicyType.values()) {
@@ -51,12 +51,12 @@ public <T> ClassCacheEntry<T> loadClass(@NotNull Class<T> clazz, AeroMapper mapp
5151
ClassCacheEntry<T> entry = cacheMap.get(clazz);
5252
if (entry == null) {
5353
try {
54-
entry = new ClassCacheEntry<T>(clazz, mapper, getClassConfig(clazz),
55-
determinePolicy(clazz, PolicyType.READ),
56-
(WritePolicy)determinePolicy(clazz, PolicyType.WRITE),
57-
(BatchPolicy)determinePolicy(clazz, PolicyType.BATCH),
58-
(QueryPolicy)determinePolicy(clazz, PolicyType.QUERY),
59-
(ScanPolicy)determinePolicy(clazz, PolicyType.SCAN));
54+
entry = new ClassCacheEntry<>(clazz, mapper, getClassConfig(clazz),
55+
determinePolicy(clazz, PolicyType.READ),
56+
(WritePolicy) determinePolicy(clazz, PolicyType.WRITE),
57+
(BatchPolicy) determinePolicy(clazz, PolicyType.BATCH),
58+
(QueryPolicy) determinePolicy(clazz, PolicyType.QUERY),
59+
(ScanPolicy) determinePolicy(clazz, PolicyType.SCAN));
6060
}
6161
catch (IllegalArgumentException iae) {
6262
return null;
@@ -114,7 +114,7 @@ private Policy determinePolicy(@NotNull Class<?> clazz, @NotNull PolicyType poli
114114
if (result != null) {
115115
return result;
116116
}
117-
// Otherwise, iterate up class heirarchy looking for the policy.
117+
// Otherwise, iterate up class hierarchy looking for the policy.
118118
Class<?> thisClass = clazz;
119119
while (thisClass != null) {
120120
Policy aPolicy = childrenPolicies.get(policyType).get(thisClass);

0 commit comments

Comments
 (0)