Skip to content

Commit

Permalink
Refined and tested Facets API
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Dec 11, 2012
1 parent 87372b3 commit 22e1d6a
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
/**
* A base convenience {@link Facet} abstract class.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>, <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>, <a
* href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
*/
public abstract class BaseFacet implements Facet
public abstract class BaseFacet<FACETED extends Faceted> implements Facet<FACETED>
{
protected Faceted facetedInstance;
protected FACETED origin;

@Override
public Faceted getFaceted()
protected BaseFacet(FACETED origin)
{
return this.facetedInstance;
if (origin == null)
throw new IllegalArgumentException("Origin must not be null");

this.origin = origin;
}

@Override
public void setFaceted(Faceted faceted)
public FACETED getOrigin()
{
this.facetedInstance = faceted;
return this.origin;
}

@Override
Expand All @@ -41,7 +44,7 @@ public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result) + ((facetedInstance == null) ? 0 : facetedInstance.hashCode());
result = (prime * result) + ((origin == null) ? 0 : origin.hashCode());
return result;
}

Expand All @@ -54,13 +57,13 @@ public boolean equals(final Object obj)
return false;
if (getClass() != obj.getClass())
return false;
BaseFacet other = (BaseFacet) obj;
if (facetedInstance == null)
BaseFacet<?> other = (BaseFacet<?>) obj;
if (origin == null)
{
if (other.facetedInstance != null)
if (other.origin != null)
return false;
}
else if (!facetedInstance.equals(other.facetedInstance))
else if (!origin.equals(other.origin))
return false;
return true;
}
Expand Down
106 changes: 106 additions & 0 deletions facets/api/src/main/java/org/jboss/forge/addon/facets/BaseFaceted.java
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;
}

}
25 changes: 10 additions & 15 deletions facets/api/src/main/java/org/jboss/forge/addon/facets/Facet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
package org.jboss.forge.addon.facets;

/**
* A {@link Facet} is an access point to common functionality, file manipulations,
* descriptors that extend a {@link Faceted} instance. When implementing this interface, consider extending
* {@link BaseFacet} for convenience.
* A {@link Facet} is an access point to common functionality, file manipulations, descriptors that extend a
* {@link Faceted} instance. When implementing this interface, consider extending {@link BaseFacet} for convenience.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>, <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
* @see {@link BaseFacet}
*/
public interface Facet
public interface Facet<FACETED extends Faceted>
{
/**
* Return the {@link Faceted} instance on which this {@link Facet} operates.
*/
Faceted getFaceted();

/**
* Initialize this {@link Facet} for operation on the {@link Faceted} instance. This method is responsible for ensuring
* that the {@link Facet} instance is ready for use, and must be called before any other methods.
* @param <T>
*/
void setFaceted(Faceted faceted);
FACETED getOrigin();

/**
* Perform necessary setup for this {@link Facet} to be considered installed in the given {@link Faceted} instance. This
* method should NOT register the facet; facet registration is handled by the {@link Faceted} instance if installation is successful.
* Perform necessary setup for this {@link Facet} to be considered installed in the given {@link Faceted} instance.
* This method should NOT register the facet; facet registration is handled by the {@link Faceted} instance if
* installation is successful.
*
* @return true if installation was successful, false if not.
*/
Expand Down
24 changes: 11 additions & 13 deletions facets/api/src/main/java/org/jboss/forge/addon/facets/Faceted.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,52 @@
*/
public interface Faceted
{

/**
* Return true if a facet of the given type is present; return false otherwise.
*/
boolean hasFacet(Class<? extends Facet> type);
boolean hasFacet(Class<? extends Facet<?>> type);

/**
* Return true if all {@link Facet}s of the given types are present; otherwise, if any of the given facet types is
* missing, return false.
*/
boolean hasAllFacets(Iterable<Class<? extends Facet>> facetDependencies);
boolean hasAllFacets(Iterable<Class<? extends Facet<?>>> facetDependencies);

/**
* Return true if all {@link Facet}s of the given types are present; otherwise, if any of the given facet types is
* missing, return false.
*/
boolean hasAllFacets(Class<? extends Facet>... facetDependencies);
boolean hasAllFacets(Class<? extends Facet<?>>... facetDependencies);

/**
* Return the instance of the requested {@link Facet} type, or throw a {@link FacetNotFoundException} if no
* {@link Facet} of that type is registered.
* {@link Facet} of that type is installed.
*/
<F extends Facet> F getFacet(Class<F> type) throws FacetNotFoundException;
<F extends Facet<?>> F getFacet(Class<F> type) throws FacetNotFoundException;

/**
* Return a {@link Iterable} of the currently installed {@link Facet}s. Return an empty list if no facets of that
* type were found.
*/
Iterable<Facet> getFacets();
Iterable<? extends Facet<?>> getFacets();

/**
* Return a {@link Iterable} of the currently installed {@link Facet}s matching the given type.
*/
<F extends Facet> Iterable<F> getFacets(Class<F> type);
<F extends Facet<?>> Iterable<F> getFacets(Class<F> type);

/**
* Install and register the given {@link Facet}. If the facet is already installed, register it instead (See
* {@link #registerFacet(Facet)}.
* Install and register the given {@link Facet}. If the facet is already installed, return true.
*/
boolean install(Facet facet);
boolean install(Facet<?> facet);

/**
* Remove the given {@link Facet} from the internal collection of installed facets.
*/
boolean uninstall(Facet facet);
boolean uninstall(Facet<?> facet);

/**
* Return true if the given {@link Facet} is supported.
*/
boolean supports(Class<? extends Facet> type);
boolean supports(Class<? extends Facet<?>> type);
}
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);
}
}
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));
}
}
Loading

0 comments on commit 22e1d6a

Please sign in to comment.