package edu.stanford.rt.credential; import java.util.*; /** * @author Ninghui Li, Sandra Qiu
* * Implementation of IntersectionContainment element. * */ public class RoleIntersection implements PrincipalExpression { /** * list of members in this intersection. */ private ArrayList parts; // required element. /** * Constuctor for an empty RoleIntersection. */ public RoleIntersection() { parts = new ArrayList(); } /** * Constructor for RoleIntersection */ public RoleIntersection(List p) { if (p == null || p.size() == 0) throw new IllegalArgumentException("RoleIntersection: empty parts."); // copy over parts = new ArrayList(); Iterator it = p.iterator(); while (it.hasNext()) { Object pr = it.next(); if (!(pr instanceof RoleTerm) && !(pr instanceof Role)) throw new IllegalArgumentException("RoleIntersection: wrong element type"); parts.add(pr); } } /** * Method getParts. * returns an unmodifiable view of the members. * @return List */ public List getParts() { return Collections.unmodifiableList(parts); } /** * Method and. * AND operation. * @param part * @throws CredException */ public void and(Object part) throws CredException { if (!(part instanceof RoleTerm) && !(part instanceof Role)) throw new CredException("Illegal part type"); // add this new part to the set. if (!parts.contains(part)) parts.add(part); } /* (non-Javadoc) * @see edu.stanford.rt.credential.PrincipalExpression#toString(String) */ public String toString(String indent) { String thisIndent = indent + " "; StringBuffer sb = new StringBuffer(); sb.append(thisIndent).append("Role Intersection: \n"); for (int i = 0; i < parts.size(); i++) { if ((parts.get(i)) instanceof Role) { sb.append( ((Role) parts.get(i)).toString( thisIndent + " ")); } else if ((parts.get(i)) instanceof RoleTerm) { sb.append( ((RoleTerm) parts.get(i)).toString( thisIndent + " ")); } sb.append("\n"); } return sb.toString(); } }