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