DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14780>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14780 Utility class FIFO name value pair collection Summary: Utility class FIFO name value pair collection Product: Struts Version: 1.1 Beta 2 Platform: All OS/Version: All Status: NEW Severity: Enhancement Priority: Other Component: Custom Tags AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] /** * a collection to merge and support the differences in the way struts can * handle collections for the purposes of html select option lists */ package org.apache.struts.util; import java.util.Collection; import java.util.Iterator; import java.util.Set; import org.apache.commons.beanutils.PropertyUtils; /** * this is a FIFO list designed for handling small sets for use in html * select statements and is specifically designed to work with * LabelValueBeans. * @author Edgar P. Dollin */ class ObjectListElement implements java.io.Serializable { /** * the payload of the item */ protected Object element = null; /** * the next in the collection */ protected ObjectListElement next = null; /** * the prior element in the collection */ protected ObjectListElement prior = null; /** * get the value of the element in the collection * @return the value of the payload */ public String getValue() { if (element instanceof LabelValueBean) return ((LabelValueBean) element).getValue(); return element.toString(); } /** * get the label value of the element * @return the label of the payload */ public String getLabel() { if (element instanceof LabelValueBean) return ((LabelValueBean) element).getLabel(); return element.toString(); } /** * test if these are the same items * @param o is the object to test equality against * @return equality of the items */ public boolean equals(Object o) { return element.equals(o); } } /** * class implements a FIFO set, library only has a LIFO set * @author Edgar P. Dollin */ class IteratorFIFOList implements Iterator { /** * the position in the list */ private ObjectListElement position = null; /** * when we start the interator it is 'already' at the first item in * the list */ private boolean alreadyFirst = true; /** * prevent random instantiation */ private IteratorFIFOList() { ; } /** * the constructor of the iterator * @param o is the object to build the iterator for */ public IteratorFIFOList (LabelValueList o) { position = o.first; alreadyFirst = true; } /** * the has next operation * @return whether there is another item to be had in the set */ public boolean hasNext() { if (position == null) return false; if (alreadyFirst == true) { if (position == null) return false; else return true; } if (position.next == null) return false; return true; } /** * the next operation * @return the next object in the set */ public Object next() { if (alreadyFirst == true) { alreadyFirst = false; return position.element; } if (position != null) position = position.next; return position.element; } /** * we don't need to remove but the spec says we do */ public void remove() { } } /** * the actual class implementation * @author Edgar P. Dollin */ public class LabelValueList implements Set { /** * the first element in the collection */ protected ObjectListElement first = null; /** * the last element in the collection */ protected ObjectListElement last = null; /** * the number of items in the colleciton */ private int count = 0; /** * getter method for the the number of elements in the collection * @return the count */ public int size() { return count; } /** * add the object on the end of the list * @param o the object to add * @return whether the object was added */ public boolean add(Object o) { // defend against bad adds if (o == null) return false; // skip duplications of LabelValueBeans if (o instanceof LabelValueBean) { for (ObjectListElement i = first; i != null; i = i.next) { if (i.element instanceof LabelValueBean) { if (i.equals(o)) return false; } } } // add the object ObjectListElement addObject = new ObjectListElement(); addObject.element = o; count++; // add the first object if (first == null) { first = addObject; last = addObject; return true; } // add it as the last object addObject.prior = last; last.next = addObject; last = addObject; return true; } /** * a simplified add to build label value pairs * @param value the value of the label / value pair * @param label the label of the label / value pair */ public void add(String value, String label) { LabelValueBean lvb = new LabelValueBean(label, value); add(lvb); } /** * Returns true if this set contains the specified element. * @param o the object to check for containment * @return whether the object is a member of the set */ public boolean contains(Object o) { for (ObjectListElement i = first; i != null; i = i.next) { if (i.element instanceof LabelValueBean) { LabelValueBean lvb = (LabelValueBean) i.element; if (o instanceof LabelValueBean) { if (lvb.equals(o)) return true; } else if (o instanceof String) { if (lvb.equals(o)) return true; } else if (o.equals(i.element)) return true; } else if (o.equals(i.element)) return true; } return false; } /** * another simplified access for LabelValueBean * @param value the value to test if exists in one of the beans in * the collection * @return whether the set contains the value */ public boolean contains(String value) { return contains(new LabelValueBean(value, value)); } /** * Returns true if this set contains all of the elements of the * specified collection. * @param c the collection to compare to the existing collection * @return success or failure of the test */ public boolean containsAll(Collection c) { // weed out the simple tests if (this == c) return true; // try each of the elements for (Iterator i = c.iterator(); i.hasNext();) { if (!contains(i.next())) return false; } return true; } /** * Returns true if this set contains no elements. * @return existance of any members of the set */ public boolean isEmpty() { if (count == 0) return true; return false; } /** * Returns an iterator over the elements in this set. * @return the java.util iterator */ public Iterator iterator() { return new IteratorFIFOList(this); } /** * Returns an array containing all of the elements in this set. * @return the collection as a java array */ public Object[] toArray() { Object[] wrk = new Object[count]; int j = 0; for (ObjectListElement i = first; i != null; i = i.next) { wrk[j++] = i.element; } return wrk; } /** * turn the objects elements into an array * @param a the array to copy items into * @return the new array */ public Object[] toArray(Object[] a) { Object[] wrk = new Object[java.lang.reflect.Array.getLength(a)]; for (int i = 0; i < java.lang.reflect.Array.getLength(a); i++) { wrk[i] = a[i]; } return wrk; } /** * Removes the specified element from this set if it is present * (optional operation). * @param o the object to remove from the collection * @return success or failure of the operation */ public boolean remove(Object o) { // can't delete a null if (o == null) return false; ObjectListElement i; for (i = first; i != null; i = i.next) { if (o.equals(i.element)) break; } // it wasn't there, just exit if (i == null) return false; // update the first and last if (i == first) first = i.next; if (i == last) last = i.prior; // update the references if (i.next != null) i.next.prior = i.prior; if (i.prior != null) i.prior.next = i.next; // release the object i.element = null; i.next = null; i.prior = null; i = null; // decrement the count count--; return true; } /** * add all the items in the collection * @param c the collection to add * @return success of the addition */ public boolean addAll(Collection c) { java.util.Iterator i = c.iterator(); if (i == null) return false; while (i.hasNext()) add(i.next()); return true; } /** * not implemented - retain only items in collection * @param c the collection of objects to retain * @return the success of the items */ public boolean retainAll(Collection c) { return false; } /** * Removes from this set all of its elements that are contained in * the specified collection (optional operation). * @param c the collection to remove * @return whether the collection was removed */ public boolean removeAll(Collection c) { java.util.Iterator i = c.iterator(); if (i == null) return false; while (i.hasNext()) remove(i.next()); return true; } /** * remove all the elements from the set */ public void clear() { ObjectListElement wrk = null; for (ObjectListElement i = first; i != null;) { i.element = null; wrk = i; i = i.next; wrk.next = null; } first = null; last = null; } /** * get the equivalent object * @param o find the equivalent object * @return the equivalent object in the set */ public Object read(Object o) { if (o == null) return null; for (ObjectListElement i = first; i != null; i = i.next) { if (i.equals(o)) { return i.element; } } return null; } /** * case insensitive lookup of the label assocated with a value * LabelValueBean objects are handled more efficiently * @param value the string value to use as the key to the label / * value pair * @return the label associated with the value */ public String lookup(String value) { // don't do anything if you don't have to if (value == null) return null; // process each item in the list for (ObjectListElement o = first ; o != null ; o = o.next) { // check LVB's first if (o.element instanceof LabelValueBean) { LabelValueBean lvb = (LabelValueBean) o.element; if (value.equalsIgnoreCase(lvb.getValue())) return lvb.getLabel(); continue; } // work through each of the items in the collection // looking for elements with value and label get // methods try { // Get the value for this option String pairValue = PropertyUtils.getProperty (o.element, "value").toString(); if (pairValue == null) continue; // check we have the correct value if (!value.equalsIgnoreCase(pairValue)) continue; // Get the label for this option String pairLabel = (PropertyUtils.getProperty (o.element, "label")).toString(); return pairLabel; } // print the stack trace, but don't stop the processing catch (Exception e) { e.printStackTrace(); return null; } } // ran through the whole collection and couldn't find any match return null; } } -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>