[ 
https://issues.apache.org/jira/browse/GROOVY-7877?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15375128#comment-15375128
 ] 

ASF GitHub Bot commented on GROOVY-7877:
----------------------------------------

Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/366#discussion_r70638231
  
    --- Diff: src/main/groovy/lang/NumberRange.java ---
    @@ -0,0 +1,536 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + *
    + */
    +package groovy.lang;
    +
    +import org.codehaus.groovy.runtime.InvokerHelper;
    +import org.codehaus.groovy.runtime.IteratorClosureAdapter;
    +
    +import java.math.BigDecimal;
    +import java.math.BigInteger;
    +import java.util.AbstractList;
    +import java.util.Iterator;
    +import java.util.List;
    +
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareEqual;
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareGreaterThan;
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareGreaterThanEqual;
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareLessThan;
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareLessThanEqual;
    +import static 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareNotEqual;
    +import static org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareTo;
    +import static org.codehaus.groovy.runtime.dgmimpl.NumberNumberMinus.minus;
    +import static org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus.plus;
    +import static 
org.codehaus.groovy.runtime.dgmimpl.NumberNumberMultiply.multiply;
    +
    +/**
    + * Represents an inclusive list of Numbers from a value to a value with a 
particular step size.
    + */
    +public class NumberRange extends AbstractList<Comparable> implements 
