/** * 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; } } }
/** * 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; }