首页

分享webwork源码包基于java.util.Iterator实现的计算器类Counter源码示例

标签:webwork源码,java.util.Iterator,计算器类,Counter     发布时间:2018-10-17   

一、前言

关于webwork源码包(2.2.5)中的com.opensymphony.webwork.util.Counter计数器类,基于迭代遍历java.util.Iterator接口源码示例。

二、代码示例

package com.opensymphony.webwork.util;@b@@b@import java.io.Serializable;@b@@b@@b@public class Counter implements java.util.Iterator, Serializable {@b@@b@    boolean wrap = false;@b@@b@    // Attributes ----------------------------------------------------@b@    long first = 1;@b@    long current = first;@b@    long interval = 1;@b@    long last = -1;@b@@b@@b@    public void setAdd(long addition) {@b@        current += addition;@b@    }@b@@b@    public void setCurrent(long current) {@b@        this.current = current;@b@    }@b@@b@    public long getCurrent() {@b@        return current;@b@    }@b@@b@    public void setFirst(long first) {@b@        this.first = first;@b@        current = first;@b@    }@b@@b@    public long getFirst() {@b@        return first;@b@    }@b@@b@    public void setInterval(long interval) {@b@        this.interval = interval;@b@    }@b@@b@    public long getInterval() {@b@        return interval;@b@    }@b@@b@    public void setLast(long last) {@b@        this.last = last;@b@    }@b@@b@    public long getLast() {@b@        return last;@b@    }@b@@b@    // Public --------------------------------------------------------@b@    public long getNext() {@b@        long next = current;@b@        current += interval;@b@@b@        if (wrap && (current > last)) {@b@            current -= ((1 + last) - first);@b@        }@b@@b@        return next;@b@    }@b@@b@    public long getPrevious() {@b@        current -= interval;@b@@b@        if (wrap && (current < first)) {@b@            current += (last - first + 1);@b@        }@b@@b@        return current;@b@    }@b@@b@    public void setWrap(boolean wrap) {@b@        this.wrap = wrap;@b@    }@b@@b@    public boolean isWrap() {@b@        return wrap;@b@    }@b@@b@    public boolean hasNext() {@b@        return ((last == -1) || wrap) ? true : (current <= last);@b@    }@b@@b@    public Object next() {@b@        return new Long(getNext());@b@    }@b@@b@    public void remove() {@b@        // Do nothing@b@    }@b@}