Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.scene.control; 027 028/** 029 * Class representing a contiguous range of integral values. 030 */ 031public final class IndexRange { 032 private int start; 033 private int end; 034 035 /** 036 * Index range value delimiter. 037 */ 038 public static final String VALUE_DELIMITER = ","; 039 040 /** 041 * Creates an instance of IndexRange representing the range between 042 * <code>start</code> and <code>end</code>. 043 * 044 * @param start The start position of the range. 045 * @param end The end position of the range. 046 */ 047 public IndexRange(int start, int end) { 048 if (end < start) { 049 throw new IllegalArgumentException(); 050 } 051 052 this.start = start; 053 this.end = end; 054 } 055 056 /** 057 * Creates an instance of IndexRange by copying the values from the 058 * given IndexRange object. 059 * 060 * @param range The IndexRange instance from which to copy the start and end 061 * values. 062 */ 063 public IndexRange(IndexRange range) { 064 this.start = range.start; 065 this.end = range.end; 066 } 067 068 /** 069 * Returns the start position of the range. 070 */ 071 public int getStart() { 072 return start; 073 } 074 075 /** 076 * Returns the end position of the range (exclusive). 077 */ 078 public int getEnd() { 079 return end; 080 } 081 082 /** 083 * Returns the length of the range. 084 */ 085 public int getLength() { 086 return end - start; 087 } 088 089 /** 090 * Indicates whether some other object is "equal to" this one. 091 * @param object the reference object with which to compare. 092 * @return {@code true} if this object is equal to the {@code object} argument; {@code false} otherwise. 093 */ 094 @Override 095 public boolean equals(Object object) { 096 if (object == this) return true; 097 if (object instanceof IndexRange) { 098 IndexRange range = (IndexRange)object; 099 return (start == range.start 100 && end == range.end); 101 } 102 103 return false; 104 } 105 106 /** 107 * Returns a hash code for this {@code Range} object. 108 * @return a hash code for this {@code Range} object. 109 */ 110 @Override 111 public int hashCode() { 112 return 31 * start + end; 113 } 114 115 /** 116 * Returns a string representation of this {@code Range} object. 117 * @return a string representation of this {@code Range} object. 118 */ 119 @Override 120 public String toString() { 121 return start + VALUE_DELIMITER + " " + end; 122 } 123 124 /** 125 * Convenience method to create an IndexRange instance that has the smaller 126 * value as the start index, and the larger value as the end index. 127 * 128 * @param v1 The first value to use in the range. 129 * @param v2 The second value to use in the range. 130 * @return A IndexRange instance where the smaller value is the start, and the 131 * larger value is the end. 132 */ 133 public static IndexRange normalize(int v1, int v2) { 134 return new IndexRange(Math.min(v1, v2), Math.max(v1, v2)); 135 } 136 137 /** 138 * Convenience method to parse in a String of the form '2,6', which will 139 * create an IndexRange instance with a start value of 2, and an end value 140 * of 6. 141 * 142 * @param value The string to be parsed, and converted to an IndexRange. 143 * @return An IndexRange instance representing the start and end values provided 144 * in the value string. 145 */ 146 public static IndexRange valueOf(String value) { 147 if (value == null) { 148 throw new IllegalArgumentException(); 149 } 150 151 String[] values = value.split(VALUE_DELIMITER); 152 if (values.length != 2) { 153 throw new IllegalArgumentException(); 154 } 155 156 // NOTE As of Java 6, Integer#parseInt() appears to require 157 // trimmed values 158 int start = Integer.parseInt(values[0].trim()); 159 int end = Integer.parseInt(values[1].trim()); 160 161 return IndexRange.normalize(start, end); 162 } 163}