You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/08/22 02:45:09 UTC

[commons-lang] branch master updated: Sort members.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new f67852e  Sort members.
f67852e is described below

commit f67852e811e2f1659448ea164427c557e82317f6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 21 22:45:05 2019 -0400

    Sort members.
---
 src/main/java/org/apache/commons/lang3/Range.java | 440 +++++++++++-----------
 1 file changed, 220 insertions(+), 220 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/Range.java b/src/main/java/org/apache/commons/lang3/Range.java
index cbf521d..4f3da66 100644
--- a/src/main/java/org/apache/commons/lang3/Range.java
+++ b/src/main/java/org/apache/commons/lang3/Range.java
@@ -32,33 +32,67 @@ import java.util.Comparator;
  */
 public final class Range<T> implements Serializable {
 
+    //-----------------------------------------------------------------------
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private enum ComparableComparator implements Comparator {
+        INSTANCE;
+        /**
+         * Comparable based compare implementation.
+         *
+         * @param obj1 left hand side of comparison
+         * @param obj2 right hand side of comparison
+         * @return negative, 0, positive comparison value
+         */
+        @Override
+        public int compare(final Object obj1, final Object obj2) {
+            return ((Comparable) obj1).compareTo(obj2);
+        }
+    }
+
     /**
      * Serialization version.
      * @see java.io.Serializable
      */
     private static final long serialVersionUID = 1L;
-
-    /**
-     * The ordering scheme used in this range.
-     */
-    private final Comparator<T> comparator;
     /**
-     * The minimum value in this range (inclusive).
-     */
-    private final T minimum;
-    /**
-     * The maximum value in this range (inclusive).
-     */
-    private final T maximum;
-    /**
-     * Cached output hashCode (class is immutable).
+     * <p>Obtains a range with the specified minimum and maximum values (both inclusive).</p>
+     *
+     * <p>The range uses the natural ordering of the elements to determine where
+     * values lie in the range.</p>
+     *
+     * <p>The arguments may be passed in the order (min,max) or (max,min).
+     * The getMinimum and getMaximum methods will return the correct values.</p>
+     *
+     * @param <T> the type of the elements in this range
+     * @param fromInclusive  the first value that defines the edge of the range, inclusive
+     * @param toInclusive  the second value that defines the edge of the range, inclusive
+     * @return the range object, not null
+     * @throws IllegalArgumentException if either element is null
+     * @throws ClassCastException if the elements are not {@code Comparable}
      */
-    private transient int hashCode;
+    public static <T extends Comparable<T>> Range<T> between(final T fromInclusive, final T toInclusive) {
+        return between(fromInclusive, toInclusive, null);
+    }
     /**
-     * Cached output toString (class is immutable).
+     * <p>Obtains a range with the specified minimum and maximum values (both inclusive).</p>
+     *
+     * <p>The range uses the specified {@code Comparator} to determine where
+     * values lie in the range.</p>
+     *
+     * <p>The arguments may be passed in the order (min,max) or (max,min).
+     * The getMinimum and getMaximum methods will return the correct values.</p>
+     *
+     * @param <T> the type of the elements in this range
+     * @param fromInclusive  the first value that defines the edge of the range, inclusive
+     * @param toInclusive  the second value that defines the edge of the range, inclusive
+     * @param comparator  the comparator to be used, null for natural ordering
+     * @return the range object, not null
+     * @throws IllegalArgumentException if either element is null
+     * @throws ClassCastException if using natural ordering and the elements are not {@code Comparable}
      */
-    private transient String toString;
-
+    public static <T> Range<T> between(final T fromInclusive, final T toInclusive, final Comparator<T> comparator) {
+        return new Range<>(fromInclusive, toInclusive, comparator);
+    }
     /**
      * <p>Obtains a range using the specified element as both the minimum
      * and maximum in this range.</p>
@@ -75,7 +109,6 @@ public final class Range<T> implements Serializable {
     public static <T extends Comparable<T>> Range<T> is(final T element) {
         return between(element, element, null);
     }
-
     /**
      * <p>Obtains a range using the specified element as both the minimum
      * and maximum in this range.</p>
@@ -95,45 +128,32 @@ public final class Range<T> implements Serializable {
     }
 
     /**
-     * <p>Obtains a range with the specified minimum and maximum values (both inclusive).</p>
-     *
-     * <p>The range uses the natural ordering of the elements to determine where
-     * values lie in the range.</p>
-     *
-     * <p>The arguments may be passed in the order (min,max) or (max,min).
-     * The getMinimum and getMaximum methods will return the correct values.</p>
-     *
-     * @param <T> the type of the elements in this range
-     * @param fromInclusive  the first value that defines the edge of the range, inclusive
-     * @param toInclusive  the second value that defines the edge of the range, inclusive
-     * @return the range object, not null
-     * @throws IllegalArgumentException if either element is null
-     * @throws ClassCastException if the elements are not {@code Comparable}
+     * The ordering scheme used in this range.
      */
-    public static <T extends Comparable<T>> Range<T> between(final T fromInclusive, final T toInclusive) {
-        return between(fromInclusive, toInclusive, null);
-    }
+    private final Comparator<T> comparator;
 
     /**
-     * <p>Obtains a range with the specified minimum and maximum values (both inclusive).</p>
-     *
-     * <p>The range uses the specified {@code Comparator} to determine where
-     * values lie in the range.</p>
-     *
-     * <p>The arguments may be passed in the order (min,max) or (max,min).
-     * The getMinimum and getMaximum methods will return the correct values.</p>
-     *
-     * @param <T> the type of the elements in this range
-     * @param fromInclusive  the first value that defines the edge of the range, inclusive
-     * @param toInclusive  the second value that defines the edge of the range, inclusive
-     * @param comparator  the comparator to be used, null for natural ordering
-     * @return the range object, not null
-     * @throws IllegalArgumentException if either element is null
-     * @throws ClassCastException if using natural ordering and the elements are not {@code Comparable}
+     * Cached output hashCode (class is immutable).
      */
-    public static <T> Range<T> between(final T fromInclusive, final T toInclusive, final Comparator<T> comparator) {
-        return new Range<>(fromInclusive, toInclusive, comparator);
-    }
+    private transient int hashCode;
+
+    /**
+     * The maximum value in this range (inclusive).
+     */
+    private final T maximum;
+
+    /**
+     * The minimum value in this range (inclusive).
+     */
+    private final T minimum;
+
+    /**
+     * Cached output toString (class is immutable).
+     */
+    private transient String toString;
+
+    // Accessors
+    //--------------------------------------------------------------------
 
     /**
      * Creates an instance.
@@ -162,159 +182,167 @@ public final class Range<T> implements Serializable {
         }
     }
 
-    // Accessors
-    //--------------------------------------------------------------------
-
     /**
-     * <p>Gets the minimum value in this range.</p>
-     *
-     * @return the minimum value in this range, not null
-     */
-    public T getMinimum() {
-        return minimum;
-    }
-
-    /**
-     * <p>Gets the maximum value in this range.</p>
+     * <p>Checks whether the specified element occurs within this range.</p>
      *
-     * @return the maximum value in this range, not null
+     * @param element  the element to check for, null returns false
+     * @return true if the specified element occurs within this range
      */
-    public T getMaximum() {
-        return maximum;
+    public boolean contains(final T element) {
+        if (element == null) {
+            return false;
+        }
+        return comparator.compare(element, minimum) > -1 && comparator.compare(element, maximum) < 1;
     }
 
     /**
-     * <p>Gets the comparator being used to determine if objects are within the range.</p>
+     * <p>Checks whether this range contains all the elements of the specified range.</p>
      *
-     * <p>Natural ordering uses an internal comparator implementation, thus this
-     * method never returns null. See {@link #isNaturalOrdering()}.</p>
+     * <p>This method may fail if the ranges have two different comparators or element types.</p>
      *
-     * @return the comparator being used, not null
+     * @param otherRange  the range to check, null returns false
+     * @return true if this range contains the specified range
+     * @throws RuntimeException if ranges cannot be compared
      */
-    public Comparator<T> getComparator() {
-        return comparator;
+    public boolean containsRange(final Range<T> otherRange) {
+        if (otherRange == null) {
+            return false;
+        }
+        return contains(otherRange.minimum)
+            && contains(otherRange.maximum);
     }
 
     /**
-     * <p>Whether or not the Range is using the natural ordering of the elements.</p>
+     * <p>Checks where the specified element occurs relative to this range.</p>
      *
-     * <p>Natural ordering uses an internal comparator implementation, thus this
-     * method is the only way to check if a null comparator was specified.</p>
+     * <p>The API is reminiscent of the Comparable interface returning {@code -1} if
+     * the element is before the range, {@code 0} if contained within the range and
+     * {@code 1} if the element is after the range. </p>
      *
-     * @return true if using natural ordering
+     * @param element  the element to check for, not null
+     * @return -1, 0 or +1 depending on the element's location relative to the range
      */
-    public boolean isNaturalOrdering() {
-        return comparator == ComparableComparator.INSTANCE;
+    public int elementCompareTo(final T element) {
+        // Comparable API says throw NPE on null
+        Validate.notNull(element, "Element is null");
+        if (isAfter(element)) {
+            return -1;
+        } else if (isBefore(element)) {
+            return 1;
+        } else {
+            return 0;
+        }
     }
 
     // Element tests
     //--------------------------------------------------------------------
 
     /**
-     * <p>Checks whether the specified element occurs within this range.</p>
+     * <p>Compares this range to another object to test if they are equal.</p>.
      *
-     * @param element  the element to check for, null returns false
-     * @return true if the specified element occurs within this range
+     * <p>To be equal, the minimum and maximum values must be equal, which
+     * ignores any differences in the comparator.</p>
+     *
+     * @param obj the reference object with which to compare
+     * @return true if this object is equal
      */
-    public boolean contains(final T element) {
-        if (element == null) {
+    @Override
+    public boolean equals(final Object obj) {
+        if (obj == this) {
+            return true;
+        } else if (obj == null || obj.getClass() != getClass()) {
             return false;
+        } else {
+            @SuppressWarnings("unchecked") // OK because we checked the class above
+            final
+            Range<T> range = (Range<T>) obj;
+            return minimum.equals(range.minimum) &&
+                   maximum.equals(range.maximum);
         }
-        return comparator.compare(element, minimum) > -1 && comparator.compare(element, maximum) < 1;
     }
 
     /**
-     * <p>Checks whether this range is after the specified element.</p>
+     * <p>Gets the comparator being used to determine if objects are within the range.</p>
      *
-     * @param element  the element to check for, null returns false
-     * @return true if this range is entirely after the specified element
+     * <p>Natural ordering uses an internal comparator implementation, thus this
+     * method never returns null. See {@link #isNaturalOrdering()}.</p>
+     *
+     * @return the comparator being used, not null
      */
-    public boolean isAfter(final T element) {
-        if (element == null) {
-            return false;
-        }
-        return comparator.compare(element, minimum) < 0;
+    public Comparator<T> getComparator() {
+        return comparator;
     }
 
     /**
-     * <p>Checks whether this range starts with the specified element.</p>
+     * <p>Gets the maximum value in this range.</p>
      *
-     * @param element  the element to check for, null returns false
-     * @return true if the specified element occurs within this range
+     * @return the maximum value in this range, not null
      */
-    public boolean isStartedBy(final T element) {
-        if (element == null) {
-            return false;
-        }
-        return comparator.compare(element, minimum) == 0;
+    public T getMaximum() {
+        return maximum;
     }
 
     /**
-     * <p>Checks whether this range ends with the specified element.</p>
+     * <p>Gets the minimum value in this range.</p>
      *
-     * @param element  the element to check for, null returns false
-     * @return true if the specified element occurs within this range
+     * @return the minimum value in this range, not null
      */
-    public boolean isEndedBy(final T element) {
-        if (element == null) {
-            return false;
-        }
-        return comparator.compare(element, maximum) == 0;
+    public T getMinimum() {
+        return minimum;
     }
 
     /**
-     * <p>Checks whether this range is before the specified element.</p>
+     * <p>Gets a suitable hash code for the range.</p>
      *
-     * @param element  the element to check for, null returns false
-     * @return true if this range is entirely before the specified element
+     * @return a hash code value for this object
      */
-    public boolean isBefore(final T element) {
-        if (element == null) {
-            return false;
+    @Override
+    public int hashCode() {
+        int result = hashCode;
+        if (hashCode == 0) {
+            result = 17;
+            result = 37 * result + getClass().hashCode();
+            result = 37 * result + minimum.hashCode();
+            result = 37 * result + maximum.hashCode();
+            hashCode = result;
         }
-        return comparator.compare(element, maximum) > 0;
+        return result;
     }
 
     /**
-     * <p>Checks where the specified element occurs relative to this range.</p>
-     *
-     * <p>The API is reminiscent of the Comparable interface returning {@code -1} if
-     * the element is before the range, {@code 0} if contained within the range and
-     * {@code 1} if the element is after the range. </p>
-     *
-     * @param element  the element to check for, not null
-     * @return -1, 0 or +1 depending on the element's location relative to the range
+     * Calculate the intersection of {@code this} and an overlapping Range.
+     * @param other overlapping Range
+     * @return range representing the intersection of {@code this} and {@code other} ({@code this} if equal)
+     * @throws IllegalArgumentException if {@code other} does not overlap {@code this}
+     * @since 3.0.1
      */
-    public int elementCompareTo(final T element) {
-        // Comparable API says throw NPE on null
-        Validate.notNull(element, "Element is null");
-        if (isAfter(element)) {
-            return -1;
-        } else if (isBefore(element)) {
-            return 1;
-        } else {
-            return 0;
+    public Range<T> intersectionWith(final Range<T> other) {
+        if (!this.isOverlappedBy(other)) {
+            throw new IllegalArgumentException(String.format(
+                "Cannot calculate intersection with non-overlapping range %s", other));
         }
+        if (this.equals(other)) {
+            return this;
+        }
+        final T min = getComparator().compare(minimum, other.minimum) < 0 ? other.minimum : minimum;
+        final T max = getComparator().compare(maximum, other.maximum) < 0 ? maximum : other.maximum;
+        return between(min, max, getComparator());
     }
 
     // Range tests
     //--------------------------------------------------------------------
 
     /**
-     * <p>Checks whether this range contains all the elements of the specified range.</p>
-     *
-     * <p>This method may fail if the ranges have two different comparators or element types.</p>
+     * <p>Checks whether this range is after the specified element.</p>
      *
-     * @param otherRange  the range to check, null returns false
-     * @return true if this range contains the specified range
-     * @throws RuntimeException if ranges cannot be compared
+     * @param element  the element to check for, null returns false
+     * @return true if this range is entirely after the specified element
      */
-    public boolean containsRange(final Range<T> otherRange) {
-        if (otherRange == null) {
+    public boolean isAfter(final T element) {
+        if (element == null) {
             return false;
         }
-        return contains(otherRange.minimum)
-            && contains(otherRange.maximum);
+        return comparator.compare(element, minimum) < 0;
     }
 
     /**
@@ -334,24 +362,16 @@ public final class Range<T> implements Serializable {
     }
 
     /**
-     * <p>Checks whether this range is overlapped by the specified range.</p>
-     *
-     * <p>Two ranges overlap if there is at least one element in common.</p>
-     *
-     * <p>This method may fail if the ranges have two different comparators or element types.</p>
+     * <p>Checks whether this range is before the specified element.</p>
      *
-     * @param otherRange  the range to test, null returns false
-     * @return true if the specified range overlaps with this
-     *  range; otherwise, {@code false}
-     * @throws RuntimeException if ranges cannot be compared
+     * @param element  the element to check for, null returns false
+     * @return true if this range is entirely before the specified element
      */
-    public boolean isOverlappedBy(final Range<T> otherRange) {
-        if (otherRange == null) {
+    public boolean isBefore(final T element) {
+        if (element == null) {
             return false;
         }
-        return otherRange.contains(minimum)
-            || otherRange.contains(maximum)
-            || contains(otherRange.minimum);
+        return comparator.compare(element, maximum) > 0;
     }
 
     /**
@@ -371,68 +391,65 @@ public final class Range<T> implements Serializable {
     }
 
     /**
-     * Calculate the intersection of {@code this} and an overlapping Range.
-     * @param other overlapping Range
-     * @return range representing the intersection of {@code this} and {@code other} ({@code this} if equal)
-     * @throws IllegalArgumentException if {@code other} does not overlap {@code this}
-     * @since 3.0.1
+     * <p>Checks whether this range ends with the specified element.</p>
+     *
+     * @param element  the element to check for, null returns false
+     * @return true if the specified element occurs within this range
      */
-    public Range<T> intersectionWith(final Range<T> other) {
-        if (!this.isOverlappedBy(other)) {
-            throw new IllegalArgumentException(String.format(
-                "Cannot calculate intersection with non-overlapping range %s", other));
-        }
-        if (this.equals(other)) {
-            return this;
+    public boolean isEndedBy(final T element) {
+        if (element == null) {
+            return false;
         }
-        final T min = getComparator().compare(minimum, other.minimum) < 0 ? other.minimum : minimum;
-        final T max = getComparator().compare(maximum, other.maximum) < 0 ? maximum : other.maximum;
-        return between(min, max, getComparator());
+        return comparator.compare(element, maximum) == 0;
     }
 
     // Basics
     //--------------------------------------------------------------------
 
     /**
-     * <p>Compares this range to another object to test if they are equal.</p>.
+     * <p>Whether or not the Range is using the natural ordering of the elements.</p>
      *
-     * <p>To be equal, the minimum and maximum values must be equal, which
-     * ignores any differences in the comparator.</p>
+     * <p>Natural ordering uses an internal comparator implementation, thus this
+     * method is the only way to check if a null comparator was specified.</p>
      *
-     * @param obj the reference object with which to compare
-     * @return true if this object is equal
+     * @return true if using natural ordering
      */
-    @Override
-    public boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        } else if (obj == null || obj.getClass() != getClass()) {
+    public boolean isNaturalOrdering() {
+        return comparator == ComparableComparator.INSTANCE;
+    }
+
+    /**
+     * <p>Checks whether this range is overlapped by the specified range.</p>
+     *
+     * <p>Two ranges overlap if there is at least one element in common.</p>
+     *
+     * <p>This method may fail if the ranges have two different comparators or element types.</p>
+     *
+     * @param otherRange  the range to test, null returns false
+     * @return true if the specified range overlaps with this
+     *  range; otherwise, {@code false}
+     * @throws RuntimeException if ranges cannot be compared
+     */
+    public boolean isOverlappedBy(final Range<T> otherRange) {
+        if (otherRange == null) {
             return false;
-        } else {
-            @SuppressWarnings("unchecked") // OK because we checked the class above
-            final
-            Range<T> range = (Range<T>) obj;
-            return minimum.equals(range.minimum) &&
-                   maximum.equals(range.maximum);
         }
+        return otherRange.contains(minimum)
+            || otherRange.contains(maximum)
+            || contains(otherRange.minimum);
     }
 
     /**
-     * <p>Gets a suitable hash code for the range.</p>
+     * <p>Checks whether this range starts with the specified element.</p>
      *
-     * @return a hash code value for this object
+     * @param element  the element to check for, null returns false
+     * @return true if the specified element occurs within this range
      */
-    @Override
-    public int hashCode() {
-        int result = hashCode;
-        if (hashCode == 0) {
-            result = 17;
-            result = 37 * result + getClass().hashCode();
-            result = 37 * result + minimum.hashCode();
-            result = 37 * result + maximum.hashCode();
-            hashCode = result;
+    public boolean isStartedBy(final T element) {
+        if (element == null) {
+            return false;
         }
-        return result;
+        return comparator.compare(element, minimum) == 0;
     }
 
     /**
@@ -466,21 +483,4 @@ public final class Range<T> implements Serializable {
         return String.format(format, minimum, maximum, comparator);
     }
 
-    //-----------------------------------------------------------------------
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private enum ComparableComparator implements Comparator {
-        INSTANCE;
-        /**
-         * Comparable based compare implementation.
-         *
-         * @param obj1 left hand side of comparison
-         * @param obj2 right hand side of comparison
-         * @return negative, 0, positive comparison value
-         */
-        @Override
-        public int compare(final Object obj1, final Object obj2) {
-            return ((Comparable) obj1).compareTo(obj2);
-        }
-    }
-
 }