public static int extractWrappedViewType(long packedViewType) { final int segment = (int)(packedViewType >>> 32) & 0xFFFF; final int viewType = (int) (packedViewType & 0xFFFFFFFFL); return ItemViewTypeComposer.composeSegment(segment, viewType); }
@Test public void composeSegment() throws Exception { // zero assertThat(ItemViewTypeComposer.composeSegment(0, 0), is(0)); // one assertThat(ItemViewTypeComposer.composeSegment(1, 0), is(1 << ItemViewTypeComposer.BIT_OFFSET_SEGMENT)); // min assertThat(ItemViewTypeComposer.composeSegment(ItemViewTypeComposer.MIN_SEGMENT, 0), is(ItemViewTypeComposer.MIN_SEGMENT << ItemViewTypeComposer.BIT_OFFSET_SEGMENT)); // max assertThat(ItemViewTypeComposer.composeSegment(ItemViewTypeComposer.MAX_SEGMENT, 0), is(ItemViewTypeComposer.MAX_SEGMENT << ItemViewTypeComposer.BIT_OFFSET_SEGMENT)); }
@Test public void composeSegment_MaxOutOfRange() throws Exception { thrown.expect(IllegalArgumentException.class); //noinspection Range ItemViewTypeComposer.composeSegment(ItemViewTypeComposer.MAX_SEGMENT + 1, 0); }
public int wrapItemViewType(int segment, int viewType) { final int packedSegments = (segment << 16) | ItemViewTypeComposer.extractSegmentPart(viewType); final int flattenSegments; final int index = mWrapSegmentMap.indexOfKey(packedSegments); if (index >= 0) { flattenSegments = mWrapSegmentMap.valueAt(index); } else { flattenSegments = mWrapSegmentMap.size() + 1; if (flattenSegments > ItemViewTypeComposer.MAX_SEGMENT) { throw new IllegalStateException("Failed to allocate a new wrapped view type."); } mWrapSegmentMap.put(packedSegments, flattenSegments); mUnwrapSegmentMap.put(flattenSegments, packedSegments); } return ItemViewTypeComposer.composeSegment(flattenSegments, viewType); }
@Test public void composeSegment_MinOutOfRange() throws Exception { thrown.expect(IllegalArgumentException.class); //noinspection Range ItemViewTypeComposer.composeSegment(ItemViewTypeComposer.MIN_SEGMENT - 1, 0); }