Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added createFacets method to FacetFactory

  • Loading branch information...
commit b46141aeacad54786325876b2217dd1dba46db06 1 parent b70dcf5
@gastaldi gastaldi authored
View
14 facets/api/src/main/java/org/jboss/forge/facets/FacetFactory.java
@@ -10,7 +10,7 @@
/**
* Responsible for instantiation of new {@link Facet} instances.
- *
+ *
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@Exported
@@ -26,4 +26,16 @@
* use the given instance as the {@link Facet#getOrigin()}.
*/
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET create(Class<FACET> type, E origin);
+
+ /**
+ * Returns all the facets that implements a certain {@link Facet} type
+ */
+ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<FACET> createFacets(Class<FACET> type);
+
+ /**
+ * Returns all the facets that implements a certain {@link Facet} type. If it is also an instance of
+ * {@link MutableOrigin}, then use the given instance as the {@link Facet#getOrigin()}.
+ */
+ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<FACET> createFacets(
+ Class<FACET> type, E origin);
}
View
31 facets/impl/src/main/java/org/jboss/forge/facets/FacetFactoryImpl.java
@@ -6,6 +6,9 @@
*/
package org.jboss.forge.facets;
+import java.util.HashSet;
+import java.util.Set;
+
import javax.inject.Inject;
import org.jboss.forge.container.addons.AddonRegistry;
@@ -36,4 +39,32 @@
throw new IllegalArgumentException("Facet type [" + type.getName() + "] does not support setting an origin.");
return instance;
}
+
+ @Override
+ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<FACET> createFacets(Class<FACET> type)
+ {
+ Set<ExportedInstance<FACET>> instances = registry.getExportedInstances(type);
+ Set<FACET> facets = new HashSet<FACET>(instances.size());
+ for (ExportedInstance<FACET> instance : instances)
+ {
+ facets.add(instance.get());
+ }
+ return facets;
+ }
+
+ @Override
+ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<FACET> createFacets(
+ Class<FACET> type, E origin)
+ {
+ Iterable<FACET> facets = createFacets(type);
+ for (FACET facet : facets)
+ {
+ if (facet instanceof MutableOrigin)
+ ((MutableOrigin<E>) facet).setOrigin(origin);
+ else
+ throw new IllegalArgumentException("Facet type [" + type.getName()
+ + "] does not support setting an origin.");
+ }
+ return facets;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.