This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-ognl.git
The following commit(s) were added to refs/heads/master by this push: new ecabbb2 Java 8 improvements: new 812a35e Merge pull request #40 from arturobernalg/feature/java8 ecabbb2 is described below commit ecabbb231f5628980ba480970c4b298256f215c4 Author: Arturo Bernal <arturobern...@gmail.com> AuthorDate: Sun Jun 20 17:19:10 2021 +0200 Java 8 improvements: * Use Comparator combinator * Replace Lambda with method reference --- .../java/org/apache/commons/ognl/OgnlCache.java | 26 +++------------------- src/main/java/org/apache/commons/ognl/OgnlOps.java | 4 ++-- .../internal/entry/MethodCacheEntryFactory.java | 8 +------ .../entry/PropertyDescriptorCacheEntryFactory.java | 12 ++-------- .../ognl/test/CompilingPropertyAccessor.java | 24 +++++--------------- 5 files changed, 14 insertions(+), 60 deletions(-) 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 @@ public class OgnlCache { 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 @@ public class OgnlCache { 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 @@ public abstract class OgnlOps 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 @@ public abstract class MethodCacheEntryFactory<T extends MethodCacheEntry> 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 @@ public class PropertyDescriptorCacheEntryFactory 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 @@ public class CompilingPropertyAccessor 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 ); } };