001/*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package jet;
018
019import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
020
021@AssertInvisibleInResolver
022public final class IntRange implements Range<Integer>, Progression<Integer> {
023    public static final IntRange EMPTY = new IntRange(1, 0);
024
025    private final int start;
026    private final int end;
027
028    public IntRange(int start, int end) {
029        this.start = start;
030        this.end = end;
031    }
032
033    @Override
034    public boolean contains(Integer item) {
035        return start <= item && item <= end;
036    }
037
038    public boolean contains(int item) {
039        return start <= item && item <= end;
040    }
041
042    @Override
043    public Integer getStart() {
044        return start;
045    }
046
047    @Override
048    public Integer getEnd() {
049        return end;
050    }
051
052    @Override
053    public Integer getIncrement() {
054        return 1;
055    }
056
057    @Override
058    public IntIterator iterator() {
059        return new IntProgressionIterator(start, end, 1);
060    }
061
062    @Override
063    public String toString() {
064        return start + ".." + end;
065    }
066
067    @Override
068    public boolean equals(Object o) {
069        if (this == o) {
070            return true;
071        }
072        if (o == null || getClass() != o.getClass()) {
073            return false;
074        }
075
076        IntRange range = (IntRange) o;
077        return end == range.end && start == range.start;
078    }
079
080    @Override
081    public int hashCode() {
082        int result = start;
083        result = 31 * result + end;
084        return result;
085    }
086}