private static void checkTicksPerWheel(final int ticksPerWheel) { if (!BitUtil.isPowerOfTwo(ticksPerWheel)) { throw new IllegalArgumentException("ticks per wheel must be a power of 2: " + ticksPerWheel); } }
private static void checkResolution(final int tickResolution) { if (!BitUtil.isPowerOfTwo(tickResolution)) { throw new IllegalArgumentException(" tick resolution must be a power of 2: " + tickResolution); } } }
/** * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}). * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is incorrect. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH)) { final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Check the the buffer capacity is the correct size. * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is not a power of 2. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity)) { final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Is an address aligned on a boundary. * * @param address to be tested. * @param alignment boundary the address is tested against. * @return true if the address is on the aligned boundary otherwise false. * @throws IllegalArgumentException if the alignment is not a power of 2. */ public static boolean isAligned(final long address, final int alignment) { if (!BitUtil.isPowerOfTwo(alignment)) { throw new IllegalArgumentException("alignment must be a power of 2: alignment=" + alignment); } return (address & (alignment - 1)) == 0; } }
/** * Check that page size is valid and alignment is valid. * * @param pageSize to be checked. * @throws IllegalStateException if the size is not as expected. */ public static void checkPageSize(final int pageSize) { if (pageSize < PAGE_MIN_SIZE) { throw new IllegalStateException( "Page size less than min size of " + PAGE_MIN_SIZE + ": page size=" + pageSize); } if (pageSize > PAGE_MAX_SIZE) { throw new IllegalStateException( "Page size more than max size of " + PAGE_MAX_SIZE + ": page size=" + pageSize); } if (!BitUtil.isPowerOfTwo(pageSize)) { throw new IllegalStateException("Page size not a power of 2: page size=" + pageSize); } }
/** * Check that term length is valid and alignment is valid. * * @param termLength to be checked. * @throws IllegalStateException if the length is not as expected. */ public static void checkTermLength(final int termLength) { if (termLength < TERM_MIN_LENGTH) { throw new IllegalStateException( "Term length less than min length of " + TERM_MIN_LENGTH + ": length=" + termLength); } if (termLength > TERM_MAX_LENGTH) { throw new IllegalStateException( "Term length more than max length of " + TERM_MAX_LENGTH + ": length=" + termLength); } if (!BitUtil.isPowerOfTwo(termLength)) { throw new IllegalStateException("Term length not a power of 2: length=" + termLength); } }
/** * Allocate a new direct {@link ByteBuffer} that is aligned on a given alignment boundary. * * @param capacity required for the buffer. * @param alignment boundary at which the buffer should begin. * @return a new {@link ByteBuffer} with the required alignment. * @throws IllegalArgumentException if the alignment is not a power of 2. */ public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); } }
/** * Validate that page size is valid and alignment is valid. * * @param pageSize to be checked. * @throws ConfigurationException if the size is not as expected. */ static void validatePageSize(final int pageSize) { if (pageSize < PAGE_MIN_SIZE) { throw new ConfigurationException( "Page size less than min size of " + PAGE_MIN_SIZE + ": " + pageSize); } if (pageSize > PAGE_MAX_SIZE) { throw new ConfigurationException( "Page size greater than max size of " + PAGE_MAX_SIZE + ": " + pageSize); } if (!BitUtil.isPowerOfTwo(pageSize)) { throw new ConfigurationException("Page size not a power of 2: " + pageSize); } } }
if (!BitUtil.isPowerOfTwo(segmentFileLength))
private static void checkTicksPerWheel(final int ticksPerWheel) { if (!BitUtil.isPowerOfTwo(ticksPerWheel)) { throw new IllegalArgumentException("ticks per wheel must be a power of 2: " + ticksPerWheel); } }
private static void checkResolution(final int tickResolution) { if (!BitUtil.isPowerOfTwo(tickResolution)) { throw new IllegalArgumentException(" tick resolution must be a power of 2: " + tickResolution); } } }
/** * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}). * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is incorrect. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH)) { final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Check the the buffer capacity is the correct size. * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is not a power of 2. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity)) { final String msg = "Capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}). * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is incorrect. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH)) { final String msg = "Capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Check the the buffer capacity is the correct size. * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is not a power of 2. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity)) { final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }
/** * Is an address aligned on a boundary. * * @param address to be tested. * @param alignment boundary the address is tested against. * @return true if the address is on the aligned boundary otherwise false. * @throws IllegalArgumentException if the alignment is not a power of 2. */ public static boolean isAligned(final long address, final int alignment) { if (!BitUtil.isPowerOfTwo(alignment)) { throw new IllegalArgumentException("alignment must be a power of 2: alignment=" + alignment); } return (address & (alignment - 1)) == 0; } }
public LongRingBuffer(int capacity) { /* * For ease of implementation, the actual capacity must be a power of 2; this allows * easy remainder calculations, etc. */ if (BitUtil.isPowerOfTwo(capacity)) { this.bufferCapacity = capacity; } else { this.bufferCapacity = BitUtil.findNextPositivePowerOfTwo(capacity); } elements = new long[bufferCapacity]; Arrays.fill(elements, -1); head = -1; tail = -1; this.capacity = capacity; }
public LongRingBuffer(int capacity) { /* * For ease of implementation, the actual capacity must be a power of 2; this allows * easy remainder calculations, etc. */ if (BitUtil.isPowerOfTwo(capacity)) { this.bufferCapacity = capacity; } else { this.bufferCapacity = BitUtil.findNextPositivePowerOfTwo(capacity); } elements = new long[bufferCapacity]; Arrays.fill(elements, -1); head = -1; tail = -1; this.capacity = capacity; }
static void checkIndexFileSize(final int indexFileSize) { final int recordCapacity = recordCapacity(indexFileSize); if (!BitUtil.isPowerOfTwo(recordCapacity)) { throw new IllegalStateException( "IndexFileSize must be a positive power of 2 + INITIAL_RECORD_OFFSET: indexFileSize=" + indexFileSize); } if ((recordCapacity % RECORD_LENGTH) != 0) { throw new IllegalStateException( "IndexFileSize must be a multiple of RECORD_LENGTH + INITIAL_RECORD_OFFSET: indexFileSize=" + indexFileSize); } } }