001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.util;
016:
017: import java.util.Iterator;
018:
019: /**
020: * Represents a sequence of integer values, either ascending or descending. The sequence is always
021: * inclusive (of the finish value).
022: */
023: public final class IntegerRange implements Iterable<Integer> {
024: private final int _start;
025:
026: private final int _finish;
027:
028: private class RangeIterator implements Iterator<Integer> {
029: private final int _increment;
030:
031: private int _value = _start;
032:
033: private boolean _hasNext = true;
034:
035: RangeIterator() {
036: _increment = _start < _finish ? +1 : -1;
037: }
038:
039: public boolean hasNext() {
040: return _hasNext;
041: }
042:
043: public Integer next() {
044: if (!_hasNext)
045: throw new IllegalStateException();
046:
047: int result = _value;
048:
049: _hasNext = _value != _finish;
050:
051: _value += _increment;
052:
053: return result;
054: }
055:
056: public void remove() {
057: throw new UnsupportedOperationException();
058: }
059:
060: }
061:
062: public IntegerRange(final int start, final int finish) {
063: _start = start;
064: _finish = finish;
065: }
066:
067: public int getFinish() {
068: return _finish;
069: }
070:
071: public int getStart() {
072: return _start;
073: }
074:
075: @Override
076: public String toString() {
077: return String.format("%d..%d", _start, _finish);
078: }
079:
080: /**
081: * The main puprose of a range object is to produce an Iterator. Since IntegerRange is iterable,
082: * it is useful with the Tapestry Loop component, but also with the Java for loop!
083: */
084: public Iterator<Integer> iterator() {
085: return new RangeIterator();
086: }
087:
088: @Override
089: public int hashCode() {
090: final int PRIME = 31;
091:
092: int result = PRIME + _finish;
093:
094: result = PRIME * result + _start;
095:
096: return result;
097: }
098:
099: /** Returns true if the other object is an IntegerRange with the same start and finish values. */
100: @Override
101: public boolean equals(Object obj) {
102: if (this == obj)
103: return true;
104: if (obj == null)
105: return false;
106: if (getClass() != obj.getClass())
107: return false;
108: final IntegerRange other = (IntegerRange) obj;
109: if (_finish != other._finish)
110: return false;
111: if (_start != other._start)
112: return false;
113: return true;
114: }
115:
116: }
|