View Javadoc

1   /*
2    * $Id: Counter.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      // Attributes ----------------------------------------------------
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      // Public --------------------------------------------------------
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         // Do nothing
122     }
123 }