ruper2/src/java/core/org/krysalis/ruper/util/select CompositeSelector.java,NONE,1.1 SelectorSet.java,1.4,NONE
| Newsgroups | gmane.comp.krysalis.metamorphosis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper/util/select
In directory sc8-pr-cvs1:/tmp/cvs-serv32387/src/java/core/org/krysalis/ruper/util/select
Added Files:
CompositeSelector.java
Removed Files:
SelectorSet.java
Log Message:
Refactored SelectorSet & added some logic selectors
--- NEW FILE: CompositeSelector.java ---
package org.krysalis.ruper.util.select;
import org.krysalis.version.util.debug.Dumpable;
import org.krysalis.version.util.debug.DebugUtils;
import org.krysalis.version.util.SystemUtils;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.PrintWriter;
/**
* Abstract selector thas is composed of child selectors. Doesn't
* specify how the select method should be resolved, that is left
* to specific implementations.
*
* A composite selector maintains the order in which children are added
* (in case that is significant for the evaluation) and guarantees that
* children will only be added once.
*
* @author Ben Sommerville
*/
public abstract class CompositeSelector implements Selector, Dumpable {
// Would be much easier if we could use a LinkedHashSet, but that
// isn't available until JDK 1.4
private List m_children = new LinkedList();
/**
* Add a selector to this composite if it is not already present.
* If selector has already been added, the add will fail (and
* return false).
* @param selector Selector to add
* @return true if this composite did not already contain the selector
* @throws NullPointerException If selector is null
*/
public boolean addSelector(Selector selector) {
if( selector == null ) {
throw new NullPointerException("Can't add null selector to composite");
}
boolean wasAdded;
if( m_children.contains( selector ) ) {
wasAdded = false;
} else {
wasAdded = m_children.add( selector );
}
return wasAdded;
}
/**
* Get the number of children
* @return Number of children
*/
public int childCount() {
return m_children.size();
}
/**
* Get an iterator to access children
* @return Iterator that iterates over children
*/
public Iterator childIterator() {
return m_children.iterator();
}
/**
* Check if this selector has children
* @return true if it has children
*/
public boolean hasChildren() {
return !m_children.isEmpty();
}
/**
* Get the name of this type of selector. This is used for
* debug output.
* By default will return type name based on class name
*
* @return Selector type name.
*/
public String getTypeName() {
String name = SystemUtils.getShortName( this );
String selectorSuffix = "Selector";
if( name.endsWith( selectorSuffix ) ) {
name = name.substring(0, name.length() - selectorSuffix.length() );
}
return name;
}
/**
* Override of equals method (@see java.lang.Object#equals).
* CompositeSelectors are equal if they are of the same
* type and have the same children (number and order).
* @param obj Object compare with
* @return True if objects are equal
*/
public boolean equals(Object obj) {
boolean isEqual = false;
if( sameType( obj ) ) {
CompositeSelector rhs = (CompositeSelector) obj;
if( childCount() == rhs.childCount() ){
Iterator lhsIter = childIterator();
Iterator rhsIter = rhs.childIterator();
isEqual = true;
while( lhsIter.hasNext() && rhsIter.hasNext() && isEqual ) {
Object lhsChild = lhsIter.next();
Object rhsChild = rhsIter.next();
isEqual = lhsChild.equals( rhsChild );
}
}
}
return isEqual;
}
public int hashCode() {
int typeHash = getType().hashCode();
return typeHash * 41 + m_children.hashCode();
}
public void dump(PrintWriter out, int depth, boolean verbose) {
String indent = DebugUtils.getIndent( depth );
out.print( indent );
out.print( getTypeName() );
out.println( " (");
Iterator iter = childIterator();
while (iter.hasNext()) {
Selector selector = (Selector) iter.next();
DebugUtils.dump( out, depth+1, verbose, selector );
}
out.print(indent);
out.println( ")");
}
/**
* Determine if object is same type of selector
* @param obj Object to check
* @return true if the types are the same
*/
private boolean sameType( Object obj ) {
boolean isSame = false;
if( obj instanceof CompositeSelector ) {
CompositeSelector rhs = (CompositeSelector) obj;
return getType().equals( rhs.getType() );
}
return isSame;
}
/**
* Get an object representing the selector type
* @return Type object
*/
private Object getType() {
return getClass();
}
}
--- SelectorSet.java DELETED ---
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf