Coverage Report - com.sun.javafx.runtime.sequence.CompositeSequence
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeSequence
96%
23/24
86%
12/14
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  
 /**
 29  
  * Intermediate (view) sequence implementation that represents the concatenation of zero or more other sequences of
 30  
  * the same element type.  Concatenating sequences should be done through the Sequences.concatenate() factory,
 31  
  * not through the CompositeSequence constructor.  O(nSeq) space and time construction costs.
 32  
  *
 33  
  * @author Brian Goetz
 34  
  */
 35  
 class CompositeSequence<T> extends AbstractSequence<T> implements Sequence<T> {
 36  
 
 37  
     private final Sequence<? extends T>[] sequences;
 38  
     private final int[] startPositions;
 39  
     private final int size, depth;
 40  
 
 41  
     public CompositeSequence(Class<T> clazz, Sequence<? extends T>... sequences) {
 42  
         // @@@ TODO: Deal with nulls in sequences
 43  2300
         super(clazz);
 44  2300
         this.sequences = sequences.clone();
 45  2300
         this.startPositions = new int[sequences.length];
 46  2300
         int size = 0;
 47  2300
         int depth = 0;
 48  7602
         for (int i = 0, offset = 0; i < sequences.length; i++) {
 49  5302
             Class eClass = sequences[i].getElementType();
 50  5302
             if (!clazz.isAssignableFrom(eClass))
 51  0
                 throw new ClassCastException("cannot cast "+eClass.getName()
 52  
                                              +" segment to "+clazz.getName()+" sequence");
 53  5302
             startPositions[i] = offset;
 54  5302
             size += sequences[i].size();
 55  5302
             offset += sequences[i].size();
 56  5302
             depth = Math.max(depth, sequences[i].getDepth());
 57  
         }
 58  2300
         this.size = size;
 59  2300
         this.depth = depth + 1;
 60  2300
     }
 61  
 
 62  
     @Override
 63  
     public int size() {
 64  650494
         return size;
 65  
     }
 66  
 
 67  
     @Override
 68  
     public int getDepth() {
 69  4608
         return depth;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public T get(int position) {
 74  637975
         if (position < 0 || position >= size)
 75  133
             throw new IndexOutOfBoundsException(Integer.toString(position));
 76  
         // Linear search should be good enough for now
 77  
         // @@@ OPT: cache last chunk accessed, use that as predictive starting point for next get
 78  637842
         int chunk = 0;
 79  
         while (chunk < sequences.length - 1
 80  693716
                 && (position >= startPositions[chunk+1] || sequences[chunk].size() == 0))
 81  55874
             ++chunk;
 82  637842
         return sequences[chunk].get(position - startPositions[chunk]);
 83  
     }
 84  
 }