Coverage Report - com.sun.javafx.runtime.sequence.AbstractSequence
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSequence
98%
63/64
89%
25/28
0
AbstractSequence$1
71%
5/7
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.Iterator;
 30  
 import java.util.Formattable;
 31  
 import java.util.Formatter;
 32  
 import java.util.NoSuchElementException;
 33  
 import com.sun.javafx.runtime.sequence.SequenceMutator.Listener;
 34  
 
 35  
 /**
 36  
  * Abstract base class for sequence classes.  A subclass need only define the size() and get() methods; subclasses
 37  
  * may also want to provide optimized versions of some other methods, such as toArray() or getBits().  Subclasses that
 38  
  * represent views onto other sequences should also implement the getDepth() method.
 39  
  *
 40  
  * @author Brian Goetz
 41  
  */
 42  
 public abstract class AbstractSequence<T> implements Sequence<T>, Formattable {
 43  
     protected final Class<T> clazz;
 44  
 
 45  25869
     protected AbstractSequence(Class<T> clazz) {
 46  25869
         this.clazz = clazz;
 47  25869
     }
 48  
 
 49  
     public abstract int size();
 50  
 
 51  
     public abstract T get(int position);
 52  
 
 53  
     public Sequence<T> getSlice(int startPos, int endPos) {
 54  91
         return Sequences.subsequence(this, startPos, endPos+1);
 55  
     }
 56  
 
 57  
     public BitSet getBits(SequencePredicate<? super T> predicate) {
 58  4661
         int length = size();
 59  4661
         BitSet bits = new BitSet(length);
 60  10669
         for (int i = 0; i < length; i++)
 61  6008
             if (predicate.matches(this, i, get(i)))
 62  1624
                 bits.set(i);
 63  4661
         return bits;
 64  
     }
 65  
 
 66  
     public Class<T> getElementType() {
 67  49023
         return clazz;
 68  
     }
 69  
 
 70  
     public boolean isEmpty() {
 71  4255
         return (size() == 0);
 72  
     }
 73  
 
 74  
     public int getDepth() {
 75  9419
         return 0;
 76  
     }
 77  
     
 78  
     public <V> Sequence<V> map(Class<V> clazz, SequenceMapper<T, V> sequenceMapper) {
 79  1
         return Sequences.map(clazz, this, sequenceMapper);
 80  
     }
 81  
 
 82  
     public void toArray(Object[] array, int destOffset) {
 83  206435
         for (int i = 0; i < size(); i++)
 84  199967
             array[i + destOffset] = get(i);
 85  6468
     }
 86  
 
 87  
     public Sequence<T> get(SequencePredicate<? super T> predicate) {
 88  4578
         return Sequences.filter(this, getBits(predicate));
 89  
     }
 90  
 
 91  
 
 92  
     public Iterator<T> iterator() {
 93  11888
         return new Iterator<T>() {
 94  11888
             private int next = 0;
 95  
 
 96  
             public boolean hasNext() {
 97  998946
                 return next < size();
 98  
             }
 99  
 
 100  
             public T next() {
 101  1235120
                 if (next >= size())
 102  0
                     throw new NoSuchElementException();
 103  
                 else
 104  1235120
                     return get(next++);
 105  
             }
 106  
 
 107  
             public void remove() {
 108  0
                 throw new UnsupportedOperationException();
 109  
             }
 110  
         };
 111  
     }
 112  
 
 113  
     @Override
 114  
     @SuppressWarnings("unchecked")
 115  
     public boolean equals(Object obj) {
 116  7976
         if (!(obj instanceof Sequence))
 117  0
             return false;
 118  7976
         Sequence other = (Sequence) obj;
 119  7976
         return (other.getElementType().isAssignableFrom(getElementType())) && Sequences.isEqual(this, (Sequence<T>) other);
 120  
     }
 121  
 
 122  
     @Override
 123  
     public int hashCode() {
 124  1027
         int hash = 0;
 125  1386
         for (int i = 0; i < size(); i++) {
 126  359
             T val = get(i);
 127  359
             hash = 31 * hash + (val != null ? val.hashCode() : 0);
 128  
         }
 129  1027
         return hash;
 130  
     }
 131  
 
 132  
 
 133  
     @Override
 134  
     public String toString() {
 135  3022
         if (isEmpty())
 136  775
             return "[ ]";
 137  2247
         StringBuilder sb = new StringBuilder();
 138  2247
         sb.append("[ ");
 139  15004
         for (int i = 0; i < size(); i++) {
 140  12757
             if (i > 0)
 141  10510
                 sb.append(", ");
 142  12757
             sb.append(get(i));
 143  
         }
 144  2247
         sb.append(" ]");
 145  2247
         return sb.toString();
 146  
     }
 147  
 
 148  
 
 149  
     public Sequence<T> subsequence(int start, int end) {
 150  4656
         return Sequences.subsequence(this, start, end);
 151  
     }
 152  
 
 153  
     public Sequence<T> reverse() {
 154  42
         return Sequences.reverse(this);
 155  
     }
 156  
 
 157  
     public Sequence<T> flatten() {
 158  4491
         if (getDepth() == 0)
 159  3016
             return this;
 160  
         else {
 161  1475
             SequenceBuilder<T> sb = new SequenceBuilder<T>(getElementType(), size());
 162  1475
             sb.add(this);
 163  1475
             return sb.toSequence();
 164  
         }
 165  
     }
 166  
 
 167  
     public final Sequence<T> set(int position, T value) {
 168  16
         return SequenceMutator.set(this, null, position, value);
 169  
     }
 170  
 
 171  
     public Sequence<T> replaceSlice(int startPos, int endPos, Sequence<? extends T> newValues) {
 172  997
         return SequenceMutator.replaceSlice(this, null, startPos, endPos, newValues);
 173  
     }
 174  
 
 175  
     public final Sequence<T> delete(int position) {
 176  107
         return SequenceMutator.delete(this, null, position);
 177  
     }
 178  
 
 179  
     public final Sequence<T> delete(SequencePredicate<? super T> predicate) {
 180  129
         return SequenceMutator.delete(this, (Listener<T>) null, predicate);
 181  
     }
 182  
 
 183  
     public final Sequence<T> insert(T value) {
 184  82
         return SequenceMutator.insert(this, (Listener<T>) null, value);
 185  
     }
 186  
 
 187  
     public final Sequence<T> insert(Sequence<? extends T> values) {
 188  31
         return SequenceMutator.insert(this, (Listener<T>) null, values);
 189  
     }
 190  
 
 191  
     public final Sequence<T> insertFirst(T value) {
 192  32
         return SequenceMutator.insertFirst(this, null, value);
 193  
     }
 194  
 
 195  
     public final Sequence<T> insertFirst(Sequence<? extends T> values) {
 196  31
         return SequenceMutator.insertFirst(this, (Listener<T>) null, values);
 197  
     }
 198  
 
 199  
     public final Sequence<T> insertBefore(T value, int position) {
 200  29
         return SequenceMutator.insertBefore(this, null, value, position);
 201  
     }
 202  
 
 203  
     public final Sequence<T> insertBefore(Sequence<? extends T> values, int position) {
 204  24
         return SequenceMutator.<T>insertBefore(this, null, values, position);
 205  
     }
 206  
 
 207  
     public final Sequence<T> insertAfter(T value, int position) {
 208  29
         return SequenceMutator.insertAfter(this, null, value, position);
 209  
     }
 210  
 
 211  
     public final Sequence<T> insertAfter(Sequence<? extends T> values, int position) {
 212  24
         return SequenceMutator.<T>insertAfter(this, null, values, position);
 213  
     }
 214  
 
 215  
     public final Sequence<T> insertBefore(T value, SequencePredicate<? super T> predicate) {
 216  37
         return SequenceMutator.insertBefore(this, null, value, predicate);
 217  
     }
 218  
 
 219  
     public final Sequence<T> insertBefore(Sequence<? extends T> values, SequencePredicate<? super T> predicate) {
 220  32
         return SequenceMutator.insertBefore(this, null, values, predicate);
 221  
     }
 222  
 
 223  
     public final Sequence<T> insertAfter(T value, SequencePredicate<? super T> predicate) {
 224  37
         return SequenceMutator.insertAfter(this, null, value, predicate);
 225  
     }
 226  
 
 227  
     public final Sequence<T> insertAfter(Sequence<? extends T> values, SequencePredicate<? super T> predicate) {
 228  32
         return SequenceMutator.insertAfter(this, null, values, predicate);
 229  
     }
 230  
 
 231  
     
 232  
     // Allow sequences to be formatted - toString() is just for debugging
 233  
     // i.e
 234  
     // var seq = [1, 2];
 235  
     // for (i in seq) "{%d i}"
 236  
     // should yield: "12"
 237  
     // not: "[1, 2]"
 238  
     public void formatTo(Formatter formatter,
 239  
                          int flags,
 240  
                          int width, 
 241  
                          int precision) {
 242  
         // TBD handle flags, width, and precision
 243  758
         for (int i = 0; i < size(); i++) {
 244  593
             formatter.format("%s", get(i));
 245  
         }
 246  165
     }
 247  
 }