001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.api.services.functions;
017
018import static com.google.common.collect.ImmutableList.copyOf;
019import static java.util.Arrays.asList;
020import static java.util.Objects.requireNonNull;
021import static org.fcrepo.kernel.api.FedoraJcrTypes.FROZEN_MIXIN_TYPES;
022import static org.fcrepo.kernel.api.services.functions.JcrPropertyFunctions.isFrozen;
023import static org.fcrepo.kernel.api.services.functions.JcrPropertyFunctions.property2values;
024import static org.fcrepo.kernel.api.utils.UncheckedFunction.uncheck;
025import static org.fcrepo.kernel.api.utils.UncheckedPredicate.uncheck;
026
027import java.util.Collection;
028import java.util.List;
029import javax.jcr.Node;
030import javax.jcr.Property;
031import javax.jcr.RepositoryException;
032import javax.jcr.Value;
033
034import org.fcrepo.kernel.api.utils.UncheckedPredicate;
035
036
037/**
038 * Predicate to match nodes with any of the given mixin types
039 * @author armintor@gmail.com
040 * @author ajs6f
041 *
042 */
043public class AnyTypesPredicate implements UncheckedPredicate<Node> {
044    protected final Collection<String> nodeTypes;
045
046    /**
047     * True if any of the types specified match.
048     * @param types the types
049     */
050    public AnyTypesPredicate(final String...types) {
051        nodeTypes = asList(types);
052    }
053
054    @Override
055    public boolean testThrows(final Node input) throws RepositoryException {
056        requireNonNull(input, "null node has no types!");
057        if (isFrozen.test(input) && input.hasProperty(FROZEN_MIXIN_TYPES)) {
058            final Property frozenTypesProperty = input.getProperty(FROZEN_MIXIN_TYPES);
059            final List<Value> values = copyOf(property2values.apply(frozenTypesProperty));
060            if (values.stream().map(uncheck(Value::getString)).anyMatch(nodeTypes::contains)) {
061                return true;
062            }
063        }
064        return nodeTypes.stream().anyMatch(uncheck(input::isNodeType));
065    }
066}