001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import java.io.Serializable;
008:
009: /**
010: * A bean that can be used to keep track of a counter.
011: * <p/>
012: * Since it is an Iterator it can be used by the iterator tag
013: *
014: * @author Rickard Öberg (rickard@middleware-company.com)
015: * @version $Revision: 1282 $
016: * @see <related>
017: */
018: public class Counter implements java.util.Iterator, Serializable {
019:
020: boolean wrap = false;
021:
022: // Attributes ----------------------------------------------------
023: long first = 1;
024: long current = first;
025: long interval = 1;
026: long last = -1;
027:
028: public void setAdd(long addition) {
029: current += addition;
030: }
031:
032: public void setCurrent(long current) {
033: this .current = current;
034: }
035:
036: public long getCurrent() {
037: return current;
038: }
039:
040: public void setFirst(long first) {
041: this .first = first;
042: current = first;
043: }
044:
045: public long getFirst() {
046: return first;
047: }
048:
049: public void setInterval(long interval) {
050: this .interval = interval;
051: }
052:
053: public long getInterval() {
054: return interval;
055: }
056:
057: public void setLast(long last) {
058: this .last = last;
059: }
060:
061: public long getLast() {
062: return last;
063: }
064:
065: // Public --------------------------------------------------------
066: public long getNext() {
067: long next = current;
068: current += interval;
069:
070: if (wrap && (current > last)) {
071: current -= ((1 + last) - first);
072: }
073:
074: return next;
075: }
076:
077: public long getPrevious() {
078: current -= interval;
079:
080: if (wrap && (current < first)) {
081: current += (last - first + 1);
082: }
083:
084: return current;
085: }
086:
087: public void setWrap(boolean wrap) {
088: this .wrap = wrap;
089: }
090:
091: public boolean isWrap() {
092: return wrap;
093: }
094:
095: public boolean hasNext() {
096: return ((last == -1) || wrap) ? true : (current <= last);
097: }
098:
099: public Object next() {
100: return new Long(getNext());
101: }
102:
103: public void remove() {
104: // Do nothing
105: }
106: }
|