Range<Comparable> {
    +
    +    /**
    +     * The first value in the range.
    +     */
    +    private final Comparable from;
    +
    +    /**
    +     * The last value in the range.
    +     */
    +    private final Comparable to;
    +
    +    /**
    +     * The step size in the range.
    +     */
    +    private final Number stepSize;
    +
    +    /**
    +     * The cached size, or -1 if not yet computed
    +     */
    +    private int size = -1;
    +
    +    /**
    +     * <code>true</code> if the range counts backwards from 
<code>to</code> to <code>from</code>.
    +     */
    +    private final boolean reverse;
    +
    +    /**
    +     * <code>true</code> if the range includes the upper bound.
    +     */
    +    private final boolean inclusive;
    +
    +    /**
    +     * Creates an inclusive {@link NumberRange} with step size 1.
    +     * Creates a reversed range if <code>from</code> &lt; <code>to</code>.
    +     *
    +     * @param from the first value in the range
    +     * @param to   the last value in the range
    +     */
    +    public <T extends Number & Comparable, U extends Number & Comparable>
    +    NumberRange(T from, U to) {
    +        this(from, to, null, true);
    +    }
    +
    +    /**
    +     * Creates a new {@link NumberRange} with step size 1.
    +     * Creates a reversed range if <code>from</code> &lt; <code>to</code>.
    +     *
    +     * @param from start of the range
    +     * @param to   end of the range
    +     * @param inclusive whether the range is inclusive
    +     */
    +    public <T extends Number & Comparable, U extends Number & Comparable>
    +    NumberRange(T from, U to, boolean inclusive) {
    +        this(from, to, null, inclusive);
    +    }
    +
    +    /**
    +     * Creates an inclusive {@link NumberRange}.
    +     * Creates a reversed range if <code>from</code> &lt; <code>to</code>.
    +     *
    +     * @param from start of the range
    +     * @param to   end of the range
    +     * @param stepSize the gap between discrete elements in the range
    +     */
    +    public <T extends Number & Comparable, U extends Number & Comparable, 
V extends
    +            Number & Comparable<? super Number>>
    +    NumberRange(T from, U to, V stepSize) {
    +        this(from, to, stepSize, true);
    +    }
    +
    +    /**
    +     * Creates a {@link NumberRange}.
    +     * Creates a reversed range if <code>from</code> &lt; <code>to</code>.
    +     *
    +     * @param from start of the range
    +     * @param to   end of the range
    +     * @param stepSize the gap between discrete elements in the range
    +     * @param inclusive whether the range is inclusive
    +     */
    +    public <T extends Number & Comparable, U extends Number & Comparable, 
V extends
    +            Number & Comparable>
    +    NumberRange(T from, U to, V stepSize, boolean inclusive) {
    +        if (from == null) {
    +            throw new IllegalArgumentException("Must specify a non-null 
value for the 'from' index in a Range");
    +        }
    +        if (to == null) {
    +            throw new IllegalArgumentException("Must specify a non-null 
value for the 'to' index in a Range");
    +        }
    +        reverse = areReversed(from, to);
    +        Number tempFrom;
    +        Number tempTo;
    +        if (reverse) {
    +            tempFrom = to;
    +            tempTo = from;
    +        } else {
    +            tempFrom = from;
    +            tempTo = to;
    +        }
    +        if (tempFrom instanceof Short) {
    +            tempFrom = tempFrom.intValue();
    +        } else if (tempFrom instanceof Float) {
    +            tempFrom = tempFrom.doubleValue();
    +        }
    +        if (tempTo instanceof Short) {
    +            tempTo = tempTo.intValue();
    +        } else if (tempTo instanceof Float) {
    +            tempTo = tempTo.doubleValue();
    +        }
    +
    +        if (tempFrom instanceof Integer && tempTo instanceof Long) {
    +            tempFrom = tempFrom.longValue();
    +        } else if (tempTo instanceof Integer && tempFrom instanceof Long) {
    +            tempTo = tempTo.longValue();
    +        }
    +
    +        this.from = (Comparable) tempFrom;
    +        this.to = (Comparable) tempTo;
    +        this.stepSize = stepSize == null ? 1 : stepSize;
    +        this.inclusive = inclusive;
    +    }
    +
    +    /**
    +     * For a NumberRange with step size 1, creates a new NumberRange with 
the same
    +     * <code>from</code> and <code>to</code> as this NumberRange
    +     * but with a step size of <code>stepSize</code>.
    +     *
    +     * @param stepSize the desired step size
    +     * @return a new NumberRange
    +     */
    +    public <T extends Number & Comparable> NumberRange by(T stepSize) {
    +        if (!Integer.valueOf(1).equals(this.stepSize)) {
    +            throw new IllegalStateException("by only allowed on ranges 
with original stepSize = 1 but found " + this.stepSize);
    +        }
    +        return new NumberRange(comparableNumber(from), 
comparableNumber(to), stepSize, inclusive);
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    /* package private */ static <T extends Number & Comparable> T 
comparableNumber(Comparable c) {
    +        return (T) c;
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    /* package private */ static <T extends Number & Comparable> T 
comparableNumber(Number n) {
    +        return (T) n;
    +    }
    +
    +    private static boolean areReversed(Number from, Number to) {
    +        try {
    +            return compareGreaterThan(from, to);
    +        } catch (ClassCastException cce) {
    +            throw new IllegalArgumentException("Unable to create range due 
to incompatible types: " + from.getClass().getSimpleName() + ".." + 
to.getClass().getSimpleName() + " (possible missing brackets around range?)", 
cce);
    +        }
    +    }
    +
    +    public boolean equals(Object that) {
    +        return (that instanceof NumberRange) ? equals((NumberRange) that) 
: super.equals(that);
    +    }
    +
    +    /**
    +     * Compares an {@link NumberRange} to another {@link NumberRange}.
    +     *
    +     * @param that the object to check equality with
    +     * @return <code>true</code> if the ranges are equal
    +     */
    +    public boolean equals(NumberRange that) {
    +        return that != null
    +                && reverse == that.reverse
    +                && compareEqual(from, that.from)
    +                && compareEqual(to, that.to)
    +                && compareEqual(stepSize, that.stepSize);
    +    }
    +
    +    @Override
    +    public Comparable getFrom() {
    +        return from;
    +    }
    +
    +    @Override
    +    public Comparable getTo() {
    +        return to;
    +    }
    +
    +    public Comparable getStepSize() {
    +        return (Comparable) stepSize;
    +    }
    +
    +    @Override
    +    public boolean isReverse() {
    +        return reverse;
    +    }
    +
    +    @Override
    +    public Comparable get(int index) {
    +        if (index < 0) {
    +            throw new IndexOutOfBoundsException("Index: " + index + " 
should not be negative");
    +        }
    +        final Iterator<Comparable> iter = new StepIterator(this, stepSize);
    +
    +        Comparable value = iter.next();
    +        for (int i = 0; i < index; i++) {
    +            if (!iter.hasNext()) {
    +                throw new IndexOutOfBoundsException("Index: " + index + " 
is too big for range: " + this);
    +            }
    +            value = iter.next();
    +        }
    +        return value;
    +    }
    +
    +    /**
    +     * Checks whether a value is between the from and to values of a Range
    +     *
    +     * @param value the value of interest
    +     * @return true if the value is within the bounds
    +     */
    +    @Override
    +    public boolean containsWithinBounds(Object value) {
    +        final int result = compareTo(from, value);
    +        return result == 0 || result < 0 && compareTo(to, value) >= 0;
    +    }
    +
    +    /**
    +     * protection against calls from Groovy
    +     */
    +    @SuppressWarnings("unused")
    +    private void setSize(int size) {
    +        throw new UnsupportedOperationException("size must not be 
changed");
    +    }
    +
    +    @Override
    +    public int size() {
    +        if (size == -1) {
    +            calcSize(from, to, stepSize);
    +        }
    +        return size;
    +    }
    +
    +    void calcSize(Comparable from, Comparable to, Number stepSize) {
    +        int tempsize = 0;
    +        boolean shortcut = false;
    +        if (isIntegral(stepSize)) {
    +            if ((from instanceof Integer || from instanceof Long)
    +                    && (to instanceof Integer || to instanceof Long)) {
    +                // let's fast calculate the size
    +                final BigInteger fromNum = new BigInteger(from.toString());
    +                final BigInteger toTemp = new BigInteger(to.toString());
    +                final BigInteger toNum = inclusive ? toTemp : 
toTemp.subtract(BigInteger.ONE);
    +                final BigInteger sizeNum = new 
BigDecimal(toNum.subtract(fromNum)).divide(new 
BigDecimal(stepSize.longValue()), 
BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
    +                tempsize = 
sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? 
sizeNum.intValue() : Integer.MAX_VALUE;
    +                shortcut = true;
    +            } else if (((from instanceof BigDecimal || from instanceof 
BigInteger) && to instanceof Number) ||
    +                    ((to instanceof BigDecimal || to instanceof 
BigInteger) && from instanceof Number)) {
    +                // let's fast calculate the size
    +                final BigDecimal fromNum = new BigDecimal(from.toString());
    +                final BigDecimal toTemp = new BigDecimal(to.toString());
    +                final BigDecimal toNum = inclusive ? toTemp : 
toTemp.subtract(new BigDecimal("1.0"));
    +                final BigInteger sizeNum = 
toNum.subtract(fromNum).divide(new BigDecimal(stepSize.longValue()), 
BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
    +                tempsize = 
sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? 
sizeNum.intValue() : Integer.MAX_VALUE;
    +                shortcut = true;
    +            }
    +        }
    +        if (!shortcut) {
    +            // let's brute-force calculate the size by iterating start to 
end
    +            final Iterator iter = new StepIterator(this, stepSize);
    +            while (iter.hasNext()) {
    +                tempsize++;
    +                // integer overflow
    +                if (tempsize < 0) {
    +                    break;
    +                }
    +                iter.next();
    +            }
    +            // integer overflow
    +            if (tempsize < 0) {
    +                tempsize = Integer.MAX_VALUE;
    +            }
    +        }
    +        size = tempsize;
    +    }
    +
    +    private boolean isIntegral(Number stepSize) {
    +        BigDecimal tempStepSize = new BigDecimal(stepSize.toString());
    +        return tempStepSize.equals(new 
BigDecimal(tempStepSize.toBigInteger()));
    +    }
    +
    +    @Override
    +    public List<Comparable> subList(int fromIndex, int toIndex) {
    +        if (fromIndex < 0) {
    +            throw new IndexOutOfBoundsException("fromIndex = " + 
fromIndex);
    +        }
    +        if (fromIndex > toIndex) {
    +            throw new IllegalArgumentException("fromIndex(" + fromIndex + 
") > toIndex(" + toIndex + ")");
    +        }
    +        if (fromIndex == toIndex) {
    +            return new EmptyRange<Comparable>(from);
    +        }
    +
    +        // Performance detail:
    +        // not using get(fromIndex), get(toIndex) in the following to 
avoid stepping over elements twice
    +        final Iterator<Comparable> iter = new StepIterator(this, stepSize);
    +
    +        Comparable value = iter.next();
    +        int i = 0;
    +        for (; i < fromIndex; i++) {
    +            if (!iter.hasNext()) {
    +                throw new IndexOutOfBoundsException("Index: " + i + " is 
too big for range: " + this);
    +            }
    +            value = iter.next();
    +        }
    +        final Comparable fromValue = value;
    +        for (; i < toIndex - 1; i++) {
    +            if (!iter.hasNext()) {
    +                throw new IndexOutOfBoundsException("Index: " + i + " is 
too big for range: " + this);
    +            }
    +            value = iter.next();
    +        }
    +        final Comparable toValue = value;
    +
    +        return new NumberRange(comparableNumber(fromValue), 
comparableNumber(toValue), comparableNumber(stepSize), true);
    +    }
    +
    +    public String toString() {
    --- End diff --
    
    @Override


> The Range abstraction could support numeric ranges where the items in the 
> range differ by some step size different to 1
> -----------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-7877
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7877
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Paul King
>            Assignee: Paul King
>




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to