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