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