ItemIdComposer { public static long composeSegment(@IntRange(from = MIN_SEGMENT, to = MAX_SEGMENT) int segment, long wrappedId) { if (segment < MIN_SEGMENT || segment > MAX_SEGMENT) { throw new IllegalArgumentException("Segment value is out of range. (segment = " + segment + ")"); } return (((long) segment) << BIT_OFFSET_SEGMENT) | (wrappedId & (BIT_MASK_RESERVED_SIGN_FLAG | BIT_MASK_GROUP_ID | BIT_MASK_CHILD_ID)); } private  ItemIdComposer(); static long composeExpandableChildId(@IntRange(from = MIN_GROUP_ID, to = MAX_GROUP_ID) long groupId, @IntRange(from = MIN_CHILD_ID, to = MAX_CHILD_ID) long childId); static long composeExpandableGroupId(@IntRange(from = MIN_GROUP_ID, to = MAX_GROUP_ID) long groupId); static boolean isExpandableGroup(long composedId); @IntRange(from = MIN_SEGMENT, to = MAX_SEGMENT) static int extractSegmentPart(long composedId); @IntRange(from = MIN_GROUP_ID, to = MAX_GROUP_ID) static long extractExpandableGroupIdPart(long composedId); @IntRange(from = MIN_CHILD_ID, to = MAX_CHILD_ID) static long extractExpandableChildIdPart(long composedId); static long extractWrappedIdPart(long composedId); static long composeSegment(@IntRange(from = MIN_SEGMENT, to = MAX_SEGMENT) int segment, long wrappedId); static final int BIT_OFFSET_RESERVED_SIGN_FLAG; static final int BIT_OFFSET_SEGMENT; static final int BIT_OFFSET_GROUP_ID; static final int BIT_OFFSET_CHILD_ID; static final int BIT_WIDTH_RESERVED_SIGN_FLAG; static final int BIT_WIDTH_SEGMENT; static final int BIT_WIDTH_GROUP_ID; static final int BIT_WIDTH_CHILD_ID; static final long BIT_MASK_RESERVED_SIGN_FLAG; static final long BIT_MASK_SEGMENT; static final long BIT_MASK_GROUP_ID; static final long BIT_MASK_CHILD_ID; static final int MIN_SEGMENT; static final int MAX_SEGMENT; static final long MIN_GROUP_ID; static final long MAX_GROUP_ID; static final long MIN_CHILD_ID; static final long MAX_CHILD_ID; static final long MIN_WRAPPED_ID; static final long MAX_WRAPPED_ID; }
@Test public void composeSegment() throws Exception { assertThat(ItemIdComposer.composeSegment(0, 0L), is(0L)); assertThat(ItemIdComposer.composeSegment(1, 0L), is(1L << ItemIdComposer.BIT_OFFSET_SEGMENT)); assertThat(ItemIdComposer.composeSegment(ItemIdComposer.MIN_SEGMENT, 0L), is(((long) ItemIdComposer.MIN_SEGMENT) << ItemIdComposer.BIT_OFFSET_SEGMENT)); assertThat(ItemIdComposer.composeSegment(ItemIdComposer.MAX_SEGMENT, 0L), is(((long) ItemIdComposer.MAX_SEGMENT) << ItemIdComposer.BIT_OFFSET_SEGMENT)); }
@Test public void composeSegment_MinOutOfRange() throws Exception { thrown.expect(IllegalArgumentException.class); ItemIdComposer.composeSegment(ItemIdComposer.MIN_SEGMENT - 1, 0L); }
@Test public void composeSegment_MaxOutOfRange() throws Exception { thrown.expect(IllegalArgumentException.class); ItemIdComposer.composeSegment(ItemIdComposer.MAX_SEGMENT + 1, 0L); }