@Test public void testAddEmptyPartitionAttributesThrows() { PowerSetPartitionMap<ListenableAsyncCloseable> map = new PowerSetPartitionMap<>(address -> VALUE); PartitionAttributes emptyAttributes = new DefaultPartitionAttributesBuilder(0).build(); try { map.add(emptyAttributes); fail(); } catch (IllegalArgumentException expected) { } }
@Override public List<T> add(final PartitionAttributes partition) { final int partitionSize = partition.size(); if (partitionSize <= 0 || partitionSize > MAX_PARTITION_ATTRIBUTE_SIZE) { throw new IllegalArgumentException("attribute size: " + partitionSize + " must be in the range [0," + MAX_PARTITION_ATTRIBUTE_SIZE + ")"); } Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap); List<Object> entries = new ArrayList<>(partitionSize << 1); partition.forEach((key, value) -> { entries.add(key); entries.add(value); }); final int numCombinations = 1 << partitionSize; final List<T> effectedPartitions = new ArrayList<>(numCombinations); for (int i = 1; i < numCombinations; ++i) { PartitionAttributesBuilder wildCardAttributesBuilder = partitionAttributesBuilderFunc.apply(bitCount(i)); int remainingBits = i; do { int entriesIndex = numberOfTrailingZeros(remainingBits); remainingBits &= ~(1 << entriesIndex); wildCardAttributesBuilder.add((PartitionAttributes.Key) entries.get(entriesIndex << 1), entries.get((entriesIndex << 1) + 1)); } while (remainingBits != 0); PartitionAttributes wildCardAttributes = wildCardAttributesBuilder.build(); ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttributes); if (valueHolder != null) { ++valueHolder.refCount; } else { valueHolder = new ValueHolder<>(valueFactory.apply(wildCardAttributes)); newWildCardToAttributes.put(wildCardAttributes, valueHolder); } absoluteToWildCardIndexMap.computeIfAbsent(partition, attributes -> new HashSet<>(2)).add(wildCardAttributes); effectedPartitions.add(valueHolder.value); } wildCardToValueMap = newWildCardToAttributes; if (closed > 0) { closeAllValues(newWildCardToAttributes, closed == CLOSED_GRACEFULLY).subscribe(); } return effectedPartitions; }
PowerSetPartitionMap implements PartitionMap<T> { @Override public List<T> add(final PartitionAttributes partition) { final int partitionSize = partition.size(); if (partitionSize <= 0 || partitionSize > MAX_PARTITION_ATTRIBUTE_SIZE) { throw new IllegalArgumentException("attribute size: " + partitionSize + " must be in the range [0," + MAX_PARTITION_ATTRIBUTE_SIZE + ")"); } Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap); List<Object> entries = new ArrayList<>(partitionSize << 1); partition.forEach((key, value) -> { entries.add(key); entries.add(value); }); final int numCombinations = 1 << partitionSize; final List<T> effectedPartitions = new ArrayList<>(numCombinations); for (int i = 1; i < numCombinations; ++i) { PartitionAttributesBuilder wildCardAttributesBuilder = partitionAttributesBuilderFunc.apply(bitCount(i)); int remainingBits = i; do { int entriesIndex = numberOfTrailingZeros(remainingBits); remainingBits &= ~(1 << entriesIndex); wildCardAttributesBuilder.add((PartitionAttributes.Key) entries.get(entriesIndex << 1), entries.get((entriesIndex << 1) + 1)); } while (remainingBits != 0); PartitionAttributes wildCardAttributes = wildCardAttributesBuilder.build(); ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttributes); if (valueHolder != null) { ++valueHolder.refCount; } else { valueHolder = new ValueHolder<>(valueFactory.apply(wildCardAttributes)); newWildCardToAttributes.put(wildCardAttributes, valueHolder); } absoluteToWildCardIndexMap.computeIfAbsent(partition, attributes -> new HashSet<>(2)).add(wildCardAttributes); effectedPartitions.add(valueHolder.value); } wildCardToValueMap = newWildCardToAttributes; if (closed > 0) { closeAllValues(newWildCardToAttributes, closed == CLOSED_GRACEFULLY).subscribe(); } return effectedPartitions; } }
PowerSetPartitionMap implements PartitionMap<T> { @Override public List<T> add(final PartitionAttributes partition) { final int partitionSize = partition.size(); if (partitionSize <= 0 || partitionSize > MAX_PARTITION_ATTRIBUTE_SIZE) { throw new IllegalArgumentException("attribute size: " + partitionSize + " must be in the range [0," + MAX_PARTITION_ATTRIBUTE_SIZE + ")"); } Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap); List<Object> entries = new ArrayList<>(partitionSize << 1); partition.forEach((key, value) -> { entries.add(key); entries.add(value); }); final int numCombinations = 1 << partitionSize; final List<T> effectedPartitions = new ArrayList<>(numCombinations); for (int i = 1; i < numCombinations; ++i) { PartitionAttributesBuilder wildCardAttributesBuilder = partitionAttributesBuilderFunc.apply(bitCount(i)); int remainingBits = i; do { int entriesIndex = numberOfTrailingZeros(remainingBits); remainingBits &= ~(1 << entriesIndex); wildCardAttributesBuilder.add((PartitionAttributes.Key) entries.get(entriesIndex << 1), entries.get((entriesIndex << 1) + 1)); } while (remainingBits != 0); PartitionAttributes wildCardAttributes = wildCardAttributesBuilder.build(); ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttributes); if (valueHolder != null) { ++valueHolder.refCount; } else { valueHolder = new ValueHolder<>(valueFactory.apply(wildCardAttributes)); newWildCardToAttributes.put(wildCardAttributes, valueHolder); } absoluteToWildCardIndexMap.computeIfAbsent(partition, attributes -> new HashSet<>(2)).add(wildCardAttributes); effectedPartitions.add(valueHolder.value); } wildCardToValueMap = newWildCardToAttributes; if (closed > 0) { closeAllValues(newWildCardToAttributes, closed == CLOSED_GRACEFULLY).subscribe(); } return effectedPartitions; } PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory);  PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory,
                                IntFunction<PartitionAttributesBuilder> partitionAttributesBuilderFunc); }
