-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87372b3
commit 22e1d6a
Showing
9 changed files
with
315 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
facets/api/src/main/java/org/jboss/forge/addon/facets/BaseFaceted.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.jboss.forge.addon.facets; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public abstract class BaseFaceted implements Faceted | ||
{ | ||
private Set<Facet<?>> facets = Collections.newSetFromMap(new ConcurrentHashMap<Facet<?>, Boolean>()); | ||
|
||
@Override | ||
public boolean hasFacet(Class<? extends Facet<?>> type) | ||
{ | ||
try | ||
{ | ||
getFacet(type); | ||
return true; | ||
} | ||
catch (FacetNotFoundException e) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasAllFacets(Iterable<Class<? extends Facet<?>>> iterable) | ||
{ | ||
for (Class<? extends Facet<?>> type : iterable) | ||
{ | ||
if (!hasFacet(type)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean hasAllFacets(Class<? extends Facet<?>>... types) | ||
{ | ||
for (Class<? extends Facet<?>> type : types) | ||
{ | ||
if (!hasFacet(type)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <F extends Facet<?>> F getFacet(Class<F> type) throws FacetNotFoundException | ||
{ | ||
for (Facet<?> facet : facets) | ||
{ | ||
if (type.isAssignableFrom(facet.getClass())) | ||
return (F) facet; | ||
} | ||
throw new FacetNotFoundException("No Facet of type [" + type + "] is installed."); | ||
} | ||
|
||
@Override | ||
public Iterable<? extends Facet<?>> getFacets() | ||
{ | ||
return Collections.unmodifiableCollection(facets); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <F extends Facet<?>> Iterable<F> getFacets(Class<F> type) | ||
{ | ||
Set<F> result = new HashSet<F>(); | ||
for (Facet<?> facet : facets) | ||
{ | ||
if (type.isAssignableFrom(facet.getClass())) | ||
result.add((F) facet); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean install(Facet<?> facet) | ||
{ | ||
if (facet.getOrigin() != this) | ||
throw new IllegalArgumentException("Facet.getOrigin() was [" + facet.getOrigin() | ||
+ "] but needed to be [" + this + "]."); | ||
|
||
if (supports((Class<? extends Facet<?>>) facet.getClass())) | ||
{ | ||
if (facet.isInstalled() || facet.install()) | ||
{ | ||
facets.add(facet); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean uninstall(Facet<?> facet) | ||
{ | ||
if (facet.isInstalled()) | ||
return facet.uninstall(); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
facets/api/src/test/java/org/jboss/forge/addon/facets/BaseFacetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.jboss.forge.addon.facets; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class BaseFacetTest | ||
{ | ||
@Test | ||
public void testGetOriginReturnsOriginFromInstantiation() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
MockFacet facet = new MockFacet(faceted); | ||
|
||
Assert.assertEquals(faceted, facet.getOrigin()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testInstantiationWithoutOriginThrowsException() | ||
{ | ||
new MockFacet(null); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
facets/api/src/test/java/org/jboss/forge/addon/facets/BaseFacetedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.jboss.forge.addon.facets; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
public class BaseFacetedTest | ||
{ | ||
@Test | ||
public void testInstall() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
MockFacet facet = new MockFacet(faceted); | ||
|
||
Assert.assertTrue(faceted.install(facet)); | ||
Assert.assertTrue(faceted.hasFacet(MockFacet.class)); | ||
List<Class<? extends Facet<?>>> list = new ArrayList<Class<? extends Facet<?>>>(); | ||
list.add(MockFacet.class); | ||
Assert.assertTrue(faceted.hasAllFacets(list)); | ||
Assert.assertEquals(facet, faceted.getFacet(MockFacet.class)); | ||
} | ||
|
||
@Test | ||
public void testInstallUnsupported() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
MockFacet2 facet = new MockFacet2(faceted); | ||
|
||
Assert.assertFalse(faceted.install(facet)); | ||
} | ||
|
||
@Test | ||
public void testInstallIsIdempotent() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
MockFacet facet = new MockFacet(faceted); | ||
|
||
Assert.assertTrue(faceted.install(facet)); | ||
Assert.assertTrue(faceted.install(facet)); | ||
|
||
Assert.assertTrue(faceted.hasFacet(MockFacet.class)); | ||
|
||
Iterator<? extends Facet<?>> iterator = faceted.getFacets().iterator(); | ||
Assert.assertEquals(facet, iterator.next()); | ||
Assert.assertFalse(iterator.hasNext()); | ||
} | ||
|
||
@Test | ||
public void testUnInstall() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
MockFacet facet = new MockFacet(faceted); | ||
|
||
Assert.assertTrue(faceted.uninstall(facet)); | ||
Assert.assertTrue(faceted.install(facet)); | ||
Assert.assertTrue(faceted.uninstall(facet)); | ||
} | ||
|
||
@Test | ||
public void testSupports() | ||
{ | ||
MockFaceted faceted = new MockFaceted(); | ||
|
||
Assert.assertTrue(faceted.supports(MockFacet.class)); | ||
Assert.assertFalse(faceted.supports(MockFacet2.class)); | ||
} | ||
} |
Oops, something went wrong.