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

org.jopendocument.util.cc.IPredicate Maven / Gradle / Ivy

Go to download

jOpenDocument is a free library for developers looking to use Open Document files without OpenOffice.org.

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2008-2013 jOpenDocument, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU
 * General Public License Version 3 only ("GPL").  
 * You may not use this file except in compliance with the License. 
 * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
 * See the License for the specific language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 * 
 */

package org.jopendocument.util.cc;

import org.apache.commons.collections.Predicate;

public abstract class IPredicate implements Predicate {

    private static final IPredicate truePred = new IPredicate() {
        @Override
        public boolean evaluateChecked(Object input) {
            return true;
        }
    };
    private static final IPredicate falsePred = new IPredicate() {
        @Override
        public boolean evaluateChecked(Object input) {
            return false;
        }
    };
    private static final IPredicate NotNullPred = new IPredicate() {
        @Override
        public boolean evaluateChecked(Object input) {
            return input != null;
        }
    };

    @SuppressWarnings("unchecked")
    public static final  IPredicate truePredicate() {
        return (IPredicate) truePred;
    }

    @SuppressWarnings("unchecked")
    public static final  IPredicate falsePredicate() {
        return (IPredicate) falsePred;
    }

    @SuppressWarnings("unchecked")
    public static final  IPredicate notNullPredicate() {
        return (IPredicate) NotNullPred;
    }

    @SuppressWarnings("unchecked")
    public boolean evaluate(Object object) {
        return this.evaluateChecked((E) object);
    }

    public abstract boolean evaluateChecked(E input);

    public final  IPredicate cast() {
        // this class never returns E, only takes it as argument
        // but we cannot return this instance :
        // class Sub extends IPredicate {
        // E someMethod();
        // }
        // Sub n;
        // IPredicate cast = n. cast();
        // Sub n2 = (Sub) cast;
        // the cast in the line above will succeed but n2.someMethod()
        // will fail if n.someMethod() returns a BigDecimal

        // since we're returning a new IPredicate instance, the n2 line would correctly fail
        return new IPredicate() {
            @Override
            public boolean evaluateChecked(F input) {
                return IPredicate.this.evaluateChecked(input);
            }
        };
    }

    public final IPredicate not() {
        if (this == truePred)
            return falsePredicate();
        else if (this == falsePred)
            return truePredicate();
        else
            return new IPredicate() {
                @Override
                public boolean evaluateChecked(E input) {
                    return !IPredicate.this.evaluateChecked(input);
                }
            };
    }

    public final IPredicate and(final IPredicate o) {
        if (o == this || o == truePred)
            return this;
        else if (this == truePred)
            return o.cast();
        else if (o == falsePred || this == falsePred)
            return falsePredicate();
        else
            return new IPredicate() {
                @Override
                public boolean evaluateChecked(E input) {
                    return IPredicate.this.evaluateChecked(input) && o.evaluateChecked(input);
                }
            };
    }

    public final IPredicate or(final IPredicate o) {
        if (o == this || o == falsePred)
            return this;
        else if (this == falsePred)
            return o.cast();
        else if (o == truePred || this == truePred)
            return truePredicate();
        else
            return new IPredicate() {
                @Override
                public boolean evaluateChecked(E input) {
                    return IPredicate.this.evaluateChecked(input) || o.evaluateChecked(input);
                }
            };
    }
}