/**
  * Set the refs collection to the entity reference strings from the entities collection (and
  * nothing more)
  *
  * @param refs The collection (String) of entity references to modify.
  * @param entities The collection (Entity) of entity objects to use.
  */
 public static void setEntityRefsFromEntities(Collection refs, Collection entities) {
   refs.clear();
   for (Iterator i = entities.iterator(); i.hasNext(); ) {
     Entity entity = (Entity) i.next();
     refs.add(entity.getReference());
   }
 }
  /**
   * Test a collection of Entity object for the specified entity reference
   *
   * @param entities The collection (Entity) of entities
   * @param entityRef The string entity reference to find.
   * @return true if found, false if not.
   */
  public static boolean entityCollectionContainsRefString(Collection entities, String entityRef) {
    for (Iterator i = entities.iterator(); i.hasNext(); ) {
      Entity entity = (Entity) i.next();
      if (entity.getReference().equals(entityRef)) return true;
    }

    return false;
  }
  /**
   * Test a collection of Entity reference Strings for the specified Entity
   *
   * @param refs The collection (String) of entity refs
   * @param entity The Entity to find.
   * @return true if found, false if not.
   */
  public static boolean refCollectionContainsEntity(Collection refs, Entity entity) {
    String targetRef = entity.getReference();
    for (Iterator i = refs.iterator(); i.hasNext(); ) {
      String entityRef = (String) i.next();
      if (entityRef.equals(targetRef)) return true;
    }

    return false;
  }
  /**
   * 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.
   */
  public static void computeAddedRemovedEntityRefsFromNewEntitiesOldRefs(
      Collection addedEntities,
      Collection removedEntities,
      Collection newEntities,
      Collection oldEntityRefs) {
    // added
    for (Iterator i = newEntities.iterator(); i.hasNext(); ) {
      Entity entity = (Entity) i.next();
      if (!refCollectionContainsEntity(oldEntityRefs, entity)) {
        addedEntities.add(entity.getReference());
      }
    }

    // removed
    for (Iterator i = oldEntityRefs.iterator(); i.hasNext(); ) {
      String entityRef = (String) i.next();
      if (!entityCollectionContainsRefString(newEntities, entityRef)) {
        removedEntities.add(entityRef);
      }
    }
  }