~ubuntu-branches/ubuntu/natty/electric/natty

« back to all changes in this revision

Viewing changes to com/sun/electric/database/ImmutableElectricObject.java

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2010-01-09 16:26:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100109162604-1ypvmy8ijmlc6oq7
Tags: 8.10-1
* New upstream version.
* debian/control
  - Add libjava3d-java and quilt build dependencies.
  - Update standards version to 3.8.3.
  - Add libjava3d-java as recommends to binary package.
* debian/rules
  - Use quilt patch system instead of simple patchsys.
  - Add java3d related jar files to DEB_JARS.
* debian/patches/*
  - Update as per current upstream source. Convert to quilt.
* debian/ant.properties
  - Do not disable 3D plugin anymore.
  - Use new property to disable compilation of OS X related classes.
* debian/wrappers/electric
  - Add java3d related jar files to runtime classpath.
* debian/README.source
  - Change text to the appropriate one for quilt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
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;
44
44
 
45
 
        /**
46
 
         * The package-private constructor of ImmutableElectricObject.
 
45
    /**
 
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.
50
 
         */
 
50
     */
51
51
    ImmutableElectricObject(Variable[] vars, int flags) {
52
52
        this.vars = vars;
53
53
        this.flags = flags;
54
54
    }
55
55
 
56
 
        /**
57
 
         * Returns array of Variables which differs from array of this ImmutableElectricObject by additional Variable.
 
56
    /**
 
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
62
 
         */
 
59
     * @param var additional Variable.
 
60
     * @return array of Variables with additional Variable.
 
61
     * @throws NullPointerException if var is null
 
62
     */
63
63
    Variable[] arrayWithVariable(Variable var) {
64
64
        return arrayWithVariable(vars, var);
65
65
    }
66
66
 
67
 
        /**
68
 
         * Returns array of Variables which differs from given array of Variables by additional Variable.
 
67
    /**
 
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
74
 
         */
 
71
     * @param var additional Variable.
 
72
     * @return array of Variables with additional Variable.
 
73
     * @throws NullPointerException if var is null
 
74
     */
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;
80
80
            newLength++;
81
 
        } else if (vars[varIndex] == var) return vars;
 
81
        } else if (vars[varIndex] == var) {
 
82
            return vars;
 
83
        }
82
84
        Variable[] newVars = new Variable[newLength];
83
85
        System.arraycopy(vars, 0, newVars, 0, varIndex);
84
86
        newVars[varIndex] = var;
87
89
        return newVars;
88
90
    }
89
91
 
90
 
        /**
91
 
         * Returns array of Variable which differs from array of this ImmutableElectricObject by removing Variable
 
92
    /**
 
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
96
 
         */
 
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
 
98
     */
97
99
    Variable[] arrayWithoutVariable(Variable.Key key) {
98
100
        return arrayWithoutVariable(vars, key);
99
101
    }
100
102
 
101
 
        /**
102
 
         * Returns array of Variable which differs from given array of Vasriables by removing Variable
 
103
    /**
 
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
108
 
         */
 
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
 
110
     */
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;
 
113
        if (varIndex < 0) {
 
114
            return vars;
 
115
        }
 
116
        if (vars.length == 1 && varIndex == 0) {
 
117
            return Variable.NULL_ARRAY;
 
118
        }
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);
116
122
        return newVars;
117
123
    }
118
124
 
119
 
        /**
120
 
         * Returns array of Variable which differs from array of this ImmutableElectricObject by renamed Ids.
 
125
    /**
 
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.
124
 
         */
 
130
     */
125
131
    Variable[] arrayWithRenamedIds(IdMapper idMapper) {
126
132
        return arrayWithRenamedIds(vars, idMapper);
127
133
    }
128
134
 
129
 
        /**
130
 
         * Returns array of Variable which differs from given array of Variables by renamed Ids.
 
135
    /**
 
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.
134
 
         */
 
140
     */
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);
143
149
            }
144
 
            if (newVars != null)
 
150
            if (newVars != null) {
145
151
                newVars[i] = newVar;
 
152
            }
146
153
        }
147
154
        return newVars != null ? newVars : vars;
148
155
    }
149
156
 
150
 
        /**
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
155
 
         */
156
 
        public Variable getVar(Variable.Key key)
157
 
        {
 
157
    /**
 
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
 
162
     */
 
163
    public Variable getVar(Variable.Key key) {
158
164
        int varIndex = searchVar(key);
159
165
        return varIndex >= 0 ? vars[varIndex] : null;
160
 
        }
 
166
    }
161
167
 
162
 
        /**
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
169
 
         */
170
 
        public <T> T getVarValue(Variable.Key key, Class type) {
171
 
                Variable var = getVar(key);
 
168
    /**
 
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
 
175
     */
 
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))
175
 
                return (T)value;
 
180
            if (type.isInstance(value)) {
 
181
                return (T) value;
 
182
            }
176
183
        }
177
 
                return null;
178
 
        }
179
 
 
180
 
        /**
181
 
         * Method to return an Iterator over all Variables on this ImmutableElectricObject.
182
 
         * @return an Iterator over all Variables on this ImmutableElectricObject.
183
 
         */
184
 
        public Iterator<Variable> getVariables() { return ArrayIterator.iterator(vars); }
185
 
 
186
 
        /**
187
 
         * Method to return an array of all Variables on this ImmutableElectricObject.
188
 
         * @return an array of all Variables on this ImmutableElectricObject.
189
 
         */
190
 
        public Variable[] toVariableArray() {
191
 
        return vars.length == 0 ? vars : (Variable[])vars.clone();
192
 
    }
