public void testCreation_arrayOfArray() { Comparator<String[]> comparator = Ordering.natural() .lexicographical() .onResultOf( new Function<String[], Iterable<Comparable>>() { @Override public Iterable<Comparable> apply(String[] input) { return Arrays.<Comparable>asList(input); } }); String[] array = new String[] {"a"}; Multiset<String[]> multiset = ImmutableSortedMultiset.orderedBy(comparator).add(array).build(); Multiset<String[]> expected = HashMultiset.create(); expected.add(array); assertEquals(expected, multiset); }
public void testBuilderSetCount() { ImmutableSortedMultiset<String> multiset = ImmutableSortedMultiset.<String>naturalOrder() .add("a") .setCount("a", 2) .setCount("b", 3) .build(); assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset); }
/** * Computes a histogram for the inputs. The result is a multiset with an entry * for each bin in ascending order, the count of each entry indicates the size * of the bin. A bin is indicated by its leftmost value, for example if the * <code>binSize</code> is <code>2</code> and the result contains * <code>4</code> with count <code>3</code> this means that there are * <code>3</code> values in range <code>2 ≤ x < 4</code>. * @param input The values to compute the histogram of. * @param binSize The size of the bins. * @return An {@link ImmutableSortedMultiset} representing the histogram. */ public static ImmutableSortedMultiset<Double> computeHistogram( Iterable<Double> input, double binSize) { final ImmutableSortedMultiset.Builder<Double> builder = ImmutableSortedMultiset .naturalOrder(); for (final double d : input) { checkArgument(!Double.isInfinite(d) && !Double.isNaN(d), "Only finite numbers are accepted, found %s.", d); builder.add(Math.floor(d / binSize) * binSize); } return builder.build(); }
/** * Computes a histogram for the inputs. The result is a multiset with an entry * for each bin in ascending order, the count of each entry indicates the size * of the bin. A bin is indicated by its leftmost value, for example if the * <code>binSize</code> is <code>2</code> and the result contains * <code>4</code> with count <code>3</code> this means that there are * <code>3</code> values in range <code>2 ≤ x < 4</code>. * @param input The values to compute the histogram of. * @param binSize The size of the bins. * @return An {@link ImmutableSortedMultiset} representing the histogram. */ public static ImmutableSortedMultiset<Double> computeHistogram( Iterable<Double> input, double binSize) { final ImmutableSortedMultiset.Builder<Double> builder = ImmutableSortedMultiset .naturalOrder(); for (final double d : input) { checkArgument(!Double.isInfinite(d) && !Double.isNaN(d), "Only finite numbers are accepted, found %s.", d); builder.add(Math.floor(d / binSize) * binSize); } return builder.build(); }
public void testCreation_arrayOfArray() { Comparator<String[]> comparator = Ordering.natural().lexicographical() .onResultOf(new Function<String[], Iterable<Comparable>>() { @Override public Iterable<Comparable> apply(String[] input) { return Arrays.<Comparable>asList(input); } }); String[] array = new String[] {"a"}; Multiset<String[]> multiset = ImmutableSortedMultiset.orderedBy(comparator).add(array).build(); Multiset<String[]> expected = HashMultiset.create(); expected.add(array); assertEquals(expected, multiset); }
public void testBuilderAddHandlesNullsCorrectly() { ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder(); try { builder.add((String) null); fail("expected NullPointerException"); } catch (NullPointerException expected) {} }
public void testBuilderSetCount() { ImmutableSortedMultiset<String> multiset = ImmutableSortedMultiset.<String>naturalOrder().add("a").setCount("a", 2).setCount("b", 3) .build(); assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset); }
public void testForEachEntry() { ImmutableSortedMultiset<String> multiset = ImmutableSortedMultiset.<String>naturalOrder().add("a").add("b").add("a").add("c").build(); List<Multiset.Entry<String>> entries = new ArrayList<>(); multiset.forEachEntry((e, c) -> entries.add(Multisets.immutableEntry(e, c))); assertThat(entries) .containsExactly( Multisets.immutableEntry("a", 2), Multisets.immutableEntry("b", 1), Multisets.immutableEntry("c", 1)) .inOrder(); }
public void testForEachEntry() { ImmutableSortedMultiset<String> multiset = ImmutableSortedMultiset.<String>naturalOrder().add("a").add("b").add("a").add("c").build(); List<Multiset.Entry<String>> entries = new ArrayList<>(); multiset.forEachEntry((e, c) -> entries.add(Multisets.immutableEntry(e, c))); assertThat(entries) .containsExactly( Multisets.immutableEntry("a", 2), Multisets.immutableEntry("b", 1), Multisets.immutableEntry("c", 1)) .inOrder(); }
public void testBuilderAddHandlesNullsCorrectly() { ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder(); try { builder.add((String) null); fail("expected NullPointerException"); } catch (NullPointerException expected) { } }