File: Barcode.java

package info (click to toggle)
libglazedlists-java 1.8.0.dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,016 kB
  • sloc: java: 21,991; xml: 860; sh: 48; makefile: 5
file content (518 lines) | stat: -rw-r--r-- 17,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.adt;

import java.util.Iterator;

/**
 * A Barcode is an ADT to replace the more general CompressableList
 * ADT.  CompressableList provides list compression capabilites that allow
 * a list to be accessed by both the real index and a compressed index.
 * The compressed index corresponds to the index of the current value as
 * though no nulls existed in the list.
 *
 * <p>This provides a huge performance boost over ArrayList on partially empty
 * lists.  However, the CompressableList is one of the volatile implementation
 * classes for internal development and isn't the best structure for the current
 * usage.  The GlazedLists use CompressableList to store only three values:
 * Boolean.TRUE, Boolean.FALSE, and null.  As such, it was slower and more
 * memory intensive than it could be due to its general purpose design.
 *
 * <p>The Barcode is designed such that a list of n elements of the same
 * colour will contain at most one node for BLACK and no nodes for WHITE.
 * This will improve the performance and scalability of the GlazedLists
 * which currently make use of CompressableList.
 *
 * <p>Barcode does not support more than two values stored in the list.
 * Three different values are used by one of the GlazedLists at this time.
 * Until UniqueList is refactored to make use of only two values, Barcode
 * cannot completely replace CompressableList and they will exist in parallel.
 *
 * <p>In an effort to maximize performance this ADT does NOT validate that arguments
 * passed to methods are valid in any way.  While this adds inherent risk to
 * the use of this code, this is a volatile implementation class.  As such, it
 * should only be used for internal GlazedList development.  It is up to the
 * calling code to do any argument validation which may be necessary.
 *
 * <p>Every effort has been made to squeeze the highest performance and smallest
 * footprint out of this data structure.  These benefits hopefully don't come at
 * the cost of code clarity or maintainability.  The memory usage of this ADT
 * is bound to the number of sequences of BLACK elements.  WHITE elements
 * have no memory impact on the data structure.
 *
 *
 * @author <a href="mailto:kevin@swank.ca">Kevin Maltby</a>
 *
 */
public final class Barcode {

    /** barcode colour constants */
    public static final Object WHITE = Boolean.FALSE;
    public static final Object BLACK = Boolean.TRUE;

    /** the root of the underlying tree */
    private BarcodeNode root = null;

    /** the size of the trailing whitespace */
    private int whiteSpace = 0;

    /** the size of tree */
    private int treeSize = 0;

    /**
     * Prints internal debug information for this barcode
     */
    public void printDebug() {
        System.out.println("\nTotal Size: " + size());
        System.out.println("Trailing Whitespace : " + whiteSpace);
        System.out.println("Tree Size: " + treeSize);
        System.out.println("Tree Structure:\n" + root);
    }

    /**
     * Validates the barcode's internal structure
     */
    public void validate() {
        if(root != null) root.validate();
    }

    /**
     * Gets the size of this barcode
     */
    public int size() {
        return treeSize + whiteSpace;
    }

    /**
     * Whether or not this barcode is empty
     */
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * Gets the size of the white portion of this barcode
     */
    public int whiteSize() {
        return root == null ? whiteSpace : root.whiteSize() + whiteSpace;
    }

    /**
     * Gets the size of the black portion of this barcode
     */
    public int blackSize() {
        return root == null ? 0 : root.blackSize();
    }

    /**
     * Gets the size of the given colour portion of this barcode
     */
    public int colourSize(Object colour) {
        if(colour == WHITE) return whiteSize();
        else return blackSize();
    }

    /**
     * Inserts a sequence of the specified colour into the barcode
     */
    public void add(int index, Object colour, int length) {
        if(colour == WHITE) addWhite(index, length);
        else addBlack(index, length);
    }

    /**
     * Inserts a sequence of white into the list
     */
    public void addWhite(int index, int length) {
        if(length < 0) throw new IllegalStateException();
        if(length == 0) return;

        // Adding to the trailing whitespace
        if(root == null || index >= treeSize) {
            whiteSpace += length;

        // Adding whitespace to the actual list
        } else {
            root.insertWhite(index, length);
            treeSizeChanged();

        }
    }

    /**
     * Inserts a sequence of black into the list
     */
    public void addBlack(int index, int length) {
        if(length < 0) throw new IllegalArgumentException();
        if(length == 0) return;

        // Make a new root
        if(root == null) {
            root = new BarcodeNode(this, null, length, index);
            treeSize = index + length;
            whiteSpace -= index;

        // Add in the trailing whitespace
        } else if(index >= treeSize) {
            int movingWhitespace = index - treeSize;
            whiteSpace -= movingWhitespace;
            root.insertBlackAtEnd(length, movingWhitespace);
            treeSizeChanged();

        // Add values to the actual list
        } else {
            root.insertBlack(index, length);
            treeSizeChanged();

        }
    }

