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