/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
@SuppressWarnings("unchecked") public void testCopyOfSortedDefensiveCopy() { // Depending on JDK version, either toArray() or toArray(T[]) may be called... use this class // rather than mocking to ensure that one of those methods is called. class TestHashSet<E> extends HashSet<E> { boolean toArrayCalled = false; @Override public Object[] toArray() { toArrayCalled = true; return super.toArray(); } @Override public <T> T[] toArray(T[] a) { toArrayCalled = true; return super.toArray(a); } } // Test that toArray() is used to make a defensive copy in copyOf(), so concurrently modified // synchronized collections can be safely copied. SortedMultiset<String> toCopy = mock(SortedMultiset.class); TestHashSet<Entry<String>> entrySet = new TestHashSet<>(); when((Comparator<Comparable<String>>) toCopy.comparator()) .thenReturn(Ordering.<Comparable<String>>natural()); when(toCopy.entrySet()).thenReturn(entrySet); ImmutableSortedMultiset<String> unused = ImmutableSortedMultiset.copyOfSorted(toCopy); assertTrue(entrySet.toArrayCalled); }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
/** * Returns a newly-created {@code ImmutableSortedMultiset} based on the contents of the {@code * Builder}. */ @Override public ImmutableSortedMultiset<E> build() { return copyOfSorted((SortedMultiset<E>) contents); } }
@SuppressWarnings("unchecked") public void testCopyOfSortedDefensiveCopy() { // Depending on JDK version, either toArray() or toArray(T[]) may be called... use this class // rather than mocking to ensure that one of those methods is called. class TestHashSet<E> extends HashSet<E> { boolean toArrayCalled = false; @Override public Object[] toArray() { toArrayCalled = true; return super.toArray(); } @Override public <T> T[] toArray(T[] a) { toArrayCalled = true; return super.toArray(a); } } // Test that toArray() is used to make a defensive copy in copyOf(), so concurrently modified // synchronized collections can be safely copied. SortedMultiset<String> toCopy = mock(SortedMultiset.class); TestHashSet<Entry<String>> entrySet = new TestHashSet<>(); when((Comparator<Comparable<String>>) toCopy.comparator()) .thenReturn(Ordering.<Comparable<String>>natural()); when(toCopy.entrySet()).thenReturn(entrySet); ImmutableSortedMultiset<String> unused = ImmutableSortedMultiset.copyOfSorted(toCopy); assertTrue(entrySet.toArrayCalled); }