Coverage Report - org.apache.commons.collections.primitives.RandomAccessLongList
 
Classes in this File Line Coverage Branch Coverage Complexity
RandomAccessLongList
100%
55/55
100%
26/26
2.119
RandomAccessLongList$ComodChecker
100%
12/12
100%
2/2
2.119
RandomAccessLongList$RandomAccessLongListIterator
100%
52/52
100%
18/18
2.119
RandomAccessLongList$RandomAccessLongSubList
100%
47/47
100%
14/14
2.119
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.collections.primitives;
 18  
 
 19  
 import java.util.ConcurrentModificationException;
 20  
 import java.util.NoSuchElementException;
 21  
 
 22  
 /**
 23  
  * Abstract base class for {@link LongList}s backed 
 24  
  * by random access structures like arrays.
 25  
  * <p />
 26  
  * Read-only subclasses must override {@link #get}
 27  
  * and {@link #size}.  Mutable subclasses
 28  
  * should also override {@link #set}.  Variably-sized
 29  
  * subclasses should also override {@link #add(long)} 
 30  
  * and {@link #removeElementAt}.  All other methods
 31  
  * have at least some base implementation derived from 
 32  
  * these.  Subclasses may choose to override these methods
 33  
  * to provide a more efficient implementation.
 34  
  * 
 35  
  * @since Commons Primitives 0.1
 36  
  * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $
 37  
  * 
 38  
  * @author Rodney Waldhoff 
 39  
  */
 40  
 public abstract class RandomAccessLongList extends AbstractLongCollection implements LongList {
 41  
 
 42  
     // constructors
 43  
     //-------------------------------------------------------------------------
 44  
 
 45  
     /** Constructs an empty list. */
 46  1354
     protected RandomAccessLongList() { 
 47  1354
     }    
 48  
 
 49  
     // fully abstract methods
 50  
     //-------------------------------------------------------------------------
 51  
     
 52  
     public abstract long get(int index);
 53  
     public abstract int size();
 54  
 
 55  
     // unsupported in base
 56  
     //-------------------------------------------------------------------------
 57  
     
 58  
     /** 
 59  
      * Unsupported in this implementation. 
 60  
      * @throws UnsupportedOperationException since this method is not supported
 61  
      */
 62  
     public long removeElementAt(int index) {
 63  1
         throw new UnsupportedOperationException();
 64  
     }
 65  
     
 66  
     /** 
 67  
      * Unsupported in this implementation. 
 68  
      * @throws UnsupportedOperationException since this method is not supported
 69  
      */
 70  
     public long set(int index, long element) {
 71  2
         throw new UnsupportedOperationException();
 72  
     }
 73  
         
 74  
     /** 
 75  
      * Unsupported in this implementation. 
 76  
      * @throws UnsupportedOperationException since this method is not supported
 77  
      */
 78  
     public void add(int index, long element) {
 79  2
         throw new UnsupportedOperationException();
 80  
     }
 81  
 
 82  
     //-------------------------------------------------------------------------
 83  
 
 84  
     // javadocs here are inherited
 85  
     
 86  
     public boolean add(long element) {
 87  10716
         add(size(),element);
 88  10712
         return true;
 89  
     }
 90  
 
 91  
     public boolean addAll(int index, LongCollection collection) {
 92  4
         boolean modified = false;
 93  4
         for(LongIterator iter = collection.iterator(); iter.hasNext(); ) {
 94  52
             add(index++,iter.next());
 95  52
             modified = true;
 96  
         }
 97  4
         return modified;
 98  
     }
 99  
 
 100  
     public int indexOf(long element) {
 101  148
         int i = 0;
 102  148
         for(LongIterator iter = iterator(); iter.hasNext(); ) {
 103  1741
             if(iter.next() == element) { 
 104  74
                 return i;
 105  
             } else {
 106  1667
                 i++;
 107  
             }
 108  
         }
 109  74
         return -1;
 110  
     }
 111  
 
 112  
     public int lastIndexOf(long element) {
 113  148
         for(LongListIterator iter = listIterator(size()); iter.hasPrevious(); ) {
 114  1741
             if(iter.previous() == element) {
 115  74
                 return iter.nextIndex();
 116  
             }
 117  
         }
 118  74
         return -1;
 119  
     }
 120  
 
 121  
     public LongIterator iterator() {
 122  12257
         return listIterator();
 123  
     }
 124  
 
 125  
     public LongListIterator listIterator() {
 126  14508
         return listIterator(0);
 127  
     }
 128  
 
 129  
     public LongListIterator listIterator(int index) {
 130  14911
         return new RandomAccessLongListIterator(this,index);            
 131  
     }
 132  
 
 133  
     public LongList subList(int fromIndex, int toIndex) {
 134  344
         return new RandomAccessLongSubList(this,fromIndex,toIndex);
 135  
     }
 136  
 
 137  
     public boolean equals(Object that) {
 138  63
         if(this == that) { 
 139  6
             return true; 
 140  57
         } else if(that instanceof LongList) {
 141  51
             LongList thatList = (LongList)that;
 142  51
             if(size() != thatList.size()) {
 143  8
                 return false;
 144  
             }
 145  43
             for(LongIterator thatIter = thatList.iterator(), thisIter = iterator(); thisIter.hasNext();) {
 146  230
                 if(thisIter.next() != thatIter.next()) { 
 147  4
                     return false; 
 148  
                 }
 149  
             }
 150  39
             return true;
 151  
         } else {
 152  6
             return false;
 153  
         }        
 154  
     }
 155  
     
 156  
     public int hashCode() {
 157  2103
         int hash = 1;
 158  2103
         for(LongIterator iter = iterator(); iter.hasNext(); ) {
 159  29317
                 long val = iter.next();
 160  29317
             hash = 31*hash + ((int)(val ^ (val >>> 32)));
 161  29317
         }
 162  2103
         return hash;
 163  
     }
 164  
     
 165  
     public String toString() {
 166  10
         StringBuffer buf = new StringBuffer();
 167  10
         buf.append("[");
 168  10
         for(LongIterator iter = iterator(); iter.hasNext();) {
 169  74
             buf.append(iter.next());
 170  74
             if(iter.hasNext()) {
 171  69
                 buf.append(", ");
 172  
             }
 173  
         }
 174  10
         buf.append("]");
 175  10
         return buf.toString();
 176  
     }
 177  
     
 178  
     // protected utilities
 179  
     //-------------------------------------------------------------------------
 180  
     
 181  
     /** Get my count of structural modifications. */
 182  
     protected int getModCount() {
 183  823841
         return _modCount;
 184  
     }
 185  
 
 186  
     /** Increment my count of structural modifications. */
 187  
     protected void incrModCount() {
 188  24545
         _modCount++;
 189  24545
     }
 190  
 
 191  
     // attributes
 192  
     //-------------------------------------------------------------------------
 193  
     
 194  1354
     private int _modCount = 0;
 195  
 
 196  
     // inner classes
 197  
     //-------------------------------------------------------------------------
 198  
     
 199  
     private static class ComodChecker {
 200  15247
         ComodChecker(RandomAccessLongList source) {
 201  15247
             _source = source;  
 202  15247
             resyncModCount();             
 203  15247
         }
 204  
         
 205  
         protected RandomAccessLongList getList() {
 206  1432663
             return _source;
 207  
         }
 208  
         
 209  
         protected void assertNotComodified() throws ConcurrentModificationException {
 210  791849
             if(_expectedModCount != getList().getModCount()) {
 211  2
                 throw new ConcurrentModificationException();
 212  
             }
 213  791847
         }
 214  
             
 215  
         protected void resyncModCount() {
 216  31992
             _expectedModCount = getList().getModCount();
 217  31992
         }
 218  
         
 219  15247
         private RandomAccessLongList _source = null;
 220  15247
         private int _expectedModCount = -1;
 221  
     }
 222  
     
 223  
     protected static class RandomAccessLongListIterator extends ComodChecker implements LongListIterator {
 224  
         RandomAccessLongListIterator(RandomAccessLongList list, int index) {
 225  14911
             super(list);
 226  14911
             if(index < 0 || index > getList().size()) {
 227  16
                 throw new IndexOutOfBoundsException("Index " + index + " not in [0," + getList().size() + ")");
 228  
             } else {
 229  14895
                 _nextIndex = index;
 230  14895
                 resyncModCount();
 231  
             }
 232  14895
         }
 233  
             
 234  
         public boolean hasNext() {
 235  396839
             assertNotComodified();
 236  396839
             return _nextIndex < getList().size();
 237  
         }
 238  
         
 239  
         public boolean hasPrevious() {
 240  6625
             assertNotComodified();
 241  6625
             return _nextIndex > 0;
 242  
         }
 243  
         
 244  
         public int nextIndex() {
 245  2416
             assertNotComodified();
 246  2416
             return _nextIndex;
 247  
         }
 248  
         
 249  
         public int previousIndex() {
 250  2342
             assertNotComodified();
 251  2342
             return _nextIndex - 1;
 252  
         }
 253  
         
 254  
         public long next() {
 255  193127
             assertNotComodified();
 256  193125
             if(!hasNext()) {
 257  120
                 throw new NoSuchElementException();
 258  
             } else {
 259  193005
                 long val = getList().get(_nextIndex);
 260  193005
                 _lastReturnedIndex = _nextIndex;
 261  193005
                 _nextIndex++;
 262  193005
                 return val;
 263  
             }
 264  
         }
 265  
         
 266  
         public long previous() {
 267  3355
             assertNotComodified();
 268  3355
             if(!hasPrevious()) {
 269  175
                 throw new NoSuchElementException();
 270  
             } else {
 271  3180
                 long val = getList().get(_nextIndex-1);
 272  3180
                 _lastReturnedIndex = _nextIndex-1;
 273  3180
                 _nextIndex--;
 274  3180
                 return val;
 275  
             }
 276  
         }
 277  
         
 278  
         public void add(long value) {
 279  256
             assertNotComodified();
 280  256
             getList().add(_nextIndex,value);
 281  256
             _nextIndex++;
 282  256
             _lastReturnedIndex = -1;
 283  256
             resyncModCount();
 284  256
         }
 285  
     
 286  
         public void remove() {
 287  574
             assertNotComodified();
 288  574
             if (_lastReturnedIndex == -1) {
 289  38
                 throw new IllegalStateException();
 290  
             }
 291  536
             if (_lastReturnedIndex == _nextIndex) {
 292  
                 // remove() following previous()
 293  5
                 getList().removeElementAt(_lastReturnedIndex);
 294  
             } else {
 295  
                 // remove() following next()
 296  531
                 getList().removeElementAt(_lastReturnedIndex);
 297  531
                 _nextIndex--;
 298  
             }
 299  536
             _lastReturnedIndex = -1;
 300  536
             resyncModCount();
 301  536
         }
 302  
         
 303  
         public void set(long value) {
 304  108
             assertNotComodified();
 305  108
             if(-1 == _lastReturnedIndex) {
 306  22
                 throw new IllegalStateException();
 307  
             } else {
 308  86
                 getList().set(_lastReturnedIndex,value);
 309  86
                 resyncModCount();
 310  
             }
 311  86
         }
 312  
         
 313  14911
         private int _nextIndex = 0;
 314  14911
         private int _lastReturnedIndex = -1;        
 315  
     }   
 316  
 
 317  
     protected static class RandomAccessLongSubList extends RandomAccessLongList implements LongList {
 318  344
         RandomAccessLongSubList(RandomAccessLongList list, int fromIndex, int toIndex) {
 319  344
             if(fromIndex < 0 || toIndex > list.size()) {
 320  6
                 throw new IndexOutOfBoundsException();
 321  338
             } else if(fromIndex > toIndex) {
 322  2
                 throw new IllegalArgumentException();                
 323  
             } else {
 324  336
                 _list = list;
 325  336
                 _offset = fromIndex;
 326  336
                 _limit = toIndex - fromIndex;
 327  336
                 _comod = new ComodChecker(list);
 328  336
                 _comod.resyncModCount();
 329  
             }            
 330  336
         }
 331  
     
 332  
         public long get(int index) {
 333  47355
             checkRange(index);
 334  47337
             _comod.assertNotComodified();
 335  47337
             return _list.get(toUnderlyingIndex(index));
 336  
         }
 337  
     
 338  
         public long removeElementAt(int index) {
 339  334
             checkRange(index);
 340  316
             _comod.assertNotComodified();
 341  316
             long val = _list.removeElementAt(toUnderlyingIndex(index));
 342  316
             _limit--;
 343  316
             _comod.resyncModCount();
 344  316
             incrModCount();
 345  316
             return val;
 346  
         }
 347  
     
 348  
         public long set(int index, long element) {
 349  74
             checkRange(index);
 350  56
             _comod.assertNotComodified();
 351  56
             long val = _list.set(toUnderlyingIndex(index),element);
 352  56
             incrModCount();
 353  56
             _comod.resyncModCount();
 354  56
             return val;
 355  
         }
 356  
     
 357  
         public void add(int index, long element) {
 358  280
             checkRangeIncludingEndpoint(index);
 359  264
             _comod.assertNotComodified();
 360  264
              _list.add(toUnderlyingIndex(index),element);
 361  264
             _limit++;
 362  264
             _comod.resyncModCount();
 363  264
             incrModCount();
 364  264
         }
 365  
     
 366  
         public int size() {
 367  138234
             _comod.assertNotComodified();
 368  138234
             return _limit;
 369  
         }
 370  
     
 371  
         private void checkRange(int index) {
 372  47763
             if(index < 0 || index >= size()) {
 373  54
                 throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + ")");
 374  
             }
 375  47709
         }
 376  
           
 377  
         private void checkRangeIncludingEndpoint(int index) {
 378  280
             if(index < 0 || index > size()) {
 379  16
                 throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + "]");
 380  
             }
 381  264
         }
 382  
           
 383  
         private int toUnderlyingIndex(int index) {
 384  47973
             return (index + _offset);
 385  
         }
 386  
         
 387  344
         private int _offset = 0;        
 388  344
         private int _limit = 0; 
 389  344
         private RandomAccessLongList _list = null;
 390  344
         private ComodChecker _comod = null;
 391  
     
 392  
     }
 393  
 }
 394