View Javadoc

1   /*
2    * $Id: Counter.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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      // Attributes ----------------------------------------------------
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      // Public --------------------------------------------------------
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         // Do nothing
119     }
120 }