View Javadoc

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