PowerSetPartitionMap implements PartitionMap<T> { @Override public List<T> add(final PartitionAttributes partition) { final int partitionSize = partition.size(); if (partitionSize <= 0 || partitionSize > MAX_PARTITION_ATTRIBUTE_SIZE) { throw new IllegalArgumentException("attribute size: " + partitionSize + " must be in the range [0," + MAX_PARTITION_ATTRIBUTE_SIZE + ")"); } Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap); List<Object> entries = new ArrayList<>(partitionSize << 1); partition.forEach((key, value) -> { entries.add(key); entries.add(value); }); final int numCombinations = 1 << partitionSize; final List<T> effectedPartitions = new ArrayList<>(numCombinations); for (int i = 1; i < numCombinations; ++i) { PartitionAttributesBuilder wildCardAttributesBuilder = partitionAttributesBuilderFunc.apply(bitCount(i)); int remainingBits = i; do { int entriesIndex = numberOfTrailingZeros(remainingBits); remainingBits &= ~(1 << entriesIndex); wildCardAttributesBuilder.add((PartitionAttributes.Key) entries.get(entriesIndex << 1), entries.get((entriesIndex << 1) + 1)); } while (remainingBits != 0); PartitionAttributes wildCardAttributes = wildCardAttributesBuilder.build(); ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttributes); if (valueHolder != null) { ++valueHolder.refCount; } else { valueHolder = new ValueHolder<>(valueFactory.apply(wildCardAttributes)); newWildCardToAttributes.put(wildCardAttributes, valueHolder); } absoluteToWildCardIndexMap.computeIfAbsent(partition, attributes -> new HashSet<>(2)).add(wildCardAttributes); effectedPartitions.add(valueHolder.value); } wildCardToValueMap = newWildCardToAttributes; if (closed > 0) { closeAllValues(newWildCardToAttributes, closed == CLOSED_GRACEFULLY).subscribe(); } return effectedPartitions; } PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory);  PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory,
                                IntFunction<PartitionAttributesBuilder> partitionAttributesBuilderFunc); @Override T get(@Nullable PartitionAttributes wildCardAttributes); @Override List<T> add(final PartitionAttributes partition); @Override List<T> remove(PartitionAttributes partition); @Override Completable onClose(); @Override Completable closeAsync(); @Override Completable closeAsyncGracefully(); }
PowerSetPartitionMap implements PartitionMap<T> { @Override public List<T> add(final PartitionAttributes partition) { final int partitionSize = partition.size(); if (partitionSize <= 0 || partitionSize > MAX_PARTITION_ATTRIBUTE_SIZE) { throw new IllegalArgumentException("attribute size: " + partitionSize + " must be in the range [0," + MAX_PARTITION_ATTRIBUTE_SIZE + ")"); } Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap); List<Object> entries = new ArrayList<>(partitionSize << 1); partition.forEach((key, value) -> { entries.add(key); entries.add(value); }); final int numCombinations = 1 << partitionSize; final List<T> effectedPartitions = new ArrayList<>(numCombinations); for (int i = 1; i < numCombinations; ++i) { PartitionAttributesBuilder wildCardAttributesBuilder = partitionAttributesBuilderFunc.apply(bitCount(i)); int remainingBits = i; do { int entriesIndex = numberOfTrailingZeros(remainingBits); remainingBits &= ~(1 << entriesIndex); wildCardAttributesBuilder.add((PartitionAttributes.Key) entries.get(entriesIndex << 1), entries.get((entriesIndex << 1) + 1)); } while (remainingBits != 0); PartitionAttributes wildCardAttributes = wildCardAttributesBuilder.build(); ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttributes); if (valueHolder != null) { ++valueHolder.refCount; } else { valueHolder = new ValueHolder<>(valueFactory.apply(wildCardAttributes)); newWildCardToAttributes.put(wildCardAttributes, valueHolder); } absoluteToWildCardIndexMap.computeIfAbsent(partition, attributes -> new HashSet<>(2)).add(wildCardAttributes); effectedPartitions.add(valueHolder.value); } wildCardToValueMap = newWildCardToAttributes; if (closed > 0) { closeAllValues(newWildCardToAttributes, closed == CLOSED_GRACEFULLY).subscribe(); } return effectedPartitions; } PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory);  PowerSetPartitionMap(Function<PartitionAttributes, T> valueFactory,
                                IntFunction<PartitionAttributesBuilder> partitionAttributesBuilderFunc); @Override T get(@Nullable PartitionAttributes wildCardAttributes); @Override List<T> add(final PartitionAttributes partition); @Override List<T> remove(PartitionAttributes partition); @Override Completable onClose(); @Override Completable closeAsync(); @Override Completable closeAsyncGracefully();  }