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