Coverage Report - com.sun.javafx.runtime.sequence.SequenceBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SequenceBuilder
75%
21/28
50%
4/8
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  
 
 30  
 /**
 31  
  * Helper classes for building sequences, much like StringBuilder assists in building Strings.  SequenceBuilder
 32  
  * stores the sequence building built in an array, which is automatically resized as needed.  It can be converted
 33  
  * to a Sequence by calling toSequence().
 34  
  *
 35  
  * @author Brian Goetz
 36  
  */
 37  
 public class SequenceBuilder<T> {
 38  
     private final static int DEFAULT_SIZE = 16;
 39  
 
 40  
     private final Class clazz;
 41  
     private T[] array;
 42  
     private int size;
 43  
 
 44  
     /** Create a SequenceBuilder for a Sequence of type T */
 45  
     public SequenceBuilder(Class clazz) {
 46  2901
         this(clazz, DEFAULT_SIZE);
 47  2901
     }
 48  
 
 49  
     /** Create a SequenceBuilder for a Sequence of type T, ensuring that there is initially room for at least
 50  
      * initialSize elements. */
 51  4378
     public SequenceBuilder(Class clazz, int initialSize) {
 52  4378
         this.clazz = clazz;
 53  4378
         array = Util.<T>newObjectArray(Util.powerOfTwo(1, initialSize));
 54  4378
     }
 55  
 
 56  
     private void ensureSize(int newSize) {
 57  13131
         if (array.length < newSize) {
 58  171
             int newCapacity = Util.powerOfTwo(array.length, newSize);
 59  171
             T[] newArray = Util.<T>newObjectArray(newCapacity);
 60  171
             System.arraycopy(array, 0, newArray, 0, size);
 61  171
             array = newArray;
 62  
         }
 63  13131
     }
 64  
 
 65  
     /** Add a single element to the sequence */
 66  
     public void add(T element) {
 67  11092
         if (element != null) {
 68  11090
             ensureSize(size + 1);
 69  11090
             array[size++] = element;
 70  
         }
 71  11092
     }
 72  
 
 73  
     /** Add the contents of an existing sequence to the sequence */
 74  
     public void add(Sequence<? extends T> elements) {
 75  2041
         ensureSize(size + elements.size());
 76  2041
         elements.toArray(array, size);
 77  2041
         size += elements.size();
 78  2041
     }
 79  
 
 80  
     /** Get the current size of the sequence being constructed */
 81  
     public int size() {
 82  0
         return size;
 83  
     }
 84  
 
 85  
     /** Get the nth element of the sequence being constructed, returning the null sequence value if n is out of range */
 86  
     public T get(int n) {
 87  0
         if (n < 0 || n >= size)
 88  0
             throw new IndexOutOfBoundsException(Integer.toString(n));
 89  
         else
 90  0
             return array[n];
 91  
     }
 92  
 
 93  
     /** Erase the current contents of the SequenceBuilder */
 94  
     public void clear() {
 95  0
         array = Util.<T>newObjectArray(Util.powerOfTwo(1, DEFAULT_SIZE));
 96  0
         size = 0;
 97  0
     }
 98  
 
 99  
     /** Convert the SequenceBuilder to a Sequence.  The elements will be copied to a new sequence, and will remain
 100  
      * in the SequenceBuilder, so it can continue to be used to make more sequences */
 101  
     public Sequence<T> toSequence() {
 102  
         // OPT: This causes the array to be copied; we can do the same trick as in StringBuilder to transfer
 103  
         // ownership of the array instead and avoid the copy
 104  4378
         return Sequences.make(clazz, array, size);
 105  
     }
 106  
 }