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