KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > sql > dynamic > elements > IterateContext


1 /*
2  * Created on Apr 17, 2005
3  *
4  */

5 package com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements;
6
7 import java.lang.reflect.Array JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import com.ibatis.sqlmap.client.SqlMapException;
15
16 /**
17  * @author Brandon Goodin
18  *
19  */

20 public class IterateContext implements Iterator JavaDoc {
21
22   private Iterator JavaDoc iterator;
23   private int index = -1;
24
25   private String JavaDoc property;
26   private boolean allowNext = true;
27
28   private boolean isFinal = false;
29   private SqlTag tag;
30   
31   public IterateContext(Object JavaDoc collection,SqlTag tag) {
32     this.tag = tag;
33     if (collection instanceof Collection JavaDoc) {
34       this.iterator = ((Collection JavaDoc) collection).iterator();
35     } else if (collection instanceof Iterator JavaDoc) {
36       this.iterator = ((Iterator JavaDoc) collection);
37     } else if (collection.getClass().isArray()) {
38       List JavaDoc 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 JavaDoc 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 JavaDoc arrayToList(Object JavaDoc array) {
71     List JavaDoc list = null;
72     if (array instanceof Object JavaDoc[]) {
73       list = Arrays.asList((Object JavaDoc[]) array);
74     } else {
75       list = new ArrayList JavaDoc();
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   /**
84    * @return Returns the property.
85    */

86   public String JavaDoc getProperty() {
87     return property;
88   }
89   
90   /**
91    * This property specifies whether to increment the iterate in
92    * the doEndFragment. The ConditionalTagHandler has the ability
93    * to increment the IterateContext, so it is neccessary to avoid
94    * incrementing in both the ConditionalTag and the IterateTag.
95    *
96    * @param property The property to set.
97    */

98   public void setProperty(String JavaDoc property) {
99     this.property = property;
100   }
101   
102   /**
103    * @return Returns the allowNext.
104    */

105   public boolean isAllowNext() {
106     return allowNext;
107   }
108   
109   /**
110    * @param performIterate The allowNext to set.
111    */

112   public void setAllowNext(boolean performIterate) {
113     this.allowNext = performIterate;
114   }
115   /**
116    * @return Returns the tag.
117    */

118   public SqlTag getTag() {
119     return tag;
120   }
121   /**
122    * @param tag The tag to set.
123    */

124   public void setTag(SqlTag tag) {
125     this.tag = tag;
126   }
127
128   /**
129    *
130    * @return
131    */

132   public boolean isFinal() {
133     return isFinal;
134   }
135
136   /**
137    * This attribute is used to mark whether an iterate tag is
138    * in it's final iteration. Since the ConditionalTagHandler
139    * can increment the iterate the final iterate in the doEndFragment
140    * of the IterateTagHandler needs to know it is in it's final iterate.
141    *
142    * @param aFinal
143    */

144   public void setFinal(boolean aFinal) {
145     isFinal = aFinal;
146   }
147
148 }
Popular Tags