193
 
 
194
 
        /**
195
 
         * Method to return the number of Variables on this ImmutableElectricObject.
196
 
         * @return the number of Variables on this ImmutableElectricObject.
197
 
         */
198
 
        public int getNumVariables() { return vars.length; }
199
 
 
200
 
        /**
201
 
         * Method to return the Variable by its varIndex.
 
184
        return null;
 
185
    }
 
186
 
 
187
    /**
 
188
     * Method to return an Iterator over all Variables on this ImmutableElectricObject.
 
189
     * @return an Iterator over all Variables on this ImmutableElectricObject.
 
190
     */
 
191
    public Iterator<Variable> getVariables() {
 
192
        return ArrayIterator.iterator(vars);
 
193
    }
 
194
 
 
195
    /**
 
196
     * Method to return an array of all Variables on this ImmutableElectricObject.
 
197
     * @return an array of all Variables on this ImmutableElectricObject.
 
198
     */
 
199
    public Variable[] toVariableArray() {
 
200
        return vars.length == 0 ? vars : (Variable[]) vars.clone();
 
201
    }
 
202
 
 
203
    /**
 
204
     * Method to return the number of Variables on this ImmutableElectricObject.
 
205
     * @return the number of Variables on this ImmutableElectricObject.
 
206
     */
 
207
    public int getNumVariables() {
 
208
        return vars.length;
 
209
    }
 
210
 
 
211
    /**
 
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.
205
 
         */
206
 
        public Variable getVar(int varIndex) { return vars[varIndex]; }
 
216
     */
 
217
    public Variable getVar(int varIndex) {
 
218
        return vars[varIndex];
 
219
    }
207
220
 
208
 
        /**
209
 
         * The package-private method to get Variable array.
 
221
    /**
 
222
     * The package-private method to get Variable array.
210
223
     * @return Variable array of this ImmutableElectricObject.
211
 
         */
212
 
    Variable[] getVars() { return vars; }
 
224
     */
 
225
    Variable[] getVars() {
 
226
        return vars;
 
227
    }
213
228
 
214
229
    /**
215
230
     * Searches the variables for the specified variable key using the binary
223
238
     *         elements in the list are less than the specified name.  Note
224
239
     *         that this guarantees that the return value will be &gt;= 0 if
225
240
     *         and only if the Variable is found.
226
 
         * @throws NullPointerException if key is null
 
241
     * @throws NullPointerException if key is null
227
242
     */
228
 
        public int searchVar(Variable.Key key) {
229
 
        if (key == null) throw new NullPointerException("key");
 
243
    public int searchVar(Variable.Key key) {
 
244
        if (key == null) {
 
245
            throw new NullPointerException("key");
 
246
        }
230
247
        return searchVar(vars, key);
231
248
    }
232
249
 
244
261
     *         that this guarantees that the return value will be &gt;= 0 if
245
262
     *         and only if the Variable is found.
246
263
     */
247
 
        static int searchVar(Variable[] vars, Variable.Key key)
248
 
        {
 
264
    static int searchVar(Variable[] vars, Variable.Key key) {
249
265
        int low = 0;
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);
255
271
 
256
 
                        if (cmp < 0)
257
 
                                low = mid + 1;
258
 
                        else if (cmp > 0)
259
 
                                high = mid - 1;
260
 
                        else
261
 
                                return mid; // Variable found
262
 
                }
263
 
                return -(low + 1);  // Variable not found.
 
272
            if (cmp < 0) {
 
273
                low = mid + 1;
 
274
            } else if (cmp > 0) {
 
275
                high = mid - 1;
 
276
            } else {
 
277
                return mid; // Variable found
 
278
            }
 
279
        }
 
280
        return -(low + 1);  // Variable not found.
264
281
    }
265
282
 
266
283
    /**
270
287
    void write(IdWriter writer) throws IOException {
271
288
        boolean hasVars = vars.length > 0;
272
289
        writer.writeBoolean(hasVars);
273
 
        if (hasVars)
 
290
        if (hasVars) {
274
291
            writeVars(writer);
 
292
        }
275
293
    }
276
294
 
277
295
    /**
288
306
     */
289
307
    static void writeVars(Variable[] vars, IdWriter writer) throws IOException {
290
308
        writer.writeInt(vars.length);
291
 
        for (int i = 0; i < vars.length; i++)
 
309
        for (int i = 0; i < vars.length; i++) {
292
310
            vars[i].write(writer);
 
311
        }
293
312
    }
294
313
 
295
314
    /**
298
317
     */
299
318
    static Variable[] readVars(IdReader reader) throws IOException {
300
319
        int length = reader.readInt();
301
 
        if (length == 0) return Variable.NULL_ARRAY;
 
320
        if (length == 0) {
 
321
            return Variable.NULL_ARRAY;
 
322
        }
302
323
        Variable[] vars = new Variable[length];
303
 
        for (int i = 0; i < length; i++)
 
324
        for (int i = 0; i < length; i++) {
304
325
            vars[i] = Variable.read(reader);
 
326
        }
305
327
        return vars;
306
328
    }
307
329
 
320
342
    public abstract boolean equalsExceptVariables(ImmutableElectricObject o);
321
343
 
322
344
    /**
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.
326
 
         */
327
 
        void check(boolean inheritAllowed) {
328
 
        if (vars.length == 0) return;
 
347
     * @throws AssertionError if invariant is broken.
 
348
     */
 
349
    void check(boolean inheritAllowed) {
 
350
        if (vars.length == 0) {
 
351
            return;
 
352
        }
329
353
        vars[0].check(false, inheritAllowed);
330
354
        for (int i = 1; i < vars.length; i++) {
331
355
            vars[i].check(false, inheritAllowed);