下面列出了com.google.common.collect.Multiset#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void clear() {
final Multiset<E> multiset = get();
if (multiset == null) {
EMPTY_MULTISET.clear();
} else {
multiset.clear();
}
}
/**
* Creates a unidirectional content binding from the given source
* {@link Multiset} to the given target {@link ObservableMultiset}.
*
* @param <E>
* The element type of the given {@link Multiset} and
* {@link ObservableMultiset}.
* @param source
* The {@link Multiset} whose content to update when the given
* {@link ObservableMultiset} changes.
* @param target
* The {@link ObservableMultiset} whose content is to be
* observed.
*/
public static <E> void bindContent(Multiset<E> source,
ObservableMultiset<? extends E> target) {
if (source == null) {
throw new NullPointerException("Cannot bind null value.");
}
if (target == null) {
throw new NullPointerException("Cannot bind to null value.");
}
if (source == target) {
throw new IllegalArgumentException("Cannot bind source to itself.");
}
if (source instanceof ObservableMultiset) {
// ensure we use an atomic operation in case the source multiset is
// observable.
((ObservableMultiset<E>) source).replaceAll(target);
} else {
source.clear();
source.addAll(target);
}
final UnidirectionalMultisetContentBinding<E> contentBinding = new UnidirectionalMultisetContentBinding<>(
source);
// clear any previous bindings
target.removeListener(contentBinding);
// add new binding as listener
target.addListener(contentBinding);
}
@Test
public void clear() {
// initialize multiset with some values
observable.add(1, 1);
observable.add(2, 2);
observable.add(3, 3);
// prepare backup multiset
Multiset<Integer> backupMultiset = HashMultiset.create();
backupMultiset.add(1, 1);
backupMultiset.add(2, 2);
backupMultiset.add(3, 3);
check(observable, backupMultiset);
// register listeners
registerListeners();
// clear
invalidationListener.expect(1);
multisetChangeListener.addAtomicExpectation();
multisetChangeListener.addElementaryExpection(1, 1, 0);
multisetChangeListener.addElementaryExpection(2, 2, 0);
multisetChangeListener.addElementaryExpection(3, 3, 0);
observable.clear();
backupMultiset.clear();
check(observable, backupMultiset);
checkListeners();
// clear again (while already empty)
invalidationListener.expect(0);
observable.clear();
check(observable, backupMultiset);
checkListeners();
}
@Test
public void replaceAll() {
// initialize multiset with some values
observable.add(1, 1);
observable.add(2, 2);
observable.add(3, 3);
observable.add(4, 4);
// prepare backup multiset
Multiset<Integer> backupMultiset = HashMultiset.create();
backupMultiset.add(1, 1);
backupMultiset.add(2, 2);
backupMultiset.add(3, 3);
backupMultiset.add(4, 4);
check(observable, backupMultiset);
// register listeners
registerListeners();
// replaceAll
invalidationListener.expect(1);
multisetChangeListener.addAtomicExpectation();
multisetChangeListener.addElementaryExpection(2, 1, 0); // decrease
// count
multisetChangeListener.addElementaryExpection(4, 4, 0); // remove
multisetChangeListener.addElementaryExpection(3, 0, 3); // increase
// count
multisetChangeListener.addElementaryExpection(5, 0, 5); // add
Multiset<Integer> toReplace = HashMultiset.create();
toReplace.add(1);
toReplace.add(2, 1);
toReplace.add(3, 6);
toReplace.add(5, 5);
observable.replaceAll(toReplace);
backupMultiset.clear();
backupMultiset.addAll(toReplace);
check(observable, backupMultiset);
checkListeners();
// replace with same contents (should not have any effect)
invalidationListener.expect(0);
observable.replaceAll(toReplace);
check(observable, backupMultiset);
checkListeners();
}