All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.cq.commons.predicate.AbstractNodePredicate Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*
 * Copyright 1997-2008 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */

package com.day.cq.commons.predicate;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.commons.collections.Predicate;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Predicate used to filter jcr nodes.
 */
abstract public class AbstractNodePredicate implements Predicate {

    /**
     * default logger
     */
    private final Logger log = LoggerFactory.getLogger(AbstractNodePredicate.class);

    /**
     * {@inheritDoc}
     *
     * @return true if o is a JCR Node or a Resource that can be
     * adapted to a JCR Node and the subsequent call to {@link #evaluate(Node)}
     * returns true; false otherwise.
     */
    public boolean evaluate(Object o) {
        Node n = null;
        if (o instanceof Resource) {
            n = ((Resource) o).adaptTo(Node.class);
        } else if (o instanceof Node) {
            n = (Node) o;
        }
        if (n != null) {
            try {
                return evaluate(n);
            } catch (RepositoryException e) {
                log.warn("RepositoryException in evaluate()", e);
            }
        }
        return false;
    }

    /**
     * Evaluates this predicate on the given node.
     *
     * @param node the node
     * @return true if this node satisfies this predicate
     * @throws RepositoryException if an error during evaluation occurs
     */
    abstract public boolean evaluate(Node node) throws RepositoryException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy