1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import javafx.beans.NamedArg;
  29 
  30 
  31 /**
  32  * Class representing a contiguous range of integral values.
  33  * @since JavaFX 2.0
  34  */
  35 public final class IndexRange {
  36     private int start;
  37     private int end;
  38 
  39     /**
  40      * Index range value delimiter.
  41      */
  42     public static final String VALUE_DELIMITER = ",";
  43 
  44     /**
  45      * Creates an instance of IndexRange representing the range between
  46      * <code>start</code> and <code>end</code>.
  47      *
  48      * @param start The start position of the range.
  49      * @param end The end position of the range.
  50      */
  51     public IndexRange(@NamedArg("start") int start, @NamedArg("end") int end) {
  52         if (end < start) {
  53             throw new IllegalArgumentException();
  54         }
  55 
  56         this.start = start;
  57         this.end = end;
  58     }
  59 
  60     /**
  61      * Creates an instance of IndexRange by copying the values from the
  62      * given IndexRange object.
  63      *
  64      * @param range The IndexRange instance from which to copy the start and end
  65      *      values.
  66      */
  67     public IndexRange(@NamedArg("range") IndexRange range) {
  68         this.start = range.start;
  69         this.end = range.end;
  70     }
  71 
  72     /**
  73      * Returns the start position of the range.
  74      */
  75     public int getStart() {
  76         return start;
  77     }
  78 
  79     /**
  80      * Returns the end position of the range (exclusive).
  81      */
  82     public int getEnd() {
  83         return end;
  84     }
  85 
  86     /**
  87      * Returns the length of the range.
  88      */
  89     public int getLength() {
  90         return end - start;
  91     }
  92 
  93     /**
  94      * Indicates whether some other object is "equal to" this one.
  95      * @param object the reference object with which to compare.
  96      * @return {@code true} if this object is equal to the {@code object} argument; {@code false} otherwise.
  97      */
  98     @Override
  99     public boolean equals(Object object) {
 100         if (object == this) return true;
 101         if (object instanceof IndexRange) {
 102             IndexRange range = (IndexRange)object;
 103             return (start == range.start
 104                 && end == range.end);
 105         }
 106 
 107         return false;
 108     }
 109 
 110     /**
 111      * Returns a hash code for this {@code Range} object.
 112      * @return a hash code for this {@code Range} object.
 113      */
 114     @Override
 115     public int hashCode() {
 116         return 31 * start + end;
 117     }
 118 
 119     /**
 120      * Returns a string representation of this {@code Range} object.
 121      * @return a string representation of this {@code Range} object.
 122      */
 123     @Override
 124     public String toString() {
 125         return start + VALUE_DELIMITER + " " + end;
 126     }
 127 
 128     /**
 129      * Convenience method to create an IndexRange instance that has the smaller
 130      * value as the start index, and the larger value as the end index.
 131      *
 132      * @param v1 The first value to use in the range.
 133      * @param v2 The second value to use in the range.
 134      * @return A IndexRange instance where the smaller value is the start, and the
 135      *      larger value is the end.
 136      */
 137     public static IndexRange normalize(int v1, int v2) {
 138         return new IndexRange(Math.min(v1, v2), Math.max(v1, v2));
 139     }
 140 
 141     /**
 142      * Convenience method to parse in a String of the form '2,6', which will
 143      * create an IndexRange instance with a start value of 2, and an end value
 144      * of 6.
 145      *
 146      * @param value The string to be parsed, and converted to an IndexRange.
 147      * @return An IndexRange instance representing the start and end values provided
 148      *      in the value string.
 149      */
 150     public static IndexRange valueOf(String value) {
 151         if (value == null) {
 152             throw new IllegalArgumentException();
 153         }
 154 
 155         String[] values = value.split(VALUE_DELIMITER);
 156         if (values.length != 2) {
 157             throw new IllegalArgumentException();
 158         }
 159 
 160         // NOTE As of Java 6, Integer#parseInt() appears to require
 161         // trimmed values
 162         int start = Integer.parseInt(values[0].trim());
 163         int end = Integer.parseInt(values[1].trim());
 164 
 165         return IndexRange.normalize(start, end);
 166     }
 167 }