示例#1
0
 /**
  * Helper method for subclasses to implement getFooAttribute(String attributeName), as such:
  *
  * <pre>
  * public class User
  * {
  *     private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>();
  *
  *     public String getUserAttribute(String attributeName)
  *     {
  *         return PersistentAttribute.getAttribute(userAttributes, attributeName);
  *     }
  * }
  * </pre>
  *
  * @param attributeSet the set of objects derived from PersistentAttribute to turn into a
  *     Map<String,String>
  * @param attributeName the name of the desired attribute
  * @return the attribute value or null if the attribute does not exist
  */
 public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>>
     String getAttribute(Set<AttributeClass> attributeSet, String attributeName) {
   for (AttributeClass attribute : attributeSet) {
     if (attribute.getName().equals(attributeName)) return attribute.getValue();
   }
   return null;
 }
示例#2
0
 /**
  * Helper method for subclasses to implement getFooAttributes(), as such:
  *
  * <pre>
  * public class User
  * {
  *     private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>();
  *
  *     public Map<String,String> getUserAttributes()
  *     {
  *         return PersistentAttribute.buildMap(userAttributes);
  *     }
  * }
  * </pre>
  *
  * @param attributeSet the set of objects derived from PersistentAttribute to turn into a
  *     Map<String,String>
  * @return an unmodifiable Map representing the attributes attached to the object
  */
 public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>>
     Map<String, String> buildMap(Set<AttributeClass> attributeSet) {
   Map<String, String> map = new HashMap<String, String>(attributeSet.size());
   for (AttributeClass attribute : attributeSet) {
     map.put(attribute.getName(), attribute.getValue());
   }
   return Collections.unmodifiableMap(map);
 }
示例#3
0
  /**
   * Helper method for subclasses to implement removeFooAttribute(), as such:
   *
   * <pre>
   * public class User
   * {
   *     private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>();
   *
   *     public void removeUserAttribute(String name)
   *     {
   *         PersistentAttribute.clearAttribute(userAttributes, name);
   *     }
   * }
   * </pre>
   *
   * @param attributeSet the set to modify by removing the target attribute
   * @param name the attribute name
   */
  public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>>
      void clearAttribute(Set<AttributeClass> attributeSet, String name) {
    validateNotNull(attributeSet, "clearExistingAttribute.attributeSet must be non-null");
    validateNotNull(name, "clearExistingAttribute.name must be non-null");

    for (AttributeClass existingAttribute : attributeSet) {
      if (existingAttribute.getName().equals(name)) {
        attributeSet.remove(existingAttribute);
        existingAttribute.setOwner(null);
        return;
      }
    }
  }
示例#4
0
  /**
   * Helper method for subclasses to implement setFooAttribute(), as such:
   *
   * <pre>
   * public class User
   * {
   *     private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>();
   *
   *     public void setUserAttribute(String name, String value)
   *     {
   *         PersistentAttribute.setAttribute(this, userAttributes, name, value, UserAttribute.class);
   *     }
   * }
   * </pre>
   *
   * This function will overwrite any existing attribute of the same name.
   *
   * @param owner the owner of the attribute collection (usually the <code>this</code> pointer of
   *     the caller
   * @param attributeSet the set to modify by adding or replacing the target attribute
   * @param name the attribute name
   * @param value the attribute value
   * @param attributeClass the Class of the PersistentAttribute subclass
   * @return the newly created attribute object (already stored in the attributeSet)
   */
  public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>>
      AttributeClass setAttribute(
          OwnerClass owner,
          Set<AttributeClass> attributeSet,
          String name,
          String value,
          Class<? extends AttributeClass> attributeClass) {
    validateNotNull(attributeSet, "setAttribute.attributeSet must be non-null");
    validateNotNull(name, "clearExistingAttribute.name must be non-null");
    validateNotNull(value, "clearExistingAttribute.value must be non-null");
    validateNotNull(attributeClass, "clearExistingAttribute.attributeClass must be non-null");

    for (AttributeClass existingAttribute : attributeSet) {
      if (existingAttribute.getName().equals(name)) {
        // we must remove/re-add the existing attribute when changing its
        // value so that it gets filed in the correct hash bucket
        attributeSet.remove(existingAttribute);
        existingAttribute.setValue(value);
        attributeSet.add(existingAttribute);
        return existingAttribute;
      }
    }

    AttributeClass attribute;
    try {
      Constructor<? extends AttributeClass> constructor =
          attributeClass.getConstructor(String.class, String.class);
      attribute = constructor.newInstance(name, value);
    } catch (Exception e) {
      throw new IllegalArgumentException("can't instantiate class: " + attributeClass.getName(), e);
    }

    attributeSet.add(attribute);
    attribute.setOwner(owner);

    return attribute;
  }