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.modeshape.services.functions;
017
018import static java.util.Arrays.asList;
019import static java.util.Objects.requireNonNull;
020import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.FROZEN_MIXIN_TYPES;
021import static org.fcrepo.kernel.modeshape.services.functions.JcrPropertyFunctions.isFrozen;
022import static org.fcrepo.kernel.modeshape.services.functions.JcrPropertyFunctions.property2values;
023import static org.fcrepo.kernel.modeshape.utils.UncheckedFunction.uncheck;
024import static org.fcrepo.kernel.modeshape.utils.UncheckedPredicate.uncheck;
025
026import java.util.Collection;
027import javax.jcr.Node;
028import javax.jcr.RepositoryException;
029import javax.jcr.Value;
030
031import org.fcrepo.kernel.modeshape.utils.UncheckedPredicate;
032
033
034/**
035 * Predicate to match nodes with any of the given mixin types
036 * @author armintor@gmail.com
037 * @author ajs6f
038 *
039 */
040public class AnyTypesPredicate implements UncheckedPredicate<Node> {
041    protected final Collection<String> nodeTypes;
042
043    /**
044     * True if any of the types specified match.
045     * @param types the types
046     */
047    public AnyTypesPredicate(final String...types) {
048        nodeTypes = asList(types);
049    }
050
051    @Override
052    public boolean testThrows(final Node input) throws RepositoryException {
053        requireNonNull(input, "null node has no types!");
054        if (isFrozen.test(input) && input.hasProperty(FROZEN_MIXIN_TYPES)) {
055            if (property2values.apply(input.getProperty(FROZEN_MIXIN_TYPES)).map(uncheck(Value::getString))
056                    .anyMatch(nodeTypes::contains)) {
057                return true;
058            }
059        }
060        return nodeTypes.stream().anyMatch(uncheck(input::isNodeType));
061    }
062}