Skip to content

Commit e3ee8a6

Browse files
authored
Merge pull request #42 from avaje/feature/tidy-coreadapterbuilder
Refactor tidy CoreAdapterBuilder
2 parents 20c7202 + 562dd20 commit e3ee8a6

File tree

3 files changed

+23
-58
lines changed

3 files changed

+23
-58
lines changed

jsonb-generator/src/main/java/io/avaje/jsonb/generator/BeanReader.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package io.avaje.jsonb.generator;
22

3+
import io.avaje.jsonb.Json;
4+
5+
import javax.lang.model.element.Element;
6+
import javax.lang.model.element.TypeElement;
37
import java.util.HashSet;
48
import java.util.List;
5-
import java.util.Map;
69
import java.util.Set;
710
import java.util.TreeSet;
811

9-
import javax.lang.model.element.Element;
10-
import javax.lang.model.element.TypeElement;
11-
12-
import io.avaje.jsonb.Json;
13-
1412
class BeanReader {
1513

1614
private final TypeElement beanType;
@@ -44,27 +42,22 @@ class BeanReader {
4442
this.constructor = typeReader.constructor();
4543
}
4644

47-
public BeanReader(
48-
TypeElement beanType,
49-
TypeElement mixInElement,
50-
ProcessingContext context) {
51-
52-
this.beanType = beanType;
53-
this.type = beanType.getQualifiedName().toString();
54-
this.shortName = shortName(beanType);
55-
final NamingConventionReader ncReader = new NamingConventionReader(beanType);
56-
this.namingConvention = ncReader.get();
57-
this.typeProperty = ncReader.typeProperty();
58-
this.typeReader = new TypeReader(beanType, mixInElement, context, namingConvention);
59-
typeReader.process();
60-
this.nonAccessibleField = typeReader.nonAccessibleField();
61-
this.hasSubTypes = typeReader.hasSubTypes();
62-
this.allFields = typeReader.allFields();
63-
this.constructor = typeReader.constructor();
64-
45+
public BeanReader(TypeElement beanType, TypeElement mixInElement, ProcessingContext context) {
46+
this.beanType = beanType;
47+
this.type = beanType.getQualifiedName().toString();
48+
this.shortName = shortName(beanType);
49+
final NamingConventionReader ncReader = new NamingConventionReader(beanType);
50+
this.namingConvention = ncReader.get();
51+
this.typeProperty = ncReader.typeProperty();
52+
this.typeReader = new TypeReader(beanType, mixInElement, context, namingConvention);
53+
typeReader.process();
54+
this.nonAccessibleField = typeReader.nonAccessibleField();
55+
this.hasSubTypes = typeReader.hasSubTypes();
56+
this.allFields = typeReader.allFields();
57+
this.constructor = typeReader.constructor();
6558
}
6659

67-
@Override
60+
@Override
6861
public String toString() {
6962
return beanType.toString();
7063
}
@@ -198,6 +191,7 @@ void writeViewSupport(Append writer) {
198191
writeViewBuild(writer);
199192
}
200193
}
194+
201195
private void writeView(Append writer) {
202196
writer.eol();
203197
writer.append(" @Override").eol();
@@ -375,4 +369,5 @@ private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, Stri
375369
writer.append(" }").eol();
376370
writer.append(" reader.endObject();").eol();
377371
}
372+
378373
}

jsonb/src/main/java/io/avaje/jsonb/core/CollectionAdapter.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,6 @@ abstract class CollectionAdapter<C extends Collection<T>, T> extends JsonAdapter
3939
return null;
4040
};
4141

42-
/**
43-
* Helper that takes a base JsonAdapter and returns another JsonAdapter for a List
44-
* containing elements of the base type.
45-
*/
46-
static <T> JsonAdapter<List<T>> listOf(JsonAdapter<T> base) {
47-
return new CollectionAdapter<List<T>, T>(base) {
48-
@Override
49-
List<T> newCollection() {
50-
return new ArrayList<>();
51-
}
52-
};
53-
}
54-
55-
static <T> JsonAdapter<Set<T>> setOf(JsonAdapter<T> base) {
56-
return new CollectionAdapter<Set<T>, T>(base) {
57-
@Override
58-
Set<T> newCollection() {
59-
return new LinkedHashSet<>();
60-
}
61-
};
62-
}
63-
6442
static <T> JsonAdapter<Collection<T>> newListAdapter(Type type, Jsonb jsonb) {
6543
Type elementType = Util.collectionElementType(type);
6644
JsonAdapter<T> elementAdapter = jsonb.adapter(elementType);

jsonb/src/main/java/io/avaje/jsonb/core/CoreAdapterBuilder.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.lang.reflect.Type;
2323
import java.util.*;
24+
import java.util.concurrent.ConcurrentHashMap;
2425
import java.util.concurrent.locks.ReentrantLock;
2526

2627
/**
@@ -31,7 +32,7 @@ class CoreAdapterBuilder {
3132
private final DJsonb context;
3233
private final List<JsonAdapter.Factory> factories;
3334
private final ThreadLocal<LookupChain> lookupChainThreadLocal = new ThreadLocal<>();
34-
private final Map<Object, JsonAdapter<?>> adapterCache = new LinkedHashMap<>();
35+
private final Map<Object, JsonAdapter<?>> adapterCache = new ConcurrentHashMap<>();
3536
private final ReentrantLock lock = new ReentrantLock();
3637

3738
CoreAdapterBuilder(DJsonb context, List<JsonAdapter.Factory> userFactories, boolean mathAsString) {
@@ -51,16 +52,7 @@ class CoreAdapterBuilder {
5152
*/
5253
@SuppressWarnings("unchecked")
5354
<T> JsonAdapter<T> get(Object cacheKey) {
54-
lock.lock();
55-
try {
56-
JsonAdapter<?> result = adapterCache.get(cacheKey);
57-
if (result != null) {
58-
return (JsonAdapter<T>) result;
59-
}
60-
} finally {
61-
lock.unlock();
62-
}
63-
return null;
55+
return (JsonAdapter<T>) adapterCache.get(cacheKey);
6456
}
6557

6658
/**

0 commit comments

Comments
 (0)