Coverage Report - com.sun.javafx.runtime.sequence.ArraySequence
 
Classes in this File Line Coverage Branch Coverage Complexity
ArraySequence
88%
37/42
94%
15/16
0
ArraySequence$1
67%
4/6
75%
3/4
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 java.util.BitSet;
 29  
 import java.util.List;
 30  
 import java.util.Iterator;
 31  
 import java.util.NoSuchElementException;
 32  
 
 33  
 import com.sun.javafx.runtime.Util;
 34  
 
 35  
 /**
 36  
  * Sequence implementation class that stores sequence elements in an array.  To create array-based sequences, use
 37  
  * the make() factory methods in Sequences instead of the ArraySequence constructor.   O(n) space and time construction
 38  
  * costs.  
 39  
  *
 40  
  * @author Brian Goetz
 41  
  */
 42  387088
 class ArraySequence<T> extends AbstractSequence<T> implements Sequence<T> {
 43  
 
 44  
     private final T[] array;
 45  
 
 46  
 
 47  
     public ArraySequence(Class<T> clazz, T... values) {
 48  1441
         super(clazz);
 49  1441
         this.array = Util.<T>newObjectArray(values.length);
 50  1441
         System.arraycopy(values, 0, array, 0, values.length);
 51  1441
         checkForNulls();
 52  1441
     }
 53  
 
 54  
     public ArraySequence(Class<T> clazz, T[] values, int size) {
 55  4334
         super(clazz);
 56  4334
         this.array =  Util.<T>newObjectArray(size);
 57  4334
         System.arraycopy(values, 0, array, 0, size);
 58  4334
         checkForNulls();
 59  4334
     }
 60  
 
 61  
     @SuppressWarnings("unchecked")
 62  
     public ArraySequence(Class<T> clazz, List<? extends T> values) {
 63  0
         super(clazz);
 64  0
         this.array = (T[]) values.toArray();
 65  0
         checkForNulls();
 66  0
     }
 67  
 
 68  
     public ArraySequence(Class<T> clazz, Sequence<? extends T>... sequences) {
 69  2380
         super(clazz);
 70  2380
         int size = 0;
 71  8816
         for (Sequence<? extends T> seq : sequences)
 72  6436
             size += seq.size();
 73  2380
         this.array = Util.<T>newObjectArray(size);
 74  2380
         int next = 0;
 75  8816
         for (Sequence<? extends T> seq : sequences) {
 76  6436
             seq.toArray(array, next);
 77  6436
             next += seq.size();
 78  
         }
 79  2380
         checkForNulls();
 80  2380
     }
 81  
 
 82  
     private void checkForNulls() {
 83  254692
         for (T v : array)
 84  246537
             if (v == null)
 85  0
                 throw new IllegalArgumentException("cannot create sequence with null values");
 86  8155
     }
 87  
 
 88  
     @Override
 89  
     public int size() {
 90  153829
         return array.length;
 91  
     }
 92  
 
 93  
     @Override
 94  
     public T get(int position) {
 95  709549
         if (position < 0 || position >= array.length)
 96  327
             throw new IndexOutOfBoundsException(Integer.toString(position));
 97  
         else 
 98  709222
             return array[position];
 99  
     }
 100  
 
 101  
 
 102  
     // optimized versions
 103  
     @Override
 104  
     public BitSet getBits(SequencePredicate<? super T> predicate) {
 105  727
         BitSet bits = new BitSet(array.length);
 106  1988
         for (int i = 0; i < array.length; i++)
 107  1261
             if (predicate.matches(this, i, array[i]))
 108  502
                 bits.set(i);
 109  727
         return bits;
 110  
     }
 111  
 
 112  
     @Override
 113  
     public void toArray(Object[] dest, int destOffset) {
 114  2676
         System.arraycopy(array, 0, dest, destOffset, array.length);
 115  2676
     }
 116  
 
 117  
     @Override
 118  
     public Iterator<T> iterator() {
 119  8937
         return new Iterator<T>() {
 120  
             int index;
 121  
 
 122  
             public boolean hasNext() {
 123  231431
                 return index < array.length;
 124  
             }
 125  
 
 126  
             public T next() {
 127  155657
                 if (hasNext())
 128  155657
                     return array[index++];
 129  
                 else
 130  0
                     throw new NoSuchElementException();
 131  
             }
 132  
 
 133  
             public void remove() {
 134  0
                 throw new UnsupportedOperationException();
 135  
             }
 136  
         };
 137  
     }
 138  
 }