1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.datastructures.slice;
\r
15 * @author Tuukka Lehtonen
\r
17 public class ValueRange {
\r
19 public static final ValueRange NO_RANGE = new ValueRange(0, Integer.MAX_VALUE);
\r
20 public static final ValueRange EMPTY = new ValueRange(0, 0);
\r
21 public static final ValueRange ONE = new ValueRange(0, 1);
\r
22 public static final ValueRange TWO = new ValueRange(0, 2);
\r
24 private final int start;
\r
25 private final int size;
\r
27 public static ValueRange make(int size) {
\r
34 return new ValueRange(size);
\r
37 public static ValueRange make(int start, int size) {
\r
46 return new ValueRange(start, size);
\r
49 private ValueRange(int size) {
\r
53 private ValueRange(int start, int size) {
\r
55 throw new IllegalArgumentException("negative range start: " + start);
\r
57 throw new IllegalArgumentException("negative range size: " + size);
\r
62 public boolean isEmpty() {
\r
66 public boolean isSingle() {
\r
70 public int start() {
\r
75 return start + size;
\r
83 public String toString() {
\r
87 return "[" + start + "]";
\r
88 return "[" + start + "-" + (start + size - 1) + "]";
\r
91 public String toString(int offset) {
\r
95 return "[" + (start+offset) + "]";
\r
96 return "[" + (start+offset) + "-" + (start + size - 1 + offset) + "]";
\r
100 public int hashCode() {
\r
101 final int prime = 31;
\r
103 result = prime * result + size;
\r
104 result = prime * result + start;
\r
109 public boolean equals(Object obj) {
\r
114 if (getClass() != obj.getClass())
\r
116 ValueRange other = (ValueRange) obj;
\r
117 return size == other.size && start == other.start;
\r
120 // public ChildReference toChildReference() {
\r
122 // throw new UnsupportedOperationException("cannot create a value reference for an empty range");
\r
124 // return new IndexReference(start);
\r
125 // //throw new RangeReference(start, start+size-1);
\r
126 // throw new UnsupportedOperationException("cannot create a value reference for a range with cardinality > 1");
\r
130 * @return a Simantics Databoard child reference string describing this range.
\r
132 public String toChildReferenceString() {
\r
134 throw new UnsupportedOperationException("cannot create a value reference for an empty range");
\r
135 StringBuilder sb = new StringBuilder();
\r
137 return sb.append("i-").append(start).toString();
\r
138 // TODO: proposal: http://dev.simantics.org/index.php/Talk:Databoard_Specification
\r
139 return sb.append("r-").append(start).append('-').append(start+size-1).toString();
\r