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

org.opentripplanner.graph_builder.module.shapefile.CompositeBooleanConverter Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/* This program is free software: you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public License
 as published by the Free Software Foundation, either version 3 of
 the License, or (at your option) any later version.

 This program 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 this program.  If not, see . */

package org.opentripplanner.graph_builder.module.shapefile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.opengis.feature.simple.SimpleFeature;
import org.opentripplanner.graph_builder.services.shapefile.SimpleFeatureConverter;

/**
 * A converter which is a composite of other converters. It can combine them with an "and" or "or"
 * strategy. The orPermissions variable controls that.
 * 
 * @author rob, novalis
 * 
 */
public class CompositeBooleanConverter implements SimpleFeatureConverter {

    private Collection> converters = new ArrayList>();

    private boolean orPermissions = false;

    public CompositeBooleanConverter() {
    }

    public CompositeBooleanConverter(SimpleFeatureConverter... converters) {
        this.converters = new ArrayList>(Arrays.asList(converters));
    }
    
    /**
     * Is the or combination strategy being used?
     * 
     * @return whether the or combination strategy is used
     */
    public boolean isOrPermissions() {
        return orPermissions;
    }

    public void setOrPermissions(boolean orPermissions) {
        this.orPermissions = orPermissions;
    }

    /**
     * set the list of converters used to the passed in parameter
     * 
     * @param converters
     *            list of converters to use
     */
    public void setConverters(
            Collection> converters) {
        this.converters = converters;
    }

    /**
     * use the permission combination strategy to combine the results of the list of converters
     */
    @Override
    public Boolean convert(SimpleFeature feature) {
        Boolean result = null;
        for (SimpleFeatureConverter converter : converters) {
            Boolean value = converter.convert(feature);
            if (result == null) {
                result = value;
            } else {
                if (orPermissions) {
                    result = result || value;
                } else {
                    result = result && value;
                }
            }

        }
        return result;
    }

    /**
     * add a converter to the list to be applied
     * @param converter the new converter
     */
    public void add(SimpleFeatureConverter converter) {
        converters.add(converter);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy