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 javax.jcr.Node;
019import javax.jcr.Property;
020import javax.jcr.Value;
021
022import org.fcrepo.kernel.modeshape.utils.UncheckedFunction;
023
024import java.util.Iterator;
025import java.util.function.Function;
026import java.util.function.Predicate;
027
028import static com.google.common.collect.Iterators.forArray;
029import static com.google.common.collect.Iterators.singletonIterator;
030import static javax.jcr.PropertyType.BINARY;
031import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.FROZEN_NODE;
032import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
033import static org.fcrepo.kernel.modeshape.utils.UncheckedPredicate.uncheck;
034
035/**
036 * @author cabeer
037 * @since 9/25/14
038 */
039public final class JcrPropertyFunctions {
040
041    private JcrPropertyFunctions() {
042    }
043
044    /**
045     * Constructs an {@link java.util.Iterator} of {@link javax.jcr.Value}s from any {@link javax.jcr.Property},
046     * multi- or single-valued.
047     */
048    public static Function<Property, Iterator<Value>> property2values = UncheckedFunction.uncheck(
049            (final Property p) -> p.isMultiple() ? forArray(p.getValues()) : singletonIterator(p.getValue()));
050
051    /**
052     * Check if a JCR property is a binary jcr:data property
053     */
054    public static Predicate<Property> isBinaryContentProperty = uncheck(p -> p.getType() == BINARY &&
055            p.getName().equals(JCR_DATA));
056
057    /**
058     * Predicate for determining whether this {@link javax.jcr.Node} is a frozen node
059     * (a part of the system version history).
060     */
061    public static Predicate<Node> isFrozen = uncheck(n -> n.isNodeType(FROZEN_NODE));
062
063}