/** A factory that chooses the most space-efficient representation of the table. */ static <R, C, V> RegularImmutableTable<R, C, V> forOrderedComponents( ImmutableList<Cell<R, C, V>> cellList, ImmutableSet<R> rowSpace, ImmutableSet<C> columnSpace) { // use a dense table if more than half of the cells have values // TODO(gak): tune this condition based on empirical evidence return (cellList.size() > (((long) rowSpace.size() * columnSpace.size()) / 2)) ? new DenseImmutableTable<R, C, V>(cellList, rowSpace, columnSpace) : new SparseImmutableTable<R, C, V>(cellList, rowSpace, columnSpace); }
/** * Gets an iterator representing an immutable snapshot of all subscribers to the given event at * the time this method is called. */ Iterator<Subscriber> getSubscribers(Object event) { ImmutableSet<Class<?>> eventTypes = flattenHierarchy(event.getClass()); List<Iterator<Subscriber>> subscriberIterators = Lists.newArrayListWithCapacity(eventTypes.size()); for (Class<?> eventType : eventTypes) { CopyOnWriteArraySet<Subscriber> eventSubscribers = subscribers.get(eventType); if (eventSubscribers != null) { // eager no-copy snapshot subscriberIterators.add(eventSubscribers.iterator()); } } return Iterators.concat(subscriberIterators.iterator()); }
/** A factory that chooses the most space-efficient representation of the table. */ static <R, C, V> RegularImmutableTable<R, C, V> forOrderedComponents( ImmutableList<Cell<R, C, V>> cellList, ImmutableSet<R> rowSpace, ImmutableSet<C> columnSpace) { // use a dense table if more than half of the cells have values // TODO(gak): tune this condition based on empirical evidence return (cellList.size() > (((long) rowSpace.size() * columnSpace.size()) / 2)) ? new DenseImmutableTable<R, C, V>(cellList, rowSpace, columnSpace) : new SparseImmutableTable<R, C, V>(cellList, rowSpace, columnSpace); } }
public ImmutableSet<String> getAllTargetImageTags() { ImmutableSet.Builder<String> allTargetImageTags = ImmutableSet.builderWithExpectedSize(1 + additionalTargetImageTags.size()); allTargetImageTags.add(targetImageConfiguration.getImageTag()); allTargetImageTags.addAll(additionalTargetImageTags); return allTargetImageTags.build(); }
private long distinctCount(List<Boolean> inputSequence) { return ImmutableSet.copyOf(inputSequence).size(); }
/** Creates an ImmutableSetMultimap from an asMap.entrySet. */ static <K, V> ImmutableSetMultimap<K, V> fromMapEntries( Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries, @Nullable Comparator<? super V> valueComparator) { if (mapEntries.isEmpty()) { return of(); } ImmutableMap.Builder<K, ImmutableSet<V>> builder = new ImmutableMap.Builder<>(mapEntries.size()); int size = 0; for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) { K key = entry.getKey(); Collection<? extends V> values = entry.getValue(); ImmutableSet<V> set = valueSet(valueComparator, values); if (!set.isEmpty()) { builder.put(key, set); size += set.size(); } } return new ImmutableSetMultimap<>(builder.build(), size, valueComparator); }
public void testCollisionsDueToIncorrectSignExtension() { byte[] col1 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0x80}; byte[] col2 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0x81}; byte[] col3 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0xff}; ImmutableSet<HashCode> hashCodes = ImmutableSet.of( SIP_WITH_KEY.hashBytes(col1), SIP_WITH_KEY.hashBytes(col2), SIP_WITH_KEY.hashBytes(col3)); assertEquals(3, hashCodes.size()); }
@Test public void testTableSampleBernoulli() { DescriptiveStatistics stats = new DescriptiveStatistics(); int total = computeExpected("SELECT orderkey FROM orders", ImmutableList.of(BIGINT)).getMaterializedRows().size(); for (int i = 0; i < 100; i++) { List<MaterializedRow> values = computeActual("SELECT orderkey FROM orders TABLESAMPLE BERNOULLI (50)").getMaterializedRows(); assertEquals(values.size(), ImmutableSet.copyOf(values).size(), "TABLESAMPLE produced duplicate rows"); stats.addValue(values.size() * 1.0 / total); } double mean = stats.getGeometricMean(); assertTrue(mean > 0.45 && mean < 0.55, format("Expected mean sampling rate to be ~0.5, but was %s", mean)); }
public void testBuilderWithDuplicateElements() { ImmutableSet<String> set = this.<String>builder() .add("a") .add("a", "a") .add("a", "a", "a") .add("a", "a", "a", "a") .build(); assertTrue(set.contains("a")); assertFalse(set.contains("b")); assertEquals(1, set.size()); }
@Override SerializedForm createSerializedForm() { Map<C, Integer> columnKeyToIndex = Maps.indexMap(columnKeySet()); int[] cellColumnIndices = new int[cellSet().size()]; int i = 0; for (Cell<R, C, V> cell : cellSet()) { cellColumnIndices[i++] = columnKeyToIndex.get(cell.getColumnKey()); } return SerializedForm.create(this, cellRowIndices, cellColumnIndices); } }
public void testIntersection() { Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave"); Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom"); Set<String> frenemies = Sets.intersection(friends, enemies); assertEquals(1, frenemies.size()); ImmutableSet<String> immut = Sets.intersection(friends, enemies).immutableCopy(); HashSet<String> mut = Sets.intersection(friends, enemies).copyInto(new HashSet<String>()); enemies.add("Joe"); assertEquals(2, frenemies.size()); assertEquals(1, immut.size()); assertEquals(1, mut.size()); }
public void testDifference() { Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave"); Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom"); Set<String> goodFriends = Sets.difference(friends, enemies); assertEquals(2, goodFriends.size()); ImmutableSet<String> immut = Sets.difference(friends, enemies).immutableCopy(); HashSet<String> mut = Sets.difference(friends, enemies).copyInto(new HashSet<String>()); enemies.add("Dave"); assertEquals(1, goodFriends.size()); assertEquals(2, immut.size()); assertEquals(2, mut.size()); }
public void testUnion() { Set<String> all = Sets.union(friends, enemies); assertEquals(5, all.size()); ImmutableSet<String> immut = Sets.union(friends, enemies).immutableCopy(); HashSet<String> mut = Sets.union(friends, enemies).copyInto(new HashSet<String>()); enemies.add("Buck"); assertEquals(6, all.size()); assertEquals(5, immut.size()); assertEquals(5, mut.size()); }
public static void serialize( SerializationStreamWriter writer, ImmutableTable<Object, Object, Object> table) throws SerializationException { writer.writeInt(table.rowKeySet().size()); for (Object rowKey : table.rowKeySet()) { writer.writeObject(rowKey); Map_CustomFieldSerializerBase.serialize(writer, table.row(rowKey)); } }
@Override SerializedForm createSerializedForm() { Map<C, Integer> columnKeyToIndex = Maps.indexMap(columnKeySet()); int[] cellColumnIndices = new int[cellSet().size()]; int i = 0; for (Cell<R, C, V> cell : cellSet()) { cellColumnIndices[i++] = columnKeyToIndex.get(cell.getColumnKey()); } return SerializedForm.create(this, cellRowIndices, cellColumnIndices); } }
public void testComplexBuilder() { List<Integer> colorElem = asList(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF); // javac won't compile this without "this.<Integer>" ImmutableSet.Builder<Integer> webSafeColorsBuilder = this.<Integer>builder(); for (Integer red : colorElem) { for (Integer green : colorElem) { for (Integer blue : colorElem) { webSafeColorsBuilder.add((red << 16) + (green << 8) + blue); } } } ImmutableSet<Integer> webSafeColors = webSafeColorsBuilder.build(); assertEquals(216, webSafeColors.size()); Integer[] webSafeColorArray = webSafeColors.toArray(new Integer[webSafeColors.size()]); assertEquals(0x000000, (int) webSafeColorArray[0]); assertEquals(0x000033, (int) webSafeColorArray[1]); assertEquals(0x000066, (int) webSafeColorArray[2]); assertEquals(0x003300, (int) webSafeColorArray[6]); assertEquals(0x330000, (int) webSafeColorArray[36]); ImmutableSet<Integer> addedColor = webSafeColorsBuilder.add(LAST_COLOR_ADDED).build(); assertEquals( "Modifying the builder should not have changed any already built sets", 216, webSafeColors.size()); assertEquals("the new array should be one bigger than webSafeColors", 217, addedColor.size()); Integer[] appendColorArray = addedColor.toArray(new Integer[addedColor.size()]); assertEquals(getComplexBuilderSetLastElement(), (int) appendColorArray[216]); }
public void testSymmetricDifference() { Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave"); Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom"); Set<String> symmetricDifferenceFriendsFirst = Sets.symmetricDifference(friends, enemies); assertEquals(4, symmetricDifferenceFriendsFirst.size()); Set<String> symmetricDifferenceEnemiesFirst = Sets.symmetricDifference(enemies, friends); assertEquals(4, symmetricDifferenceEnemiesFirst.size()); assertEquals(symmetricDifferenceFriendsFirst, symmetricDifferenceEnemiesFirst); ImmutableSet<String> immut = Sets.symmetricDifference(friends, enemies).immutableCopy(); HashSet<String> mut = Sets.symmetricDifference(friends, enemies).copyInto(new HashSet<String>()); enemies.add("Dave"); assertEquals(3, symmetricDifferenceFriendsFirst.size()); assertEquals(4, immut.size()); assertEquals(4, mut.size()); immut = Sets.symmetricDifference(enemies, friends).immutableCopy(); mut = Sets.symmetricDifference(enemies, friends).copyInto(new HashSet<String>()); friends.add("Harry"); assertEquals(2, symmetricDifferenceEnemiesFirst.size()); assertEquals(3, immut.size()); assertEquals(3, mut.size()); } }
@Test public void testMultipleNodes() { FixedCountScheduler nodeScheduler = new FixedCountScheduler( (node, partition, totalPartitions) -> Optional.of(taskFactory.createTableScanTask( new TaskId("test", 1, 1), node, ImmutableList.of(), new PartitionedSplitCountTracker(delta -> {}))), generateRandomNodes(5)); ScheduleResult result = nodeScheduler.schedule(); assertTrue(result.isFinished()); assertTrue(result.getBlocked().isDone()); assertEquals(result.getNewTasks().size(), 5); assertEquals(result.getNewTasks().stream().map(RemoteTask::getNodeId).collect(toImmutableSet()).size(), 5); }
public static void testSuppressingSuppressorIfPossible() throws IOException { // can't test the JDK7 suppressor when not running on JDK7 if (!Closer.SuppressingSuppressor.isAvailable()) { return; } Closer closer = new Closer(new Closer.SuppressingSuppressor()); IOException thrownException = new IOException(); IOException c1Exception = new IOException(); RuntimeException c2Exception = new RuntimeException(); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(c1Exception)); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(c2Exception)); try { try { throw thrownException; } catch (Throwable e) { throw closer.rethrow(thrownException, IOException.class); } finally { assertThat(getSuppressed(thrownException)).isEmpty(); closer.close(); } } catch (IOException expected) { assertSame(thrownException, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); ImmutableSet<Throwable> suppressed = ImmutableSet.copyOf(getSuppressed(thrownException)); assertEquals(2, suppressed.size()); assertEquals(ImmutableSet.of(c1Exception, c2Exception), suppressed); }