1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.util;
23
24 import java.io.Serializable;
25
26
27 /***
28 * A bean that can be used to keep track of a counter.
29 * <p/>
30 * Since it is an Iterator it can be used by the iterator tag
31 *
32 */
33 public class Counter implements java.util.Iterator, Serializable {
34
35 private static final long serialVersionUID = 2796965884308060179L;
36
37 boolean wrap = false;
38
39
40 long first = 1;
41 long current = first;
42 long interval = 1;
43 long last = -1;
44
45
46 public void setAdd(long addition) {
47 current += addition;
48 }
49
50 public void setCurrent(long current) {
51 this.current = current;
52 }
53
54 public long getCurrent() {
55 return current;
56 }
57
58 public void setFirst(long first) {
59 this.first = first;
60 current = first;
61 }
62
63 public long getFirst() {
64 return first;
65 }
66
67 public void setInterval(long interval) {
68 this.interval = interval;
69 }
70
71 public long getInterval() {
72 return interval;
73 }
74
75 public void setLast(long last) {
76 this.last = last;
77 }
78
79 public long getLast() {
80 return last;
81 }
82
83
84 public long getNext() {
85 long next = current;
86 current += interval;
87
88 if (wrap && (current > last)) {
89 current -= ((1 + last) - first);
90 }
91
92 return next;
93 }
94
95 public long getPrevious() {
96 current -= interval;
97
98 if (wrap && (current < first)) {
99 current += (last - first + 1);
100 }
101
102 return current;
103 }
104
105 public void setWrap(boolean wrap) {
106 this.wrap = wrap;
107 }
108
109 public boolean isWrap() {
110 return wrap;
111 }
112
113 public boolean hasNext() {
114 return ((last == -1) || wrap) ? true : (current <= last);
115 }
116
117 public Object next() {
118 return Long.valueOf(getNext());
119 }
120
121 public void remove() {
122
123 }
124 }