IndexToIndexMultiMapFactory { public static IndexToIndexMultiMap buildIndexToIndexMultiMap( @NotNull final Collection<? extends Collection<Integer>> valueToDocuments, final int documentsCount) { final int uniqueValuesCount = valueToDocuments.size(); if (uniqueValuesCount == 0) throw new IllegalArgumentException("Nonpositive values count"); if (documentsCount <= 0) throw new IllegalArgumentException("Nonpositive documents count"); if (hasDocumentsWithMultipleKeys(valueToDocuments, documentsCount)) { if (((long) uniqueValuesCount) * documentsCount / Long.SIZE < documentsCount * Integer.BYTES) { return buildIndexToIndexMultiMap( V1DatabaseFormat.MultiMapType.LONG_ARRAY_BIT_SET_BASED, valueToDocuments, documentsCount); } else { return buildIndexToIndexMultiMap( V1DatabaseFormat.MultiMapType.LIST_BASED, valueToDocuments, documentsCount); } } else { return new AscendingBitSetIndexToIndexMultiMap(valueToDocuments, documentsCount); } } private  IndexToIndexMultiMapFactory(); static boolean hasDocumentsWithMultipleKeys(
            @NotNull final Collection<? extends Collection<Integer>> valueToDocuments,
            final int documentsCount); static IndexToIndexMultiMap buildIndexToIndexMultiMap(
            @NotNull final Collection<? extends Collection<Integer>> valueToDocuments,
            final int documentsCount); static IndexToIndexMultiMap buildIndexToIndexMultiMap(
            @Nullable final V1DatabaseFormat.MultiMapType type,
            @NotNull final Collection<? extends Collection<Integer>> valueToDocuments,
            final int documentsCount);  }
@Test public void ascendingTyped() { @SuppressWarnings("unchecked") final IndexToIndexMultiMap map = IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( V1DatabaseFormat.MultiMapType.ASCENDING_BIT_SET_BASED, Arrays.asList(singletonList(0), singletonList(1), singletonList(2)), 128); assertTrue(map instanceof AscendingBitSetIndexToIndexMultiMap); }
@Test public void nullTypedShouldBeDefault() { @SuppressWarnings("unchecked") final IndexToIndexMultiMap map = IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( null, Arrays.asList(singletonList(0), singletonList(1), singletonList(2)), 128); assertTrue(map instanceof AscendingBitSetIndexToIndexMultiMap); }
@Test(expected = IllegalArgumentException.class) public void zeroValues() { IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( Collections.<Collection<Integer>>emptyList(), 1); }
@Test(expected = IllegalArgumentException.class) public void zeroDocuments() { IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( singletonList(singletonList(0)), 0); }
@Test(expected = IllegalArgumentException.class) public void zeroValuesTyped() { IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( V1DatabaseFormat.MultiMapType.LIST_BASED, Collections.<Collection<Integer>>emptyList(), 1); }
@Test(expected = IllegalArgumentException.class) public void zeroDocumentsTyped() { IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( V1DatabaseFormat.MultiMapType.LIST_BASED, singletonList(singletonList(0)), 0); }
@Test public void list() { final TreeMultimap<Integer, Integer> elements = TreeMultimap.create(); for (int i = 0; i < 1024; i++) { elements.put(i, i); elements.put(i, i + 1); } final IndexToIndexMultiMap map = IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( elements.asMap().values(), 1024); assertTrue(map instanceof IntIndexToIndexMultiMap); }
@Test public void bitset() { @SuppressWarnings("unchecked") final IndexToIndexMultiMap map = IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( Arrays.asList(singletonList(0), singletonList(1), singletonList(1)), 128); assertTrue(map instanceof BitSetIndexToIndexMultiMap); }
@Test public void ascending() { @SuppressWarnings("unchecked") final IndexToIndexMultiMap map = IndexToIndexMultiMapFactory.buildIndexToIndexMultiMap( Arrays.asList(singletonList(0), singletonList(1), singletonList(2)), 128); assertTrue(map instanceof AscendingBitSetIndexToIndexMultiMap); }