http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java deleted file mode 100644 index 0db2d57..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java +++ /dev/null @@ -1,401 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.activation; - -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.Set; -import java.util.stream.Stream; -import org.apache.zest.api.activation.Activation; -import org.apache.zest.api.activation.ActivationEvent; -import org.apache.zest.api.activation.ActivationEventListener; -import org.apache.zest.api.activation.ActivationException; -import org.apache.zest.api.activation.PassivationException; -import org.apache.zest.api.composite.ModelDescriptor; -import org.apache.zest.api.identity.Identity; -import org.apache.zest.api.service.ServiceReference; - -import static org.apache.zest.api.activation.ActivationEvent.EventType.ACTIVATED; -import static org.apache.zest.api.activation.ActivationEvent.EventType.ACTIVATING; -import static org.apache.zest.api.activation.ActivationEvent.EventType.PASSIVATED; -import static org.apache.zest.api.activation.ActivationEvent.EventType.PASSIVATING; - -/** - * This class manage Activation of a target and propagates to children. - */ -@SuppressWarnings( "raw" ) -public final class ActivationDelegate - extends ActivationEventListenerSupport -{ - private final Object target; - private final boolean fireEvents; - private ActivatorsInstance targetActivators = null; - private final LinkedList<Activation> activeChildren = new LinkedList<>(); - - /** - * Create a new ActivationDelegate that will fire events. - * @param target target of Activation - */ - public ActivationDelegate( Object target ) - { - this( target, true ); - } - - /** - * Create a new ActivationDelegate. - * @param target target of Activation - * @param fireEvents if {@link ActivationEvent}s should be fired - */ - public ActivationDelegate( Object target, boolean fireEvents ) - { - super(); - this.target = target; - this.fireEvents = fireEvents; - } - - public void activate( ActivatorsInstance targetActivators, Activation child ) - throws Exception - { - activate( targetActivators, Collections.singleton( child ), null ); - } - - public void activate( ActivatorsInstance targetActivators, Activation child, Runnable callback ) - throws Exception - { - activate( targetActivators, Collections.singleton( child ), callback ); - } - - public void activate( ActivatorsInstance targetActivators, Iterable<? extends Activation> children ) - throws ActivationException - { - activate( targetActivators, children, null ); - } - - @SuppressWarnings( "unchecked" ) - public void activate( ActivatorsInstance targetActivators, Iterable<? extends Activation> children, Runnable callback ) - throws ActivationException - { - if( this.targetActivators != null ) - { - throw new IllegalStateException( "Activation.activate() called multiple times " - + "or without calling passivate() first!" ); - } - - try - { - // Before Activation Events - if( fireEvents ) - { - fireEvent( new ActivationEvent( target, ACTIVATING ) ); - } - - // Before Activation for Activators - targetActivators.beforeActivation( target instanceof ServiceReference - ? new PassiveServiceReference( (ServiceReference) target ) - : target ); - - // Activation - for( Activation child : children ) - { - if( !activeChildren.contains( child ) ) - { - child.activate(); - } - activeChildren.addFirst( child ); - } - - // Internal Activation Callback - if( callback != null ) - { - callback.run(); - } - - // After Activation - targetActivators.afterActivation( target ); - - // After Activation Events - if( fireEvents ) - { - fireEvent( new ActivationEvent( target, ACTIVATED ) ); - } - - // Activated - this.targetActivators = targetActivators; - } - catch( Exception e ) - { - // Passivate actives - try - { - passivate(); - } - catch( PassivationException e1 ) - { - ActivationException activationEx = new ActivationException( "Unable to Activate application.", e ); - activationEx.addSuppressed( e1 ); - throw activationEx; - } - if( e instanceof ActivationException ) - { - throw ( (ActivationException) e ); - } - throw new ActivationException( "Unable to Activate application.", e ); - } - } - - public void passivate() - throws PassivationException - { - passivate( null ); - } - - @SuppressWarnings( "unchecked" ) - public void passivate( Runnable callback ) - throws PassivationException - { - Set<Exception> exceptions = new LinkedHashSet<>(); - - // Before Passivation Events - if( fireEvents ) - { - ActivationEvent event = new ActivationEvent( target, PASSIVATING ); - for( ActivationEventListener listener : listeners ) - { - try - { - listener.onEvent( event ); - } - catch( Exception ex ) - { - if( ex instanceof PassivationException ) - { - exceptions.addAll( ( (PassivationException) ex ).causes() ); - } - else - { - exceptions.add( ex ); - } - } - } - } - - // Before Passivation for Activators - if( targetActivators != null ) - { - try - { - targetActivators.beforePassivation( target ); - } - catch( PassivationException ex ) - { - exceptions.addAll( ex.causes() ); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - - // Passivation - while( !activeChildren.isEmpty() ) - { - passivateOneChild( exceptions ); - } - - // Internal Passivation Callback - if( callback != null ) - { - try - { - callback.run(); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - - // After Passivation for Activators - if( targetActivators != null ) - { - try - { - targetActivators.afterPassivation( target instanceof ServiceReference - ? new PassiveServiceReference( (ServiceReference) target ) - : target ); - } - catch( PassivationException ex ) - { - exceptions.addAll( ex.causes() ); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - targetActivators = null; - - // After Passivation Events - if( fireEvents ) - { - ActivationEvent event = new ActivationEvent( target, PASSIVATED ); - for( ActivationEventListener listener : listeners ) - { - try - { - listener.onEvent( event ); - } - catch( Exception ex ) - { - if( ex instanceof PassivationException ) - { - exceptions.addAll( ( (PassivationException) ex ).causes() ); - } - else - { - exceptions.add( ex ); - } - } - } - } - - // Error handling - if( exceptions.isEmpty() ) - { - return; - } - throw new PassivationException( exceptions ); - } - - @SuppressWarnings( "TooBroadCatch" ) - private void passivateOneChild( Set<Exception> exceptions ) - { - Activation activeChild = activeChildren.removeFirst(); - try - { - activeChild.passivate(); - } - catch( PassivationException ex ) - { - exceptions.addAll( ex.causes() ); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - - @SuppressWarnings( "raw" ) - private static class PassiveServiceReference - implements ServiceReference - { - - private final ServiceReference reference; - - private PassiveServiceReference( ServiceReference reference ) - { - this.reference = reference; - } - - @Override - public Identity identity() - { - return reference.identity(); - } - - @Override - public Object get() - { - throw new IllegalStateException( "Service is passive, either activating and" - + " cannot be used yet or passivating and cannot be used anymore." ); - } - - @Override - public boolean isActive() - { - return false; - } - - @Override - public boolean isAvailable() - { - return false; - } - - @Override - public ModelDescriptor model() - { - return reference.model(); - } - - @Override - public Stream<Class<?>> types() - { - return reference.types(); - } - - @Override - public <T> T metaInfo( Class<T> infoType ) - { - return reference.metaInfo( infoType ); - } - - @Override - public void registerActivationEventListener( ActivationEventListener listener ) - { - reference.registerActivationEventListener( listener ); - } - - @Override - public void deregisterActivationEventListener( ActivationEventListener listener ) - { - reference.deregisterActivationEventListener( listener ); - } - - @Override - public int hashCode() - { - return identity().hashCode(); - } - - @Override - public boolean equals( Object obj ) - { - if( obj == null ) - { - return false; - } - if( getClass() != obj.getClass() ) - { - return false; - } - final ServiceReference other = (ServiceReference) obj; - return identity().equals( other.identity() ); - } - - @Override - public String toString() - { - return reference.toString(); - } - } - -}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationEventListenerSupport.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationEventListenerSupport.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationEventListenerSupport.java deleted file mode 100644 index e0643b7..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationEventListenerSupport.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.activation; - -import java.util.ArrayList; -import java.util.List; -import org.apache.zest.api.activation.ActivationEvent; -import org.apache.zest.api.activation.ActivationEventListener; -import org.apache.zest.api.activation.ActivationEventListenerRegistration; - -/** - * Internal helper for managing registrations and firing events - */ -/* package */ class ActivationEventListenerSupport - implements ActivationEventListenerRegistration, ActivationEventListener -{ - protected List<ActivationEventListener> listeners = new ArrayList<>(); - - @Override - public void registerActivationEventListener( ActivationEventListener listener ) - { - List<ActivationEventListener> newListeners = new ArrayList<>(); - newListeners.addAll( listeners ); - newListeners.add( listener ); - listeners = newListeners; - } - - @Override - public void deregisterActivationEventListener( ActivationEventListener listener ) - { - List<ActivationEventListener> newListeners = new ArrayList<>(); - newListeners.addAll( listeners ); - newListeners.remove( listener ); - listeners = newListeners; - } - - /* package */ void fireEvent( ActivationEvent event ) - throws Exception - { - for( ActivationEventListener listener : listeners ) - { - listener.onEvent( event ); - } - } - - @Override - public void onEvent( ActivationEvent event ) - throws Exception - { - fireEvent( event ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorModel.java deleted file mode 100644 index fa7a3c5..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorModel.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.activation; - -import org.apache.zest.api.activation.Activator; -import org.apache.zest.api.activation.ActivatorDescriptor; -import org.apache.zest.api.common.ConstructionException; -import org.apache.zest.api.util.HierarchicalVisitor; -import org.apache.zest.api.util.VisitableHierarchy; -import org.apache.zest.runtime.composite.ConstructorsModel; -import org.apache.zest.runtime.injection.InjectedFieldsModel; -import org.apache.zest.runtime.injection.InjectedMethodsModel; -import org.apache.zest.runtime.injection.InjectionContext; - -/** - * Model for a single Activator. - * - * @param <ActivateeType> Type of the activation target - */ -public class ActivatorModel<ActivateeType> - implements ActivatorDescriptor, VisitableHierarchy<Object, Object> -{ - private final Class<? extends Activator<ActivateeType>> activatorType; - private final ConstructorsModel constructorsModel; - private final InjectedFieldsModel injectedFieldsModel; - private final InjectedMethodsModel injectedMethodsModel; - - public ActivatorModel( Class<? extends Activator<ActivateeType>> activatorType ) - { - this.activatorType = activatorType; - this.constructorsModel = new ConstructorsModel( activatorType ); - this.injectedFieldsModel = new InjectedFieldsModel( activatorType ); - this.injectedMethodsModel = new InjectedMethodsModel( activatorType ); - } - - @Override - public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super Object, ? super Object, ThrowableType> visitor ) - throws ThrowableType - { - if( visitor.visitEnter( this ) ) - { - if( constructorsModel.accept( visitor ) ) - { - if( injectedFieldsModel.accept( visitor ) ) - { - injectedMethodsModel.accept( visitor ); - } - } - } - return visitor.visitLeave( this ); - } - - public Activator<ActivateeType> newInstance() - { - try - { - return activatorType.newInstance(); - } - catch( InstantiationException | IllegalAccessException ex ) - { - throw new ConstructionException( "Could not instantiate " + activatorType.getName(), ex ); - } - } - - @SuppressWarnings( "unchecked" ) - public Activator<ActivateeType> newInstance( InjectionContext injectionContext ) - { - try - { - Activator<ActivateeType> instance = (Activator<ActivateeType>) constructorsModel.newInstance( injectionContext ); - injectionContext = new InjectionContext( injectionContext.module(), injectionContext.uses(), instance ); - inject( injectionContext, instance ); - return instance; - } - catch( Exception ex ) - { - throw new ConstructionException( "Could not instantiate " + activatorType.getName(), ex ); - } - } - - public void inject( InjectionContext injectionContext, Activator<ActivateeType> instance ) - { - injectedFieldsModel.inject( injectionContext, instance ); - injectedMethodsModel.inject( injectionContext, instance ); - } - - @Override - public String toString() - { - return activatorType.getName(); - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsInstance.java deleted file mode 100644 index 83631fc..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsInstance.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.activation; - -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.Set; -import java.util.stream.StreamSupport; -import org.apache.zest.api.activation.Activator; -import org.apache.zest.api.activation.PassivationException; - -import static java.util.stream.Collectors.toCollection; - -/** - * Instance of a Polygene Activators of one Activation target. Contains ordered - * Activators and roll the Activation on the target. - * - * @param <ActivateeType> Type of the activation target - */ -public class ActivatorsInstance<ActivateeType> - implements Activator<ActivateeType> -{ - @SuppressWarnings( { "raw", "unchecked" } ) - public static final ActivatorsInstance EMPTY = new ActivatorsInstance( Collections.emptyList() ); - - private final Iterable<Activator<ActivateeType>> activators; - - public ActivatorsInstance( Iterable<Activator<ActivateeType>> activators ) - { - this.activators = activators; - } - - @Override - public void beforeActivation( ActivateeType activating ) - throws Exception - { - for( Activator<ActivateeType> activator : activators ) - { - activator.beforeActivation( activating ); - } - } - - @Override - public void afterActivation( ActivateeType activated ) - throws Exception - { - for( Activator<ActivateeType> activator : activators ) - { - activator.afterActivation( activated ); - } - } - - @Override - public void beforePassivation( ActivateeType passivating ) - throws Exception - { - Set<Exception> exceptions = new LinkedHashSet<>(); - Iterator<Activator<ActivateeType>> iterator = reverseActivatorsIterator(); - while( iterator.hasNext() ) - { - Activator<ActivateeType> activator = iterator.next(); - try - { - activator.beforePassivation( passivating ); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - if( !exceptions.isEmpty() ) - { - throw new PassivationException( exceptions ); - } - } - - @Override - public void afterPassivation( ActivateeType passivated ) - throws Exception - { - Set<Exception> exceptions = new LinkedHashSet<>(); - Iterator<Activator<ActivateeType>> iterator = reverseActivatorsIterator(); - while( iterator.hasNext() ) - { - Activator<ActivateeType> activator = iterator.next(); - try - { - activator.afterPassivation( passivated ); - } - catch( Exception ex ) - { - exceptions.add( ex ); - } - } - if( !exceptions.isEmpty() ) - { - throw new PassivationException( exceptions ); - } - } - - private Iterator<Activator<ActivateeType>> reverseActivatorsIterator() - { - return StreamSupport.stream( activators.spliterator(), false ) - .collect( toCollection( LinkedList::new ) ) - .descendingIterator(); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsModel.java deleted file mode 100644 index 6aa25c7..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivatorsModel.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.activation; - -import java.util.ArrayList; -import java.util.List; -import org.apache.zest.api.activation.ActivationException; -import org.apache.zest.api.activation.Activator; -import org.apache.zest.api.structure.ModuleDescriptor; -import org.apache.zest.api.util.HierarchicalVisitor; -import org.apache.zest.api.util.VisitableHierarchy; -import org.apache.zest.runtime.composite.UsesInstance; -import org.apache.zest.runtime.injection.InjectionContext; - -/** - * Activators Model. - * - * @param <ActivateeType> Type of the activation target - */ -public class ActivatorsModel<ActivateeType> - implements VisitableHierarchy<Object, Object> -{ - - private final List<ActivatorModel<ActivateeType>> activatorModels = new ArrayList<>(); - private final Iterable<Class<? extends Activator<ActivateeType>>> activatorsClasses; - - public ActivatorsModel( Iterable<Class<? extends Activator<ActivateeType>>> activatorsClasses ) - { - this.activatorsClasses = activatorsClasses; - for( Class<? extends Activator<ActivateeType>> activatorClass : activatorsClasses ) - { - activatorModels.add( new ActivatorModel<>( activatorClass ) ); - } - } - - @Override - public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super Object, ? super Object, ThrowableType> visitor ) - throws ThrowableType - { - if( visitor.visitEnter( this ) ) - { - for( ActivatorModel<ActivateeType> activatorModel : activatorModels ) - { - if( !activatorModel.accept( visitor ) ) - { - break; - } - } - } - return visitor.visitLeave( this ); - } - - public Iterable<ActivatorModel<ActivateeType>> models() - { - return activatorModels; - } - - public Iterable<Activator<ActivateeType>> newInstances() - throws ActivationException - { - List<Activator<ActivateeType>> activators = new ArrayList<>(); - for( ActivatorModel<ActivateeType> activatorModel : activatorModels ) - { - activators.add( activatorModel.newInstance() ); - } - return activators; - } - - public Iterable<Activator<ActivateeType>> newInstances( ModuleDescriptor module ) - throws ActivationException - { - List<Activator<ActivateeType>> activators = new ArrayList<>(); - for( ActivatorModel<ActivateeType> activatorModel : activatorModels ) - { - InjectionContext injectionContext = new InjectionContext( module, UsesInstance.EMPTY_USES ); - activators.add( activatorModel.newInstance( injectionContext ) ); - } - return activators; - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/AbstractAssociationInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AbstractAssociationInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AbstractAssociationInstance.java deleted file mode 100644 index 6175cd4..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AbstractAssociationInstance.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.Type; -import java.util.function.BiFunction; -import org.apache.zest.api.association.AbstractAssociation; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.identity.HasIdentity; - -/** - * Implementation of AbstractAssociation. Includes helper methods for subclasses - */ -public abstract class AbstractAssociationInstance<T> - implements AbstractAssociation -{ - protected AssociationInfo associationInfo; - private final BiFunction<EntityReference, Type, Object> entityFunction; - - public AbstractAssociationInstance( AssociationInfo associationInfo, - BiFunction<EntityReference, Type, Object> entityFunction - ) - { - this.associationInfo = associationInfo; - this.entityFunction = entityFunction; - } - - public AssociationInfo associationInfo() - { - return associationInfo; - } - - public void setAssociationInfo( AssociationInfo newInfo ) - { - this.associationInfo = newInfo; - } - - @SuppressWarnings( "unchecked" ) - protected T getEntity( EntityReference entityId ) - { - if( entityId == null ) - { - return null; - } - - return (T) entityFunction.apply( entityId, associationInfo.type() ); - } - - protected EntityReference getEntityReference( Object composite ) - { - if( composite == null ) - { - return null; - } - - return EntityReference.create(((HasIdentity) composite).identity().get()); - } - - protected void checkType( Object instance ) - { - - if( instance instanceof HasIdentity || instance == null ) - { - return; - } - throw new IllegalArgumentException( "Object must be a subtype of org.apache.zest.api.reference.Identity: " + instance.getClass() ); - } - - protected void checkImmutable() - throws IllegalStateException - { - if( associationInfo.isImmutable() ) - { - throw new IllegalStateException( "Association [" + associationInfo.qualifiedName() + "] is immutable." ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInfo.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInfo.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInfo.java deleted file mode 100644 index 2e79fe1..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInfo.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.Type; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.runtime.composite.ConstraintsCheck; - -/** - * TODO - */ -public interface AssociationInfo - extends ConstraintsCheck -{ - boolean isImmutable(); - - QualifiedName qualifiedName(); - - Type type(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInstance.java deleted file mode 100644 index ae7729e..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationInstance.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -package org.apache.zest.runtime.association; - -import java.lang.reflect.Type; -import java.util.function.BiFunction; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.AssociationWrapper; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.identity.HasIdentity; -import org.apache.zest.api.property.Property; - -/** - * Implementation of Association to a single Entity. - */ -public final class AssociationInstance<T> - extends AbstractAssociationInstance<T> - implements Association<T> -{ - private Property<EntityReference> associationState; - - public AssociationInstance( AssociationInfo associationInfo, - BiFunction<EntityReference, Type, Object> entityFunction, - Property<EntityReference> associationState - ) - { - super( associationInfo, entityFunction ); - this.associationState = associationState; - } - - // Association implementation - @Override - public T get() - { - return getEntity( associationState.get() ); - } - - @Override - public void set( T newValue ) - throws IllegalArgumentException - { - checkImmutable(); - checkType( newValue ); - - associationInfo.checkConstraints( newValue ); - - // Change association - associationState.set( EntityReference.create( ((HasIdentity) newValue ).identity().get())); - } - - @Override - public EntityReference reference() - { - return associationState.get(); - } - - public Property<EntityReference> getAssociationState() - { - return associationState; - } - - @Override - public String toString() - { - if( associationState.get() == null ) - { - return ""; - } - else - { - return associationState.get().toString(); - } - } - - @Override - public int hashCode() - { - int hash = associationInfo.hashCode() * 39; // Descriptor - if( associationState.get() != null ) - { - hash = hash * 997 + associationState.get().hashCode(); // State - } - return hash; - } - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - Association<?> that = (Association) o; - // Unwrap if needed - while( that instanceof AssociationWrapper ) - { - that = ( (AssociationWrapper) that ).next(); - } - // Descriptor equality - AssociationInstance<?> thatInstance = (AssociationInstance) that; - AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo(); - if( !associationInfo.equals( thatDescriptor ) ) - { - return false; - } - // State equality - if( associationState.get() != null - ? !associationState.get().equals( thatInstance.associationState.get() ) - : thatInstance.associationState.get() != null ) - { - return false; - } - return true; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java deleted file mode 100644 index b0edd8b..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -package org.apache.zest.runtime.association; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Member; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.GenericAssociationInfo; -import org.apache.zest.api.common.MetaInfo; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.constraint.ConstraintViolationException; -import org.apache.zest.api.entity.Aggregated; -import org.apache.zest.api.entity.Queryable; -import org.apache.zest.api.property.Immutable; -import org.apache.zest.api.util.Classes; -import org.apache.zest.api.util.Visitable; -import org.apache.zest.api.util.Visitor; -import org.apache.zest.bootstrap.BindingException; -import org.apache.zest.runtime.composite.ValueConstraintsInstance; -import org.apache.zest.runtime.model.Binder; -import org.apache.zest.runtime.model.Resolution; - -/** - * Model for an Association. - * - * <p>Equality is based on the Association accessor object (associated type and name), not on the QualifiedName.</p> - */ -public final class AssociationModel - implements AssociationDescriptor, AssociationInfo, Binder, Visitable<AssociationModel> -{ - private MetaInfo metaInfo; - private Type type; - private AccessibleObject accessor; - private QualifiedName qualifiedName; - private ValueConstraintsInstance constraints; - private ValueConstraintsInstance associationConstraints; - private boolean queryable; - private boolean immutable; - private boolean aggregated; - private AssociationInfo builderInfo; - - public AssociationModel( AccessibleObject accessor, - ValueConstraintsInstance valueConstraintsInstance, - ValueConstraintsInstance associationConstraintsInstance, - MetaInfo metaInfo - ) - { - this.metaInfo = metaInfo; - this.constraints = valueConstraintsInstance; - this.associationConstraints = associationConstraintsInstance; - this.accessor = accessor; - initialize(); - } - - private void initialize() - { - this.type = GenericAssociationInfo.associationTypeOf( accessor ); - this.qualifiedName = QualifiedName.fromAccessor( accessor ); - this.immutable = metaInfo.get( Immutable.class ) != null; - this.aggregated = metaInfo.get( Aggregated.class ) != null; - - final Queryable queryable = accessor.getAnnotation( Queryable.class ); - this.queryable = queryable == null || queryable.value(); - } - - @Override - public <T> T metaInfo( Class<T> infoType ) - { - return metaInfo.get( infoType ); - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public boolean isImmutable() - { - return immutable; - } - - @Override - public boolean isAggregated() - { - return aggregated; - } - - @Override - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public boolean queryable() - { - return queryable; - } - - public AssociationInfo getBuilderInfo() - { - return builderInfo; - } - - @Override - public <ThrowableType extends Throwable> boolean accept( Visitor<? super AssociationModel, ThrowableType> visitor ) - throws ThrowableType - { - return visitor.visit( this ); - } - - @Override - public void checkConstraints( Object value ) - throws ConstraintViolationException - { - constraints.checkConstraints( value, accessor ); - } - - public void checkAssociationConstraints( Association<?> association ) - throws ConstraintViolationException - { - associationConstraints.checkConstraints( association, accessor ); - } - - @Override - public void bind( Resolution resolution ) - throws BindingException - { - builderInfo = new AssociationInfo() - { - @Override - public boolean isImmutable() - { - return false; - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public void checkConstraints( Object value ) - throws ConstraintViolationException - { - AssociationModel.this.checkConstraints( value ); - } - }; - - if( type instanceof TypeVariable ) - { - - Class mainType = resolution.model().types().findFirst().orElse( null ); - type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); - } - } - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - AssociationModel that = (AssociationModel) o; - return accessor.equals( that.accessor ); - } - - @Override - public int hashCode() - { - return accessor.hashCode(); - } - - @Override - public String toString() - { - if( accessor instanceof Field ) - { - return ( (Field) accessor ).toGenericString(); - } - else - { - return ( (Method) accessor ).toGenericString(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java deleted file mode 100644 index 8f5aa46..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Member; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.stream.Stream; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.AssociationStateHolder; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.util.HierarchicalVisitor; -import org.apache.zest.api.util.VisitableHierarchy; - -/** - * Model for Associations. - */ -public final class AssociationsModel - implements VisitableHierarchy<AssociationsModel, AssociationModel> -{ - private final Map<AccessibleObject, AssociationModel> mapAccessorAssociationModel = new LinkedHashMap<>(); - - public AssociationsModel() - { - } - - public Stream<AssociationModel> associations() - { - return mapAccessorAssociationModel.values().stream(); - } - - public void addAssociation( AssociationModel associationModel ) - { - mapAccessorAssociationModel.put( associationModel.accessor(), associationModel ); - } - - @Override - public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super AssociationsModel, ? super AssociationModel, ThrowableType> visitor ) - throws ThrowableType - { - if( visitor.visitEnter( this ) ) - { - for( AssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( !associationModel.accept( visitor ) ) - { - break; - } - } - } - return visitor.visitLeave( this ); - } - - public AssociationModel getAssociation( AccessibleObject accessor ) - throws IllegalArgumentException - { - AssociationModel associationModel = mapAccessorAssociationModel.get( accessor ); - if( associationModel == null ) - { - throw new IllegalArgumentException( "No association found with name:" + ( (Member) accessor ).getName() ); - } - return associationModel; - } - - public AssociationDescriptor getAssociationByName( String name ) - throws IllegalArgumentException - { - for( AssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( associationModel.qualifiedName().name().equals( name ) ) - { - return associationModel; - } - } - throw new IllegalArgumentException( "No association found with name:" + name ); - } - - public AssociationDescriptor getAssociationByQualifiedName( QualifiedName name ) - throws IllegalArgumentException - { - for( AssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( associationModel.qualifiedName().equals( name ) ) - { - return associationModel; - } - } - throw new IllegalArgumentException( "No association found with qualified name:" + name ); - } - - public void checkConstraints( AssociationStateHolder state ) - { - for( AssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - Association<Object> association = state.<Object>associationFor( associationModel.accessor() ); - associationModel.checkAssociationConstraints( association ); - associationModel.checkConstraints( association.get() ); - } - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationInstance.java deleted file mode 100644 index 6a50ee9..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationInstance.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.function.BiFunction; -import java.util.stream.Stream; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.association.ManyAssociationWrapper; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.identity.HasIdentity; -import org.apache.zest.api.util.NullArgumentException; -import org.apache.zest.spi.entity.ManyAssociationState; - -/** - * JAVADOC - */ -public class ManyAssociationInstance<T> - extends AbstractAssociationInstance<T> - implements ManyAssociation<T> -{ - private ManyAssociationState manyAssociationState; - - public ManyAssociationInstance( AssociationInfo associationInfo, - BiFunction<EntityReference, Type, Object> associationFunction, - ManyAssociationState manyAssociationState - ) - { - super( associationInfo, associationFunction ); - this.manyAssociationState = manyAssociationState; - } - - @Override - public int count() - { - return manyAssociationState.count(); - } - - @Override - public boolean contains( T entity ) - { - return manyAssociationState.contains( getEntityReference( entity ) ); - } - - @Override - public boolean add( int i, T entity ) - { - NullArgumentException.validateNotNull( "entity", entity ); - checkImmutable(); - checkType( entity ); - associationInfo.checkConstraints( entity ); - return manyAssociationState.add( i, EntityReference.create( ((HasIdentity) entity ).identity().get()) ); - } - - @Override - public boolean add( T entity ) - { - return add( manyAssociationState.count(), entity ); - } - - @Override - public boolean remove( T entity ) - { - NullArgumentException.validateNotNull( "entity", entity ); - checkImmutable(); - checkType( entity ); - - return manyAssociationState.remove( EntityReference.create( ((HasIdentity) entity).identity().get() ) ); - } - - @Override - public T get( int i ) - { - return getEntity( manyAssociationState.get( i ) ); - } - - @Override - public List<T> toList() - { - ArrayList<T> list = new ArrayList<>(); - for( EntityReference entityReference : manyAssociationState ) - { - list.add( getEntity( entityReference ) ); - } - - return list; - } - - @Override - public Set<T> toSet() - { - Set<T> set = new HashSet<>(); - for( EntityReference entityReference : manyAssociationState ) - { - set.add( getEntity( entityReference ) ); - } - - return set; - } - - @Override - public Stream<EntityReference> references() - { - return manyAssociationState.stream(); - } - - @Override - public String toString() - { - return manyAssociationState.toString(); - } - - @Override - public Iterator<T> iterator() - { - return new ManyAssociationIterator( manyAssociationState.iterator() ); - } - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - ManyAssociation<?> that = (ManyAssociation) o; - // Unwrap if needed - while( that instanceof ManyAssociationWrapper ) - { - that = ( (ManyAssociationWrapper) that ).next(); - } - // Descriptor equality - ManyAssociationInstance<?> thatInstance = (ManyAssociationInstance) that; - AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo(); - if( !associationInfo.equals( thatDescriptor ) ) - { - return false; - } - // State equality - if( manyAssociationState.count() != thatInstance.manyAssociationState.count() ) - { - return false; - } - for( EntityReference ref : manyAssociationState ) - { - if( !thatInstance.manyAssociationState.contains( ref ) ) - { - return false; - } - } - return true; - } - - @Override - public int hashCode() - { - int hash = associationInfo.hashCode() * 31; // Descriptor - for( EntityReference ref : manyAssociationState ) - { - hash += ref.hashCode() * 7; // State - } - return hash; - } - - public ManyAssociationState getManyAssociationState() - { - return manyAssociationState; - } - - protected class ManyAssociationIterator - implements Iterator<T> - { - private final Iterator<EntityReference> idIterator; - - public ManyAssociationIterator( Iterator<EntityReference> idIterator ) - { - this.idIterator = idIterator; - } - - @Override - public boolean hasNext() - { - return idIterator.hasNext(); - } - - @Override - public T next() - { - return getEntity( idIterator.next() ); - } - - @Override - public void remove() - { - checkImmutable(); - idIterator.remove(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java deleted file mode 100644 index 1db4d4f..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Member; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.util.List; -import java.util.function.BiFunction; -import java.util.stream.Stream; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.GenericAssociationInfo; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.common.MetaInfo; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.constraint.ConstraintViolation; -import org.apache.zest.api.constraint.ConstraintViolationException; -import org.apache.zest.api.entity.Aggregated; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.entity.Queryable; -import org.apache.zest.api.property.Immutable; -import org.apache.zest.api.util.Classes; -import org.apache.zest.api.util.Visitable; -import org.apache.zest.api.util.Visitor; -import org.apache.zest.bootstrap.BindingException; -import org.apache.zest.runtime.composite.ValueConstraintsInstance; -import org.apache.zest.runtime.model.Binder; -import org.apache.zest.runtime.model.Resolution; -import org.apache.zest.runtime.unitofwork.ModuleUnitOfWork; -import org.apache.zest.runtime.unitofwork.BuilderEntityState; -import org.apache.zest.spi.entity.EntityState; - -/** - * Model for a ManyAssociation. - * - * <p>Equality is based on the ManyAssociation accessor object (associated type and name), not on the QualifiedName.</p> - */ -public final class ManyAssociationModel - implements AssociationDescriptor, AssociationInfo, Binder, Visitable<ManyAssociationModel> -{ - private final ValueConstraintsInstance associationConstraints; - private final MetaInfo metaInfo; - private Type type; - private final AccessibleObject accessor; - private QualifiedName qualifiedName; - private final ValueConstraintsInstance constraints; - private boolean queryable; - private boolean immutable; - private boolean aggregated; - private AssociationInfo builderInfo; - - public ManyAssociationModel( AccessibleObject accessor, - ValueConstraintsInstance valueConstraintsInstance, - ValueConstraintsInstance associationConstraintsInstance, - MetaInfo metaInfo - ) - { - this.metaInfo = metaInfo; - this.constraints = valueConstraintsInstance; - this.associationConstraints = associationConstraintsInstance; - this.accessor = accessor; - initialize(); - } - - private void initialize() - { - this.type = GenericAssociationInfo.associationTypeOf( accessor ); - this.qualifiedName = QualifiedName.fromAccessor( accessor ); - this.immutable = metaInfo.get( Immutable.class ) != null; - this.aggregated = metaInfo.get( Aggregated.class ) != null; - - final Queryable queryable = accessor.getAnnotation( Queryable.class ); - this.queryable = queryable == null || queryable.value(); - } - - @Override - public <T> T metaInfo( Class<T> infoType ) - { - return metaInfo.get( infoType ); - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public boolean isImmutable() - { - return immutable; - } - - @Override - public boolean isAggregated() - { - return aggregated; - } - - @Override - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public boolean queryable() - { - return queryable; - } - - public AssociationInfo getBuilderInfo() - { - return builderInfo; - } - - public <T> ManyAssociation<T> newInstance( final ModuleUnitOfWork uow, EntityState state ) - { - return new ManyAssociationInstance<>( state instanceof BuilderEntityState ? builderInfo : this, new BiFunction<EntityReference, Type, Object>() - { - @Override - public Object apply( EntityReference entityReference, Type type ) - { - return uow.get( Classes.RAW_CLASS.apply( type ), entityReference.identity() ); - } - }, state.manyAssociationValueOf( qualifiedName ) ); - } - - @Override - public void checkConstraints( Object composite ) - throws ConstraintViolationException - { - if( constraints != null ) - { - List<ConstraintViolation> violations = constraints.checkConstraints( composite ); - if( !violations.isEmpty() ) - { - Stream<Class<?>> empty = Stream.empty(); - throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); - } - } - } - - public void checkAssociationConstraints( ManyAssociation manyAssociation ) - throws ConstraintViolationException - { - if( associationConstraints != null ) - { - List<ConstraintViolation> violations = associationConstraints.checkConstraints( manyAssociation ); - if( !violations.isEmpty() ) - { - Stream<Class<?>> empty = Stream.empty(); - throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); - } - } - } - - @Override - public <ThrowableType extends Throwable> boolean accept( Visitor<? super ManyAssociationModel, ThrowableType> visitor ) - throws ThrowableType - { - return visitor.visit( this ); - } - - @Override - public void bind( Resolution resolution ) - throws BindingException - { - builderInfo = new AssociationInfo() - { - @Override - public boolean isImmutable() - { - return false; - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public void checkConstraints( Object value ) - throws ConstraintViolationException - { - ManyAssociationModel.this.checkConstraints( value ); - } - }; - - if( type instanceof TypeVariable ) - { - Class mainType = resolution.model().types().findFirst().orElse( null ); - type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); - } - } - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - - ManyAssociationModel that = (ManyAssociationModel) o; - - return accessor.equals( that.accessor ); - } - - @Override - public int hashCode() - { - return accessor.hashCode(); - } - - @Override - public String toString() - { - if( accessor instanceof Field ) - { - return ( (Field) accessor ).toGenericString(); - } - else - { - return ( (Method) accessor ).toGenericString(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java deleted file mode 100644 index cde6d79..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Member; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.stream.Stream; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.util.HierarchicalVisitor; -import org.apache.zest.api.util.VisitableHierarchy; -import org.apache.zest.runtime.unitofwork.ModuleUnitOfWork; -import org.apache.zest.runtime.value.ValueStateInstance; -import org.apache.zest.spi.entity.EntityState; - -/** - * Model for ManyAssociations. - */ -public final class ManyAssociationsModel - implements VisitableHierarchy<ManyAssociationsModel, ManyAssociationModel> -{ - private final Map<AccessibleObject, ManyAssociationModel> mapAccessorAssociationModel = new LinkedHashMap<>(); - - public ManyAssociationsModel() - { - } - - public Stream<ManyAssociationModel> manyAssociations() - { - return mapAccessorAssociationModel.values().stream(); - } - - public void addManyAssociation( ManyAssociationModel model ) - { - mapAccessorAssociationModel.put( model.accessor(), model ); - } - - @Override - public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super ManyAssociationsModel, ? super ManyAssociationModel, ThrowableType> visitor ) - throws ThrowableType - { - if( visitor.visitEnter( this ) ) - { - for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( !associationModel.accept( visitor ) ) - { - break; - } - } - } - return visitor.visitLeave( this ); - } - - public <T> ManyAssociation<T> newInstance( AccessibleObject accessor, - EntityState entityState, - ModuleUnitOfWork uow ) - { - return mapAccessorAssociationModel.get( accessor ).newInstance( uow, entityState ); - } - - public ManyAssociationModel getManyAssociation( AccessibleObject accessor ) - throws IllegalArgumentException - { - ManyAssociationModel manyAssociationModel = mapAccessorAssociationModel.get( accessor ); - if( manyAssociationModel == null ) - { - throw new IllegalArgumentException( "No many-association found with name:" + ( (Member) accessor ).getName() ); - } - return manyAssociationModel; - } - - public AssociationDescriptor getManyAssociationByName( String name ) - throws IllegalArgumentException - { - for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( associationModel.qualifiedName().name().equals( name ) ) - { - return associationModel; - } - } - throw new IllegalArgumentException( "No many-association found with name:" + name ); - } - - public AssociationDescriptor getManyAssociationByQualifiedName( QualifiedName name ) - throws IllegalArgumentException - { - for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() ) - { - if( associationModel.qualifiedName().equals( name ) ) - { - return associationModel; - } - } - throw new IllegalArgumentException( "No many-association found with qualified name:" + name ); - } - - public void checkConstraints( ValueStateInstance state ) - { - for( ManyAssociationModel manyAssociationModel : mapAccessorAssociationModel.values() ) - { - manyAssociationModel.checkAssociationConstraints( state.manyAssociationFor( manyAssociationModel.accessor() ) ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java deleted file mode 100644 index 27e2826..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.Type; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.association.NamedAssociationWrapper; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.identity.HasIdentity; -import org.apache.zest.api.util.NullArgumentException; -import org.apache.zest.spi.entity.NamedAssociationState; - -public class NamedAssociationInstance<T> - extends AbstractAssociationInstance<T> - implements NamedAssociation<T> -{ - - private final NamedAssociationState namedAssociationState; - - public NamedAssociationInstance( AssociationInfo associationInfo, - BiFunction<EntityReference, Type, Object> associationFunction, - NamedAssociationState namedAssociationState - ) - { - super( associationInfo, associationFunction ); - this.namedAssociationState = namedAssociationState; - } - - @Override - public Iterator<String> iterator() - { - return namedAssociationState.iterator(); - } - - @Override - public int count() - { - return namedAssociationState.count(); - } - - @Override - public boolean containsName( String name ) - { - return namedAssociationState.containsName( name ); - } - - @Override - public boolean put( String name, T entity ) - { - NullArgumentException.validateNotNull( "entity", entity ); - checkImmutable(); - checkType( entity ); - associationInfo.checkConstraints( entity ); - return namedAssociationState.put( name, EntityReference.create( ((HasIdentity) entity).identity().get() ) ); - } - - @Override - public boolean remove( String name ) - { - checkImmutable(); - return namedAssociationState.remove( name ); - } - - @Override - public T get( String name ) - { - return getEntity( namedAssociationState.get( name ) ); - } - - @Override - public String nameOf( T entity ) - { - return namedAssociationState.nameOf( getEntityReference( entity ) ); - } - - @Override - public Map<String, T> toMap() - { - Map<String, T> map = new HashMap<>(); - for( String name : namedAssociationState ) - { - map.put( name, getEntity( namedAssociationState.get( name ) ) ); - } - return map; - } - - @Override - public Stream<Map.Entry<String, EntityReference>> references() - { - return namedAssociationState.stream(); - } - - @Override - public EntityReference referenceOf( String name ) - { - return namedAssociationState.get( name ); - } - - public Iterable<Map.Entry<String, EntityReference>> getEntityReferences() - { - return Collections.unmodifiableMap( - StreamSupport.stream( namedAssociationState.spliterator(), false ) - .collect( Collectors.toMap( Function.identity(), namedAssociationState::get ) ) - ).entrySet(); - } - - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - NamedAssociation<?> that = (NamedAssociation) o; - // Unwrap if needed - while( that instanceof NamedAssociationWrapper ) - { - that = ( (NamedAssociationWrapper) that ).next(); - } - // Descriptor equality - NamedAssociationInstance<?> thatInstance = (NamedAssociationInstance) that; - AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo(); - if( !associationInfo.equals( thatDescriptor ) ) - { - return false; - } - // State equality - if( namedAssociationState.count() != thatInstance.namedAssociationState.count() ) - { - return false; - } - for( String name : namedAssociationState ) - { - if( !thatInstance.namedAssociationState.containsName( name ) ) - { - return false; - } - EntityReference thisReference = namedAssociationState.get( name ); - EntityReference thatReference = thatInstance.namedAssociationState.get( name ); - if( !thisReference.equals( thatReference ) ) - { - return false; - } - } - return true; - } - - @Override - public int hashCode() - { - int hash = associationInfo.hashCode() * 31; // Descriptor - for( String name : namedAssociationState ) - { - hash += name.hashCode(); - hash += namedAssociationState.get( name ).hashCode() * 7; // State - } - return hash; - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java deleted file mode 100644 index 29e1165..0000000 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.runtime.association; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Member; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.util.List; -import java.util.function.BiFunction; -import java.util.stream.Stream; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.GenericAssociationInfo; -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.common.MetaInfo; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.constraint.ConstraintViolation; -import org.apache.zest.api.constraint.ConstraintViolationException; -import org.apache.zest.api.entity.Aggregated; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.entity.Queryable; -import org.apache.zest.api.property.Immutable; -import org.apache.zest.api.util.Classes; -import org.apache.zest.api.util.Visitable; -import org.apache.zest.api.util.Visitor; -import org.apache.zest.bootstrap.BindingException; -import org.apache.zest.runtime.composite.ValueConstraintsInstance; -import org.apache.zest.runtime.model.Binder; -import org.apache.zest.runtime.model.Resolution; -import org.apache.zest.runtime.unitofwork.ModuleUnitOfWork; -import org.apache.zest.runtime.unitofwork.BuilderEntityState; -import org.apache.zest.spi.entity.EntityState; - -/** - * Model for a NamedAssociation. - * - * <p>Equality is based on the NamedAssociation accessor object (associated type and name), not on the QualifiedName.</p> - */ -public final class NamedAssociationModel - implements AssociationDescriptor, AssociationInfo, Binder, Visitable<NamedAssociationModel> -{ - private final ValueConstraintsInstance associationConstraints; - private final MetaInfo metaInfo; - private Type type; - private final AccessibleObject accessor; - private QualifiedName qualifiedName; - private final ValueConstraintsInstance constraints; - private boolean queryable; - private boolean immutable; - private boolean aggregated; - private AssociationInfo builderInfo; - - public NamedAssociationModel( AccessibleObject accessor, - ValueConstraintsInstance valueConstraintsInstance, - ValueConstraintsInstance associationConstraintsInstance, - MetaInfo metaInfo - ) - { - this.metaInfo = metaInfo; - this.constraints = valueConstraintsInstance; - this.associationConstraints = associationConstraintsInstance; - this.accessor = accessor; - initialize(); - } - - private void initialize() - { - this.type = GenericAssociationInfo.associationTypeOf( accessor ); - this.qualifiedName = QualifiedName.fromAccessor( accessor ); - this.immutable = metaInfo.get( Immutable.class ) != null; - this.aggregated = metaInfo.get( Aggregated.class ) != null; - - final Queryable queryable = accessor.getAnnotation( Queryable.class ); - this.queryable = queryable == null || queryable.value(); - } - - @Override - public <T> T metaInfo( Class<T> infoType ) - { - return metaInfo.get( infoType ); - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public boolean isImmutable() - { - return immutable; - } - - @Override - public boolean isAggregated() - { - return aggregated; - } - - @Override - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public boolean queryable() - { - return queryable; - } - - public AssociationInfo getBuilderInfo() - { - return builderInfo; - } - - public <T> NamedAssociation<T> newInstance( final ModuleUnitOfWork uow, EntityState state ) - { - return new NamedAssociationInstance<>( state instanceof BuilderEntityState ? builderInfo : this, new BiFunction<EntityReference, Type, Object>() - { - @Override - public Object apply( EntityReference entityReference, Type type ) - { - return uow.get( Classes.RAW_CLASS.apply( type ), entityReference.identity() ); - } - }, state.namedAssociationValueOf( qualifiedName ) ); - } - - @Override - public void checkConstraints( Object composite ) - throws ConstraintViolationException - { - if( constraints != null ) - { - List<ConstraintViolation> violations = constraints.checkConstraints( composite ); - if( !violations.isEmpty() ) - { - Stream<Class<?>> empty = Stream.empty(); - throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); - } - } - } - - public void checkAssociationConstraints( NamedAssociation association ) - throws ConstraintViolationException - { - if( associationConstraints != null ) - { - List<ConstraintViolation> violations = associationConstraints.checkConstraints( association ); - if( !violations.isEmpty() ) - { - Stream<Class<?>> empty = Stream.empty(); - throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); - } - } - } - - @Override - public <ThrowableType extends Throwable> boolean accept( Visitor<? super NamedAssociationModel, ThrowableType> visitor ) - throws ThrowableType - { - return visitor.visit( this ); - } - - @Override - public void bind( Resolution resolution ) - throws BindingException - { - builderInfo = new AssociationInfo() - { - @Override - public boolean isImmutable() - { - return false; - } - - @Override - public QualifiedName qualifiedName() - { - return qualifiedName; - } - - @Override - public Type type() - { - return type; - } - - @Override - public void checkConstraints( Object value ) - throws ConstraintViolationException - { - NamedAssociationModel.this.checkConstraints( value ); - } - }; - - if( type instanceof TypeVariable ) - { - Class mainType = resolution.model().types().findFirst().orElse( null ); - type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); - } - } - - @Override - public boolean equals( Object o ) - { - if( this == o ) - { - return true; - } - if( o == null || getClass() != o.getClass() ) - { - return false; - } - - NamedAssociationModel that = (NamedAssociationModel) o; - - return accessor.equals( that.accessor ); - } - - @Override - public int hashCode() - { - return accessor.hashCode(); - } - - @Override - public String toString() - { - if( accessor instanceof Field ) - { - return ( (Field) accessor ).toGenericString(); - } - else - { - return ( (Method) accessor ).toGenericString(); - } - } -}
