Skip to content

Commit

Permalink
AbstractFaceted uses JDK 8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 12, 2016
1 parent 42d1903 commit aab2a80
Showing 1 changed file with 8 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
*
Expand All @@ -20,9 +20,10 @@ public abstract class AbstractFaceted<FACETTYPE extends Facet<?>> implements Mut
@Override
public boolean hasFacet(Class<? extends FACETTYPE> type)
{
return safeGetFacet(type) != null;
return getFacetAsOptional(type).isPresent();
}

@SuppressWarnings("unchecked")
@Override
public boolean hasAllFacets(Class<? extends FACETTYPE>... facetDependencies)
{
Expand All @@ -45,21 +46,15 @@ public boolean hasAllFacets(Iterable<Class<? extends FACETTYPE>> facetDependenci
@Override
public <F extends FACETTYPE> F getFacet(Class<F> type) throws FacetNotFoundException
{
F facet = safeGetFacet(type);
if (facet == null)
{
throw new FacetNotFoundException("No Facet of type [" + type + "] is installed.");
}
else
{
return facet;
}
return getFacetAsOptional(type)
.orElseThrow(() -> new FacetNotFoundException("No Facet of type [" + type + "] is installed."));
}

@Override
@SuppressWarnings("unchecked")
public <F extends FACETTYPE> Optional<F> getFacetAsOptional(Class<F> type)
{
return Optional.<F> ofNullable(safeGetFacet(type));
return (Optional<F>) facets.stream().filter((facet) -> type.isInstance(facet)).findFirst();
}

@Override
Expand Down Expand Up @@ -112,37 +107,7 @@ public boolean register(FACETTYPE facet)
@SuppressWarnings("unchecked")
public <F extends FACETTYPE> Iterable<F> getFacets(Class<F> type)
{
Set<F> result = new HashSet<F>();
for (FACETTYPE facet : facets)
{
if (type.isInstance(facet))
{
result.add((F) facet);
}
}
return result;
}

/**
* Returns the installed facet that is an instance of the provided type argument, null otherwise.
*
* It does not throw any exception
*
* @param type the facet type
* @return the Facet if found, otherwise, null
*/
@SuppressWarnings("unchecked")
private <F extends FACETTYPE> F safeGetFacet(Class<F> type)

{
for (FACETTYPE facet : facets)
{
if (type.isInstance(facet))
{
return (F) facet;
}
}
return null;
return (Set<F>) facets.stream().filter((facet) -> type.isInstance(facet)).collect(Collectors.toSet());
}

@Override
Expand Down

0 comments on commit aab2a80

Please sign in to comment.