Coverage Report - com.sun.javafx.runtime.sequence.DumbMutableSequence
 
Classes in this File Line Coverage Branch Coverage Complexity
DumbMutableSequence
81%
44/54
65%
17/26
0
DumbMutableSequence$1
0%
0/7
0%
0/4
0
 
 1  
 package com.sun.javafx.runtime.sequence;
 2  
 
 3  
 import java.util.Arrays;
 4  
 import java.util.Iterator;
 5  
 import java.util.NoSuchElementException;
 6  
 
 7  
 import com.sun.javafx.runtime.Util;
 8  
 
 9  
 /**
 10  
  * Relatively inefficient implementation of a mutable sequence with a slice-replace operation.  This is used internally
 11  
  * in sequence binding implementations.
 12  
  *
 13  
  * @author Brian Goetz
 14  
  */
 15  0
 public class DumbMutableSequence<T> implements Iterable<T> {
 16  
     private T[] array;
 17  
     private int size;
 18  
 
 19  
     public DumbMutableSequence(T[] initialValues) {
 20  0
         this(initialValues.length);
 21  0
         System.arraycopy(initialValues, 0, array, 0, initialValues.length);
 22  0
         size = initialValues.length;
 23  0
     }
 24  
 
 25  49
     public DumbMutableSequence(int initialSize) {
 26  49
         this.array = Util.<T>newObjectArray(Util.powerOfTwo(1, initialSize));
 27  49
         size = 0;
 28  49
     }
 29  
 
 30  
     public DumbMutableSequence() {
 31  4
         this(8);
 32  4
     }
 33  
 
 34  
     public T get(int i) {
 35  48
         return (i < 0 || i >= size)
 36  
                 ? null
 37  
                 : array[i];
 38  
     }
 39  
 
 40  
     public void set(int i, T value) {
 41  5
         if (i < 0 && i > size)
 42  0
             throw new IndexOutOfBoundsException(Integer.toString(i));
 43  5
         if (i == size && size + 1 < array.length) {
 44  1
             T[] temp = Util.<T>newObjectArray(Util.powerOfTwo(size, size + 1));
 45  1
             System.arraycopy(array, 0, temp, 0, size);
 46  1
             array[size++] = value;
 47  1
         }
 48  
         else
 49  4
             array[i] = value;
 50  5
     }
 51  
 
 52  
     public void replaceSlice(int startPos, int endPos, T[] newElements) {
 53  346
         int insertedCount = newElements.length;
 54  346
         int deletedCount = endPos - startPos + 1;
 55  346
         int netAdded = insertedCount - deletedCount;
 56  346
         if (netAdded == 0)
 57  0
             System.arraycopy(newElements, 0, array, startPos, insertedCount);
 58  346
         else if (size + netAdded < array.length) {
 59  322
             System.arraycopy(array, endPos + 1, array, endPos + 1 + netAdded, size - (endPos + 1));
 60  322
             System.arraycopy(newElements, 0, array, startPos, insertedCount);
 61  322
             if (netAdded < 0)
 62  53
                 Arrays.fill(array, size + netAdded, size, null);
 63  322
             size += netAdded;
 64  
         }
 65  
         else {
 66  24
             int newSize = size + netAdded;
 67  24
             T[] temp = Util.<T>newObjectArray(Util.powerOfTwo(size, newSize));
 68  24
             System.arraycopy(array, 0, temp, 0, startPos);
 69  24
             System.arraycopy(newElements, 0, temp, startPos, insertedCount);
 70  24
             System.arraycopy(array, endPos + 1, temp, startPos + insertedCount, size - (endPos + 1));
 71  24
             array = temp;
 72  24
             size = newSize;
 73  
         }
 74  346
     }
 75  
 
 76  
     public Sequence<? extends T> replaceSlice(int startPos, int endPos, Sequence<? extends T> newElements) {
 77  249
         T[] temp = Util.<T>newObjectArray(Sequences.size(newElements));
 78  249
         newElements.toArray(temp, 0);
 79  249
         replaceSlice(startPos, endPos, temp);
 80  249
         return newElements;
 81  
     }
 82  
 
 83  
     public Sequence<T> get(Class<T> clazz) {
 84  252
         return Sequences.make(clazz, array, size);
 85  
     }
 86  
 
 87  
     public int size() {
 88  319
         return size;
 89  
     }
 90  
 
 91  
     public Iterator<T> iterator() {
 92  0
         return new Iterator<T>() {
 93  0
             private int index = 0;
 94  
 
 95  
             public boolean hasNext() {
 96  0
                 return index < size;
 97  
             }
 98  
 
 99  
             public T next() {
 100  0
                 if (hasNext())
 101  0
                     return array[index++];
 102  
                 else
 103  0
                     throw new NoSuchElementException();
 104  
             }
 105  
 
 106  
             public void remove() {
 107  0
                 throw new UnsupportedOperationException();
 108  
             }
 109  
         };
 110  
     }
 111  
 
 112  
     void testValid() {
 113  20787
         for (int i = 0; i < size; i++)
 114  20538
             if (array[i] == null)
 115  0
                 throw new AssertionError("Null element at " + i);
 116  8311
         for (int i = size; i < array.length; i++)
 117  8062
             if (array[i] != null)
 118  0
                 throw new AssertionError("Non-null element at " + i);
 119  249
     }
 120  
 }