001: /*
002: * $Id: Counter.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.io.Serializable;
024:
025: /**
026: * A bean that can be used to keep track of a counter.
027: * <p/>
028: * Since it is an Iterator it can be used by the iterator tag
029: *
030: */
031: public class Counter implements java.util.Iterator, Serializable {
032:
033: private static final long serialVersionUID = 2796965884308060179L;
034:
035: boolean wrap = false;
036:
037: // Attributes ----------------------------------------------------
038: long first = 1;
039: long current = first;
040: long interval = 1;
041: long last = -1;
042:
043: public void setAdd(long addition) {
044: current += addition;
045: }
046:
047: public void setCurrent(long current) {
048: this .current = current;
049: }
050:
051: public long getCurrent() {
052: return current;
053: }
054:
055: public void setFirst(long first) {
056: this .first = first;
057: current = first;
058: }
059:
060: public long getFirst() {
061: return first;
062: }
063:
064: public void setInterval(long interval) {
065: this .interval = interval;
066: }
067:
068: public long getInterval() {
069: return interval;
070: }
071:
072: public void setLast(long last) {
073: this .last = last;
074: }
075:
076: public long getLast() {
077: return last;
078: }
079:
080: // Public --------------------------------------------------------
081: public long getNext() {
082: long next = current;
083: current += interval;
084:
085: if (wrap && (current > last)) {
086: current -= ((1 + last) - first);
087: }
088:
089: return next;
090: }
091:
092: public long getPrevious() {
093: current -= interval;
094:
095: if (wrap && (current < first)) {
096: current += (last - first + 1);
097: }
098:
099: return current;
100: }
101:
102: public void setWrap(boolean wrap) {
103: this .wrap = wrap;
104: }
105:
106: public boolean isWrap() {
107: return wrap;
108: }
109:
110: public boolean hasNext() {
111: return ((last == -1) || wrap) ? true : (current <= last);
112: }
113:
114: public Object next() {
115: return new Long(getNext());
116: }
117:
118: public void remove() {
119: // Do nothing
120: }
121: }
|