Java Code Examples for com.google.common.collect.Multiset#clear()

The following examples show how to use com.google.common.collect.Multiset#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultisetExpression.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void clear() {
	final Multiset<E> multiset = get();
	if (multiset == null) {
		EMPTY_MULTISET.clear();
	} else {
		multiset.clear();
	}
}
 
Example 2
Source File: BindingUtils.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: ObservableMultisetTests.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example 4
Source File: ObservableMultisetTests.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@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();
}