1 16 package org.springframework.webflow.core.collection; 17 18 import java.io.Serializable ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 31 public class CollectionUtils { 32 33 36 public static final Iterator EMPTY_ITERATOR = new EmptyIterator(); 37 38 41 public static final AttributeMap EMPTY_ATTRIBUTE_MAP = new LocalAttributeMap(Collections.EMPTY_MAP); 42 43 46 private CollectionUtils() { 47 } 48 49 54 public static Iterator toIterator(Enumeration enumeration) { 55 return new EnumerationIterator(enumeration); 56 } 57 58 65 public static AttributeMap singleEntryMap(String attributeName, Object attributeValue) { 66 return new LocalAttributeMap(attributeName, attributeValue); 67 } 68 69 77 public static boolean addAllNoDuplicates(List target, Object [] objects) { 78 if (objects == null || objects.length == 0) { 79 return false; 80 } 81 else { 82 boolean changed = false; 83 for (int i = 0; i < objects.length; i++) { 84 if (!target.contains(objects[i])) { 85 target.add(objects[i]); 86 changed = true; 87 } 88 } 89 return changed; 90 } 91 } 92 93 96 private static class EmptyIterator implements Iterator , Serializable { 97 98 private EmptyIterator() { 99 } 100 101 public boolean hasNext() { 102 return false; 103 } 104 105 public Object next() { 106 throw new UnsupportedOperationException ("There are no elements"); 107 } 108 109 public void remove() { 110 throw new UnsupportedOperationException ("There are no elements"); 111 } 112 } 113 114 117 private static class EnumerationIterator implements Iterator { 118 119 private Enumeration enumeration; 120 121 public EnumerationIterator(Enumeration enumeration) { 122 this.enumeration = enumeration; 123 } 124 125 public boolean hasNext() { 126 return enumeration.hasMoreElements(); 127 } 128 129 public Object next() { 130 return enumeration.nextElement(); 131 } 132 133 public void remove() throws UnsupportedOperationException { 134 throw new UnsupportedOperationException ("Not supported"); 135 } 136 } 137 } | Popular Tags |