KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > Counter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import java.io.Serializable JavaDoc;
8
9
10 /**
11  * A bean that can be used to keep track of a counter.
12  * <p/>
13  * Since it is an Iterator it can be used by the iterator tag
14  *
15  * @author Rickard Öberg (rickard@middleware-company.com)
16  * @version $Revision: 1.3 $
17  * @see <related>
18  */

19 public class Counter implements java.util.Iterator JavaDoc, Serializable JavaDoc {
20     //~ Instance fields ////////////////////////////////////////////////////////
21

22     boolean wrap = false;
23
24     // Attributes ----------------------------------------------------
25
long first = 1;
26     long current = first;
27     long interval = 1;
28     long last = -1;
29
30     //~ Methods ////////////////////////////////////////////////////////////////
31

32     public void setAdd(long addition) {
33         current += addition;
34     }
35
36     public void setCurrent(long current) {
37         this.current = current;
38     }
39
40     public long getCurrent() {
41         return current;
42     }
43
44     public void setFirst(long first) {
45         this.first = first;
46         current = first;
47     }
48
49     public long getFirst() {
50         return first;
51     }
52
53     public void setInterval(long interval) {
54         this.interval = interval;
55     }
56
57     public long getInterval() {
58         return interval;
59     }
60
61     public void setLast(long last) {
62         this.last = last;
63     }
64
65     public long getLast() {
66         return last;
67     }
68
69     // Public --------------------------------------------------------
70
public long getNext() {
71         long next = current;
72         current += interval;
73
74         if (wrap && (current > last)) {
75             current -= ((1 + last) - first);
76         }
77
78         return next;
79     }
80
81     public long getPrevious() {
82         current -= interval;
83
84         if (wrap && (current < first)) {
85             current += (last - first + 1);
86         }
87
88         return current;
89     }
90
91     public void setWrap(boolean wrap) {
92         this.wrap = wrap;
93     }
94
95     public boolean isWrap() {
96         return wrap;
97     }
98
99     public boolean hasNext() {
100         return ((last == -1) || wrap) ? true : (current <= last);
101     }
102
103     public Object JavaDoc next() {
104         return new Long JavaDoc(getNext());
105     }
106
107     public void remove() {
108         // Do nothing
109
}
110 }
111
Popular Tags