    /**
     * Gets the value in this list at the given index
     */
    public Object get(int index) {
        if(getBlackIndex(index) == -1) return WHITE;
        return BLACK;
    }

    /**
     * Sets all of the values between index and index + length to either
     * WHITE or BLACK depending on the value of colour
     *
     * @param colour Determines which colour to set the values in the range
     * to.  Valid values for colour are <code>Barcode.WHITE</code> and
     * <code>Barcode.BLACK</code>.
     */
    public void set(int index, Object colour, int length) {
        if(length < 1) throw new IllegalArgumentException();

        // The set affects the trailing whitespace
        int trailingChange = index > treeSize - 1 ? length : index + length - treeSize;
        if(trailingChange > 0) {
            if(colour == BLACK) {
                whiteSpace -= trailingChange;
                addBlack(index, trailingChange);
            }
            length -= trailingChange;
            if(length == 0) return;
        }

        // The set affects the list
        if(root != null) {
            root.set(index, colour, length);
            if(root != null) treeSizeChanged();
        }
    }

    /**
     * Sets all of the values between index and index + length to WHITE
     */
    public void setWhite(int index, int length) {
        set(index, WHITE, length);
    }

    /**
     * Sets all of the values between index and index + length to WHITE
     */
    public void setBlack(int index, int length) {
        set(index, BLACK, length);
    }

    /**
     * Removes the values from the given index to index + length
     */
    public void remove(int index, int length) {
        if(length < 1) throw new IllegalArgumentException();

        // The remove affects the trailing whitespace
        int trailingChange = index > treeSize ? length : index + length - treeSize;
        if(trailingChange > 0) {
            whiteSpace -= trailingChange;
            length -= trailingChange;
        }

        // The remove occurs in the actual list
        if(root != null && index < treeSize) {
            int oldTreeSize = -1;
            while(length > 0) {
                oldTreeSize = treeSize;
                root.remove(index, length);
                if(root != null) treeSizeChanged();
                length -= (oldTreeSize - treeSize);
            }
            if(root != null) treeSizeChanged();
        }
    }

    /**
     * Clears the list
     */
    public void clear() {
        treeSize = 0;
        whiteSpace = 0;
        root = null;
    }

    /**
     * Gets the root for this Barcode.  This method is exposed for
     * Iterators on Barcode whose set() operations may create a
     * root node on a Barcode where none existed.
     */
    BarcodeNode getRootNode() {
        return root;
    }

    /**
     * Sets the root for this list.  This method is exposed for the
     * BarcodeNode in the event that the list's root is involved in
     * an AVL rotation.
     */
    void setRootNode(BarcodeNode root) {
        this.root = root;
        if(root == null) treeSize = 0;
    }

    /**
     * Gets the size of the underlying tree structure for this Barcode.  This
     * method is exposed for Iterators on Barcode who would otherwise have to
     * maintain the state of treeSize themselves.
     */
    int treeSize() {
        return treeSize;
    }

    /**
     * Notifies the list that the underlying list size has changed. This method
     * is exposed for BarcodeNode to propagate size adjustments.
     */
    void treeSizeChanged() {
        treeSize = root.size();
    }

    /**
     * Gets the real index of an element given the black index or white index.
     */
    public int getIndex(int colourIndex, Object colour) {
        // Get the real index of a WHITE element
        if(colour == WHITE) {
            // There are no black elements
            if(root == null) {
                return colourIndex;

            // Retrieving from the trailing whitespace with a tree
            } else if(colourIndex >= root.whiteSize()) {
                return colourIndex - root.whiteSize() + treeSize;

            // The index maps to an element in the tree
            } else {
                return root.getIndexByWhiteIndex(colourIndex);
            }

        // Get the real index of a BLACK element
        } else {
            return root.getIndexByBlackIndex(colourIndex);

        }
    }

    /**
     * Gets the colour-based index of the element with the given real
     * index.
     *
     * @param index the real index.
     * @param colour the colour to retrieve the colour-based index for.
     *
     * @return The colour-based index of the element at index or -1 if that
     *         element does not match the given colour.
     */
    public int getColourIndex(int index, Object colour) {
        if(colour == WHITE) return getWhiteIndex(index);
        else return getBlackIndex(index);
    }

    /**
     * Gets the white index of the node with the given real
     * index.
     *
     * @param index specifies the real index.
     *
     * @return The white index of the element at index or -1 if that element is BLACK.
     */
    public int getWhiteIndex(int index) {
        // Get a white index from the list
        if(root != null && index < treeSize) return root.getWhiteIndex(index);

        // There are only white indexes in the trailing whitespace
        else {
            if(root != null) return index - treeSize + root.whiteSize();
            else return index;
        }
    }

