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 FloatProgression implements Progression<Float> {
023    private final float start;
024    private final float end;
025    private final float increment;
026
027    public FloatProgression(float start, float end, float increment) {
028        if (Float.isNaN(increment)) {
029            throw new IllegalArgumentException("Increment must be not NaN");
030        }
031        if (increment == 0.0f || increment == -0.0f) {
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 Float getStart() {
041        return start;
042    }
043
044    @Override
045    public Float getEnd() {
046        return end;
047    }
048
049    @Override
050    public Float getIncrement() {
051        return increment;
052    }
053
054    @Override
055    public FloatIterator iterator() {
056        return new FloatProgressionIterator(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        FloatProgression floats = (FloatProgression) o;
075
076        if (Float.compare(floats.end, end) != 0) return false;
077        if (Float.compare(floats.increment, increment) != 0) return false;
078        if (Float.compare(floats.start, start) != 0) return false;
079
080        return true;
081    }
082
083    @Override
084    public int hashCode() {
085        int result = (start != +0.0f ? Float.floatToIntBits(start) : 0);
086        result = 31 * result + (end != +0.0f ? Float.floatToIntBits(end) : 0);
087        result = 31 * result + (increment != +0.0f ? Float.floatToIntBits(increment) : 0);
088        return result;
089    }
090}