001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/util/tags/sakai_2-4-1/util-util/util/src/java/org/sakaiproject/util/EntityCollections.java $
003: * $Id: EntityCollections.java 10582 2006-06-14 01:09:58Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import org.sakaiproject.entity.api.Entity;
026:
027: /**
028: * <p>
029: * EntityUtil collects some entity utility methods dealing with collections of entities and entity references.
030: * </p>
031: */
032: public class EntityCollections {
033: /**
034: * See if the collection of entity reference strings has at least one entity that is in the collection of Entity objects.
035: *
036: * @param entityRefs
037: * The collection (String) of entity references.
038: * @param entities
039: * The collection (Entity) of entity objects.
040: * @return true if there is interesection, false if not.
041: */
042: public static boolean isIntersectionEntityRefsToEntities(
043: Collection entityRefs, Collection entities) {
044: for (Iterator iRefs = entityRefs.iterator(); iRefs.hasNext();) {
045: String findThisEntityRef = (String) iRefs.next();
046: for (Iterator iEntities = entities.iterator(); iEntities
047: .hasNext();) {
048: String this EntityRef = ((Entity) iEntities.next())
049: .getReference();
050: if (this EntityRef.equals(findThisEntityRef)) {
051: return true;
052: }
053: }
054: }
055:
056: return false;
057: }
058:
059: /**
060: * See if the collection of entity reference strings is contained in the collection of entities.
061: *
062: * @param entityRefs
063: * The collection (String) of entity references.
064: * @param entities
065: * The collection (Entity) of entity objects.
066: * @return true if there is containment, false if not.
067: */
068: public static boolean isContainedEntityRefsToEntities(
069: Collection entityRefs, Collection entities) {
070: for (Iterator iRefs = entityRefs.iterator(); iRefs.hasNext();) {
071: String findThisEntityRef = (String) iRefs.next();
072: if (!entityCollectionContainsRefString(entities,
073: findThisEntityRef))
074: return false;
075: }
076:
077: return true;
078: }
079:
080: /**
081: * See if the collection of entity reference strings matches completely the collection of Entity objects.
082: *
083: * @param entityRefs
084: * The collection (String) of entity references.
085: * @param entities
086: * The collection (Entity) of entity objects.
087: * @return true if there is a match, false if not.
088: */
089: public static boolean isEqualEntityRefsToEntities(
090: Collection entityRefs, Collection entities) {
091: // if the colletions are the same size
092: if (entityRefs.size() != entities.size())
093: return false;
094:
095: // and each ref is found
096: for (Iterator iRefs = entityRefs.iterator(); iRefs.hasNext();) {
097: String findThisEntityRef = (String) iRefs.next();
098: for (Iterator iEntities = entities.iterator(); iEntities
099: .hasNext();) {
100: String this EntityRef = ((Entity) iEntities.next())
101: .getReference();
102: if (this EntityRef.equals(findThisEntityRef)) {
103: return true;
104: }
105: }
106: }
107:
108: return false;
109: }
110:
111: /**
112: * Test a collection of Entity object for the specified entity reference
113: *
114: * @param entities
115: * The collection (Entity) of entities
116: * @param entityRef
117: * The string entity reference to find.
118: * @return true if found, false if not.
119: */
120: public static boolean entityCollectionContainsRefString(
121: Collection entities, String entityRef) {
122: for (Iterator i = entities.iterator(); i.hasNext();) {
123: Entity entity = (Entity) i.next();
124: if (entity.getReference().equals(entityRef))
125: return true;
126: }
127:
128: return false;
129: }
130:
131: /**
132: * Test a collection of Entity reference Strings for the specified Entity
133: *
134: * @param refs
135: * The collection (String) of entity refs
136: * @param entity
137: * The Entity to find.
138: * @return true if found, false if not.
139: */
140: public static boolean refCollectionContainsEntity(Collection refs,
141: Entity entity) {
142: String targetRef = entity.getReference();
143: for (Iterator i = refs.iterator(); i.hasNext();) {
144: String entityRef = (String) i.next();
145: if (entityRef.equals(targetRef))
146: return true;
147: }
148:
149: return false;
150: }
151:
152: /**
153: * Set the refs collection to the entity reference strings from the entities collection (and nothing more)
154: *
155: * @param refs
156: * The collection (String) of entity references to modify.
157: * @param entities
158: * The collection (Entity) of entity objects to use.
159: */
160: public static void setEntityRefsFromEntities(Collection refs,
161: Collection entities) {
162: refs.clear();
163: for (Iterator i = entities.iterator(); i.hasNext();) {
164: Entity entity = (Entity) i.next();
165: refs.add(entity.getReference());
166: }
167: }
168:
169: /**
170: * Fill in the two collections of Entity reference strings - those added in newEntities that were not in oldEntityRefs, and those removed, i.e. in oldEntityRefs not in newEntities.
171: */
172: public static void computeAddedRemovedEntityRefsFromNewEntitiesOldRefs(
173: Collection addedEntities, Collection removedEntities,
174: Collection newEntities, Collection oldEntityRefs) {
175: // added
176: for (Iterator i = newEntities.iterator(); i.hasNext();) {
177: Entity entity = (Entity) i.next();
178: if (!refCollectionContainsEntity(oldEntityRefs, entity)) {
179: addedEntities.add(entity.getReference());
180: }
181: }
182:
183: // removed
184: for (Iterator i = oldEntityRefs.iterator(); i.hasNext();) {
185: String entityRef = (String) i.next();
186: if (!entityCollectionContainsRefString(newEntities,
187: entityRef)) {
188: removedEntities.add(entityRef);
189: }
190: }
191: }
192: }
|