/*
 * Redberry: symbolic tensor computations.
 *
 * Copyright (c) 2010-2015:
 *   Stanislav Poslavsky   
 *   Bolotin Dmitriy       
 *
 * This file is part of Redberry.
 *
 * Redberry is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Redberry is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Redberry. If not, see .
 */
package cc.redberry.core.utils;

import cc.redberry.core.tensor.Tensor;

/**
 * @author Dmitry Bolotin
 * @author Stanislav Poslavsky
 */
public interface Indicator {

    boolean is(E object);

    public static final Indicator TRUE_INDICATOR = new Indicator() {

        @Override
        public boolean is(Object object) {
            return true;
        }
    };
    public static final Indicator FALSE_INDICATOR = new Indicator() {

        @Override
        public boolean is(Object object) {
            return false;
        }
    };

    public static class Utils {

        @SafeVarargs
        public static  Indicator and(final Indicator... indicators) {
            return new Indicator() {

                @Override
                public boolean is(T object) {
                    for (Indicator indicator : indicators)
                        if (!indicator.is(object))
                            return false;
                    return true;
                }
            };
        }

        @SafeVarargs
        public static  Indicator or(final Indicator... indicators) {
            return new Indicator() {

                @Override
                public boolean is(T object) {
                    for (Indicator indicator : indicators)
                        if (indicator.is(object))
                            return true;
                    return false;
                }
            };
        }

        public static  Indicator not(final Indicator indicator) {
            return new Indicator() {

                @Override
                public boolean is(T object) {
                    return !indicator.is(object);
                }
            };
        }

        public static Indicator classIndicator(final Class clazz) {
            return new Indicator() {

                @Override
                public boolean is(Tensor object) {
                    return clazz == object.getClass();
                }
            };
        }

        /**
         * Gives a new indicator which returns true if specified condition is satisfied on object itself or at least one of its elements.
         *
         * @param indicator condition
         * @param        some type
         * @return a new indicator which returns true if specified condition is satisfied on object itself or at least one of its elements.
         */
        public static > Indicator iterativeIndicator(final Indicator indicator) {
            return new Indicator() {

                @Override
                public boolean is(T object) {
                    if (indicator.is(object)) return true;
                    for (T t : object)
                        if (indicator.is(t)) return true;
                    return false;
                }
            };
        }

    }
}

 

Ads help maintain this website.