Java 8 improvements:
* Use Comparator combinator
* Replace Lambda with method reference
diff --git a/src/main/java/org/apache/commons/ognl/OgnlCache.java b/src/main/java/org/apache/commons/ognl/OgnlCache.java
index 6a0fe58..2d836d2 100644
--- a/src/main/java/org/apache/commons/ognl/OgnlCache.java
+++ b/src/main/java/org/apache/commons/ognl/OgnlCache.java
@@ -112,14 +112,7 @@
cacheFactory.createClassCache( new PropertyDescriptorCacheEntryFactory() );
private final ClassCache<List<Constructor<?>>> constructorCache =
- cacheFactory.createClassCache( new ClassCacheEntryFactory<List<Constructor<?>>>()
- {
- public List<Constructor<?>> create( Class<?> key )
- throws CacheException
- {
- return Arrays.<Constructor<?>>asList( key.getConstructors() );
- }
- } );
+ cacheFactory.createClassCache(key -> Arrays.<Constructor<?>>asList( key.getConstructors() ));
private final Cache<DeclaredMethodCacheEntry, Map<String, List<Method>>> _methodCache =
cacheFactory.createCache( new DeclaredMethodCacheEntryFactory() );
@@ -131,26 +124,13 @@
cacheFactory.createClassCache( new FieldCacheEntryFactory() );
private final Cache<Method, Class<?>[]> _methodParameterTypesCache =
- cacheFactory.createCache( new CacheEntryFactory<Method, Class<?>[]>()
- {
- public Class<?>[] create( Method key )
- throws CacheException
- {
- return key.getParameterTypes( );
- }
- } );
+ cacheFactory.createCache(Method::getParameterTypes);
private final Cache<GenericMethodParameterTypeCacheEntry, Class<?>[]> _genericMethodParameterTypesCache =
cacheFactory.createCache( new GenericMethodParameterTypeFactory() );
private final Cache<Constructor<?>, Class<?>[]> _ctorParameterTypesCache =
- cacheFactory.createCache( new CacheEntryFactory<Constructor<?>, Class<?>[]>()
- {
- public Class<?>[] create( Constructor<?> key ) throws CacheException
- {
- return key.getParameterTypes( );
- }
- } );
+ cacheFactory.createCache(Constructor::getParameterTypes);
private final Cache<Method, MethodAccessEntryValue> _methodAccessCache =
cacheFactory.createCache( new MethodAccessCacheEntryFactory( ) );
diff --git a/src/main/java/org/apache/commons/ognl/OgnlOps.java b/src/main/java/org/apache/commons/ognl/OgnlOps.java
index 6254e20..a6b9eec 100644
--- a/src/main/java/org/apache/commons/ognl/OgnlOps.java
+++ b/src/main/java/org/apache/commons/ognl/OgnlOps.java
@@ -87,13 +87,13 @@
double dv1 = doubleValue( v1 ),
dv2 = doubleValue( v2 );
- return ( dv1 == dv2 ) ? 0 : ( ( dv1 < dv2 ) ? -1 : 1 );
+ return Double.compare(dv1, dv2);
default:
long lv1 = longValue( v1 ),
lv2 = longValue( v2 );
- return ( lv1 == lv2 ) ? 0 : ( ( lv1 < lv2 ) ? -1 : 1 );
+ return Long.compare(lv1, lv2);
}
}
return result;
diff --git a/src/main/java/org/apache/commons/ognl/internal/entry/MethodCacheEntryFactory.java b/src/main/java/org/apache/commons/ognl/internal/entry/MethodCacheEntryFactory.java
index 58c82cb..95b0f72 100644
--- a/src/main/java/org/apache/commons/ognl/internal/entry/MethodCacheEntryFactory.java
+++ b/src/main/java/org/apache/commons/ognl/internal/entry/MethodCacheEntryFactory.java
@@ -55,13 +55,7 @@
if ( shouldCache( key, method ) )
{
- List<Method> ml = result.get( method.getName() );
-
- if ( ml == null )
- {
- ml = new ArrayList<Method>();
- result.put( method.getName(), ml );
- }
+ List<Method> ml = result.computeIfAbsent(method.getName(), k -> new ArrayList<Method>());
ml.add( method );
}
diff --git a/src/main/java/org/apache/commons/ognl/internal/entry/PropertyDescriptorCacheEntryFactory.java b/src/main/java/org/apache/commons/ognl/internal/entry/PropertyDescriptorCacheEntryFactory.java
index 3c8616c..179e6d0 100644
--- a/src/main/java/org/apache/commons/ognl/internal/entry/PropertyDescriptorCacheEntryFactory.java
+++ b/src/main/java/org/apache/commons/ognl/internal/entry/PropertyDescriptorCacheEntryFactory.java
@@ -127,22 +127,14 @@
if ( isGet && ( parameterCount == 1 ) && ( method.getReturnType() != Void.TYPE ) )
{
- List<Method> pair = pairs.get( propertyName );
+ List<Method> pair = pairs.computeIfAbsent(propertyName, k -> new ArrayList<Method>());
- if ( pair == null )
- {
- pairs.put( propertyName, pair = new ArrayList<Method>() );
- }
pair.add( method );
}
if ( isSet && ( parameterCount == 2 ) && ( method.getReturnType() == Void.TYPE ) )
{
- List<Method> pair = pairs.get( propertyName );
+ List<Method> pair = pairs.computeIfAbsent(propertyName, k -> new ArrayList<Method>());
- if ( pair == null )
- {
- pairs.put( propertyName, pair = new ArrayList<Method>() );
- }
pair.add( method );
}
}
diff --git a/src/test/java/org/apache/commons/ognl/test/CompilingPropertyAccessor.java b/src/test/java/org/apache/commons/ognl/test/CompilingPropertyAccessor.java
index 675dac0..2b78a60 100644
--- a/src/test/java/org/apache/commons/ognl/test/CompilingPropertyAccessor.java
+++ b/src/test/java/org/apache/commons/ognl/test/CompilingPropertyAccessor.java
@@ -47,28 +47,16 @@
private static final NameFactory NAME_FACTORY = new NameFactory( "ognl.PropertyAccessor", "v" );
- private static final Getter NOT_FOUND_GETTER = new Getter()
- {
+ private static final Getter NOT_FOUND_GETTER = (context, target, propertyName) -> null;
- public Object get( OgnlContext context, Object target, String propertyName )
+ private static final Getter DEFAULT_GETTER = (context, target, propertyName) -> {
+ try
{
- return null;
+ return OgnlRuntime.getMethodValue( context, target, propertyName, true );
}
- };
-
- private static final Getter DEFAULT_GETTER = new Getter()
- {
-
- public Object get( OgnlContext context, Object target, String propertyName )
+ catch ( Exception ex )
{
- try
- {
- return OgnlRuntime.getMethodValue( context, target, propertyName, true );
- }
- catch ( Exception ex )
- {
- throw new RuntimeException( ex );
- }
+ throw new RuntimeException( ex );
}
};