Skip to content

Commit

Permalink
Use diamond operator when creating generic instances (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Apr 17, 2022
1 parent e82637c commit 4dda4ec
Show file tree
Hide file tree
Showing 59 changed files with 252 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onCreate(Bundle icicle) {
}

private Cart buildCart() {
List<LineItem> lineItems = new ArrayList<LineItem>();
List<LineItem> lineItems = new ArrayList<>();
lineItems.add(new LineItem("hammer", 1, 12000000, "USD"));
return new Cart(lineItems, "Happy Buyer", "4111-1111-1111-1111");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class GraphAdapterBuilder {
private final ConstructorConstructor constructorConstructor;

public GraphAdapterBuilder() {
this.instanceCreators = new HashMap<Type, InstanceCreator<?>>();
this.instanceCreators = new HashMap<>();
this.constructorConstructor = new ConstructorConstructor(instanceCreators, true, Collections.<ReflectionAccessFilter>emptyList());
}
public GraphAdapterBuilder addType(Type type) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public void registerOn(GsonBuilder gsonBuilder) {

static class Factory implements TypeAdapterFactory, InstanceCreator {
private final Map<Type, InstanceCreator<?>> instanceCreators;
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<Graph>();
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();

Factory(Map<Type, InstanceCreator<?>> instanceCreators) {
this.instanceCreators = instanceCreators;
Expand Down Expand Up @@ -121,7 +121,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@SuppressWarnings("unchecked") // graph.map guarantees consistency between value and T
Element<T> element = (Element<T>) graph.map.get(value);
if (element == null) {
element = new Element<T>(value, graph.nextName(), typeAdapter, null);
element = new Element<>(value, graph.nextName(), typeAdapter, null);
graph.map.put(value, element);
graph.queue.add(element);
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
currentName = name;
}
JsonElement element = elementAdapter.read(in);
graph.map.put(name, new Element<T>(null, name, typeAdapter, element));
graph.map.put(name, new Element<>(null, name, typeAdapter, element));
}
in.endObject();
} else {
Expand Down Expand Up @@ -242,7 +242,7 @@ static class Graph {
* The queue of elements to write during serialization. Unused during
* deserialization.
*/
private final Queue<Element> queue = new LinkedList<Element>();
private final Queue<Element> queue = new LinkedList<>();

/**
* The instance currently being deserialized. Used as a backdoor between
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class InterceptorFactory implements TypeAdapterFactory {
}

TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new InterceptorAdapter<T>(delegate, intercept);
return new InterceptorAdapter<>(delegate, intercept);
}

static class InterceptorAdapter<T> extends TypeAdapter<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (m.isAnnotationPresent(PostConstruct.class)) {
m.setAccessible(true);
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new PostConstructAdapter<T>(delegate, m);
return new PostConstructAdapter<>(delegate, m);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
private final Class<?> baseType;
private final String typeFieldName;
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>();
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<Class<?>, String>();
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<>();
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<>();
private final boolean maintainType;

private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, boolean maintainType) {
Expand All @@ -154,23 +154,23 @@ private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, boole
* {@code maintainType} flag decide if the type will be stored in pojo or not.
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) {
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, maintainType);
return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, maintainType);
}

/**
* Creates a new runtime type adapter using for {@code baseType} using {@code
* typeFieldName} as the type field name. Type field names are case sensitive.
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, false);
return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, false);
}

/**
* Creates a new runtime type adapter for {@code baseType} using {@code "type"} as
* the type field name.
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
return new RuntimeTypeAdapterFactory<T>(baseType, "type", false);
return new RuntimeTypeAdapterFactory<>(baseType, "type", false);
}

/**
Expand Down Expand Up @@ -210,10 +210,8 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
}

final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final Map<String, TypeAdapter<?>> labelToDelegate
= new LinkedHashMap<String, TypeAdapter<?>>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
= new LinkedHashMap<Class<?>, TypeAdapter<?>>();
final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public void testSerializeListOfLists() {
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();

List<List<?>> listOfLists = new ArrayList<List<?>>();
List<List<?>> listOfLists = new ArrayList<>();
listOfLists.add(listOfLists);
listOfLists.add(new ArrayList<Object>());
listOfLists.add(new ArrayList<>());

GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
Expand Down Expand Up @@ -187,7 +187,7 @@ static class Employee {

static class Company {
final String name;
final List<Employee> employees = new ArrayList<Employee>();
final List<Employee> employees = new ArrayList<>();
Company(String name) {
this.name = name;
}
Expand Down
12 changes: 6 additions & 6 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public final class Gson {
* The proxy is wired up once the initial adapter has been created.
*/
private final ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>> calls
= new ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>>();
= new ThreadLocal<>();

private final Map<TypeToken<?>, TypeAdapter<?>> typeTokenCache = new ConcurrentHashMap<TypeToken<?>, TypeAdapter<?>>();
private final Map<TypeToken<?>, TypeAdapter<?>> typeTokenCache = new ConcurrentHashMap<>();

private final ConstructorConstructor constructorConstructor;
private final JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory;
Expand Down Expand Up @@ -237,7 +237,7 @@ public Gson() {
this.numberToNumberStrategy = numberToNumberStrategy;
this.reflectionFilters = reflectionFilters;

List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
List<TypeAdapterFactory> factories = new ArrayList<>();

// built-in type adapters that cannot be overridden
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
Expand Down Expand Up @@ -453,7 +453,7 @@ private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(final TypeAda
out.endArray();
}
@Override public AtomicLongArray read(JsonReader in) throws IOException {
List<Long> list = new ArrayList<Long>();
List<Long> list = new ArrayList<>();
in.beginArray();
while (in.hasNext()) {
long value = longAdapter.read(in).longValue();
Expand Down Expand Up @@ -486,7 +486,7 @@ public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
boolean requiresThreadLocalCleanup = false;
if (threadCalls == null) {
threadCalls = new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
threadCalls = new HashMap<>();
calls.set(threadCalls);
requiresThreadLocalCleanup = true;
}
Expand All @@ -498,7 +498,7 @@ public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
}

try {
FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
FutureTypeAdapter<T> call = new FutureTypeAdapter<>();
threadCalls.put(type, call);

for (TypeAdapterFactory factory : factories) {
Expand Down
25 changes: 12 additions & 13 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ public final class GsonBuilder {
private Excluder excluder = Excluder.DEFAULT;
private LongSerializationPolicy longSerializationPolicy = LongSerializationPolicy.DEFAULT;
private FieldNamingStrategy fieldNamingPolicy = FieldNamingPolicy.IDENTITY;
private final Map<Type, InstanceCreator<?>> instanceCreators
= new HashMap<Type, InstanceCreator<?>>();
private final List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
private final Map<Type, InstanceCreator<?>> instanceCreators = new HashMap<>();
private final List<TypeAdapterFactory> factories = new ArrayList<>();
/** tree-style hierarchy factories. These come after factories for backwards compatibility. */
private final List<TypeAdapterFactory> hierarchyFactories = new ArrayList<TypeAdapterFactory>();
private final List<TypeAdapterFactory> hierarchyFactories = new ArrayList<>();
private boolean serializeNulls = DEFAULT_SERIALIZE_NULLS;
private String datePattern = DEFAULT_DATE_PATTERN;
private int dateStyle = DateFormat.DEFAULT;
Expand All @@ -102,7 +101,7 @@ public final class GsonBuilder {
private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
private ToNumberStrategy numberToNumberStrategy = DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
private final LinkedList<ReflectionAccessFilter> reflectionFilters = new LinkedList<ReflectionAccessFilter>();
private final LinkedList<ReflectionAccessFilter> reflectionFilters = new LinkedList<>();

/**
* Creates a GsonBuilder instance that can be used to build Gson with various configuration
Expand Down Expand Up @@ -227,7 +226,7 @@ public GsonBuilder serializeNulls() {
* .enableComplexMapKeySerialization()
* .create();
*
* Map<Point, String> original = new LinkedHashMap<Point, String>();
* Map<Point, String> original = new LinkedHashMap<>();
* original.put(new Point(5, 6), "a");
* original.put(new Point(8, 8), "b");
* System.out.println(gson.toJson(original, type));
Expand All @@ -254,7 +253,7 @@ public GsonBuilder serializeNulls() {
* .enableComplexMapKeySerialization()
* .create();
*
* Map<Point, String> original = new LinkedHashMap<Point, String>();
* Map<Point, String> original = new LinkedHashMap<>();
* original.put(new Point(5, 6), "a");
* original.put(new Point(8, 8), "b");
* System.out.println(gson.toJson(original, type));
Expand Down Expand Up @@ -664,23 +663,23 @@ public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
* @return an instance of Gson configured with the options currently set in this builder
*/
public Gson create() {
List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>(this.factories.size() + this.hierarchyFactories.size() + 3);
List<TypeAdapterFactory> factories = new ArrayList<>(this.factories.size() + this.hierarchyFactories.size() + 3);
factories.addAll(this.factories);
Collections.reverse(factories);

List<TypeAdapterFactory> hierarchyFactories = new ArrayList<TypeAdapterFactory>(this.hierarchyFactories);
List<TypeAdapterFactory> hierarchyFactories = new ArrayList<>(this.hierarchyFactories);
Collections.reverse(hierarchyFactories);
factories.addAll(hierarchyFactories);

addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, factories);

return new Gson(excluder, fieldNamingPolicy, new HashMap<Type, InstanceCreator<?>>(instanceCreators),
return new Gson(excluder, fieldNamingPolicy, new HashMap<>(instanceCreators),
serializeNulls, complexMapKeySerialization,
generateNonExecutableJson, escapeHtmlChars, prettyPrinting, lenient,
serializeSpecialFloatingPointValues, useJdkUnsafe, longSerializationPolicy,
datePattern, dateStyle, timeStyle, new ArrayList<TypeAdapterFactory>(this.factories),
new ArrayList<TypeAdapterFactory>(this.hierarchyFactories), factories,
objectToNumberStrategy, numberToNumberStrategy, new ArrayList<ReflectionAccessFilter>(reflectionFilters));
datePattern, dateStyle, timeStyle, new ArrayList<>(this.factories),
new ArrayList<>(this.hierarchyFactories), factories,
objectToNumberStrategy, numberToNumberStrategy, new ArrayList<>(reflectionFilters));
}

private void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,
Expand Down
4 changes: 2 additions & 2 deletions gson/src/main/java/com/google/gson/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* Creates an empty JsonArray.
*/
public JsonArray() {
elements = new ArrayList<JsonElement>();
elements = new ArrayList<>();
}

public JsonArray(int capacity) {
elements = new ArrayList<JsonElement>(capacity);
elements = new ArrayList<>(capacity);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions gson/src/main/java/com/google/gson/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
* @author Joel Leitch
*/
public final class JsonObject extends JsonElement {
private final LinkedTreeMap<String, JsonElement> members =
new LinkedTreeMap<String, JsonElement>();
private final LinkedTreeMap<String, JsonElement> members = new LinkedTreeMap<>();

/**
* Creates a deep copy of this element and all its children
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/TypeAdapterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* return null;
* }
*
* final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
* final Map<String, T> lowercaseToConstant = new HashMap<>();
* for (T constant : rawType.getEnumConstants()) {
* lowercaseToConstant.put(toLowercase(constant), constant);
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,25 @@ private static <T> ObjectConstructor<T> newDefaultImplementationConstructor(
if (SortedSet.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new TreeSet<Object>();
return (T) new TreeSet<>();
}
};
} else if (Set.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new LinkedHashSet<Object>();
return (T) new LinkedHashSet<>();
}
};
} else if (Queue.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new ArrayDeque<Object>();
return (T) new ArrayDeque<>();
}
};
} else {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new ArrayList<Object>();
return (T) new ArrayList<>();
}
};
}
Expand All @@ -305,32 +305,32 @@ private static <T> ObjectConstructor<T> newDefaultImplementationConstructor(
if (ConcurrentNavigableMap.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new ConcurrentSkipListMap<Object, Object>();
return (T) new ConcurrentSkipListMap<>();
}
};
} else if (ConcurrentMap.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new ConcurrentHashMap<Object, Object>();
return (T) new ConcurrentHashMap<>();
}
};
} else if (SortedMap.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new TreeMap<Object, Object>();
return (T) new TreeMap<>();
}
};
} else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom(
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new LinkedHashMap<Object, Object>();
return (T) new LinkedHashMap<>();
}
};
} else {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new LinkedTreeMap<String, Object>();
return (T) new LinkedTreeMap<>();
}
};
}
Expand Down
5 changes: 2 additions & 3 deletions gson/src/main/java/com/google/gson/internal/Excluder.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ public Excluder withExclusionStrategy(ExclusionStrategy exclusionStrategy,
boolean serialization, boolean deserialization) {
Excluder result = clone();
if (serialization) {
result.serializationStrategies = new ArrayList<ExclusionStrategy>(serializationStrategies);
result.serializationStrategies = new ArrayList<>(serializationStrategies);
result.serializationStrategies.add(exclusionStrategy);
}
if (deserialization) {
result.deserializationStrategies
= new ArrayList<ExclusionStrategy>(deserializationStrategies);
result.deserializationStrategies = new ArrayList<>(deserializationStrategies);
result.deserializationStrategies.add(exclusionStrategy);
}
return result;
Expand Down

0 comments on commit 4dda4ec

Please sign in to comment.