40
40
/** array of variables sorted by their keys. */
41
41
private final Variable[] vars;
42
/** flags of this IimmutableElectricObject. */
42
/** flags of this IimmutableElectricObject. */
43
43
public final int flags;
46
* The package-private constructor of ImmutableElectricObject.
46
* The package-private constructor of ImmutableElectricObject.
47
47
* Use the factory "newInstance" instead.
48
48
* @param vars array of Variables sorted by their keys.
49
49
* @param flags flags of this IimmutableElectricObject.
51
51
ImmutableElectricObject(Variable[] vars, int flags) {
53
53
this.flags = flags;
57
* Returns array of Variables which differs from array of this ImmutableElectricObject by additional Variable.
57
* Returns array of Variables which differs from array of this ImmutableElectricObject by additional Variable.
58
58
* If this ImmutableElectricObject has Variable with the same key as new, the old variable will not be in new array.
59
* @param var additional Variable.
60
* @return array of Variables with additional Variable.
61
* @throws NullPointerException if var is null
59
* @param var additional Variable.
60
* @return array of Variables with additional Variable.
61
* @throws NullPointerException if var is null
63
63
Variable[] arrayWithVariable(Variable var) {
64
64
return arrayWithVariable(vars, var);
68
* Returns array of Variables which differs from given array of Variables by additional Variable.
68
* Returns array of Variables which differs from given array of Variables by additional Variable.
69
69
* If the array has Variable with the same key as new, the old variable will not be in new array.
70
70
* @param vars array of Variables
71
* @param var additional Variable.
72
* @return array of Variables with additional Variable.
73
* @throws NullPointerException if var is null
71
* @param var additional Variable.
72
* @return array of Variables with additional Variable.
73
* @throws NullPointerException if var is null
75
75
static Variable[] arrayWithVariable(Variable[] vars, Variable var) {
76
76
int varIndex = searchVar(vars, var.getKey());
77
77
int newLength = vars.length;
78
78
if (varIndex < 0) {
79
79
varIndex = ~varIndex;
81
} else if (vars[varIndex] == var) return vars;
81
} else if (vars[varIndex] == var) {
82
84
Variable[] newVars = new Variable[newLength];
83
85
System.arraycopy(vars, 0, newVars, 0, varIndex);
84
86
newVars[varIndex] = var;
91
* Returns array of Variable which differs from array of this ImmutableElectricObject by removing Variable
93
* Returns array of Variable which differs from array of this ImmutableElectricObject by removing Variable
92
94
* with the specified key. Returns array of this ImmutableElectricObject if it doesn't contain variable with the specified key.
93
* @param key Variable Key to remove.
94
* @return array of Variables without Variable with the specified key.
95
* @throws NullPointerException if key is null
95
* @param key Variable Key to remove.
96
* @return array of Variables without Variable with the specified key.
97
* @throws NullPointerException if key is null
97
99
Variable[] arrayWithoutVariable(Variable.Key key) {
98
100
return arrayWithoutVariable(vars, key);
102
* Returns array of Variable which differs from given array of Vasriables by removing Variable
104
* Returns array of Variable which differs from given array of Vasriables by removing Variable
103
105
* with the specified key. Returns given array if it doesn't contain variable with the specified key.
104
106
* @param vars array of Variables
105
* @param key Variable Key to remove.
106
* @return array of Variables without Variable with the specified key.
107
* @throws NullPointerException if key is null
107
* @param key Variable Key to remove.
108
* @return array of Variables without Variable with the specified key.
109
* @throws NullPointerException if key is null
109
111
static Variable[] arrayWithoutVariable(Variable[] vars, Variable.Key key) {
110
112
int varIndex = searchVar(vars, key);
111
if (varIndex < 0) return vars;
112
if (vars.length == 1 && varIndex == 0) return Variable.NULL_ARRAY;
116
if (vars.length == 1 && varIndex == 0) {
117
return Variable.NULL_ARRAY;
113
119
Variable[] newVars = new Variable[vars.length - 1];
114
120
System.arraycopy(vars, 0, newVars, 0, varIndex);
115
121
System.arraycopy(vars, varIndex + 1, newVars, varIndex, newVars.length - varIndex);
120
* Returns array of Variable which differs from array of this ImmutableElectricObject by renamed Ids.
126
* Returns array of Variable which differs from array of this ImmutableElectricObject by renamed Ids.
121
127
* Returns array of this ImmutableElectricObject if it doesn't contain reanmed Ids.
122
* @param idMapper a map from old Ids to new Ids.
128
* @param idMapper a map from old Ids to new Ids.
123
129
* @return array of Variable with renamed Ids.
125
131
Variable[] arrayWithRenamedIds(IdMapper idMapper) {
126
132
return arrayWithRenamedIds(vars, idMapper);
130
* Returns array of Variable which differs from given array of Variables by renamed Ids.
136
* Returns array of Variable which differs from given array of Variables by renamed Ids.
131
137
* Returns given array if it doesn't contain reanmed Ids.
132
* @param idMapper a map from old Ids to new Ids.
138
* @param idMapper a map from old Ids to new Ids.
133
139
* @return array of Variable with renamed Ids.
135
141
static Variable[] arrayWithRenamedIds(Variable[] vars, IdMapper idMapper) {
136
142
Variable[] newVars = null;
137
143
for (int i = 0; i < vars.length; i++) {
141
147
newVars = new Variable[vars.length];
142
148
System.arraycopy(vars, 0, newVars, 0, i);
150
if (newVars != null) {
145
151
newVars[i] = newVar;
147
154
return newVars != null ? newVars : vars;
151
* Method to return the Variable on this ImmuatbleElectricObject with a given key.
152
* @param key the key of the Variable.
153
* @return the Variable with that key, or null if there is no such Variable.
154
* @throws NullPointerException if key is null
156
public Variable getVar(Variable.Key key)
158
* Method to return the Variable on this ImmuatbleElectricObject with a given key.
159
* @param key the key of the Variable.
160
* @return the Variable with that key, or null if there is no such Variable.
161
* @throws NullPointerException if key is null
163
public Variable getVar(Variable.Key key) {
158
164
int varIndex = searchVar(key);
159
165
return varIndex >= 0 ? vars[varIndex] : null;
163
* Method to return the value of the Variable on this ImmutableElectricObject with a given key and type.
164
* @param key the key of the Variable.
165
* @param type the required type of the Variable.
166
* @return the value of the Variable with that key and type, or null if there is no such Variable
167
* or default Variable value.
168
* @throws NullPointerException if key or type is null
170
public <T> T getVarValue(Variable.Key key, Class type) {
171
Variable var = getVar(key);
169
* Method to return the value of the Variable on this ImmutableElectricObject with a given key and type.
170
* @param key the key of the Variable.
171
* @param type the required type of the Variable.
172
* @return the value of the Variable with that key and type, or null if there is no such Variable
173
* or default Variable value.
174
* @throws NullPointerException if key or type is null
176
public <T> T getVarValue(Variable.Key key, Class type) {
177
Variable var = getVar(key);
172
178
if (var != null) {
173
179
Object value = var.getObject();
174
if (type.isInstance(value))
180
if (type.isInstance(value)) {
181
* Method to return an Iterator over all Variables on this ImmutableElectricObject.
182
* @return an Iterator over all Variables on this ImmutableElectricObject.
184
public Iterator<Variable> getVariables() { return ArrayIterator.iterator(vars); }
187
* Method to return an array of all Variables on this ImmutableElectricObject.
188
* @return an array of all Variables on this ImmutableElectricObject.
190
public Variable[] toVariableArray() {
191
return vars.length == 0 ? vars : (Variable[])vars.clone();
195
* Method to return the number of Variables on this ImmutableElectricObject.
196
* @return the number of Variables on this ImmutableElectricObject.
198
public int getNumVariables() { return vars.length; }
201
* Method to return the Variable by its varIndex.
188
* Method to return an Iterator over all Variables on this ImmutableElectricObject.
189
* @return an Iterator over all Variables on this ImmutableElectricObject.
191
public Iterator<Variable> getVariables() {
192
return ArrayIterator.iterator(vars);
196
* Method to return an array of all Variables on this ImmutableElectricObject.
197
* @return an array of all Variables on this ImmutableElectricObject.
199
public Variable[] toVariableArray() {
200
return vars.length == 0 ? vars : (Variable[]) vars.clone();
204
* Method to return the number of Variables on this ImmutableElectricObject.
205
* @return the number of Variables on this ImmutableElectricObject.
207
public int getNumVariables() {
212
* Method to return the Variable by its varIndex.
202
213
* @param varIndex index of Variable.
203
* @return the Variable with given varIndex.
214
* @return the Variable with given varIndex.
204
215
* @throws ArrayIndexOutOfBoundesException if varIndex out of bounds.
206
public Variable getVar(int varIndex) { return vars[varIndex]; }
217
public Variable getVar(int varIndex) {
218
return vars[varIndex];
209
* The package-private method to get Variable array.
222
* The package-private method to get Variable array.
210
223
* @return Variable array of this ImmutableElectricObject.
212
Variable[] getVars() { return vars; }
225
Variable[] getVars() {
215
230
* Searches the variables for the specified variable key using the binary
244
261
* that this guarantees that the return value will be >= 0 if
245
262
* and only if the Variable is found.
247
static int searchVar(Variable[] vars, Variable.Key key)
264
static int searchVar(Variable[] vars, Variable.Key key) {
250
int high = vars.length-1;
251
while (low <= high) {
252
int mid = (low + high) >> 1; // try in a middle
253
Variable var = vars[mid];
254
int cmp = var.getKey().compareTo(key);
266
int high = vars.length - 1;
267
while (low <= high) {
268
int mid = (low + high) >> 1; // try in a middle
269
Variable var = vars[mid];
270
int cmp = var.getKey().compareTo(key);
261
return mid; // Variable found
263
return -(low + 1); // Variable not found.
274
} else if (cmp > 0) {
277
return mid; // Variable found
280
return -(low + 1); // Variable not found.
320
342
public abstract boolean equalsExceptVariables(ImmutableElectricObject o);
323
* Checks invariant of this ImmutableElectricObject.
345
* Checks invariant of this ImmutableElectricObject.
324
346
* @param true if inherit is allowed on this ImmutableElectricObject
325
* @throws AssertionError if invariant is broken.
327
void check(boolean inheritAllowed) {
328
if (vars.length == 0) return;
347
* @throws AssertionError if invariant is broken.
349
void check(boolean inheritAllowed) {
350
if (vars.length == 0) {
329
353
vars[0].check(false, inheritAllowed);
330
354
for (int i = 1; i < vars.length; i++) {
331
355
vars[i].check(false, inheritAllowed);