http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java index 2fb8c16..ae9cf5d 100644 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java +++ b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java @@ -22,6 +22,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.zest.api.association.Association; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.AssociationStateDescriptor; @@ -45,7 +46,6 @@ import org.apache.zest.spi.ZestSPI; import org.apache.zest.spi.module.ModelModule; import org.apache.zest.spi.module.ModuleSpi; -import static org.apache.zest.functional.Iterables.map; import static org.apache.zest.library.conversion.values.Shared.STRING_COLLECTION_TYPE_SPEC; import static org.apache.zest.library.conversion.values.Shared.STRING_MAP_TYPE_SPEC; import static org.apache.zest.library.conversion.values.Shared.STRING_TYPE_SPEC; @@ -74,73 +74,50 @@ public class ValueToEntityMixin { throw new InternalError( "Zest Core Runtime codebase is corrupted. Contact Zest team: ValueToEntityMixin" ); } - MANY_ASSOC_TO_ENTITY_REF_ITERABLE = new Function<ManyAssociation<?>, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( ManyAssociation<?> manyAssoc ) + MANY_ASSOC_TO_ENTITY_REF_ITERABLE = manyAssoc -> { + if( manyAssoc == null ) { - if( manyAssoc == null ) - { - return Iterables.empty(); - } - List<EntityReference> refs = new ArrayList<>( manyAssoc.count() ); - for( Object entity : manyAssoc ) - { - refs.add( EntityReference.entityReferenceFor( entity ) ); - } - return refs; + return Iterables.empty(); + } + List<EntityReference> refs = new ArrayList<>( manyAssoc.count() ); + for( Object entity : manyAssoc ) + { + refs.add( EntityReference.entityReferenceFor( entity ) ); } + return refs; }; - NAMED_ASSOC_TO_ENTITY_REF_MAP = new Function<NamedAssociation<?>, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( NamedAssociation<?> namedAssoc ) + NAMED_ASSOC_TO_ENTITY_REF_MAP = namedAssoc -> { + if( namedAssoc == null ) { - if( namedAssoc == null ) - { - return Collections.emptyMap(); - } - Map<String, EntityReference> refs = new LinkedHashMap<>( namedAssoc.count() ); - for( String name : namedAssoc ) - { - refs.put( name, EntityReference.entityReferenceFor( namedAssoc.get( name ) ) ); - } - return refs; + return Collections.emptyMap(); + } + Map<String, EntityReference> refs = new LinkedHashMap<>( namedAssoc.count() ); + for( String name : namedAssoc ) + { + refs.put( name, EntityReference.entityReferenceFor( namedAssoc.get( name ) ) ); } + return refs; }; - STRING_COLLEC_TO_ENTITY_REF_ITERABLE = new Function<Collection<String>, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( Collection<String> stringCollec ) + STRING_COLLEC_TO_ENTITY_REF_ITERABLE = stringCollec -> { + if( stringCollec == null ) { - if( stringCollec == null ) - { - return Iterables.empty(); - } - List<EntityReference> refList = new ArrayList<>(); - for( String assId : stringCollec ) - { - refList.add( EntityReference.parseEntityReference( assId ) ); - } - return refList; + return Iterables.empty(); } + return stringCollec.stream() + .map( EntityReference::parseEntityReference ) + .collect( Collectors.toList() ); }; - STRING_MAP_TO_ENTITY_REF_MAP = new Function<Map<String, String>, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( Map<String, String> stringMap ) + STRING_MAP_TO_ENTITY_REF_MAP = stringMap -> { + if( stringMap == null ) { - if( stringMap == null ) - { - return Collections.emptyMap(); - } - Map<String, EntityReference> refMap = new LinkedHashMap<>( stringMap.size() ); - for( Map.Entry<String, String> entry : stringMap.entrySet() ) - { - refMap.put( entry.getKey(), EntityReference.parseEntityReference( entry.getValue() ) ); - } - return refMap; + return Collections.emptyMap(); + } + Map<String, EntityReference> refMap = new LinkedHashMap<>( stringMap.size() ); + for( Map.Entry<String, String> entry : stringMap.entrySet() ) + { + refMap.put( entry.getKey(), EntityReference.parseEntityReference( entry.getValue() ) ); } + return refMap; }; } @@ -166,6 +143,7 @@ public class ValueToEntityMixin public <T> T create( Class<T> entityType, Object value, Function<T, T> prototypeOpportunity ) { EntityBuilder<?> builder = doConversion( entityType, null, value ); + //noinspection unchecked prototypeOpportunity.apply( (T) builder.instance() ); return createInstance( builder ); } @@ -174,6 +152,7 @@ public class ValueToEntityMixin public <T> T create( Class<T> entityType, String identity, Object value, Function<T, T> prototypeOpportunity ) { EntityBuilder<?> builder = doConversion( entityType, identity, value ); + //noinspection unchecked prototypeOpportunity.apply( (T) builder.instance() ); return createInstance( builder ); } @@ -182,14 +161,7 @@ public class ValueToEntityMixin public <T> Iterable<T> create( final Class<T> entityType, final Iterable<Object> values ) { return Iterables.map( - new Function<Object, T>() - { - @Override - public T apply( Object value ) - { - return create( entityType, value ); - } - }, + value -> create( entityType, value ), values ); } @@ -201,14 +173,7 @@ public class ValueToEntityMixin ) { return Iterables.map( - new Function<Object, T>() - { - @Override - public T apply( Object value ) - { - return create( entityType, value, prototypeOpportunity ); - } - }, + value -> create( entityType, value, prototypeOpportunity ), values ); } @@ -220,9 +185,9 @@ public class ValueToEntityMixin { throw new EntityTypeNotFoundException( entityType.getName(), module.name(), - map( ModelModule.toStringFunction, - module.findVisibleEntityTypes() - ) ); + module.findVisibleEntityTypes() + .map( ModelModule.toStringFunction ) + ); } ValueComposite vComposite = (ValueComposite) value; @@ -244,116 +209,93 @@ public class ValueToEntityMixin final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc ) { - Function<PropertyDescriptor, Object> props = new Function<PropertyDescriptor, Object>() - { - @Override - public Object apply( PropertyDescriptor ePropDesc ) + Function<PropertyDescriptor, Object> props = ePropDesc -> { + try + { + return vState.propertyFor( ePropDesc.accessor() ).get(); + } + catch( IllegalArgumentException propNotFoundOnValue ) { + // Property not found + return null; + } + }; + Function<AssociationDescriptor, EntityReference> assocs = eAssocDesc -> { + try + { + return EntityReference.entityReferenceFor( vState.associationFor( eAssocDesc.accessor() ) ); + } + catch( IllegalArgumentException assocNotFoundOnValue ) + { + // Find String Property and convert to Association + String propName = eAssocDesc.qualifiedName().name(); try { - return vState.propertyFor( ePropDesc.accessor() ).get(); + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); + if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) + { + String assocState = (String) vState.propertyFor( vPropDesc.accessor() ).get(); + return EntityReference.parseEntityReference( assocState ); + } + return null; } catch( IllegalArgumentException propNotFoundOnValue ) { - // Property not found return null; } } }; - Function<AssociationDescriptor, EntityReference> assocs - = new Function<AssociationDescriptor, EntityReference>() - { - @Override - public EntityReference apply( AssociationDescriptor eAssocDesc ) + Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = eAssocDesc -> { + try { - try - { - return EntityReference.entityReferenceFor( vState.associationFor( eAssocDesc.accessor() ) ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find String Property and convert to Association - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocState = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - return EntityReference.parseEntityReference( assocState ); - } - return null; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return null; - } - } + ManyAssociation<Object> vAssocState = vState.manyAssociationFor( eAssocDesc.accessor() ); + return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); } - }; - Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs - = new Function<AssociationDescriptor, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( AssociationDescriptor eAssocDesc ) + catch( IllegalArgumentException assocNotFoundOnValue ) { + // Find Collection<String> Property and convert to ManyAssociation + String propName = eAssocDesc.qualifiedName().name(); try { - ManyAssociation<Object> vAssocState = vState.manyAssociationFor( eAssocDesc.accessor() ); - return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Collection<String> Property and convert to ManyAssociation - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - Collection<String> vAssocState = (Collection) vState - .propertyFor( vPropDesc.accessor() ).get(); - return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - return Iterables.empty(); - } - catch( IllegalArgumentException propNotFoundOnValue ) + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); + if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) { - return Iterables.empty(); + @SuppressWarnings( "unchecked" ) + Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); + return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); } + return Iterables.empty(); + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + return Iterables.empty(); } } }; - Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs - = new Function<AssociationDescriptor, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( AssociationDescriptor eAssocDesc ) + Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = eAssocDesc -> { + try + { + NamedAssociation<?> vAssocState = vState.namedAssociationFor( eAssocDesc.accessor() ); + return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); + } + catch( IllegalArgumentException assocNotFoundOnValue ) { + // Find Map<String,String> Property and convert to NamedAssociation + String propName = eAssocDesc.qualifiedName().name(); try { - NamedAssociation<?> vAssocState = vState.namedAssociationFor( eAssocDesc.accessor() ); - return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Map<String,String> Property and convert to NamedAssociation - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - Map<String, String> vAssocState = (Map) vState - .propertyFor( vPropDesc.accessor() ).get(); - return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - return Collections.EMPTY_MAP; - } - catch( IllegalArgumentException propNotFoundOnValue ) + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); + if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) { - return Collections.EMPTY_MAP; + //noinspection unchecked + Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); + return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); } + return Collections.EMPTY_MAP; + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + return Collections.EMPTY_MAP; } } }; @@ -367,123 +309,99 @@ public class ValueToEntityMixin final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc ) { - Function<PropertyDescriptor, Object> props - = new Function<PropertyDescriptor, Object>() - { - @Override - public Object apply( PropertyDescriptor ePropDesc ) + Function<PropertyDescriptor, Object> props = ePropDesc -> { + String propName = ePropDesc.qualifiedName().name(); + try + { + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); + return vState.propertyFor( vPropDesc.accessor() ).get(); + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + // Property not found on Value + return null; + } + }; + Function<AssociationDescriptor, EntityReference> assocs = eAssocDesc -> { + String assocName = eAssocDesc.qualifiedName().name(); + try + { + AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName( assocName ); + Object assocEntity = vState.associationFor( vAssocDesc.accessor() ).get(); + return assocEntity == null ? null : EntityReference.entityReferenceFor( assocEntity ); + } + catch( IllegalArgumentException assocNotFoundOnValue ) { - String propName = ePropDesc.qualifiedName().name(); + // Association not found on Value, find Property<String> and convert to Association try { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - return vState.propertyFor( vPropDesc.accessor() ).get(); + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); + if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) + { + String assocId = (String) vState.propertyFor( vPropDesc.accessor() ).get(); + return assocId == null ? null : EntityReference.parseEntityReference( assocId ); + } + return null; } catch( IllegalArgumentException propNotFoundOnValue ) { - // Property not found on Value return null; } } }; - Function<AssociationDescriptor, EntityReference> assocs - = new Function<AssociationDescriptor, EntityReference>() - { - @Override - public EntityReference apply( AssociationDescriptor eAssocDesc ) + Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = eAssocDesc -> { + String assocName = eAssocDesc.qualifiedName().name(); + try { - String assocName = eAssocDesc.qualifiedName().name(); - try - { - AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName( assocName ); - Object assocEntity = vState.associationFor( vAssocDesc.accessor() ).get(); - return assocEntity == null ? null : EntityReference.entityReferenceFor( assocEntity ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Association not found on Value, find Property<String> and convert to Association - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocId = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - return assocId == null ? null : EntityReference.parseEntityReference( assocId ); - } - return null; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return null; - } - } + AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName( assocName ); + ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor( vAssocDesc.accessor() ); + return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vManyAssoc ); } - }; - Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs - = new Function<AssociationDescriptor, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( AssociationDescriptor eAssocDesc ) + catch( IllegalArgumentException assocNotFoundOnValue ) { - String assocName = eAssocDesc.qualifiedName().name(); + // ManyAssociation not found on Value, find List<String> and convert to ManyAssociation try { - AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName( assocName ); - ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor( vAssocDesc.accessor() ); - return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vManyAssoc ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // ManyAssociation not found on Value, find List<String> and convert to ManyAssociation - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - Collection<String> vAssocState = (Collection) vState - .propertyFor( vPropDesc.accessor() ).get(); - return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - return Iterables.empty(); - } - catch( IllegalArgumentException propNotFoundOnValue ) + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); + if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) { - return Iterables.empty(); + @SuppressWarnings( "unchecked" ) + Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); + return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); } + return Iterables.empty(); + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + return Iterables.empty(); } } }; - Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs - = new Function<AssociationDescriptor, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( AssociationDescriptor eAssocDesc ) + Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = eAssocDesc -> { + String assocName = eAssocDesc.qualifiedName().name(); + try + { + AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName( assocName ); + NamedAssociation<Object> vAssocState = vState.namedAssociationFor( vAssocDesc.accessor() ); + return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); + } + catch( IllegalArgumentException assocNotFoundOnValue ) { - String assocName = eAssocDesc.qualifiedName().name(); + // Find Map<String,String> Property and convert to NamedAssociation try { - AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName( assocName ); - NamedAssociation<Object> vAssocState = vState.namedAssociationFor( vAssocDesc.accessor() ); - return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Map<String,String> Property and convert to NamedAssociation - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - Map<String, String> vAssocState = (Map) vState - .propertyFor( vPropDesc.accessor() ).get(); - return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - return Collections.EMPTY_MAP; - } - catch( IllegalArgumentException propNotFoundOnValue ) + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); + if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) { - return Collections.EMPTY_MAP; + @SuppressWarnings( "unchecked" ) + Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); + return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); } + return Collections.EMPTY_MAP; + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + return Collections.EMPTY_MAP; } } }; @@ -494,6 +412,7 @@ public class ValueToEntityMixin protected <T> T createInstance( EntityBuilder<?> builder ) { + //noinspection unchecked return (T) builder.newInstance(); } @@ -529,24 +448,22 @@ public class ValueToEntityMixin ) throws NoSuchEntityException { - for( PropertyDescriptor ePropDesc : eStateDesc.properties() ) + eStateDesc.properties().forEach( ePropDesc -> { - if( IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) + if( ! IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) { - // Ignore Identity, could be logged - continue; - } - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByQualifiedName( ePropDesc.qualifiedName() ); - eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found on Value, do nothing + try + { + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByQualifiedName( ePropDesc.qualifiedName() ); + eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + // Property not found on Value, do nothing + } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.associations() ) + } ); + eStateDesc.associations().forEach( eAssocDesc -> { Association<Object> eAssoc = eState.associationFor( eAssocDesc.accessor() ); try @@ -580,8 +497,8 @@ public class ValueToEntityMixin // Do nothing } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.manyAssociations() ) + }); + eStateDesc.manyAssociations().forEach( eAssocDesc -> { ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor( eAssocDesc.accessor() ); try @@ -589,14 +506,8 @@ public class ValueToEntityMixin AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByQualifiedName( eAssocDesc.qualifiedName() ); ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor( vAssocDesc.accessor() ); - for( Object assoc : eManyAssoc.toList() ) - { - eManyAssoc.remove( assoc ); - } - for( Object assoc : vManyAssoc.toList() ) - { - eManyAssoc.add( assoc ); - } + eManyAssoc.toList().forEach( eManyAssoc::remove ); + vManyAssoc.toList().forEach( eManyAssoc::add ); } catch( IllegalArgumentException assocNotFoundOnValue ) { @@ -607,11 +518,9 @@ public class ValueToEntityMixin = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) { + @SuppressWarnings( "unchecked" ) Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - for( Object assoc : eManyAssoc.toList() ) - { - eManyAssoc.remove( assoc ); - } + eManyAssoc.toList().forEach( eManyAssoc::remove ); if( vAssocState != null ) { for( String eachAssoc : vAssocState ) @@ -628,8 +537,8 @@ public class ValueToEntityMixin // Do nothing } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.namedAssociations() ) + }); + eStateDesc.namedAssociations().forEach( eAssocDesc -> { NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor( eAssocDesc.accessor() ); try @@ -637,10 +546,7 @@ public class ValueToEntityMixin AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByQualifiedName( eAssocDesc.qualifiedName() ); NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor( vAssocDesc.accessor() ); - for( String assocName : Iterables.toList( eNamedAssoc ) ) - { - eNamedAssoc.remove( assocName ); - } + Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); for( Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet() ) { eNamedAssoc.put( assocEntry.getKey(), assocEntry.getValue() ); @@ -655,11 +561,9 @@ public class ValueToEntityMixin = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) { + @SuppressWarnings( "unchecked" ) Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); - for( String assocName : Iterables.toList( eNamedAssoc ) ) - { - eNamedAssoc.remove( assocName ); - } + Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); if( vAssocState != null ) { for( Map.Entry<String, String> assocEntry : vAssocState.entrySet() ) @@ -677,7 +581,7 @@ public class ValueToEntityMixin // Do nothing } } - } + } ); } private void doUnQualifiedUpdate( @@ -685,24 +589,22 @@ public class ValueToEntityMixin AssociationStateHolder vState, AssociationStateDescriptor vStateDesc ) { - for( PropertyDescriptor ePropDesc : eStateDesc.properties() ) + eStateDesc.properties().forEach( ePropDesc -> { - if( IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) - { - // Ignore Identity, could be logged - continue; - } - try + if( ! IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( ePropDesc.qualifiedName().name() ); - eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found on Value, do nothing + try + { + PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( ePropDesc.qualifiedName().name() ); + eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); + } + catch( IllegalArgumentException propNotFoundOnValue ) + { + // Property not found on Value, do nothing + } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.associations() ) + } ); + eStateDesc.associations().forEach( eAssocDesc -> { Association<Object> eAssoc = eState.associationFor( eAssocDesc.accessor() ); try @@ -735,8 +637,8 @@ public class ValueToEntityMixin // Do nothing } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.manyAssociations() ) + } ); + eStateDesc.manyAssociations().forEach( eAssocDesc -> { ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor( eAssocDesc.accessor() ); try @@ -744,14 +646,8 @@ public class ValueToEntityMixin AssociationDescriptor vAssDesc = vStateDesc.getManyAssociationByName( eAssocDesc.qualifiedName().name() ); ManyAssociation<Object> vManyAss = vState.manyAssociationFor( vAssDesc.accessor() ); - for( Object ass : eManyAssoc.toList() ) - { - eManyAssoc.remove( ass ); - } - for( Object ass : vManyAss.toList() ) - { - eManyAssoc.add( ass ); - } + eManyAssoc.toList().forEach( eManyAssoc::remove ); + vManyAss.toList().forEach( eManyAssoc::add ); } catch( IllegalArgumentException assNotFoundOnValue ) { @@ -762,19 +658,17 @@ public class ValueToEntityMixin = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) { + @SuppressWarnings( "unchecked" ) Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - for( Object ass : eManyAssoc.toList() ) - { - eManyAssoc.remove( ass ); - } + eManyAssoc.toList().forEach( eManyAssoc::remove ); if( vAssocState != null ) { - for( String eachAssoc : vAssocState ) + vAssocState.forEach( eachAssoc -> { eManyAssoc.add( - module.currentUnitOfWork().get( (Class) eAssocDesc.type(), eachAssoc ) + module.currentUnitOfWork().get( (Class<?>) eAssocDesc.type(), eachAssoc ) ); - } + } ); } } } @@ -783,8 +677,8 @@ public class ValueToEntityMixin // Do nothing } } - } - for( AssociationDescriptor eAssocDesc : eStateDesc.namedAssociations() ) + } ); + eStateDesc.namedAssociations().forEach( eAssocDesc -> { NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor( eAssocDesc.accessor() ); try @@ -792,10 +686,7 @@ public class ValueToEntityMixin AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName( eAssocDesc.qualifiedName().name() ); NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor( vAssocDesc.accessor() ); - for( String assocName : Iterables.toList( eNamedAssoc ) ) - { - eNamedAssoc.remove( assocName ); - } + Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); for( Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet() ) { eNamedAssoc.put( assocEntry.getKey(), assocEntry.getValue() ); @@ -810,11 +701,10 @@ public class ValueToEntityMixin = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) { - Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); - for( String assocName : Iterables.toList( eNamedAssoc ) ) - { - eNamedAssoc.remove( assocName ); - } + @SuppressWarnings( "unchecked" ) + Map<String, String> vAssocState = + (Map<String,String>) vState.propertyFor( vPropDesc.accessor() ).get(); + Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); if( vAssocState != null ) { for( Map.Entry<String, String> assocEntry : vAssocState.entrySet() ) @@ -832,6 +722,6 @@ public class ValueToEntityMixin // Do nothing } } - } + } ); } }
http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/application/source/helper/ApplicationEvents.java ---------------------------------------------------------------------- diff --git a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/application/source/helper/ApplicationEvents.java b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/application/source/helper/ApplicationEvents.java index fecf3cb..6e6248d 100644 --- a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/application/source/helper/ApplicationEvents.java +++ b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/application/source/helper/ApplicationEvents.java @@ -68,50 +68,52 @@ public class ApplicationEvents // Common specifications - public static Predicate<ApplicationEvent> withNames( final Iterable<String> names ) - { - return new Predicate<ApplicationEvent>() - { - @Override - public boolean test( ApplicationEvent event ) - { - for (String name : names) - { - if (event.name().get().equals( name )) - return true; - } - return false; - } - }; - } - - public static Predicate<ApplicationEvent> withNames( final String... names ) - { - return new Predicate<ApplicationEvent>() - { - @Override - public boolean test( ApplicationEvent event ) - { - for (String name : names) - { - if (event.name().get().equals( name )) - return true; - } - return false; - } - }; - } - +// public static Predicate<ApplicationEvent> withNames( final Iterable<String> names ) +// { +// return new Predicate<ApplicationEvent>() +// { +// @Override +// public boolean test( ApplicationEvent event ) +// { +// for (String name : names) +// { +// if (event.name().get().equals( name )) +// return true; +// } +// return false; +// } +// }; +// } +// +// public static Predicate<ApplicationEvent> withNames( final String... names ) +// { +// return new Predicate<ApplicationEvent>() +// { +// @Override +// public boolean test( ApplicationEvent event ) +// { +// for (String name : names) +// { +// if (event.name().get().equals( name )) +// return true; +// } +// return false; +// } +// }; +// } +// public static Predicate<ApplicationEvent> withNames( final Class eventClass ) { - return ApplicationEvents.withNames( Iterables.map( new Function<Method, String>() - { - @Override - public String apply( Method method ) - { - return method.getName(); - } - }, Iterables.toList( Methods.METHODS_OF.apply( eventClass ) ) )); + return new WithNamesPredicate(eventClass ); + +// ApplicationEvents.withNames( Iterables.map( new Function<Method, String>() +// { +// @Override +// public String apply( Method method ) +// { +// return method.getName(); +// } +// }, Iterables.toList( Methods.METHODS_OF.apply( eventClass ) ) )); } public static Predicate<ApplicationEvent> afterDate( final Date afterDate ) @@ -191,4 +193,22 @@ public class ApplicationEvents } }; } + + private static class WithNamesPredicate implements Predicate<ApplicationEvent> + { + private final Class eventClass; + + public WithNamesPredicate( Class eventClass ) + { + this.eventClass = eventClass; + } + + @Override + public boolean test( ApplicationEvent event ) + { + return Methods.METHODS_OF.apply( eventClass ) + .map( Method::getName ) + .anyMatch( name -> event.name().get().equals( name ) ); + } + } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/factory/DomainEventFactoryService.java ---------------------------------------------------------------------- diff --git a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/factory/DomainEventFactoryService.java b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/factory/DomainEventFactoryService.java index 46c7118..c353115 100644 --- a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/factory/DomainEventFactoryService.java +++ b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/factory/DomainEventFactoryService.java @@ -1,12 +1,11 @@ /** - * * Copyright 2009-2010 Rickard Ãberg AB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,10 +16,6 @@ package org.apache.zest.library.eventsourcing.domain.factory; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONStringer; -import org.json.JSONWriter; import org.apache.zest.api.ZestAPI; import org.apache.zest.api.concern.Concerns; import org.apache.zest.api.entity.EntityComposite; @@ -30,19 +25,21 @@ import org.apache.zest.api.service.ServiceComposite; import org.apache.zest.api.value.ValueBuilder; import org.apache.zest.api.value.ValueBuilderFactory; import org.apache.zest.library.eventsourcing.domain.api.DomainEventValue; - -import static org.apache.zest.functional.Iterables.first; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONStringer; +import org.json.JSONWriter; /** * DomainEventValue factory */ -@Concerns(UnitOfWorkNotificationConcern.class) -@Mixins(DomainEventFactoryService.DomainEventFactoryMixin.class) +@Concerns( UnitOfWorkNotificationConcern.class ) +@Mixins( DomainEventFactoryService.DomainEventFactoryMixin.class ) public interface DomainEventFactoryService - extends DomainEventFactory, ServiceComposite + extends DomainEventFactory, ServiceComposite { class DomainEventFactoryMixin - implements DomainEventFactory + implements DomainEventFactory { @Structure private ValueBuilderFactory vbf; @@ -54,7 +51,11 @@ public interface DomainEventFactoryService DomainEventValue prototype = builder.prototype(); prototype.name().set( name ); - prototype.entityType().set( first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( entity ).types()).getName() ); + prototype.entityType().set( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( entity ) + .types() + .findFirst() + .get() + .getName() ); prototype.entityId().set( entity.identity().get() ); // JSON-ify parameters @@ -62,25 +63,26 @@ public interface DomainEventFactoryService try { JSONWriter params = json.object(); - for (int i = 0; i < args.length; i++) + for( int i = 0; i < args.length; i++ ) { params.key( "param" + i ); - if (args[i] == null) + if( args[ i ] == null ) + { params.value( JSONObject.NULL ); + } else - params.value( args[i] ); + { + params.value( args[ i ] ); + } } json.endObject(); - } catch (JSONException e) + } + catch( JSONException e ) { throw new IllegalArgumentException( "Could not create eventValue", e ); } - prototype.parameters().set( json.toString() ); - - DomainEventValue eventValue = builder.newInstance(); - - return eventValue; + return builder.newInstance(); } } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/source/helper/Events.java ---------------------------------------------------------------------- diff --git a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/source/helper/Events.java b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/source/helper/Events.java index dd39f64..2c9bb21 100644 --- a/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/source/helper/Events.java +++ b/libraries/eventsourcing/src/main/java/org/apache/zest/library/eventsourcing/domain/source/helper/Events.java @@ -25,8 +25,6 @@ import org.apache.zest.functional.Iterables; import org.apache.zest.library.eventsourcing.domain.api.DomainEventValue; import org.apache.zest.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue; -import static org.apache.zest.functional.Iterables.map; - /** * Helper methods for working with Iterables of DomainEvents and UnitOfWorkDomainEventsValue. */ @@ -52,151 +50,134 @@ public class Events // Common specifications public static Predicate<UnitOfWorkDomainEventsValue> afterDate( final Date afterDate ) { - return new Predicate<UnitOfWorkDomainEventsValue>() - { - @Override - public boolean test( UnitOfWorkDomainEventsValue eventValue ) - { - return eventValue.timestamp().get() > afterDate.getTime(); - } - }; + return eventValue -> eventValue.timestamp().get() > afterDate.getTime(); } public static Predicate<UnitOfWorkDomainEventsValue> beforeDate( final Date afterDate ) { - return new Predicate<UnitOfWorkDomainEventsValue>() - { - @Override - public boolean test( UnitOfWorkDomainEventsValue eventValue ) - { - return eventValue.timestamp().get() < afterDate.getTime(); - } - }; + return eventValue -> eventValue.timestamp().get() < afterDate.getTime(); } public static Predicate<UnitOfWorkDomainEventsValue> withUsecases( final String... names ) { - return new Predicate<UnitOfWorkDomainEventsValue>() - { - @Override - public boolean test( UnitOfWorkDomainEventsValue eventValue ) + return eventValue -> { + for( String name : names ) { - for (String name : names) + if( eventValue.usecase().get().equals( name ) ) { - if (eventValue.usecase().get().equals( name )) - return true; + return true; } - return false; } + return false; }; } public static Predicate<UnitOfWorkDomainEventsValue> byUser( final String... by ) { - return new Predicate<UnitOfWorkDomainEventsValue>() - { - @Override - public boolean test( UnitOfWorkDomainEventsValue eventValue ) + return eventValue -> { + for( String user : by ) { - for (String user : by) + if( eventValue.user().get().equals( user ) ) { - if (eventValue.user().get().equals( user )) - return true; + return true; } - return false; - } - }; - } - - public static Predicate<DomainEventValue> withNames( final Iterable<String> names ) - { - return new Predicate<DomainEventValue>() - { - @Override - public boolean test( DomainEventValue eventValue ) - { - for (String name : names) - { - if (eventValue.name().get().equals( name )) - return true; - } - return false; } + return false; }; } + // public static Predicate<DomainEventValue> withNames( final Iterable<String> names ) +// { +// return new Predicate<DomainEventValue>() +// { +// @Override +// public boolean test( DomainEventValue eventValue ) +// { +// for (String name : names) +// { +// if (eventValue.name().get().equals( name )) +// return true; +// } +// return false; +// } +// }; +// } +// public static Predicate<DomainEventValue> withNames( final String... names ) { - return new Predicate<DomainEventValue>() - { - @Override - public boolean test( DomainEventValue eventValue ) + return eventValue -> { + for( String name : names ) { - for (String name : names) + if( eventValue.name().get().equals( name ) ) { - if (eventValue.name().get().equals( name )) - return true; + return true; } - return false; } + return false; }; } public static Predicate<DomainEventValue> withNames( final Class eventClass ) { - return Events.withNames( map( new Function<Method, String>() - { - @Override - public String apply( Method method ) - { - return method.getName(); - } - }, Iterables.toList( Methods.METHODS_OF.apply( eventClass ) ) )); + return new WithNamesPredicate( eventClass ); +// return Events.withNames( map( new Function<Method, String>() +// { +// @Override +// public String apply( Method method ) +// { +// return method.getName(); +// } +// }, Iterables.toList( Methods.METHODS_OF.apply( eventClass ) ) )); } public static Predicate<DomainEventValue> onEntities( final String... entities ) { - return new Predicate<DomainEventValue>() - { - @Override - public boolean test( DomainEventValue eventValue ) + return eventValue -> { + for( String entity : entities ) { - for (String entity : entities) + if( eventValue.entityId().get().equals( entity ) ) { - if (eventValue.entityId().get().equals( entity )) - return true; + return true; } - return false; } + return false; }; } public static Predicate<DomainEventValue> onEntityTypes( final String... entityTypes ) { - return new Predicate<DomainEventValue>() - { - @Override - public boolean test( DomainEventValue eventValue ) + return eventValue -> { + for( String entityType : entityTypes ) { - for (String entityType : entityTypes) + if( eventValue.entityType().get().equals( entityType ) ) { - if (eventValue.entityType().get().equals( entityType )) - return true; + return true; } - return false; } + return false; }; } public static Predicate<DomainEventValue> paramIs( final String name, final String value ) { - return new Predicate<DomainEventValue>() + return eventValue -> EventParameters.getParameter( eventValue, name ).equals( value ); + } + + private static class WithNamesPredicate implements Predicate<DomainEventValue> + { + private final Class eventClass; + + public WithNamesPredicate( Class eventClass ) { - @Override - public boolean test( DomainEventValue eventValue ) - { - return EventParameters.getParameter( eventValue, name ).equals( value ); - } - }; + this.eventClass = eventClass; + } + + @Override + public boolean test( DomainEventValue event ) + { + return Methods.METHODS_OF.apply( eventClass ) + .map( Method::getName ) + .anyMatch( name -> event.name().get().equals( name ) ); + } } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java ---------------------------------------------------------------------- diff --git a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java index 1ef366d..df007fe 100644 --- a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java +++ b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java @@ -293,7 +293,7 @@ public interface ApplicationManagerService public String getType() { - Class<?> first = first( serviceDescriptor.types() ); + Class<?> first = serviceDescriptor.types().findFirst().orElse( null ); if( first == null ) { return null; @@ -303,7 +303,7 @@ public interface ApplicationManagerService public boolean isActive() { - Class<?> mainType = first( serviceDescriptor.types() ); + Class<?> mainType = serviceDescriptor.types().findFirst().orElse( null ); ServiceReference<?> first = first( filter( withId( serviceDescriptor.identity() ), module.findServices( mainType ) ) ); @@ -312,7 +312,7 @@ public interface ApplicationManagerService public boolean isAvailable() { - Class<?> mainType = first( serviceDescriptor.types() ); + Class<?> mainType = serviceDescriptor.types().findFirst().orElse( null ); ServiceReference<?> first = first( filter( withId( serviceDescriptor.identity() ), module.findServices( mainType ) ) ); @@ -321,7 +321,7 @@ public interface ApplicationManagerService public String restart() { - Iterable<?> services = module.findServices( first( serviceDescriptor.types() ) ); + Iterable<?> services = module.findServices( serviceDescriptor.types().findFirst().orElse( null ) ); ServiceReference<?> serviceRef = (ServiceReference) first( filter( withId( serviceDescriptor.identity() ), services ) ); @@ -366,7 +366,7 @@ public interface ApplicationManagerService public String getType() { - Class<?> mainType = first( serviceDescriptor.types() ); + Class<?> mainType = serviceDescriptor.types().findFirst().orElse( null ); if( mainType == null ) { return null; http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ConfigurationManagerService.java ---------------------------------------------------------------------- diff --git a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ConfigurationManagerService.java b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ConfigurationManagerService.java index 655fc31..e558a16 100644 --- a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ConfigurationManagerService.java +++ b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ConfigurationManagerService.java @@ -67,8 +67,6 @@ import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; import org.apache.zest.api.unitofwork.UnitOfWorkFactory; import org.apache.zest.spi.ZestSPI; -import static org.apache.zest.functional.Iterables.first; - /** * Expose ConfigurationComposites through JMX. * Allow configurations to be edited, and the services to be restarted. @@ -122,7 +120,7 @@ public interface ConfigurationManagerService @Service Iterable<ServiceReference<?>> configurableServices; - private List<ObjectName> configurationNames = new ArrayList<ObjectName>(); + private List<ObjectName> configurationNames = new ArrayList<>(); @Override public void exportConfigurableServices() @@ -149,7 +147,7 @@ public interface ConfigurationManagerService continue; } - String serviceClass = first(compositeInstance.types()).getName(); + String serviceClass = compositeInstance.types().findFirst().get().getName(); String name = configurableService.identity(); ServiceDescriptor serviceDescriptor = spi.serviceDescriptorFor( configurableService ); Module module = spi.moduleOf( configurableService ); @@ -157,10 +155,9 @@ public interface ConfigurationManagerService if( configurationClass != null ) { EntityDescriptor descriptor = module.entityDescriptor( configurationClass.getName() ); - List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>(); - Map<String, AccessibleObject> properties = new HashMap<String, AccessibleObject>(); - for( PropertyDescriptor persistentProperty : descriptor.state().properties() ) - { + List<MBeanAttributeInfo> attributes = new ArrayList<>(); + Map<String, AccessibleObject> properties = new HashMap<>(); + descriptor.state().properties().forEach( persistentProperty -> { if( !persistentProperty.isImmutable() ) { String propertyName = persistentProperty.qualifiedName().name(); @@ -177,7 +174,7 @@ public interface ConfigurationManagerService // Try to add legal values try { - Set<String> legalValues = new LinkedHashSet(); + Set<String> legalValues = new LinkedHashSet<>(); Class<?> enumType = getClass().getClassLoader() .loadClass( persistentProperty.valueType().mainType().getName() ); for( Field field : enumType.getFields() ) @@ -195,9 +192,9 @@ public interface ConfigurationManagerService attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ), attrDescriptor ) ); properties.put( propertyName, persistentProperty.accessor() ); } - } + } ); - List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>(); + List<MBeanOperationInfo> operations = new ArrayList<>(); operations.add( new MBeanOperationInfo( "restart", "Restart service", new MBeanParameterInfo[ 0 ], "java.lang.String", MBeanOperationInfo.ACTION_INFO ) ); MBeanInfo mbeanInfo = new MBeanInfo( serviceClass, name, attributes.toArray( new MBeanAttributeInfo[ attributes @@ -280,14 +277,15 @@ public interface ConfigurationManagerService try { EntityComposite configuration = uow.get( EntityComposite.class, identity ); - AssociationStateHolder state = spi.stateOf( (EntityComposite) configuration ); + AssociationStateHolder state = spi.stateOf( configuration ); AccessibleObject accessor = propertyNames.get( attribute.getName() ); Property<Object> property = state.propertyFor( accessor ); PropertyDescriptor propertyDescriptor = spi.propertyDescriptorFor( property ); if( EnumType.isEnum( propertyDescriptor.type() ) ) { - property.set( Enum.valueOf( (Class<Enum>) propertyDescriptor.type(), attribute.getValue() - .toString() ) ); + //noinspection unchecked + property.set( Enum.valueOf( (Class<Enum>) propertyDescriptor.type(), + attribute.getValue().toString() ) ); } else { @@ -320,15 +318,7 @@ public interface ConfigurationManagerService Object value = getAttribute( name ); list.add( new Attribute( name, value ) ); } - catch( AttributeNotFoundException e ) - { - e.printStackTrace(); - } - catch( MBeanException e ) - { - e.printStackTrace(); - } - catch( ReflectionException e ) + catch( AttributeNotFoundException | MBeanException | ReflectionException e ) { e.printStackTrace(); } @@ -350,19 +340,7 @@ public interface ConfigurationManagerService setAttribute( attribute ); list.add( attribute ); } - catch( AttributeNotFoundException e ) - { - e.printStackTrace(); - } - catch( InvalidAttributeValueException e ) - { - e.printStackTrace(); - } - catch( MBeanException e ) - { - e.printStackTrace(); - } - catch( ReflectionException e ) + catch( AttributeNotFoundException | InvalidAttributeValueException | ReflectionException | MBeanException e ) { e.printStackTrace(); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/lang-scala/src/main/java/org/apache/zest/library/scala/ScalaTraitMixin.java ---------------------------------------------------------------------- diff --git a/libraries/lang-scala/src/main/java/org/apache/zest/library/scala/ScalaTraitMixin.java b/libraries/lang-scala/src/main/java/org/apache/zest/library/scala/ScalaTraitMixin.java index be54628..95dbc5c 100644 --- a/libraries/lang-scala/src/main/java/org/apache/zest/library/scala/ScalaTraitMixin.java +++ b/libraries/lang-scala/src/main/java/org/apache/zest/library/scala/ScalaTraitMixin.java @@ -24,7 +24,6 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; -import java.util.function.Function; import org.apache.zest.api.ZestAPI; import org.apache.zest.api.common.AppliesTo; import org.apache.zest.api.common.AppliesToFilter; @@ -34,7 +33,6 @@ import org.apache.zest.api.injection.scope.Service; import org.apache.zest.api.injection.scope.This; import org.apache.zest.api.service.ServiceReference; import org.apache.zest.api.util.Classes; -import org.apache.zest.functional.Iterables; import static org.apache.zest.api.util.Classes.interfacesOf; @@ -55,7 +53,8 @@ public class ScalaTraitMixin } @Override - public Object invoke( Object composite, Method method, Object[] args ) throws Throwable + public Object invoke( Object composite, Method method, Object[] args ) + throws Throwable { InvocationHandler handler = methods.get( compositeType ).get( method ); return handler.invoke( composite, method, args ); @@ -77,7 +76,8 @@ public class ScalaTraitMixin InvocationHandler handler = new InvocationHandler() { @Override - public Object invoke( Object composite, Method method, Object[] objects ) throws Throwable + public Object invoke( Object composite, Method method, Object[] objects ) + throws Throwable { return ( (CompositeInstance) Proxy.getInvocationHandler( composite ) ).module() .findService( method.getReturnType() ); @@ -90,7 +90,8 @@ public class ScalaTraitMixin InvocationHandler handler = new InvocationHandler() { @Override - public Object invoke( Object composite, Method method, Object[] objects ) throws Throwable + public Object invoke( Object composite, Method method, Object[] objects ) + throws Throwable { return ( (CompositeInstance) Proxy.getInvocationHandler( composite ) ).module() .findService( method.getReturnType() ).get(); @@ -101,39 +102,61 @@ public class ScalaTraitMixin return true; } - // Map methods final Class<?> declaringClass = method.getDeclaringClass(); - Class traitClass = Iterables.last( Iterables.map( new Function<Class, Class>() - { - Class current; - - @Override - public Class apply( Class aClass ) - { - if( declaringClass.isAssignableFrom( aClass ) ) - { - try - { - aClass.getClassLoader().loadClass( aClass.getName() + "$class" ); - - if( current == null ) - { - current = aClass; - } - else - { - current = current.isAssignableFrom( aClass ) ? aClass : current; - } - } - catch( ClassNotFoundException e ) - { - // Ignore - no trait implementation found - } - } - - return current; - } - }, Iterables.map( Classes.RAW_CLASS, interfacesOf( compositeType ) ) ) ); + Class traitClass = interfacesOf( compositeType ).map( Classes.RAW_CLASS ) + .filter( declaringClass::isAssignableFrom ) + .reduce( null, ( current, type ) -> { + try + { + type.getClassLoader().loadClass( type.getName() + "$class" ); + if( current == null ) + { + return type; + } + else + { + return current.isAssignableFrom( type ) ? type : current; + } + } + catch( ClassNotFoundException e ) + { + // Ignore - no trait implementation found + } + return current; + } + ); + +// Class traitClass = Iterables.last( Iterables.map( new Function<Class, Class>() +// { +// Class current; +// +// @Override +// public Class apply( Class aClass ) +// { +// if( declaringClass.isAssignableFrom( aClass ) ) +// { +// try +// { +// aClass.getClassLoader().loadClass( aClass.getName() + "$class" ); +// +// if( current == null ) +// { +// current = aClass; +// } +// else +// { +// current = current.isAssignableFrom( aClass ) ? aClass : current; +// } +// } +// catch( ClassNotFoundException e ) +// { +// // Ignore - no trait implementation found +// } +// } +// +// return current; +// } +// }, Iterables.map( Classes.RAW_CLASS, interfacesOf( compositeType ) ) ) ); if( traitClass == null ) { @@ -144,30 +167,25 @@ public class ScalaTraitMixin { Class traitMixin = traitClass.getClassLoader().loadClass( traitClass.getName() + "$class" ); Class<?>[] methodParameterTypes = method.getParameterTypes(); - Class[] parameterTypes = new Class[1 + methodParameterTypes.length]; - parameterTypes[0] = traitClass; + Class[] parameterTypes = new Class[ 1 + methodParameterTypes.length ]; + parameterTypes[ 0 ] = traitClass; System.arraycopy( methodParameterTypes, 0, parameterTypes, 1, methodParameterTypes.length ); final Method traitMethod = traitMixin.getMethod( method.getName(), parameterTypes ); - Map<Method,InvocationHandler> handlers = getHandlers( compositeType ); + Map<Method, InvocationHandler> handlers = getHandlers( compositeType ); - handlers.put( method, new InvocationHandler() - { - @Override - public Object invoke( Object composite, Method method, Object[] args ) throws Throwable + handlers.put( method, ( composite, method1, args ) -> { + if( args != null ) { - if( args != null ) - { - Object[] params = new Object[args.length + 1]; - params[0] = composite; - System.arraycopy( args, 0, params, 1, args.length ); + Object[] params = new Object[ args.length + 1 ]; + params[ 0 ] = composite; + System.arraycopy( args, 0, params, 1, args.length ); - return traitMethod.invoke( null, params ); - } - else - { - return traitMethod.invoke( null, composite ); - } + return traitMethod.invoke( null, params ); + } + else + { + return traitMethod.invoke( null, composite ); } } ); @@ -202,7 +220,7 @@ public class ScalaTraitMixin private Map<Method, InvocationHandler> getHandlers( Class<?> compositeType ) { - Map<Method,InvocationHandler> handlerMap = methods.get( compositeType ); + Map<Method, InvocationHandler> handlerMap = methods.get( compositeType ); if( handlerMap == null ) { handlerMap = new HashMap<>(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebugOnConsoleSideEffect.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebugOnConsoleSideEffect.java b/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebugOnConsoleSideEffect.java index 4001486..c523a2e 100644 --- a/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebugOnConsoleSideEffect.java +++ b/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebugOnConsoleSideEffect.java @@ -29,8 +29,6 @@ import org.apache.zest.api.sideeffect.SideEffectOf; import org.apache.zest.library.logging.debug.Debug; import org.apache.zest.library.logging.log.service.LoggingService; -import static org.apache.zest.functional.Iterables.first; - /** * The DebugOnConsoleSideEffect is just a temporary solution for logging output, until a more * robust framework has been designed. @@ -62,7 +60,7 @@ public class DebugOnConsoleSideEffect extends SideEffectOf<LoggingService> private String getCompositeName( Composite composite ) { - return first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types()).getName(); + return ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types().findFirst().get().getName(); } @Override http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebuggingServiceMixin.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebuggingServiceMixin.java b/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebuggingServiceMixin.java index 80d3e12..cfa7c1b 100644 --- a/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebuggingServiceMixin.java +++ b/libraries/logging/src/main/java/org/apache/zest/library/logging/debug/service/DebuggingServiceMixin.java @@ -39,8 +39,6 @@ import org.apache.zest.library.logging.debug.records.DebugRecord; import org.apache.zest.library.logging.debug.records.EntityDebugRecordEntity; import org.apache.zest.library.logging.debug.records.ServiceDebugRecordEntity; -import static org.apache.zest.functional.Iterables.first; - public class DebuggingServiceMixin implements DebuggingService { @@ -175,6 +173,6 @@ public class DebuggingServiceMixin private String getCompositeName( Composite composite ) { - return first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types()).getName(); + return ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types().findFirst().get().getName(); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/logging/src/main/java/org/apache/zest/library/logging/log/SimpleLogConcern.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/SimpleLogConcern.java b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/SimpleLogConcern.java index 28578ce..d421435 100644 --- a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/SimpleLogConcern.java +++ b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/SimpleLogConcern.java @@ -26,20 +26,21 @@ import org.apache.zest.api.injection.scope.Structure; import org.apache.zest.api.injection.scope.This; import org.apache.zest.library.logging.log.service.LoggingService; -import static org.apache.zest.functional.Iterables.first; - public final class SimpleLogConcern implements SimpleLog { - @Structure private ZestAPI api; - @Optional @Service private LoggingService loggingService; + @Structure + private ZestAPI api; + @Optional + @Service + private LoggingService loggingService; private Composite composite; private String category; public SimpleLogConcern( @This Composite composite ) { this.composite = composite; - Class<?> type = first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types() ); + Class<?> type = ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types().findFirst().orElse( null ); category = type.getName(); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LogOnConsoleSideEffect.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LogOnConsoleSideEffect.java b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LogOnConsoleSideEffect.java index 2d83cb8..1a62d28 100644 --- a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LogOnConsoleSideEffect.java +++ b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LogOnConsoleSideEffect.java @@ -27,8 +27,6 @@ import org.apache.zest.api.injection.scope.Invocation; import org.apache.zest.api.sideeffect.SideEffectOf; import org.apache.zest.library.logging.log.LogType; -import static org.apache.zest.functional.Iterables.first; - /** * The ConsoleViewSideEffect is just a temporary solution for logging output, until a more * robust framework has been designed. @@ -54,7 +52,7 @@ public abstract class LogOnConsoleSideEffect extends SideEffectOf<LoggingService private String getCompositeName( Composite composite ) { - return first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types()).getName(); + return ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types().findFirst().get().getName(); } public void log( LogType type, Composite composite, String category, String message, Object param1 ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LoggingServiceMixin.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LoggingServiceMixin.java b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LoggingServiceMixin.java index 16b6d50..27b4fef 100644 --- a/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LoggingServiceMixin.java +++ b/libraries/logging/src/main/java/org/apache/zest/library/logging/log/service/LoggingServiceMixin.java @@ -37,7 +37,6 @@ import org.apache.zest.library.logging.log.records.EntityLogRecord; import org.apache.zest.library.logging.log.records.LogRecord; import org.apache.zest.library.logging.log.records.ServiceLogRecord; -import static org.apache.zest.functional.Iterables.first; public abstract class LoggingServiceMixin implements LoggingService @@ -166,6 +165,6 @@ public abstract class LoggingServiceMixin private String getCompositeName( Composite composite ) { - return first( ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types()).getName(); + return ZestAPI.FUNCTION_DESCRIPTOR_FOR.apply( composite ).types().findFirst().get().getName(); } }
