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

The following examples show how to use com.google.common.collect.Multiset#setCount() . 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: BindingUtils.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void onChanged(Change<? extends E> change) {
	// This cast is safe, as a
	// UnidirectionalSetMultimapContentBinding<K, V> will only be used
	// for a SetMultimap<K, V>.
	while (change.next()) {
		final Multiset<E> destination = multisetRef.get();
		if (destination == null) {
			change.getMultiset().removeListener(this);
		} else {
			// we use replaceValues() to perform an atomic change here
			// (and
			// thus don't use the added and removed values from the
			// change)
			destination.setCount(change.getElement(),
					destination.count(change.getElement()),
					destination.count(change.getElement())
							+ change.getAddCount()
							- change.getRemoveCount());
		}
	}
}
 
Example 2
Source File: NgramEnumerator.java    From pyramid with Apache License 2.0 6 votes vote down vote up
private static void add(List<String> source, Multiset<Ngram> multiset, String field, int slop, List<Integer> template){
    Multiset<Ngram> multiSetForDoc = ConcurrentHashMultiset.create();
    for (int i=0;i<source.size();i++){
        if(i+template.get(template.size()-1)<source.size()){
            List<String> list = new ArrayList<>();
            for (int j: template){
                list.add(source.get(i+j));
            }
            Ngram ngram = new Ngram();
            ngram.setNgram(Ngram.toNgramString(list));
            ngram.setSlop(slop);
            ngram.setField(field);
            ngram.setInOrder(true);
            multiSetForDoc.setCount(ngram,1);
        }
    }
     multiset.addAll(multiSetForDoc);
}
 
Example 3
Source File: MultisetExpression.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean setCount(E element, int oldCount, int newCount) {
	final Multiset<E> multiset = get();
	return (multiset == null)
			? EMPTY_MULTISET.setCount(element, oldCount, newCount)
			: multiset.setCount(element, oldCount, newCount);
}
 
Example 4
Source File: BindingUtils.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onChanged(Change<? extends E> change) {
	if (!updating) {
		final ObservableMultiset<E> multiset1 = multiset1Ref.get();
		final ObservableMultiset<E> multiset2 = multiset2Ref.get();
		if ((multiset1 == null) || (multiset2 == null)) {
			if (multiset1 != null) {
				multiset1.removeListener(this);
			}
			if (multiset2 != null) {
				multiset2.removeListener(this);
			}
		} else {
			try {
				updating = true;
				final Multiset<E> destination = multiset1 == change
						.getMultiset() ? multiset2 : multiset1;
				// we use replaceValues() to perform an atomic change
				// here (and thus don't use the added and removed values
				// from the change)
				while (change.next()) {
					destination.setCount(change.getElement(),
							destination.count(change.getElement()),
							destination.count(change.getElement())
									+ change.getAddCount()
									- change.getRemoveCount());
				}
			} finally {
				updating = false;
			}
		}
	}
}
 
Example 5
Source File: SingleSentencePartialCreditTestingStatistics.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
private static PartialCreditTriplet partialCompare(LogicalExpression gold,
		LogicalExpression label) {
	final Multiset<Pair<? extends LogicalExpression, ? extends LogicalExpression>> goldPairs = GetPredConstPairs
			.of(gold);
	final Multiset<Pair<? extends LogicalExpression, ? extends LogicalExpression>> labelPairs;
	if (label == null) {
		labelPairs = HashMultiset.create();
	} else {
		labelPairs = GetPredConstPairs.of(label);
	}

	// The "intersection" of the gold and label pair sets = the number of
	// matches
	final Multiset<Pair<? extends LogicalExpression, ? extends LogicalExpression>> intersection = HashMultiset
			.create();

	for (final Entry<Pair<? extends LogicalExpression, ? extends LogicalExpression>> entry : goldPairs
			.entrySet()) {
		intersection.setCount(
				entry.getElement(),
				Math.min(entry.getCount(),
						labelPairs.count(entry.getElement())));
	}

	return new PartialCreditTriplet(goldPairs.size(), labelPairs.size(),
			intersection.size());
}
 
Example 6
Source File: MultisetExpression.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public int setCount(E element, int count) {
	final Multiset<E> multiset = get();
	return (multiset == null) ? EMPTY_MULTISET.setCount(element, count)
			: multiset.setCount(element, count);
}
 
Example 7
Source File: GuavaMultiSetUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
    Multiset<String> bookStore = HashMultiset.create();
    bookStore.setCount("Potter", 50);
    assertThat(bookStore.count("Potter")).isEqualTo(50);
}