package edu.stanford.peer.rbtm.credential; import java.util.*; public class Intersection implements EntityExpression { /** * The parts of the intersection */ protected LinkedList parts; /** * Default constructor. Expressions to be intersected may be added using * the and() method. */ public Intersection() { parts = new LinkedList(); } /** * Default copy constructor. All part references are duplicated, so the * new copy is not relying on the old ones parts list. */ public Intersection(Intersection i) { this(); Iterator it = i.getParts(); while(it.hasNext()) parts.add(it.next()); } /** * Adds a new expression to this intersection. And() calls may be * concatenated similar to appending with a StringBuffer instance. * @param expr An expression to be added (a role or linked role) * @return The updated intersection expression */ public Intersection and(EntityExpression expr) { parts.add(expr); return this; } /** * Public accessor method for accessing each expression in the * intersection. */ public Iterator getParts() { return parts.iterator(); } /** * Public access method for determining the number of subexpressions * contained in this intersection. */ public int getPartsCount() { return parts.size(); } /** * Compare the parts of two intersections. * @return a boolean result for equivalence */ public boolean equals(Object o) { return (o instanceof Intersection) && parts.equals(((Intersection)o).parts); } /** * * @return a unique hashcode base on the parts of the intersection. */ public int hashCode() { return parts.hashCode(); } /** * Converts the intersection into a human readable form. Expressions * are separated using a '^' symbol to represent an intersection. * * @return formatted string representation of an intersection */ public String toString() { Iterator i = getParts(); StringBuffer buff = new StringBuffer(); while(i.hasNext()) { buff.append(i.next().toString()); if(i.hasNext())buff.append(" ^ "); } return buff.toString(); } }