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

org.srplib.criteria.Junction Maven / Gradle / Ivy

package org.srplib.criteria;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * A criterion representing junction operation. Is used for criteria joining (NOT, AND, OR etc.).
 *
 * @author Anton Pechinsky
 */
public class Junction implements Criterion {

    private static final long serialVersionUID = -1L;

    private JunctionType type;

    private List criteria;

    /**
     * For serialization purposes
     */
    private Junction() {
    }

    /**
     * Constructs junction with specified type and list of criteria
     *
     * @param type JunctionType type of junction
     * @param criteria List of {@link Criterion}
     */
    public Junction(JunctionType type, List criteria) {
        if (type == null) {
            throw new IllegalArgumentException("'type' should not be null");
        }
        if (criteria == null || criteria.isEmpty()) {
            throw new IllegalArgumentException("'criteria' should not be null or empty");
        }
        if (!type.isMultivalue() && criteria.size() > 1) {
            throw new IllegalArgumentException("'" + type + "' doesn't support multiple values.");
        }

        this.type = type;
        this.criteria = new ArrayList(criteria);
    }

    /**
     * Returns type of junction.
     *
     * @return JunctionType type of junction
     */
    public JunctionType getType() {
        return type;
    }

    /**
     * Returns joined criteria.
     *
     * @return List of {@link Criteria}.
     */
    public List getCriteria() {
        return Collections.unmodifiableList(criteria);
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(type).append("(");
        for (Iterator iterator = criteria.iterator(); iterator.hasNext();) {
            stringBuilder.append(iterator.next());
            stringBuilder.append(iterator.hasNext() ? "," : "");
        }
        stringBuilder.append(")");
        return stringBuilder.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy