39 40 public class ContainmentFilter implements AttrFilter { 41 private Attributes matchingAttrs; 42 43 public ContainmentFilter(Attributes match) { 44 matchingAttrs = match; 45 } 46 47 public boolean check(Attributes attrs) throws NamingException { 48 return matchingAttrs == null || 49 matchingAttrs.size() == 0 || 50 contains(attrs, matchingAttrs); 51 } 52 53 // returns true if superset contains subset 54 public static boolean contains(Attributes superset, Attributes subset) 55 throws NamingException { 56 if (subset == null) 57 return true; // an empty set is always a subset 58 59 NamingEnumeration m = subset.getAll(); 60 while (m.hasMore()) { 61 if (superset == null) { 62 return false; // contains nothing 63 } 64 Attribute target = (Attribute) m.next(); 65 Attribute fromSuper = superset.get(target.getID()); 66 if (fromSuper == null) { 67 return false; 68 } else { 69 // check whether attribute values match 70 if (target.size() > 0) { 71 NamingEnumeration vals = target.getAll(); 72 while (vals.hasMore()) { 73 if (!fromSuper.contains(vals.next())) { 74 return false; 75 } 76 } 77 } 78 } 79 } 80 return true; 81 } 82 83 } | 39 40 public class ContainmentFilter implements AttrFilter { 41 private Attributes matchingAttrs; 42 43 public ContainmentFilter(Attributes match) { 44 matchingAttrs = match; 45 } 46 47 public boolean check(Attributes attrs) throws NamingException { 48 return matchingAttrs == null || 49 matchingAttrs.size() == 0 || 50 contains(attrs, matchingAttrs); 51 } 52 53 // returns true if superset contains subset 54 public static boolean contains(Attributes superset, Attributes subset) 55 throws NamingException { 56 if (subset == null) 57 return true; // an empty set is always a subset 58 59 NamingEnumeration<? extends Attribute> m = subset.getAll(); 60 while (m.hasMore()) { 61 if (superset == null) { 62 return false; // contains nothing 63 } 64 Attribute target = m.next(); 65 Attribute fromSuper = superset.get(target.getID()); 66 if (fromSuper == null) { 67 return false; 68 } else { 69 // check whether attribute values match 70 if (target.size() > 0) { 71 NamingEnumeration<?> vals = target.getAll(); 72 while (vals.hasMore()) { 73 if (!fromSuper.contains(vals.next())) { 74 return false; 75 } 76 } 77 } 78 } 79 } 80 return true; 81 } 82 83 } |