]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/slice/ValueRange.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / slice / ValueRange.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures.slice;\r
13 \r
14 /**\r
15  * @author Tuukka Lehtonen\r
16  */\r
17 public class ValueRange {\r
18 \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
23 \r
24     private final int start;\r
25     private final int size;\r
26 \r
27     public static ValueRange make(int size) {\r
28         if (size == 0)\r
29             return EMPTY;\r
30         else if (size == 1)\r
31             return ONE;\r
32         else if (size == 2)\r
33             return TWO;\r
34         return new ValueRange(size);\r
35     }\r
36 \r
37     public static ValueRange make(int start, int size) {\r
38         if (start == 0) {\r
39             if (size == 0)\r
40                 return EMPTY;\r
41             else if (size == 1)\r
42                 return ONE;\r
43             else if (size == 2)\r
44                 return TWO;\r
45         }\r
46         return new ValueRange(start, size);\r
47     }\r
48 \r
49     private ValueRange(int size) {\r
50         this(0, size);\r
51     }\r
52 \r
53     private ValueRange(int start, int size) {\r
54         if (start < 0)\r
55             throw new IllegalArgumentException("negative range start: " + start);\r
56         if (size < 0)\r
57             throw new IllegalArgumentException("negative range size: " + size);\r
58         this.start = start;\r
59         this.size = size;\r
60     }\r
61 \r
62     public boolean isEmpty() {\r
63         return size == 0;\r
64     }\r
65 \r
66     public boolean isSingle() {\r
67         return size == 1;\r
68     }\r
69 \r
70     public int start() {\r
71         return start;\r
72     }\r
73 \r
74     public int end() {\r
75         return start + size;\r
76     }\r
77 \r
78     public int size() {\r
79         return size;\r
80     }\r
81 \r
82     @Override\r
83     public String toString() {\r
84         if (isEmpty())\r
85             return "[]";\r
86         if (isSingle())\r
87             return "[" + start + "]";\r
88         return "[" + start + "-" + (start + size - 1) + "]";\r
89     }\r
90 \r
91     public String toString(int offset) {\r
92         if (isEmpty())\r
93             return "[]";\r
94         if (isSingle())\r
95             return "[" + (start+offset) + "]";\r
96         return "[" + (start+offset) + "-" + (start + size - 1 + offset) + "]";\r
97     }\r
98 \r
99     @Override\r
100     public int hashCode() {\r
101         final int prime = 31;\r
102         int result = 1;\r
103         result = prime * result + size;\r
104         result = prime * result + start;\r
105         return result;\r
106     }\r
107 \r
108     @Override\r
109     public boolean equals(Object obj) {\r
110         if (this == obj)\r
111             return true;\r
112         if (obj == null)\r
113             return false;\r
114         if (getClass() != obj.getClass())\r
115             return false;\r
116         ValueRange other = (ValueRange) obj;\r
117         return size == other.size && start == other.start;\r
118     }\r
119 \r
120 //    public ChildReference toChildReference() {\r
121 //        if (isEmpty())\r
122 //            throw new UnsupportedOperationException("cannot create a value reference for an empty range");\r
123 //        if (isSingle())\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
127 //    }\r
128 \r
129     /**\r
130      * @return a Simantics Databoard child reference string describing this range.\r
131      */\r
132     public String toChildReferenceString() {\r
133         if (isEmpty())\r
134             throw new UnsupportedOperationException("cannot create a value reference for an empty range");\r
135         StringBuilder sb = new StringBuilder();\r
136         if (isSingle())\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
140     }\r
141 \r
142 }\r