Skip to content

Commit

Permalink
More type shuffling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 17, 2015
1 parent c4cf788 commit 7820ef9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Expand Up @@ -21,21 +21,17 @@
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;

import org.slf4j.Logger;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;

import java.util.Arrays;
import java.util.stream.Stream;

import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.fcrepo.kernel.utils.Streams.flatten;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.allTypes;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand All @@ -60,14 +56,8 @@ public TypeRdfContext(final FedoraResource resource,
}

@Override
public Stream<Triple> applyThrows(final Node node) throws RepositoryException {
final NodeType primaryNodeType = node.getPrimaryNodeType();
final NodeType[] mixins = node.getMixinNodeTypes();
final Stream<NodeType> mixinSupertypes = Stream.of(mixins).map(NodeType::getSupertypes).flatMap(Arrays::stream);
final Stream<NodeType> primarySupertypes = Stream.of(primaryNodeType.getSupertypes());
final Stream<NodeType> allTypes =
flatten(Stream.of(primaryNodeType), primarySupertypes, Stream.of(mixins), mixinSupertypes);
return allTypes.map(nodeType -> {
public Stream<Triple> applyThrows(final Node node) {
return allTypes(node).map(nodeType -> {
final String fullTypeName = nodeType.getName();
LOGGER.trace("Translating mixin name: {}", fullTypeName);
final String prefix = fullTypeName.split(":")[0];
Expand Down
Expand Up @@ -17,6 +17,8 @@


import static java.util.Arrays.asList;
import static java.util.function.Function.identity;
import static java.util.stream.Stream.of;
import static javax.jcr.PropertyType.BINARY;
import static javax.jcr.PropertyType.REFERENCE;
import static javax.jcr.PropertyType.UNDEFINED;
Expand All @@ -27,6 +29,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -243,4 +246,22 @@ public static Node getClosestExistingAncestor(final Session session,
}
return session.getRootNode();
}

/**
* Discovers the types of a node and the closure of the supertype operation on that set of types
*
* @param node
* @return all types of that node
*/
public static Stream<NodeType> allTypes(final Node node) {
try {
final NodeType primaryNodeType = node.getPrimaryNodeType();
final NodeType[] mixins = node.getMixinNodeTypes();
final Stream<NodeType> mixinSupertypes = of(mixins).map(NodeType::getSupertypes).flatMap(Arrays::stream);
final Stream<NodeType> primarySupertypes = of(primaryNodeType.getSupertypes());
return of(of(primaryNodeType), primarySupertypes, of(mixins), mixinSupertypes).flatMap(identity());
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}
}
13 changes: 2 additions & 11 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/Streams.java
Expand Up @@ -15,10 +15,7 @@
*/
package org.fcrepo.kernel.utils;

import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.function.Function.identity;
import static java.util.stream.StreamSupport.stream;

import static java.util.stream.Stream.generate;
import java.util.Iterator;
import java.util.stream.Stream;

Expand All @@ -32,12 +29,6 @@
public class Streams {

public static <T> Stream<T> fromIterator(final Iterator<T> i) {
return stream(spliteratorUnknownSize(i, 0), false);
}

@SafeVarargs
public static <T> Stream<T> flatten(final Stream<T>... streams) {
return Stream.of(streams).flatMap(identity());
return generate(i::next);
}

}

0 comments on commit 7820ef9

Please sign in to comment.