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 class LongProgression implements Progression<Long> {
023    private final long start;
024    private final long end;
025    private final long increment;
026
027    public LongProgression(long start, long end, long increment) {
028        if (increment == 0) {
029            throw new IllegalArgumentException("Increment must be non-zero: " + increment);
030        }
031        this.start = start;
032        this.end = end;
033        this.increment = increment;
034    }
035
036    @Override
037    public Long getStart() {
038        return start;
039    }
040
041    @Override
042    public Long getEnd() {
043        return end;
044    }
045
046    @Override
047    public Long getIncrement() {
048        return increment;
049    }
050
051    @Override
052    public LongIterator iterator() {
053        return new LongProgressionIterator(start, end, increment);
054    }
055
056    @Override
057    public String toString() {
058        if (increment > 0) {
059            return start + ".." + end + " step " + increment;
060        }
061        else {
062            return start + " downTo " + end + " step " + -increment;
063        }
064    }
065
066    @Override
067    public boolean equals(Object o) {
068        if (this == o) return true;
069        if (o == null || getClass() != o.getClass()) return false;
070
071        LongProgression longs = (LongProgression) o;
072
073        if (end != longs.end) return false;
074        if (increment != longs.increment) return false;
075        if (start != longs.start) return false;
076
077        return true;
078    }
079
080    @Override
081    public int hashCode() {
082        int result = (int) (start ^ (start >>> 32));
083        result = 31 * result + (int) (end ^ (end >>> 32));
084        result = 31 * result + (int) (increment ^ (increment >>> 32));
085        return result;
086    }
087}