public long dequeue() { if ( isEmpty() ) { throw new IllegalStateException( "Fail to poll first element. Queue is empty." ); } long value = values[head]; head = (head + 1) & (values.length - 1); return value; }
public void addAll( PrimitiveLongArrayQueue otherQueue ) { while ( !otherQueue.isEmpty() ) { enqueue( otherQueue.dequeue() ); } }
public long[] getIds( int numberOfIds ) { if ( freeIdCount == 0 ) { return PrimitiveLongCollections.EMPTY_LONG_ARRAY; } int reusableIds = (int) min( numberOfIds, freeIdCount ); long[] ids = new long[reusableIds]; int cursor = 0; while ( (cursor < reusableIds) && !freeIds.isEmpty() ) { ids[cursor++] = freeIds.dequeue(); } while ( cursor < reusableIds ) { ids[cursor++] = getIdFromDisk(); } freeIdCount -= reusableIds; return ids; }
private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException { channel.position( channel.size() ); writeBuffer.clear(); while ( !freeIds.isEmpty() ) { long id = freeIds.dequeue(); if ( id == NO_RESULT ) { continue; } writeBuffer.putLong( id ); if ( writeBuffer.position() == writeBuffer.capacity() ) { writeBuffer.flip(); channel.writeAll( writeBuffer ); writeBuffer.clear(); } } writeBuffer.flip(); if ( writeBuffer.hasRemaining() ) { channel.writeAll( writeBuffer ); } return channel.position(); }
@Test void newQueueIsEmpty() { assertTrue( createQueue().isEmpty() ); }
@Test void offerMoreThenQueueCapacityElements() { PrimitiveLongArrayQueue arrayQueue = createQueue(); for ( int i = 1; i < 1234; i++ ) { arrayQueue.enqueue( i ); } int currentValue = 1; while ( !arrayQueue.isEmpty() ) { assertEquals( currentValue++, arrayQueue.dequeue() ); } }
@Test void addRemoveElementKeepQueueEmpty() { PrimitiveLongArrayQueue longArrayQueue = createQueue(); for ( int i = 0; i < 1000; i++ ) { longArrayQueue.enqueue( i ); assertEquals( i, longArrayQueue.dequeue() ); assertTrue( longArrayQueue.isEmpty() ); } }
@Test void addAllElementsFromOtherQueue() { PrimitiveLongArrayQueue queue = createQueue(); queue.enqueue( 1 ); queue.enqueue( 2 ); PrimitiveLongArrayQueue otherQueue = createQueue(); otherQueue.enqueue( 3 ); otherQueue.enqueue( 4 ); queue.addAll( otherQueue ); assertTrue( otherQueue.isEmpty() ); assertEquals( 0, otherQueue.size() ); assertEquals( 4, queue.size() ); for ( int value = 1; value <= 4; value++ ) { assertEquals( value, queue.dequeue() ); } assertTrue( queue.isEmpty() ); }
public long dequeue() { if ( isEmpty() ) { throw new IllegalStateException( "Fail to poll first element. Queue is empty." ); } long value = values[head]; head = (head + 1) & (values.length - 1); return value; }
public void addAll( PrimitiveLongArrayQueue otherQueue ) { while ( !otherQueue.isEmpty() ) { enqueue( otherQueue.dequeue() ); } }
public long[] getIds( int numberOfIds ) { if ( freeIdCount == 0 ) { return PrimitiveLongCollections.EMPTY_LONG_ARRAY; } int reusableIds = (int) min( numberOfIds, freeIdCount ); long[] ids = new long[reusableIds]; int cursor = 0; while ( (cursor < reusableIds) && !freeIds.isEmpty() ) { ids[cursor++] = freeIds.dequeue(); } while ( cursor < reusableIds ) { ids[cursor++] = getIdFromDisk(); } freeIdCount -= reusableIds; return ids; }
private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException { channel.position( channel.size() ); writeBuffer.clear(); while ( !freeIds.isEmpty() ) { long id = freeIds.dequeue(); if ( id == NO_RESULT ) { continue; } writeBuffer.putLong( id ); if ( writeBuffer.position() == writeBuffer.capacity() ) { writeBuffer.flip(); channel.writeAll( writeBuffer ); writeBuffer.clear(); } } writeBuffer.flip(); if ( writeBuffer.hasRemaining() ) { channel.writeAll( writeBuffer ); } return channel.position(); }