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    
017    package jet;
018    
019    import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
020    
021    @AssertInvisibleInResolver
022    public class ShortProgression implements Progression<Short> {
023        private final short start;
024        private final short end;
025        private final int increment;
026    
027        public ShortProgression(short start, short end, int 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 Short getStart() {
038            return start;
039        }
040    
041        @Override
042        public Short getEnd() {
043            return end;
044        }
045    
046        @Override
047        public Integer getIncrement() {
048            return increment;
049        }
050    
051        @Override
052        public ShortIterator iterator() {
053            return new ShortProgressionIterator(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            ShortProgression shorts = (ShortProgression) o;
072    
073            if (end != shorts.end) return false;
074            if (increment != shorts.increment) return false;
075            if (start != shorts.start) return false;
076    
077            return true;
078        }
079    
080        @Override
081        public int hashCode() {
082            int result = (int) start;
083            result = 31 * result + (int) end;
084            result = 31 * result + increment;
085            return result;
086        }
087    }