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

prefuse.data.expression.CompositePredicate Maven / Gradle / Ivy

Go to download

Prefuse is a set of software tools for creating rich interactive data visualizations in the Java programming language.

The newest version!
package prefuse.data.expression;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Abstract base class for Predicate instances that maintain one or
 * more sub-predicates (clauses).
 * 
 * @author jeffrey heer
 */
public abstract class CompositePredicate extends AbstractPredicate {

    protected ArrayList m_clauses = new ArrayList(2);
    
    /**
     * Create a new, empty CompositePredicate.
     */
    public CompositePredicate() {
    }
    
    /**
     * Create a new CompositePredicate.
     * @param p1 the first sub-predicate
     * @param p2 the second sub-predicate
     */
    public CompositePredicate(Predicate p1, Predicate p2) {
        m_clauses.add(p1);
        m_clauses.add(p2);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * Add a new clause.
     * @param p the Predicate clause to add
     */
    public void add(Predicate p) {
        if ( m_clauses.contains(p) ) {
            throw new IllegalArgumentException("Duplicate predicate.");
        }
        m_clauses.add(p);
        fireExpressionChange();
    }
    
    /**
     * Remove a new clause.
     * @param p the Predicate clause to remove
     * @return true if removed, false if not found
     */
    public boolean remove(Predicate p) {
        if ( m_clauses.remove(p) ) {
            fireExpressionChange();
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Remove all clauses.
     */
    public void clear() {
        removeChildListeners();
        m_clauses.clear();
        fireExpressionChange();
    }
    
    /**
     * Get the number of sub-predicate clauses.
     * @return the number of clauses
     */
    public int size() {
        return m_clauses.size();
    }
    
    /**
     * Get the sub-predicate at the given index.
     * @param idx the index to lookup
     * @return the sub-predicate at the given index
     */
    public Predicate get(int idx) {
        return (Predicate)m_clauses.get(idx);
    }
    
    /**
     * Set the given predicate to be the only clause of this composite.
     * @param p the new sole sub-predicate clause
     */
    public void set(Predicate p) {
        removeChildListeners();
        m_clauses.clear();
        m_clauses.add(p);
        if ( hasListeners() ) addChildListeners();
        fireExpressionChange();
    }
    
    /**
     * Set the given predicates to be the clauses of this composite.
     * @param p the new sub-predicate clauses
     */
    public void set(Predicate[] p) {
        removeChildListeners();
        m_clauses.clear();
        for ( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy