|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SkipFirstIterable.java | 10% | 28% | 30.8% | 25% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.iter; | |
| 36 | ||
| 37 | import java.util.Iterator; | |
| 38 | import java.io.Serializable; | |
| 39 | import edu.rice.cs.plt.object.Composite; | |
| 40 | import edu.rice.cs.plt.object.ObjectUtil; | |
| 41 | ||
| 42 | /** | |
| 43 | * Contains all but the first element of a wrapped iterable. (If the wrapped iterable is | |
| 44 | * empty, this is empty as well.) Changes made to the underlying list are reflected here. | |
| 45 | * This provides a general, but clumsy, way to decompose arbitrary iterables (that is, access | |
| 46 | * the "rest" of some iterable). Care should be taken in using this approach, however, as the | |
| 47 | * first value is skipped on <em>every</em> invocation of {@code iterator}. Thus, an iterable | |
| 48 | * composed of multiple nested {@code SkipFirstIterable}s will have poor performance in | |
| 49 | * comparison to other solutions. For better performance or recursive list-decomposing | |
| 50 | * algorithms, use a {@link edu.rice.cs.plt.collect.ConsList}. | |
| 51 | */ | |
| 52 | public class SkipFirstIterable<T> extends AbstractIterable<T> | |
| 53 | implements SizedIterable<T>, OptimizedLastIterable<T>, | |
| 54 | Composite, Serializable { | |
| 55 | ||
| 56 | private final Iterable<T> _iterable; | |
| 57 | ||
| 58 | 7 | public SkipFirstIterable(Iterable<T> iterable) { _iterable = iterable; } |
| 59 | ||
| 60 | 0 | public int compositeHeight() { return ObjectUtil.compositeHeight((Object) _iterable) + 1; } |
| 61 | 0 | public int compositeSize() { return ObjectUtil.compositeSize((Object) _iterable) + 1; } |
| 62 | ||
| 63 | 7 | public Iterator<T> iterator() { |
| 64 | 7 | Iterator<T> result = _iterable.iterator(); |
| 65 | 7 | if (result.hasNext()) { result.next(); } |
| 66 | 7 | return result; |
| 67 | } | |
| 68 | ||
| 69 | 0 | public boolean isEmpty() { return IterUtil.sizeOf(_iterable, 2) < 2; } |
| 70 | ||
| 71 | 0 | public int size() { |
| 72 | // This can't be implemented in a strictly correct manner. If the nestedSize is MAX_VALUE, | |
| 73 | // that means the size of this iterable is >= MAX_VALUE-1. If we return MAX_VALUE-1, we're | |
| 74 | // asserting that it is *equal to* that size; if we return MAX_VALUE, we're asserting it's | |
| 75 | // size is *greater than* that size. There's no way to communicate the *greater than or | |
| 76 | // equal* result. | |
| 77 | 0 | int nestedSize = IterUtil.sizeOf(_iterable); |
| 78 | 0 | if (nestedSize == 0) { return 0; } |
| 79 | 0 | else if (nestedSize == Integer.MAX_VALUE) { return Integer.MAX_VALUE; } |
| 80 | 0 | else { return nestedSize - 1; } |
| 81 | } | |
| 82 | ||
| 83 | 0 | public int size(int bound) { |
| 84 | 0 | if (bound == Integer.MAX_VALUE) { return size(); } |
| 85 | else { | |
| 86 | 0 | int nestedSize = IterUtil.sizeOf(_iterable, bound + 1); |
| 87 | 0 | return (nestedSize == 0) ? 0 : nestedSize - 1; |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | 0 | public boolean isInfinite() { return IterUtil.isInfinite(_iterable); } |
| 92 | ||
| 93 | 7 | public boolean hasFixedSize() { return IterUtil.hasFixedSize(_iterable); } |
| 94 | ||
| 95 | 7 | public boolean isStatic() { return IterUtil.isStatic(_iterable); } |
| 96 | ||
| 97 | 0 | public T last() { |
| 98 | // assert that there is at least one element | |
| 99 | 0 | IterUtil.first(this); |
| 100 | 0 | return IterUtil.last(_iterable); |
| 101 | } | |
| 102 | ||
| 103 | /** Call the constructor (allows {@code T} to be inferred) */ | |
| 104 | 0 | public static <T> SkipFirstIterable<T> make(Iterable<T> iterable) { |
| 105 | 0 | return new SkipFirstIterable<T>(iterable); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Create a {@code SkipFirstIterable} and wrap it in a {@code SnapshotIterable}, forcing | |
| 110 | * immediate traversal of the list. | |
| 111 | */ | |
| 112 | 0 | public static <T> SnapshotIterable<T> makeSnapshot(Iterable<T> iterable) { |
| 113 | 0 | return new SnapshotIterable<T>(new SkipFirstIterable<T>(iterable)); |
| 114 | } | |
| 115 | } |
|
||||||||||