Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BoundSequenceBuilder |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 | * | |
5 | * This code is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 only, as | |
7 | * published by the Free Software Foundation. Sun designates this | |
8 | * particular file as subject to the "Classpath" exception as provided | |
9 | * by Sun in the LICENSE file that accompanied this code. | |
10 | * | |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * version 2 for more details (a copy is included in the LICENSE file that | |
15 | * accompanied this code). | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License version | |
18 | * 2 along with this work; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | * | |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
23 | * have any questions. | |
24 | */ | |
25 | ||
26 | package com.sun.javafx.runtime.sequence; | |
27 | ||
28 | import com.sun.javafx.runtime.Util; | |
29 | import com.sun.javafx.runtime.location.ObjectLocation; | |
30 | import com.sun.javafx.runtime.location.SequenceLocation; | |
31 | ||
32 | /** | |
33 | * Helper classes for building bound sequences of the form bind [ a, b, c], much like StringBuilder assists in building | |
34 | * Strings. BoundSequenceBuilder stores the locations building built in an array, which is automatically resized as | |
35 | * needed. It can be converted to a Sequence by calling toSequence(). | |
36 | * | |
37 | * @author Brian Goetz | |
38 | */ | |
39 | public class BoundSequenceBuilder<T> { | |
40 | private static final int DEFAULT_SIZE = 8; | |
41 | ||
42 | private final Class<T> clazz; | |
43 | private SequenceLocation<? extends T>[] array; | |
44 | private int size; | |
45 | ||
46 | public BoundSequenceBuilder(Class<T> clazz) { | |
47 | 38 | this(clazz, DEFAULT_SIZE); |
48 | 38 | } |
49 | ||
50 | 38 | public BoundSequenceBuilder(Class<T> clazz, int initialSize) { |
51 | 38 | this.clazz = clazz; |
52 | 38 | array = Util.newSequenceLocationArray(Util.powerOfTwo(1, initialSize)); |
53 | 38 | } |
54 | ||
55 | private void ensureSize(int newSize) { | |
56 | 95 | if (array.length < newSize) { |
57 | 0 | int newCapacity = Util.powerOfTwo(array.length, newSize); |
58 | 0 | SequenceLocation<? extends T>[] newArray = Util.newSequenceLocationArray(newCapacity); |
59 | 0 | System.arraycopy(array, 0, newArray, 0, size); |
60 | 0 | array = newArray; |
61 | } | |
62 | 95 | } |
63 | ||
64 | /** Get the current size of the sequence being constructed */ | |
65 | public int size() { | |
66 | 0 | return size; |
67 | } | |
68 | ||
69 | /** Add an existing SequenceLocation, which will be flattened */ | |
70 | public void add(SequenceLocation<? extends T> seq) { | |
71 | 95 | ensureSize(size + 1); |
72 | 95 | array[size++] = seq; |
73 | 95 | } |
74 | ||
75 | /** Add an instance location to the sequence */ | |
76 | public void add(ObjectLocation<? extends T> singleton) { | |
77 | 77 | add(BoundSequences.singleton(clazz, singleton)); |
78 | 77 | } |
79 | ||
80 | /** Erase the current contents */ | |
81 | public void clear() { | |
82 | 0 | array = Util.newObjectArray(Util.powerOfTwo(1, DEFAULT_SIZE)); |
83 | 0 | size = 0; |
84 | 0 | } |
85 | ||
86 | /** Convert to a SequenceLocation. The elements will be copied to a new sequence, and will remain | |
87 | * in the builder */ | |
88 | public SequenceLocation<T> toSequence() { | |
89 | 38 | return BoundSequences.concatenate(clazz, array, size); |
90 | } | |
91 | } |