1 13 package info.magnolia.cms.gui.controlx.list; 14 15 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.NoSuchElementException ; 19 20 import org.apache.commons.lang.StringUtils; 21 22 23 26 public class ListModelIteratorImpl implements ListModelIterator { 27 28 31 private final List list; 32 33 36 private int pos; 37 38 41 private Object next; 42 43 46 private Object current; 47 48 51 private String groupKey; 52 53 private ValueProvider valueProvider; 54 55 public ListModelIteratorImpl(List list, String groupKey, ValueProvider valueProvider) { 56 this.list = new ArrayList (list); 57 this.groupKey = groupKey; 58 this.pos = 0; 59 this.setValueProvider(valueProvider); 60 61 prefetchNext(); 63 } 64 65 69 public ListModelIteratorImpl(List list, String groupKey) { 70 this(list, groupKey, DefaultValueProvider.getInstance()); 71 } 72 73 76 private void prefetchNext() { 77 this.next = null; 78 while (this.next == null && this.pos < this.list.size()) { 79 this.next = this.list.get(pos); 80 } 81 } 82 83 87 public Object getValue(String name) { 88 return this.getValue(name, this.current); 89 } 90 91 96 protected Object getValue(String name, Object node) { 97 return this.getValueProvider().getValue(name, node); 98 } 99 100 103 public Object getValueObject() { 104 return this.current; 105 } 106 107 111 public String getGroupName() { 112 if (StringUtils.isEmpty(this.groupKey)) { 113 return StringUtils.EMPTY; 114 } 115 return (String ) this.getValue(this.groupKey, this.current); 116 } 117 118 121 public Object next() { 122 if (this.next == null) { 123 throw new NoSuchElementException (); 124 } 125 this.current = this.next; 126 this.pos++; 127 prefetchNext(); 128 129 return this.current; 130 } 131 132 135 public Object nextGroup() { 136 Object tmp = null; 137 while (this.hasNextInGroup()) { 138 tmp = this.next(); 139 } 140 return tmp; 141 } 142 143 147 public boolean hasNext() { 148 return this.next != null; 149 } 150 151 155 public boolean hasNextInGroup() { 156 if (StringUtils.isEmpty(this.groupKey)) { 157 return this.hasNext(); } 159 else if (this.hasNext()) { 160 if (this.current != null) { 161 String currentValue = (String ) this.getValue(this.groupKey, this.current); 162 String nextValue = (String ) this.getValue(this.groupKey, this.next); 163 return StringUtils.equalsIgnoreCase(currentValue, nextValue); 164 } 165 } 166 else { 167 return false; 168 } 169 return true; 170 } 171 172 175 public void remove() { 176 } 178 179 public void setValueProvider(ValueProvider valueProvider) { 180 this.valueProvider = valueProvider; 181 } 182 183 public ValueProvider getValueProvider() { 184 return valueProvider; 185 } 186 187 } 188 | Popular Tags |