1 5 package com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements; 6 7 import java.lang.reflect.Array ; 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import com.ibatis.sqlmap.client.SqlMapException; 15 16 20 public class IterateContext implements Iterator { 21 22 private Iterator iterator; 23 private int index = -1; 24 25 private String property; 26 private boolean allowNext = true; 27 28 private boolean isFinal = false; 29 private SqlTag tag; 30 31 public IterateContext(Object collection,SqlTag tag) { 32 this.tag = tag; 33 if (collection instanceof Collection ) { 34 this.iterator = ((Collection ) collection).iterator(); 35 } else if (collection instanceof Iterator ) { 36 this.iterator = ((Iterator ) collection); 37 } else if (collection.getClass().isArray()) { 38 List list = arrayToList(collection); 39 this.iterator = list.iterator(); 40 } else { 41 throw new SqlMapException("ParameterObject or property was not a Collection, Array or Iterator."); 42 } 43 } 44 45 public boolean hasNext() { 46 return iterator != null && iterator.hasNext(); 47 } 48 49 public Object next() { 50 index++; 51 return iterator.next(); 52 } 53 54 public void remove() { 55 iterator.remove(); 56 } 57 58 public int getIndex() { 59 return index; 60 } 61 62 public boolean isFirst() { 63 return index == 0; 64 } 65 66 public boolean isLast() { 67 return iterator != null && !iterator.hasNext(); 68 } 69 70 private List arrayToList(Object array) { 71 List list = null; 72 if (array instanceof Object []) { 73 list = Arrays.asList((Object []) array); 74 } else { 75 list = new ArrayList (); 76 for (int i = 0, n = Array.getLength(array); i < n; i++) { 77 list.add(Array.get(array, i)); 78 } 79 } 80 return list; 81 } 82 83 86 public String getProperty() { 87 return property; 88 } 89 90 98 public void setProperty(String property) { 99 this.property = property; 100 } 101 102 105 public boolean isAllowNext() { 106 return allowNext; 107 } 108 109 112 public void setAllowNext(boolean performIterate) { 113 this.allowNext = performIterate; 114 } 115 118 public SqlTag getTag() { 119 return tag; 120 } 121 124 public void setTag(SqlTag tag) { 125 this.tag = tag; 126 } 127 128 132 public boolean isFinal() { 133 return isFinal; 134 } 135 136 144 public void setFinal(boolean aFinal) { 145 isFinal = aFinal; 146 } 147 148 } | Popular Tags |