    /**
     * Gets the black index of the node with the given real
     * index.
     *
     * @param index specifies the real index.
     *
     * @return The black index of the element at index or -1 if that element is WHITE.
     */
    public int getBlackIndex(int index) {
        if(root != null && index < treeSize) return root.getBlackIndex(index);
        else return -1;
    }

    /**
     * Gets the colour-based index of the element with the given real index or
     * the colour-based index of the previous or next element matching the given
     * colour if that element is of the opposite colour.
     *
     * @param left true for opposite colour elements to return the colour-based
     *      index of the first matching element before it in the list. Such
     *      values will range from <code>-1</code> through <code>size()-1</code>.
     *      False for opposite colour elements to return the colour-based index
     *      of the first matching element after it in the list. Such values will
     *      range from <code>0</code> through <code>size()</code>.
     */
    public int getColourIndex(int index, boolean left, Object colour) {
        if(colour == WHITE) return getWhiteIndex(index, left);
        else return getBlackIndex(index, left);
    }

    /**
     * Gets the white index of the element with the given real index or
     * the white index of the previous or next WHITE element if that element
     * is BLACK.
     *
     * @param left true for BLACK elements to return the white index of the
     *      first WHITE element before it in the list. Such values will range
     *      from <code>-1</code> through <code>size()-1</code>. False for BLACK
     *      elements to return the white index of the first WHITE element after
     *      it in the list. Such values will range from <code>0</code> through
     *      <code>size()</code>.
     */
    public int getWhiteIndex(int index, boolean left) {
        if(root == null) return index;
        else if(index >= treeSize) return index - treeSize + root.whiteSize();
        else return root.getWhiteIndex(index, left);
    }

    /**
     * Gets the black index of the element with the given real index or
     * the black index of the previous or next BLACK element if that element
     * is WHITE.
     *
     * @param left true for WHITE elements to return the black index of the
     *      first BLACK element before it in the list. Such values will range
     *      from <code>-1</code> through <code>size()-1</code>. False for WHITE
     *      elements to return the black index of the first BLACK element after
     *      it in the list. Such values will range from <code>0</code> through
     *      <code>size()</code>.
     */
    public int getBlackIndex(int index, boolean left) {
        // there is no tree
        if(root == null) {
            if(left) return -1;
            else return 0;

        // if it is beyond the tree
        } else if(index >= treeSize) {
            if(left) return root.blackSize() - 1;
            return root.blackSize();

        // get from the tree
        } else {
            return root.getBlackIndex(index, left);
        }
    }

    /**
     * Gets the index of the WHITE element at whiteIndex relative to the WHITE
     * element after the previous BLACK element or the start of the list if no
     * BLACK element exists before this node.
     */
    public int getWhiteSequenceIndex(int whiteIndex) {
        // There is no tree sequence is beyond the tree
        if(root == null) {
            return whiteIndex;

        // The sequence is beyond the tree
        } else if(whiteIndex >= root.whiteSize()) {
            return whiteIndex - root.whiteSize();

        // lookup the sequence index within the tree
        } else {
            return root.getWhiteSequenceIndex(whiteIndex);
        }
    }

    /**
     * This method exists for CollectionList which needs a way to call
     * getBlackIndex(index, true) with a white-centric index.
     */
    public int getBlackBeforeWhite(int whiteIndex) {
        // there is no tree
        if(root == null) {
            return -1;

        // starting from beyond the tree
        } else if(whiteIndex >= root.whiteSize()) {
            return root.blackSize() - 1;

        // the index is from within the tree
        } else {
            return root.getBlackBeforeWhite(whiteIndex);
        }
    }

    /**
     * Finds a sequence of the given colour that is at least size elements
     * in length.
     *
     * @param size the minimum size of a matching sequence.
     *
     * @return The natural index of the first element in the sequence or -1 if
     *         no sequences of that length exist.
     */
    public int findSequenceOfMinimumSize(int size, Object colour) {
        // there is no tree
        if(root == null) {
            // There are no black sequences
            if(colour == BLACK) return -1;

            // The trailing whitespace matches
            else if(whiteSpace >= size) return 0;

            // nothing matches
            else return -1;

        // focus only within the tree
        } else if(colour == BLACK) {
            return root.findSequenceOfMinimumSize(size, colour);

        // check the tree first, if it fails check the trailing whitespace
        } else {
            int result = root.findSequenceOfMinimumSize(size, colour);
            if(result == -1 && whiteSpace >= size) result = treeSize;
            return result;
        }
    }

    /**
     * Provides a specialized {@link Iterator} that iterates over a
     * {@link Barcode} to provide high performance access to {@link Barcode}
     * functionality.
     */
    public BarcodeIterator iterator() {
        return new BarcodeIterator(this);
    }

    public String toString() {
        StringBuffer result = new StringBuffer();
        for(BarcodeIterator bi = iterator(); bi.hasNext(); ) {
            result.append(bi.next() == Barcode.BLACK ? "X" : "_");
        }
        return result.toString();
    }
}