Skip to content

Commit

Permalink
enhancement some code lines
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjelaili committed Mar 14, 2021
1 parent b6a6ca5 commit af15d06
Show file tree
Hide file tree
Showing 224 changed files with 2,292 additions and 2,645 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/ma/glasnost/orika/CustomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected MappedTypePair<A, B> inferTypes() {
java.lang.reflect.Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass != null && genericSuperclass instanceof ParameterizedType) {
final ParameterizedType superType = (ParameterizedType) genericSuperclass;
return new MappedTypePairHolder<A, B>(
return new MappedTypePairHolder<>(
(Type<A>) TypeFactory.valueOf(superType.getActualTypeArguments()[0]),
(Type<B>) TypeFactory.valueOf(superType.getActualTypeArguments()[1]));
} else {
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/ma/glasnost/orika/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public static <K, V> Type<Map.Entry<K, V>> entryType(Type<? extends Map<K, V>> m
}

public static <K, V> Set<MapEntry<K, V>> entrySet(Map<K, V> map) {
return new MapEntrySet<K, V>(map.entrySet());
return new MapEntrySet<>(map.entrySet());
}

private static class MapEntrySet<K, V> implements Set<MapEntry<K, V>> {

private Set<Map.Entry<K, V>> delegate;
private final Set<Map.Entry<K, V>> delegate;

private MapEntrySet(Set<Map.Entry<K, V>> delegate) {
this.delegate = delegate;
Expand All @@ -130,7 +130,7 @@ public boolean contains(Object o) {
}

public Iterator<MapEntry<K, V>> iterator() {
return new MapEntryIterator<K, V>(delegate.iterator());
return new MapEntryIterator<>(delegate.iterator());
}

public Object[] toArray() {
Expand Down Expand Up @@ -174,7 +174,7 @@ public void clear() {

private static class MapEntryIterator<K, V> implements Iterator<MapEntry<K, V>> {

private Iterator<Map.Entry<K, V>> delegate;
private final Iterator<Map.Entry<K, V>> delegate;

private MapEntryIterator(Iterator<Map.Entry<K, V>> delegate) {
this.delegate = delegate;
Expand All @@ -185,7 +185,7 @@ public boolean hasNext() {
}

public MapEntry<K, V> next() {
return new MapEntry<K, V>(delegate.next());
return new MapEntry<>(delegate.next());
}

public void remove() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/ma/glasnost/orika/MapperBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private IllegalStateException throwShouldNotCalledCustomMapper() {
* @param <B>
*/
public static class MapperBaseAdapter<A, B> extends CustomMapper<A, B> {
private MapperBase<A, B> delegate;
private final MapperBase<A, B> delegate;

public MapperBaseAdapter(MapperBase<A, B> delegate) {
this.delegate = delegate;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/ma/glasnost/orika/MapperFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

package ma.glasnost.orika;

import java.util.Set;

import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.metadata.ClassMap;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import ma.glasnost.orika.metadata.MapperKey;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.unenhance.UnenhanceStrategy;

import java.util.Set;

/**
* MapperFactory is used to both configure, register, and generate the
* underlying Mapper and Converter instances which will be used to perform the
Expand Down Expand Up @@ -265,7 +265,7 @@ <S, D> Type<? extends D> lookupConcreteDestinationType(Type<S> sourceType, Type<
* the type for which to look up mapped types
* @return the set of types which have been mapped for the specified type.
*/
Set<Type<? extends Object>> lookupMappedClasses(Type<?> type);
Set<Type<?>> lookupMappedClasses(Type<?> type);

/**
* @return a thread-safe MapperFacade instance as configured by this
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/java/ma/glasnost/orika/MappingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

package ma.glasnost.orika;

import ma.glasnost.orika.cern.colt.map.OpenIntObjectHashMap;
import ma.glasnost.orika.metadata.ClassMap;
import ma.glasnost.orika.metadata.MapperKey;
import ma.glasnost.orika.metadata.Type;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
Expand All @@ -26,11 +31,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import ma.glasnost.orika.cern.colt.map.OpenIntObjectHashMap;
import ma.glasnost.orika.metadata.ClassMap;
import ma.glasnost.orika.metadata.MapperKey;
import ma.glasnost.orika.metadata.Type;

/**
* MappingContext provides storage for information shared among the various
* mapping objects for a given mapping request.
Expand All @@ -53,16 +53,16 @@ public class MappingContext {
protected boolean capturesFieldContext;

public static enum StackElement {
SOURCE_NAME, SOURCE_TYPE, SOURCE, DEST_NAME, DEST_TYPE, DEST;
SOURCE_NAME, SOURCE_TYPE, SOURCE, DEST_NAME, DEST_TYPE, DEST
}

/**
* Factory constructs instances of the base MappingContext
*/
public static class Factory implements MappingContextFactory {

LinkedBlockingQueue<MappingContext> contextQueue = new LinkedBlockingQueue<MappingContext>();
ConcurrentHashMap<Object, Object> globalProperties = new ConcurrentHashMap<Object, Object>();
LinkedBlockingQueue<MappingContext> contextQueue = new LinkedBlockingQueue<>();
ConcurrentHashMap<Object, Object> globalProperties = new ConcurrentHashMap<>();

public MappingContext getContext() {
MappingContext context = contextQueue.poll();
Expand Down Expand Up @@ -95,7 +95,7 @@ public Map<Object, Object> getGlobalProperties() {
* @param globalProperties
*/
public MappingContext(Map<Object, Object> globalProperties) {
this.mapping = new HashMap<Type<?>, Type<?>>();
this.mapping = new HashMap<>();
this.typeCache = new OpenIntObjectHashMap();
this.globalProperties = globalProperties;
Boolean capture = globalProperties != null ? (Boolean)globalProperties.get(Properties.CAPTURE_FIELD_CONTEXT) : null;
Expand Down Expand Up @@ -175,7 +175,7 @@ public <S, D> void cacheMappedObject(S source, Type<Object> destinationType, D d
if (containsCycle) {
Map<Object, Object> localCache = (Map<Object, Object>) typeCache.get(destinationType.getUniqueIndex());
if (localCache == null) {
localCache = new IdentityHashMap<Object, Object>(2);
localCache = new IdentityHashMap<>(2);
typeCache.put(destinationType.getUniqueIndex(), localCache);

}
Expand Down Expand Up @@ -211,11 +211,11 @@ public <D> D getMappedObject(Object source, Type<?> destinationType) {
*/
public void registerMapperGeneration(ClassMap<?, ?> classMap) {
if (mappersSeen == null) {
mappersSeen = new ArrayList<Map<MapperKey, ClassMap<?, ?>>>();
mappersSeen = new ArrayList<>();
}
Map<MapperKey, ClassMap<?, ?>> list = mappersSeen.isEmpty() ? null : this.mappersSeen.get(depth - 1);
if (list == null) {
list = new HashMap<MapperKey, ClassMap<?, ?>>();
list = new HashMap<>();
}
list.put(classMap.getMapperKey(), classMap);
}
Expand Down Expand Up @@ -287,7 +287,7 @@ public void beginMapping(Type<?> sourceType, Object source, Type<?> destType, Ob
*/
public void beginMappingField(String sourceName, Type<?> sourceType, Object source, String destName, Type<?> destType, Object dest) {
if (fieldMappingStack == null) {
fieldMappingStack = new ArrayList<Object[]>();
fieldMappingStack = new ArrayList<>();
}
Object[] stackElement = new Object[StackElement.values().length];
stackElement[StackElement.SOURCE_NAME.ordinal()] = sourceName;
Expand Down Expand Up @@ -478,7 +478,7 @@ public void reset() {
*/
public void setProperty(Object key, Object value) {
if (this.properties == null) {
this.properties = new HashMap<Object, Object>();
this.properties = new HashMap<>();
}
this.properties.put(key, value);
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/ma/glasnost/orika/MappingException.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ public String getLocalizedMessage() {

StringBuilder message = new StringBuilder();
if (sourceClass != null) {
message.append("\nsourceClass = " + sourceClass);
message.append("\nsourceClass = ").append(sourceClass);
}
if (sourceType != null) {
message.append("\nsourceType = " + sourceType.toFullyQualifiedString());
message.append("\nsourceType = ").append(sourceType.toFullyQualifiedString());
}
if (sourceProperty != null) {
message.append("\nsourceProperty = " + sourceProperty);
message.append("\nsourceProperty = ").append(sourceProperty);
}
if (destinationType != null) {
message.append("\ndestinationType = " + destinationType.toFullyQualifiedString());
message.append("\ndestinationType = ").append(destinationType.toFullyQualifiedString());
}
if (destinationProperty != null) {
message.append("\ndestinationProperty = " + destinationProperty);
message.append("\ndestinationProperty = ").append(destinationProperty);
}
if (mappingStrategy != null) {
message.append("\nresolvedStrategy = " + mappingStrategy);
message.append("\nresolvedStrategy = ").append(mappingStrategy);
}
if (message.length() > 0) {
message.insert(0, "While attempting the following mapping:");
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/ma/glasnost/orika/MappingHint.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface MappingHint {
*/
class DefaultFieldMappingConverter implements DefaultFieldMapper {

private MappingHint delegate;
private final MappingHint delegate;
public DefaultFieldMappingConverter(MappingHint delegate) {
this.delegate = delegate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ public class OpenIntObjectHashMap {
*
* @serial
*/
protected int table[];
protected int[] table;

/**
* The hash table values.
*
* @serial
*/
protected Object values[];
protected Object[] values;

/**
* The state of each hash table entry (FREE, FULL, REMOVED).
*
* @serial
*/
protected byte state[];
protected byte[] state;

/**
* The number of table entries in state==FREE.
Expand Down Expand Up @@ -180,8 +180,8 @@ public Object get(int key) {
* inserted at slot index.
*/
protected int indexOfInsertion(int key) {
final int tab[] = table;
final byte stat[] = state;
final int[] tab = table;
final byte[] stat = state;
final int length = tab.length;

final int hash = key & 0x7FFFFFFF;
Expand Down Expand Up @@ -233,8 +233,8 @@ protected int indexOfInsertion(int key) {
* if the key was not found.
*/
protected int indexOfKey(int key) {
final int tab[] = table;
final byte stat[] = state;
final int[] tab = table;
final byte[] stat = state;
final int length = tab.length;

final int hash = key & 0x7FFFFFFF;
Expand Down Expand Up @@ -354,13 +354,13 @@ protected void rehash(int newCapacity) {
int oldCapacity = table.length;
// if (oldCapacity == newCapacity) return;

int oldTable[] = table;
Object oldValues[] = values;
byte oldState[] = state;
int[] oldTable = table;
Object[] oldValues = values;
byte[] oldState = state;

int newTable[] = new int[newCapacity];
Object newValues[] = new Object[newCapacity];
byte newState[] = new byte[newCapacity];
int[] newTable = new int[newCapacity];
Object[] newValues = new Object[newCapacity];
byte[] newState = new byte[newCapacity];

this.highWaterMark = chooseHighWaterMark(newCapacity, this.maxLoadFactor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author [email protected]
* @version 1.0, 09/24/99
*/
public class PrimeFinder extends Object {
public class PrimeFinder {
/**
* The largest prime this class can generate; currently equal to <tt>Integer.MAX_VALUE</tt>.
*/
Expand Down Expand Up @@ -158,7 +158,7 @@ protected PrimeFinder() {}
* from=16, to=1000
* from=1000, to=Integer.MAX_VALUE
*/
protected static void main(String args[]) {
protected static void main(String[] args) {
int from = Integer.parseInt(args[0]);
int to = Integer.parseInt(args[1]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package ma.glasnost.orika.constructor;

import com.thoughtworks.paranamer.AdaptiveParanamer;
import com.thoughtworks.paranamer.AnnotationParanamer;
import com.thoughtworks.paranamer.BytecodeReadingParanamer;
import com.thoughtworks.paranamer.CachingParanamer;
import com.thoughtworks.paranamer.Paranamer;
import ma.glasnost.orika.metadata.ConstructorParameter;
import ma.glasnost.orika.metadata.Property;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
Expand All @@ -11,17 +21,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import ma.glasnost.orika.metadata.ConstructorParameter;
import ma.glasnost.orika.metadata.Property;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeFactory;

import com.thoughtworks.paranamer.AdaptiveParanamer;
import com.thoughtworks.paranamer.AnnotationParanamer;
import com.thoughtworks.paranamer.BytecodeReadingParanamer;
import com.thoughtworks.paranamer.CachingParanamer;
import com.thoughtworks.paranamer.Paranamer;

/**
* ConstructorParameterResolver is a utility for resolving constructor
* parameters as Property instances.
Expand All @@ -34,7 +33,7 @@ public class ConstructorParameterResolver {
private final Paranamer paranamer = new CachingParanamer(new AdaptiveParanamer(new BytecodeReadingParanamer(),
new AnnotationParanamer()));

private final Map<java.lang.reflect.Type, Map<String, Set<Property>>> constructorPropertiesByType = new ConcurrentHashMap<java.lang.reflect.Type, Map<String, Set<Property>>>();
private final Map<java.lang.reflect.Type, Map<String, Set<Property>>> constructorPropertiesByType = new ConcurrentHashMap<>();

/**
* Resolves constructor arguments as properties for the specified type.
Expand All @@ -53,14 +52,14 @@ public Map<String, Set<Property>> getProperties(java.lang.reflect.Type type) {
Type<?> resolvedType = TypeFactory.valueOf(type);
if (resolvedType.isConcrete() && !resolvedType.isImmutable()) {
synchronized (resolvedType) {
Map<Constructor<?>, List<Property>> constructors = new ConcurrentHashMap<Constructor<?>, List<Property>>();
Map<Property, Map<Constructor<?>, Integer>> constructorsByProperty = new ConcurrentHashMap<Property, Map<Constructor<?>, Integer>>();
Map<Constructor<?>, List<Property>> constructors = new ConcurrentHashMap<>();
Map<Property, Map<Constructor<?>, Integer>> constructorsByProperty = new ConcurrentHashMap<>();

for (Constructor<?> constructor : resolvedType.getRawType().getConstructors()) {

String[] names = paranamer.lookupParameterNames(constructor, false);
java.lang.reflect.Type[] types = constructor.getGenericParameterTypes();
List<Property> constructorArgs = new ArrayList<Property>();
List<Property> constructorArgs = new ArrayList<>();

for (int i = 0; i < names.length; ++i) {

Expand All @@ -71,28 +70,20 @@ public Map<String, Set<Property>> getProperties(java.lang.reflect.Type type) {
propertyType = TypeFactory.resolveValueOf((Class<?>) types[i], resolvedType);
}
Property constructorArg = new ConstructorParameter(names[i], propertyType, null);

Map<Constructor<?>, Integer> associatedConstructors = constructorsByProperty.get(constructorArg);
if (associatedConstructors == null) {
associatedConstructors = new HashMap<Constructor<?>, Integer>();
constructorsByProperty.put(constructorArg, associatedConstructors);
}


Map<Constructor<?>, Integer> associatedConstructors = constructorsByProperty.computeIfAbsent(constructorArg, k -> new HashMap<>());

associatedConstructors.put(constructor, i);
constructorArgs.add(constructorArg);
}
constructors.put(constructor, constructorArgs);

}
properties = new HashMap<String, Set<Property>>();
properties = new HashMap<>();
for (Entry<Property, Map<Constructor<?>, Integer>> entry : constructorsByProperty.entrySet()) {
String key = entry.getKey().getExpression();
Set<Property> propertiesByName = properties.get(key);
if (propertiesByName == null) {
propertiesByName = new LinkedHashSet<Property>();
properties.put(key, propertiesByName);
}

Set<Property> propertiesByName = properties.computeIfAbsent(key, k -> new LinkedHashSet<>());

propertiesByName.add(new ConstructorParameter(entry.getKey().getName(), entry.getKey().getType(), entry.getValue()));
}
constructorPropertiesByType.put(type, properties);
Expand Down
Loading

0 comments on commit af15d06

Please sign in to comment.