Return-Path: X-Original-To: apmail-zest-commits-archive@minotaur.apache.org Delivered-To: apmail-zest-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EA87E18401 for ; Fri, 31 Jul 2015 02:47:21 +0000 (UTC) Received: (qmail 76248 invoked by uid 500); 31 Jul 2015 02:47:21 -0000 Delivered-To: apmail-zest-commits-archive@zest.apache.org Received: (qmail 76197 invoked by uid 500); 31 Jul 2015 02:47:21 -0000 Mailing-List: contact commits-help@zest.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zest.apache.org Delivered-To: mailing list commits@zest.apache.org Received: (qmail 75548 invoked by uid 99); 31 Jul 2015 02:47:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Jul 2015 02:47:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4CBF0E7136; Fri, 31 Jul 2015 02:47:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: niclas@apache.org To: commits@zest.apache.org Date: Fri, 31 Jul 2015 02:47:53 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [34/51] [abbrv] [partial] zest-java git commit: Revert "First round of changes to move to org.apache.zest namespace." http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/ServiceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/ServiceImporter.java b/core/api/src/main/java/org/qi4j/api/service/ServiceImporter.java new file mode 100644 index 0000000..ee3d6e5 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/ServiceImporter.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2007, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service; + +/** + * Import a service from some external source. + */ +public interface ServiceImporter +{ + /** + * Imports an instance of the service type described in the service descriptor. + * + * @param serviceDescriptor The service descriptor. + * + * @return The imported service instance. + * + * @throws ServiceImporterException if import failed. + */ + T importService( ImportedServiceDescriptor serviceDescriptor ) + throws ServiceImporterException; + + /** + * Ask if the service is available or not. + * + * @param instance the instance to be checked + * + * @return true if the service is available, false if not + */ + boolean isAvailable( T instance ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/ServiceImporterException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/ServiceImporterException.java b/core/api/src/main/java/org/qi4j/api/service/ServiceImporterException.java new file mode 100644 index 0000000..c8c8fc7 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/ServiceImporterException.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2007, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service; + +/** + * If a ServiceImporter could not import a service + * instance it must throw this exception. + */ +public class ServiceImporterException + extends RuntimeException +{ + public ServiceImporterException() + { + } + + public ServiceImporterException( String string ) + { + super( string ); + } + + public ServiceImporterException( String string, Throwable throwable ) + { + super( string, throwable ); + } + + public ServiceImporterException( Throwable throwable ) + { + super( throwable ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/ServiceReference.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/ServiceReference.java b/core/api/src/main/java/org/qi4j/api/service/ServiceReference.java new file mode 100644 index 0000000..7c616c2 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/ServiceReference.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service; + +import org.qi4j.api.activation.ActivationEventListenerRegistration; +import org.qi4j.api.structure.MetaInfoHolder; +import org.qi4j.api.type.HasTypes; + +/** + * From a ServiceReference you can access and modify metadata about a service. + * You can also access the actual service through get(), that can then be invoked. + */ +public interface ServiceReference + extends HasTypes, ActivationEventListenerRegistration, MetaInfoHolder +{ + /** + * @return the service's identity + */ + String identity(); + + /** + * @return the actual service + */ + T get(); + + /** + * @return TRUE if the service is active, otherwise return FALSE + */ + boolean isActive(); + + /** + * @return TRUE if the service is available, otherwise return FALSE + */ + boolean isAvailable(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/ServiceUnavailableException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/ServiceUnavailableException.java b/core/api/src/main/java/org/qi4j/api/service/ServiceUnavailableException.java new file mode 100644 index 0000000..465c945 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/ServiceUnavailableException.java @@ -0,0 +1,35 @@ +/* + * Copyright 2008 Niclas Hedhman. + * + * 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 + * + * 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.qi4j.api.service; + +/** + * Thrown when no available service is found. + */ +public class ServiceUnavailableException + extends RuntimeException +{ + public ServiceUnavailableException( String message ) + { + super( message ); + } + + public ServiceUnavailableException( String message, Throwable cause ) + { + super( message, cause ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/importer/InstanceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/importer/InstanceImporter.java b/core/api/src/main/java/org/qi4j/api/service/importer/InstanceImporter.java new file mode 100644 index 0000000..38901fb --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/importer/InstanceImporter.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.importer; + +import org.qi4j.api.injection.scope.Structure; +import org.qi4j.api.service.ImportedServiceDescriptor; +import org.qi4j.api.service.ServiceImporter; +import org.qi4j.api.service.ServiceImporterException; +import org.qi4j.api.structure.Application; +import org.qi4j.api.structure.Layer; +import org.qi4j.api.structure.MetaInfoHolder; +import org.qi4j.api.structure.Module; +import org.qi4j.functional.Function; +import org.qi4j.functional.Iterables; + +import static org.qi4j.functional.Iterables.filter; +import static org.qi4j.functional.Iterables.first; +import static org.qi4j.functional.Iterables.map; +import static org.qi4j.functional.Specifications.notNull; + +/** + * Return a predefined service instance that was provided as meta-info. Search for meta-info in the following order: + * the service itself, the module of the service, the layer of the service, the whole application. + */ +public final class InstanceImporter + implements ServiceImporter +{ + @Structure + private Application application; + + @Structure + private Layer layer; + + @Structure + private Module module; + + @Override + public T importService( final ImportedServiceDescriptor serviceDescriptor ) + throws ServiceImporterException + { + T instance = null; + Iterable holders = Iterables.iterable( serviceDescriptor, module, layer, application ); + for( final MetaInfoHolder metaInfoHolder : holders ) + { + Function, T> metaFinder = new Function, T>() + { + @Override + @SuppressWarnings( "unchecked" ) + public T map( Class type ) + { + return (T) metaInfoHolder.metaInfo( type ); + } + }; + instance = first( filter( notNull(), map( metaFinder, serviceDescriptor.types() ) ) ); + if( instance != null ) + { + break; + } + } + return instance; + } + + @Override + public boolean isAvailable( T instance ) + { + return true; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/importer/NewObjectImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/importer/NewObjectImporter.java b/core/api/src/main/java/org/qi4j/api/service/importer/NewObjectImporter.java new file mode 100644 index 0000000..d29b1ce --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/importer/NewObjectImporter.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.importer; + +import org.qi4j.api.injection.scope.Structure; +import org.qi4j.api.object.ObjectFactory; +import org.qi4j.api.service.ImportedServiceDescriptor; +import org.qi4j.api.service.ServiceImporter; +import org.qi4j.api.service.ServiceImporterException; +import org.qi4j.functional.Iterables; + +/** + * Import Services using a new registered Object instance. + */ +public final class NewObjectImporter + implements ServiceImporter +{ + @Structure + private ObjectFactory obf; + + @Override + @SuppressWarnings( "unchecked" ) + public T importService( ImportedServiceDescriptor serviceDescriptor ) + throws ServiceImporterException + { + return obf.newObject( (Class) Iterables.first( serviceDescriptor.types() ) ); + } + + @Override + public boolean isAvailable( T instance ) + { + return true; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/importer/ServiceInstanceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/importer/ServiceInstanceImporter.java b/core/api/src/main/java/org/qi4j/api/service/importer/ServiceInstanceImporter.java new file mode 100644 index 0000000..7910228 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/importer/ServiceInstanceImporter.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.importer; + +import org.qi4j.api.injection.scope.Structure; +import org.qi4j.api.service.ImportedServiceDescriptor; +import org.qi4j.api.service.ServiceFinder; +import org.qi4j.api.service.ServiceImporter; +import org.qi4j.api.service.ServiceImporterException; +import org.qi4j.api.service.ServiceReference; + +/** + * Use a registered service that implements ServiceImporter to do the actual + * import. The service id of the service that this importer should delegate to must + * be set as meta-info on this service. Example: + *

+ * module.services(MyServiceImporterService.class).identifiedBy("someid");
+ * module.importedServices(OtherService.class).importedBy(ServiceInstanceImporter.class).setMetaInfo("someid");
+ * 
+ */ +public class ServiceInstanceImporter + implements ServiceImporter +{ + @Structure + ServiceFinder finder; + + ServiceImporter service; + + String serviceId; + + @Override + public T importService( ImportedServiceDescriptor importedServiceDescriptor ) + throws ServiceImporterException + { + serviceId = importedServiceDescriptor.metaInfo( String.class ); + + return serviceImporter().importService( importedServiceDescriptor ); + } + + @Override + public boolean isAvailable( T instance ) + { + return serviceImporter().isAvailable( instance ); + } + + @SuppressWarnings( {"raw", "unchecked"} ) + private ServiceImporter serviceImporter() + { + if( service == null ) + { + for( ServiceReference reference : finder.findServices( ServiceImporter.class ) ) + { + if( reference.identity().equals( serviceId ) ) + { + service = reference.get(); + break; + } + } + } + + if( service == null ) + { + throw new ServiceImporterException( "No service importer with id '" + serviceId + "' was found" ); + } + + return service; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/importer/ServiceSelectorImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/importer/ServiceSelectorImporter.java b/core/api/src/main/java/org/qi4j/api/service/importer/ServiceSelectorImporter.java new file mode 100644 index 0000000..ad3c757 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/importer/ServiceSelectorImporter.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.importer; + +import java.util.ArrayList; +import java.util.List; +import org.qi4j.api.injection.scope.Structure; +import org.qi4j.api.service.Availability; +import org.qi4j.api.service.ImportedServiceDescriptor; +import org.qi4j.api.service.ServiceFinder; +import org.qi4j.api.service.ServiceImporter; +import org.qi4j.api.service.ServiceImporterException; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.api.service.qualifier.ServiceQualifier; +import org.qi4j.functional.Iterables; +import org.qi4j.functional.Specification; + +/** + * If several services are available with a given type, and you want to constrain + * the current module to use a specific one, then use this importer. Specify a + * Specification<ServiceReference<T>> criteria as meta-info for the service, which will be applied + * to the list of available services, and the first match will be chosen. + * + * This importer will avoid selecting itself, as could be possible if the ServiceQualifier.first() + * filter is used. + */ +public final class ServiceSelectorImporter + implements ServiceImporter +{ + @Structure + private ServiceFinder locator; + + @Override + @SuppressWarnings( { "raw", "unchecked" } ) + public T importService( ImportedServiceDescriptor serviceDescriptor ) + throws ServiceImporterException + { + Specification> selector = serviceDescriptor.metaInfo( Specification.class ); + Class serviceType = Iterables.first( serviceDescriptor.types() ); + Iterable> services = locator.findServices( serviceType ); + List> filteredServices = new ArrayList<>(); + for( ServiceReference service : services ) + { + Specification selector1 = service.metaInfo( Specification.class ); + if( selector1 != null && selector1 == selector ) + { + continue; + } + + filteredServices.add( service ); + } + T service = ServiceQualifier.firstService( selector, filteredServices ); + if( service == null ) + { + throw new ServiceImporterException( "Could not find any service to import that matches the given specification for " + serviceDescriptor + .identity() ); + } + return service; + } + + @Override + public boolean isAvailable( T instance ) + { + return !( instance instanceof Availability ) || ( (Availability) instance ).isAvailable(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/importer/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/importer/package.html b/core/api/src/main/java/org/qi4j/api/service/importer/package.html new file mode 100644 index 0000000..d960b3d --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/importer/package.html @@ -0,0 +1,21 @@ + + + +

Service Importers.

+ + http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/package.html b/core/api/src/main/java/org/qi4j/api/service/package.html new file mode 100644 index 0000000..587937c --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/package.html @@ -0,0 +1,21 @@ + + + +

Service API.

+ + http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/Active.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/Active.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/Active.java new file mode 100644 index 0000000..d06a749 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/Active.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Filter services based on whether they are active or not. + *

+ * At an injection point you can do this: + *

+ *

+ * @Service @Active MyService service;
+ * 
+ *

+ * to get only a service that is currently active. + *

+ */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( Active.ActiveQualifier.class ) +public @interface Active +{ + /** + * Active Annotation Qualifier. + * See {@link Active}. + */ + public final class ActiveQualifier + implements AnnotationQualifier + { + @Override + public Specification> qualifier( Active active ) + { + return ServiceQualifier.whereActive(); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/AnnotationQualifier.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/AnnotationQualifier.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/AnnotationQualifier.java new file mode 100644 index 0000000..7187ed1 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/AnnotationQualifier.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Annotation; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Constructs a Specification for a given qualifier annotation + */ +public interface AnnotationQualifier +{ + public Specification> qualifier( QUALIFIER qualifier ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/Available.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/Available.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/Available.java new file mode 100644 index 0000000..894451d --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/Available.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Filter services based on whether they are available or not. + * + * At an injection point you can do this: + * + *

+ * @Service @Available MyService service;
+ * 
+ * to get only a service that is currently available. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( Available.AvailableQualifier.class ) +public @interface Available +{ + /** + * Available Annotation Qualifier. + * See {@link Available}. + */ + public final class AvailableQualifier + implements AnnotationQualifier + { + @Override + public Specification> qualifier( Available active ) + { + return ServiceQualifier.whereAvailable(); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/HasMetaInfo.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/HasMetaInfo.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/HasMetaInfo.java new file mode 100644 index 0000000..aac0702 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/HasMetaInfo.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2009, Niclas Hedhman. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Filter services based on Meta Info being declared on the Service. + *

+ * Meta Info of any type can be set on the service during assembly, e.g.; + *

+ *

+ * module.addService( MyService.class ).setMetaInfo( new MyCustomInfo(someData) );
+ * 
+ *

+ * and then at an injection point you can do this: + *

+ *

+ * @Service @HasMetaInfo(MyCustomInfo.class) MyService service;
+ * 
+ *

+ * to get only a service that has a MyCustomInfo instance set as meta info. + *

+ */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( HasMetaInfo.HasMetaInfoQualifier.class ) +@Documented +public @interface HasMetaInfo +{ + /** + * The Class(es) needed to have been defined in the Service meta info for a qualifier to evaluate true. + * + * @return One or more classes that should be defined in the service's meta info for the service to be considered + * qualified. If more than one class is defined, the {@code anded()} parameter will define if they must be + * AND'ed or OR'ed together. + */ + Class[] value(); + + /** + * True if the Classes defined in the value() field should be AND'ed instead of OR'ed. + * + * @return If true, all the Class types defined in {@code value()} must be defined for the service for it to be + * qualified. If false, if any of the Class types defined in {@code value()} is defined for the service + * the service is qualified. + */ + boolean anded() default false; + + /** + * HasMetaInfo Annotation Qualifier. + * See {@link HasMetaInfo}. + */ + public static class HasMetaInfoQualifier + implements AnnotationQualifier + { + @Override + public Specification> qualifier( final HasMetaInfo hasMetaInfo ) + { + return new Specification>() + { + @Override + @SuppressWarnings( {"raw", "unchecked"} ) + public boolean satisfiedBy( ServiceReference service ) + { + for( Class metaInfoType : hasMetaInfo.value() ) + { + Object metaInfo = service.metaInfo( metaInfoType ); + if( hasMetaInfo.anded() ) + { + if( metaInfo == null ) + { + return false; + } + } + else + { + if( metaInfo != null ) + { + return true; + } + } + } + return false; + } + }; + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/IdentifiedBy.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/IdentifiedBy.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/IdentifiedBy.java new file mode 100644 index 0000000..2c715d7 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/IdentifiedBy.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Filter services based on identity. Identity can be set during assembly, like so: + *

+ * module.addService(MyService.class).identifiedBy("myservice1");
+ * 
+ * + * and then at an injection point you can do this: + *

+ * @Service @IdentifiedBy("myservice1") MyService service;
+ * 
+ * to get only a service identified "myservice1". + */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( IdentifiedBy.IdentifiedByQualifier.class ) +public @interface IdentifiedBy +{ + public abstract String value(); + + /** + * IdentifiedBy Annotation Qualifier. + * See {@link IdentifiedBy}. + */ + public final class IdentifiedByQualifier + implements AnnotationQualifier + { + @Override + public Specification> qualifier( IdentifiedBy identifiedBy ) + { + return ServiceQualifier.withId( identifiedBy.value() ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/Qualifier.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/Qualifier.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/Qualifier.java new file mode 100644 index 0000000..50efb1c --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/Qualifier.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Annotation used to declare Qualifiers annotations. + */ +@Retention( RetentionPolicy.RUNTIME ) +public @interface Qualifier +{ + public abstract Class value(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceQualifier.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceQualifier.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceQualifier.java new file mode 100644 index 0000000..75410d6 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceQualifier.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * This class helps you select a particular service + * from a list. + *

+ * Provide a Selector which does the actual + * selection from the list. A common case is to select + * based on identity of the service, which you can do this way: + *

+ * + *

+ * new ServiceQualifier<MyService>(services, ServiceQualifier.withId("someId"))
+ * 
+ *

+ * Many selectors can be combined by using firstOf. Example: + *

+ *

+ * new ServiceQualifier<MyService>(services, firstOf(withTags("sometag"), firstActive(), first()))
+ * 
+ *

+ * This will pick a service that has the tag "sometag", or if none is found take the first active one. If no + * service is active, then the first service will be picked. + *

+ */ +public abstract class ServiceQualifier +{ + public static T firstService( Specification> qualifier, + Iterable> services + ) + { + for( ServiceReference service : services ) + { + if( qualifier.satisfiedBy( service ) ) + { + return service.get(); + } + } + return null; + } + + public static Specification> withId( final String anId ) + { + return new Specification>() + { + @Override + public boolean satisfiedBy( ServiceReference service ) + { + return service.identity().equals( anId ); + } + }; + } + + public static Specification> whereMetaInfoIs( final Object metaInfo ) + { + return new Specification>() + { + @Override + public boolean satisfiedBy( ServiceReference service ) + { + Object metaObject = service.metaInfo( metaInfo.getClass() ); + return metaObject != null && metaInfo.equals( metaObject ); + } + }; + } + + public static Specification> whereActive() + { + return new Specification>() + { + @Override + public boolean satisfiedBy( ServiceReference service ) + { + return service.isActive(); + } + }; + } + + public static Specification> whereAvailable() + { + return new Specification>() + { + @Override + public boolean satisfiedBy( ServiceReference service ) + { + return service.isAvailable(); + } + }; + } + + public static Specification> withTags( final String... tags ) + { + return new Specification>() + { + @Override + public boolean satisfiedBy( ServiceReference service ) + { + ServiceTags serviceTags = service.metaInfo( ServiceTags.class ); + + return serviceTags != null && serviceTags.hasTags( tags ); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceTags.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceTags.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceTags.java new file mode 100644 index 0000000..5ee8cb5 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/ServiceTags.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.io.Serializable; + +/** + * Use this as metainfo about a Service to specify tags. Easiest way to set them on a service + * is to use the ServiceDeclaration.taggedWith(String...) method. + * + * These can be used in conjunction with the withTags() Service + * Selector. + */ +public final class ServiceTags + implements Serializable +{ + private String[] tags; + + public ServiceTags( String... tags ) + { + this.tags = tags; + } + + public String[] tags() + { + return tags; + } + + public boolean hasTag( String tag ) + { + for( String serviceTag : tags ) + { + if( serviceTag.equals( tag ) ) + { + return true; + } + } + + return false; + } + + public boolean hasTags( String... aTags ) + { + for( String tag : aTags ) + { + if( !hasTag( tag ) ) + { + return false; + } + } + + return true; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/Tagged.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/Tagged.java b/core/api/src/main/java/org/qi4j/api/service/qualifier/Tagged.java new file mode 100644 index 0000000..3c8c0b7 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/Tagged.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.qi4j.api.service.ServiceReference; +import org.qi4j.functional.Specification; + +/** + * Filter services based on tags. Tags can be set using the ServiceTags meta-info, like so: + *

+ * module.addService(MyService.class).taggedWith(new ServiceTags("onetag","twotag"));
+ * 
+ * + * and then at an injection point you can do this: + * + *

+ * @Service @Tagged("onetag") MyService service;
+ * 
+ * to get only a service tagged with MyService. If several match only the first match is used. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( Tagged.TaggedQualifier.class ) +public @interface Tagged +{ + public abstract String[] value(); + + /** + * Tagged Annotation Qualifier. + * See {@link Tagged}. + */ + public final class TaggedQualifier + implements AnnotationQualifier + { + @Override + public Specification> qualifier( Tagged tagged ) + { + return ServiceQualifier.withTags( tagged.value() ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/service/qualifier/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/service/qualifier/package.html b/core/api/src/main/java/org/qi4j/api/service/qualifier/package.html new file mode 100644 index 0000000..ec0edd3 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/service/qualifier/package.html @@ -0,0 +1,59 @@ + + + +

Service Qualifiers.

+

+ The @Service injection is only able to specify the type of the service to be injected. If any other type of + qualification has to be done it has to be done manually but for common cases it's more convenient to use + annotations to do this filtering. This package contains annotations to perform this qualification. +

+

Example:

+
+
@Service @Tagged( "sometag" ) MyService service;
+
+

+ This will only inject instances of MyService that have been tagged with "sometag". If none exist an + exception will occur at injection time since it is not optional. +

+

It also works with iterables:

+
+
@Service @Tagged( "sometag" ) Iterable<MyService> services;
+
+

+ The qualification will be evaluated upon each call to iterator(), and since the qualifier has access to a + ServiceReference, which contains the isActive() method, it can even provide some dynamicity. +

+
+
@Service @Active Iterable<SomeImportedService> importedServices;
+
+

+ Let's say these SomeImportedService are only sometimes available. Then whenever iterator() is called the + {@link org.qi4j.api.service.qualifier.Active} tag can kick in and filter out those whose + ServiceReference.isActive() returns false. +

+

Standard ones defined in the API are:

+
    +
  • {@link org.qi4j.api.service.qualifier.Active}
  • +
  • {@link org.qi4j.api.service.qualifier.Available}
  • +
  • {@link org.qi4j.api.service.qualifier.HasMetaInfo}
  • +
  • {@link org.qi4j.api.service.qualifier.IdentifiedBy}
  • +
  • {@link org.qi4j.api.service.qualifier.Tagged}
  • +
+

See tests and API for more examples, and how to implement your own qualifiers.

+ + http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/GenericSideEffect.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/GenericSideEffect.java b/core/api/src/main/java/org/qi4j/api/sideeffect/GenericSideEffect.java new file mode 100644 index 0000000..167c1ef --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/GenericSideEffect.java @@ -0,0 +1,61 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * 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 + * + * 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.qi4j.api.sideeffect; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * Base class for generic SideEffects. + */ +public abstract class GenericSideEffect + extends SideEffectOf + implements InvocationHandler +{ + + /** + * {@inheritDoc} + */ + @Override + public Object invoke( final Object proxy, final Method method, final Object[] args ) + throws Throwable + { + invoke( method, args ); + return null; + } + + /** + * Convenience method to be overridden by subclasses in order to avoid returning null, as returned value from side + * effects is not taken in consideration. + * + * @param method the method that was invoked + * @param args the arguments of the method invocation + * + * @throws Throwable - the exception to throw from the method invocation on the proxy instance. The exception's type + * must be assignable either to any of the exception types declared in the throws clause of the + * interface method or to the unchecked exception types {code}java.lang.RuntimeException{code} + * or {code}java.lang.Error{code}. If a checked exception is thrown by this method that is not + * assignable to any of the exception types declared in the throws clause of the interface method, + * then an UndeclaredThrowableException containing the exception that was thrown by this method + * will be thrown by the method invocation on the proxy instance. + */ + protected void invoke( final Method method, final Object[] args ) + throws Throwable + { + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectDescriptor.java b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectDescriptor.java new file mode 100644 index 0000000..85e8610 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.sideeffect; + +/** + * SideEffect Descriptor. + */ +public interface SideEffectDescriptor +{ + Class modifierClass(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectOf.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectOf.java b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectOf.java new file mode 100644 index 0000000..39113cc --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectOf.java @@ -0,0 +1,38 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * 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 + * + * 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.qi4j.api.sideeffect; + +import org.qi4j.api.sideeffect.internal.SideEffectFor; + +/** + * Base class for SideEffects. It introduces a typed "next" pointer + * that SideEffects can use to get the result of the original invocation. + *

+ * Generic SideEffects should subclass {@link GenericSideEffect} instead. + *

+ *

+ * SideEffects implementations must be thread-safe in their implementation, + * as multiple threads may share instances. + *

+ */ +public abstract class SideEffectOf +{ + final + @SideEffectFor + protected T result = null; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffects.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffects.java b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffects.java new file mode 100644 index 0000000..bd2e525 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffects.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2007, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.sideeffect; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used by composites and mixins to declare what SideEffects + * should apply to the type or specific method. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.TYPE, ElementType.METHOD } ) +@Documented +public @interface SideEffects +{ + Class[] value(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectsDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectsDescriptor.java b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectsDescriptor.java new file mode 100644 index 0000000..9f569ea --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/SideEffectsDescriptor.java @@ -0,0 +1,24 @@ +/* Copyright 2008 Edward Yakop. +* +* 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 +* +* 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.qi4j.api.sideeffect; + +/** + * SideEffects Descriptor. + */ +public interface SideEffectsDescriptor +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/internal/SideEffectFor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/internal/SideEffectFor.java b/core/api/src/main/java/org/qi4j/api/sideeffect/internal/SideEffectFor.java new file mode 100644 index 0000000..1ab3383 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/internal/SideEffectFor.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2007, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.sideeffect.internal; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.qi4j.api.injection.InjectionScope; + +/** + * This annotation is required once in each SideEffect, to mark the + * field where the element providing the invocation result should be + * injected. + *

+ * The type of the field must be of the same type as the SideEffect + * itself, or an InvocationHandler. + *

+ *

+ * Example; + *

+ *

+ * public interface MyStuff
+ * {
+ *     SomeResult doSomething();
+ * }
+ *
+ * public class MyStuffSideEffect
+ *     implements MyStuff
+ * {
+ *     @SideEffectFor MyStuff next;
+ *
+ *     public SomeResult doSomething()
+ *     {
+ *          SomeResult result = next.doSomething();
+ *
+ *         // HERE DO THE SIDEEFFECT STUFF.
+ *
+ *          return result; // Result value is ignored, null would work too.
+ *     }
+ * }
+ * 
+ */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD, ElementType.PARAMETER }) +@Documented +@InjectionScope +public @interface SideEffectFor +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/internal/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/internal/package.html b/core/api/src/main/java/org/qi4j/api/sideeffect/internal/package.html new file mode 100644 index 0000000..269774e --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/internal/package.html @@ -0,0 +1,25 @@ + + + +

Internal/Private package for the API.

+

+ This is an internal package, and no classes in this package is part of the API and compatibility + with these classes will not be attempted. +

+ + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/sideeffect/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/sideeffect/package.html b/core/api/src/main/java/org/qi4j/api/sideeffect/package.html new file mode 100644 index 0000000..a658adb --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/sideeffect/package.html @@ -0,0 +1,21 @@ + + + +

SideEffect API.

+ + http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/Application.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/Application.java b/core/api/src/main/java/org/qi4j/api/structure/Application.java new file mode 100644 index 0000000..6023942 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/Application.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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 + * 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.qi4j.api.structure; + +import org.qi4j.api.activation.Activation; +import org.qi4j.api.activation.ActivationEventListenerRegistration; + +/** + * The Application represents a whole Zest application. + */ +public interface Application + extends ActivationEventListenerRegistration, Activation, MetaInfoHolder +{ + /** + * Application modes. + */ + public enum Mode + { + /** + * Should be used for unit test runs. Created files etc. should be cleaned up between runs. + */ + test, + /** + * Should be used during development. Typically create in-memory databases etc. + */ + development, + /** + * Should be used in QA environments, and other production-like settings where different set of external + * resources are utilized. + */ + staging, + /** + * Should be used in production. All databases are persistent on disk etc. + */ + production + } + + /** + * @return Application name + */ + String name(); + + /** + * The version of the application. This can be in any format, but + * most likely will follow the Dewey format, i.e. x.y.z. + * + * @return the version of the application + */ + String version(); + + /** + * @return Application Mode + */ + Mode mode(); + + /** + * Find a Layer. + * + * @param layerName Layer name + * @return Found Layer, never returns null + * @throws IllegalArgumentException if there's no such Layer + */ + Layer findLayer( String layerName ) + throws IllegalArgumentException; + + /** + * Find a Module. + * + * @param layerName Layer name + * @param moduleName Module name + * @return Found Module, never returns null + * @throws IllegalArgumentException if there's no such Module + */ + Module findModule( String layerName, String moduleName ) + throws IllegalArgumentException; + + /** + * @return Application Descriptor + */ + ApplicationDescriptor descriptor(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/ApplicationDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/ApplicationDescriptor.java b/core/api/src/main/java/org/qi4j/api/structure/ApplicationDescriptor.java new file mode 100644 index 0000000..b33f134 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/ApplicationDescriptor.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.structure; + +import org.qi4j.api.Qi4j; +import org.qi4j.functional.VisitableHierarchy; + +/** + * Application Descriptor. + */ +public interface ApplicationDescriptor + extends VisitableHierarchy +{ + /** + * Create a new instance of the Application. + * @param runtime Zest Runtime + * @param importedServiceInstances Imported Services instances + * @return a new instance of the Application. + */ + Application newInstance( Qi4j runtime, Object... importedServiceInstances ); + + /** + * @return the Application's name + */ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/Layer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/Layer.java b/core/api/src/main/java/org/qi4j/api/structure/Layer.java new file mode 100644 index 0000000..4268764 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/Layer.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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 + * 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.qi4j.api.structure; + +import org.qi4j.api.activation.Activation; +import org.qi4j.api.activation.ActivationEventListenerRegistration; + +/** + * The Layer represents a single layer in a Zest application. + */ +public interface Layer + extends ActivationEventListenerRegistration, Activation, MetaInfoHolder +{ + /** + * @return the Layer's name + */ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/LayerDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/LayerDescriptor.java b/core/api/src/main/java/org/qi4j/api/structure/LayerDescriptor.java new file mode 100644 index 0000000..db0c56a --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/LayerDescriptor.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.structure; + +/** + * Layer Descriptor. + */ +public interface LayerDescriptor +{ + + /** + * @return the Layer's name + */ + String name(); + + /** + * @return Layers used by this Layer + */ + UsedLayersDescriptor usedLayers(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/MetaInfoHolder.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/MetaInfoHolder.java b/core/api/src/main/java/org/qi4j/api/structure/MetaInfoHolder.java new file mode 100644 index 0000000..f497a75 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/MetaInfoHolder.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * 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 + * + * 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.qi4j.api.structure; + +/** + * MetaInfo holder. + */ +public interface MetaInfoHolder +{ + + /** + * Get metadata that implements the given type. + * The info is registered during assembly of the application. + * + * @param infoType the type of metadata to be returned + * + * @return the metadata for the given type, or null if + * no such metadata has been registered + */ + T metaInfo( Class infoType ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/Module.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/Module.java b/core/api/src/main/java/org/qi4j/api/structure/Module.java new file mode 100644 index 0000000..47ffab8 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/Module.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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 + * 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.qi4j.api.structure; + +import org.qi4j.api.activation.ActivationEventListenerRegistration; +import org.qi4j.api.composite.TransientBuilderFactory; +import org.qi4j.api.composite.TransientDescriptor; +import org.qi4j.api.entity.EntityDescriptor; +import org.qi4j.api.injection.scope.Structure; +import org.qi4j.api.object.ObjectDescriptor; +import org.qi4j.api.object.ObjectFactory; +import org.qi4j.api.query.QueryBuilderFactory; +import org.qi4j.api.service.ServiceFinder; +import org.qi4j.api.unitofwork.UnitOfWorkFactory; +import org.qi4j.api.value.ValueBuilderFactory; +import org.qi4j.api.value.ValueDescriptor; +import org.qi4j.functional.Function; +import org.qi4j.functional.Visitable; +import org.qi4j.functional.Visitor; + +/** + * API for interacting with a Module. Instances + * of this can be accessed by using the {@link Structure} + * injection scope. + */ +public interface Module + extends ActivationEventListenerRegistration, + MetaInfoHolder, + ObjectFactory, + TransientBuilderFactory, + ValueBuilderFactory, + UnitOfWorkFactory, + QueryBuilderFactory, + ServiceFinder +{ + + /** + * @return the Module's name + */ + String name(); + + /** + * @return the Module's ClassLoader + */ + ClassLoader classLoader(); + + /** + * @param typeName name of a transient composite type + * @return the descriptor for a transient composite or null if the class could not be found or the transient composite is not visible + */ + TransientDescriptor transientDescriptor( String typeName ); + + /** + * @param typeName name of an entity composite type + * @return the descriptor for an entity composite or null if the class could not be found or the entity composite is not visible + */ + EntityDescriptor entityDescriptor( String typeName ); + + /** + * @param typeName name of an object type + * @return the descriptor for an object or null if the class could not be found or the object is not visible + */ + ObjectDescriptor objectDescriptor( String typeName ); + + /** + * @param typeName name of a value composite type + * @return the descriptor for a value composite or null if the class could not be found or the value composite is not visible + */ + ValueDescriptor valueDescriptor( String typeName ); + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/ModuleDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/ModuleDescriptor.java b/core/api/src/main/java/org/qi4j/api/structure/ModuleDescriptor.java new file mode 100644 index 0000000..ffb7b9b --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/ModuleDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.structure; + +/** + * Module Descriptor. + */ +public interface ModuleDescriptor +{ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/UsedLayersDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/UsedLayersDescriptor.java b/core/api/src/main/java/org/qi4j/api/structure/UsedLayersDescriptor.java new file mode 100644 index 0000000..ee530b7 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/UsedLayersDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.structure; + +/** + * Used Layers Descriptor. + */ +public interface UsedLayersDescriptor +{ + Iterable layers(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/structure/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/structure/package.html b/core/api/src/main/java/org/qi4j/api/structure/package.html new file mode 100644 index 0000000..3134a90 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/structure/package.html @@ -0,0 +1,21 @@ + + + +

Application Structure API.

+ + http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/type/CollectionType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/type/CollectionType.java b/core/api/src/main/java/org/qi4j/api/type/CollectionType.java new file mode 100644 index 0000000..1b85b5f --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/type/CollectionType.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.type; + +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import org.qi4j.api.util.Classes; + +/** + * Collection ValueType. + *

This handles Collection, List and Set types.

+ */ +public final class CollectionType + extends ValueType +{ + + public static boolean isCollection( Type type ) + { + Class cl = Classes.RAW_CLASS.map( type ); + return cl.equals( Collection.class ) || cl.equals( List.class ) || cl.equals( Set.class ); + } + + public static CollectionType collectionOf( Class collectedType ) + { + return new CollectionType( Collection.class, ValueType.of( collectedType ) ); + } + + public static CollectionType listOf( Class collectedType ) + { + return new CollectionType( List.class, ValueType.of( collectedType ) ); + } + + public static CollectionType setOf( Class collectedType ) + { + return new CollectionType( Set.class, ValueType.of( collectedType ) ); + } + private ValueType collectedType; + + public CollectionType( Class type, ValueType collectedType ) + { + super( type ); + this.collectedType = collectedType; + if( !isCollection( type ) ) + { + throw new IllegalArgumentException( type + " is not a Collection, List or Set." ); + } + } + + public ValueType collectedType() + { + return collectedType; + } + + @Override + public String toString() + { + return super.toString() + "<" + collectedType + ">"; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/type/EnumType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/type/EnumType.java b/core/api/src/main/java/org/qi4j/api/type/EnumType.java new file mode 100644 index 0000000..aa4ff31 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/type/EnumType.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.type; + +import java.lang.reflect.Type; + +/** + * Enum ValueType. + */ +public final class EnumType + extends ValueType +{ + + public static boolean isEnum( Type type ) + { + if( type instanceof Class ) + { + Class typeClass = (Class) type; + return ( typeClass.isEnum() ); + } + return false; + } + + public static EnumType of( Class type ) + { + return new EnumType( type ); + } + + public EnumType( Class type ) + { + super( type ); + if( !isEnum( type ) ) + { + throw new IllegalArgumentException( type + " is not an Enum." ); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/type/HasTypes.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/type/HasTypes.java b/core/api/src/main/java/org/qi4j/api/type/HasTypes.java new file mode 100644 index 0000000..0ad36a4 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/type/HasTypes.java @@ -0,0 +1,27 @@ +/* + * 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.qi4j.api.type; + +/** + * Has types. + */ +public interface HasTypes +{ + Iterable> types(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/api/src/main/java/org/qi4j/api/type/MapType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/type/MapType.java b/core/api/src/main/java/org/qi4j/api/type/MapType.java new file mode 100644 index 0000000..1cb1700 --- /dev/null +++ b/core/api/src/main/java/org/qi4j/api/type/MapType.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2009, Rickard Öberg. All Rights Reserved. + * + * 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 + * 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.qi4j.api.type; + +import java.lang.reflect.Type; +import java.util.Map; +import org.qi4j.api.util.Classes; + +/** + * Map ValueType. + *

This handles instances of Map.

+ */ +public final class MapType + extends ValueType +{ + + private ValueType keyType; + private ValueType valueType; + private final Serialization.Variant variant; + + public static boolean isMap( Type type ) + { + Class cl = Classes.RAW_CLASS.map( type ); + return Map.class.isAssignableFrom( cl ); + } + + public static MapType of( Class keyType, Class valueType ) + { + return new MapType( Map.class, ValueType.of( keyType ), ValueType.of( valueType ) ); + } + + public static MapType of( Class keyType, Class valueType, Serialization.Variant variant ) + { + return new MapType( Map.class, ValueType.of( keyType ), ValueType.of( valueType ), variant ); + } + + public MapType( Class type, ValueType keyType, ValueType valueType ) + { + this( type, keyType, valueType, Serialization.Variant.entry ); + } + + public MapType( Class type, ValueType keyType, ValueType valueType, Serialization.Variant variant ) + { + super( type ); + this.keyType = keyType; + this.valueType = valueType; + this.variant = variant; + if( !isMap( type ) ) + { + throw new IllegalArgumentException( type + " is not a Map." ); + } + } + + public ValueType keyType() + { + return keyType; + } + + public ValueType valueType() + { + return valueType; + } + + public Serialization.Variant variant() + { + return variant; + } + + @Override + public String toString() + { + return super.toString() + "<" + keyType + "," + valueType + ">"; + } +}