Refine search
/** * Returns the index of the given item in the iterator(zero-based). If no items in {@code iterator} * equals {@code item} {@code -1} is returned. * * @param item the item to look for. * @param iterator of items. * @return index of found item or -1 if not found. */ public static int indexOf( PrimitiveLongIterator iterator, long item ) { for ( int i = 0; iterator.hasNext(); i++ ) { if ( item == iterator.next() ) { return i; } } return -1; }
/** * Pulls all items from the {@code iterator} and puts them into a {@link List}, boxing each long. * * @param iterator {@link PrimitiveLongIterator} to pull values from. * @return a {@link List} containing all items. */ public static List<Long> asList( PrimitiveLongIterator iterator ) { List<Long> out = new ArrayList<>(); while ( iterator.hasNext() ) { out.add( iterator.next() ); } return out; }
@Override protected boolean fetchNext() { if ( currentIterator == null || !currentIterator.hasNext() ) { while ( iterators.hasNext() ) { currentIterator = iterators.next(); if ( currentIterator.hasNext() ) { break; } } } return (currentIterator != null && currentIterator.hasNext()) && next( currentIterator.next() ); }
public static long[] asArray( PrimitiveLongIterator iterator ) { long[] array = new long[8]; int i = 0; for ( ; iterator.hasNext(); i++ ) { if ( i >= array.length ) { array = copyOf( array, i << 1 ); } array[i] = iterator.next(); } if ( i < array.length ) { array = copyOf( array, i ); } return array; }
/** * Pulls all items from the {@code iterator} and puts them into a {@link Set}, boxing each long. * * @param iterator {@link PrimitiveLongIterator} to pull values from. * @return a {@link Set} containing all items. */ public static Set<Long> toSet( PrimitiveLongIterator iterator ) { Set<Long> set = new HashSet<>(); while ( iterator.hasNext() ) { addUnique( set, iterator.next() ); } return set; }
/** * Copy PrimitiveLongCollection into new long array * * @param collection the collection to copy * @return the new long array */ public static long[] of( PrimitiveLongCollection collection ) { int i = 0; long[] result = new long[collection.size()]; PrimitiveLongIterator iterator = collection.iterator(); while ( iterator.hasNext() ) { result[i++] = iterator.next(); } return result; }
public static long single( PrimitiveLongIterator iterator, long defaultItem ) { try { if ( !iterator.hasNext() ) { closeSafely( iterator ); return defaultItem; } long item = iterator.next(); if ( iterator.hasNext() ) { throw new NoSuchElementException( "More than one item in " + iterator + ", first:" + item + ", second:" + iterator.next() ); } closeSafely( iterator ); return item; } catch ( NoSuchElementException exception ) { closeSafely( iterator, exception ); throw exception; } }
@Test void shouldNotContinueToCallNextOnHasNextFalse() { // GIVEN AtomicLong count = new AtomicLong( 2 ); PrimitiveLongIterator iterator = new PrimitiveLongBaseIterator() { @Override protected boolean fetchNext() { return count.decrementAndGet() >= 0 && next( count.get() ); } }; // WHEN/THEN assertTrue( iterator.hasNext() ); assertTrue( iterator.hasNext() ); assertEquals( 1L, iterator.next() ); assertTrue( iterator.hasNext() ); assertTrue( iterator.hasNext() ); assertEquals( 0L, iterator.next() ); assertFalse( iterator.hasNext() ); assertFalse( iterator.hasNext() ); assertEquals( -1L, count.get() ); }
@Test void shouldIterate() { // GIVEN PrimitiveLongStack stack = new PrimitiveLongStack(); // WHEN for ( int i = 0; i < 7; i++ ) { stack.push( i ); } // THEN PrimitiveLongIterator iterator = stack.iterator(); long i = 0; while ( iterator.hasNext() ) { assertEquals( i++, iterator.next() ); } assertEquals( 7L, i ); } }
@Test void iterateOverListElements() { PrimitiveLongList longList = new PrimitiveLongList(); for ( long i = 0; i < 10L; i++ ) { longList.add( i ); } int iteratorElements = 0; long value = 0; PrimitiveLongIterator iterator = longList.iterator(); while ( iterator.hasNext() ) { iteratorElements++; assertEquals( value++, iterator.next() ); } assertEquals( iteratorElements, longList.size() ); }