Java Code Examples for java.util.Collections#synchronizedCollection()

The following examples show how to use java.util.Collections#synchronizedCollection() . These examples are extracted from open source projects. 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 Project: CQL   File: Schema.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private static <Ty, En, Sym, Fk, Att> Collection<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> conv(
		Collection<Eq<Ty, En, Sym, Fk, Att, Void, Void>> eqs2) {
	Collection<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> ret = new ArrayList<>(
			eqs2.size());
	for (Eq<Ty, En, Sym, Fk, Att, Void, Void> s : eqs2) {
		if (s.ctx.size() != 1) {
			continue;
		}
		Entry<Var, Chc<Ty, En>> yy = Util.get0(s.ctx.entrySet());
		if (yy.getValue().left) {
			continue;
		}
		ret.add(new Triple<>(conv2(yy), s.lhs, s.rhs));
	}
	return Collections.synchronizedCollection(ret);
}
 
Example 2
Source Project: rocketmq-read   File: VerifyUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Collection<Object> getFilterdMessage(Collection<Object> sendMsgs,
    Collection<Object> recvMsgs) {
    Collection<Object> recvMsgsSync = Collections.synchronizedCollection(recvMsgs);
    Collection<Object> filterdMsgs = new ArrayList<Object>();
    int filterNum = 0;
    for (Object msg : recvMsgsSync) {
        if (sendMsgs.contains(msg)) {
            filterdMsgs.add(msg);
        } else {
            filterNum++;
        }
    }

    logger.info(String.format("[%s] messages is filterd!", filterNum));
    return filterdMsgs;
}
 
Example 3
Source Project: rocketmq   File: VerifyUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Collection<Object> getFilterdMessage(Collection<Object> sendMsgs,
    Collection<Object> recvMsgs) {
    Collection<Object> recvMsgsSync = Collections.synchronizedCollection(recvMsgs);
    Collection<Object> filterdMsgs = new ArrayList<Object>();
    int filterNum = 0;
    for (Object msg : recvMsgsSync) {
        if (sendMsgs.contains(msg)) {
            filterdMsgs.add(msg);
        } else {
            filterNum++;
        }
    }

    logger.info(String.format("[%s] messages is filterd!", filterNum));
    return filterdMsgs;
}
 
Example 4
Source Project: DDMQ   File: VerifyUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Collection<Object> getFilterdMessage(Collection<Object> sendMsgs,
    Collection<Object> recvMsgs) {
    Collection<Object> recvMsgsSync = Collections.synchronizedCollection(recvMsgs);
    Collection<Object> filterdMsgs = new ArrayList<Object>();
    int filterNum = 0;
    for (Object msg : recvMsgsSync) {
        if (sendMsgs.contains(msg)) {
            filterdMsgs.add(msg);
        } else {
            filterNum++;
        }
    }

    logger.info(String.format("[%s] messages is filterd!", filterNum));
    return filterdMsgs;
}
 
Example 5
Source Project: rocketmq   File: VerifyUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Collection<Object> getFilterdMessage(Collection<Object> sendMsgs,
    Collection<Object> recvMsgs) {
    Collection<Object> recvMsgsSync = Collections.synchronizedCollection(recvMsgs);
    Collection<Object> filterdMsgs = new ArrayList<Object>();
    int filterNum = 0;
    for (Object msg : recvMsgsSync) {
        if (sendMsgs.contains(msg)) {
            filterdMsgs.add(msg);
        } else {
            filterNum++;
        }
    }

    logger.info(String.format("[%s] messages is filterd!", filterNum));
    return filterdMsgs;
}
 
Example 6
public static Collection<Object> getFilterdMessage(Collection<Object> sendMsgs,
    Collection<Object> recvMsgs) {
    Collection<Object> recvMsgsSync = Collections.synchronizedCollection(recvMsgs);
    Collection<Object> filterdMsgs = new ArrayList<Object>();
    int filterNum = 0;
    for (Object msg : recvMsgsSync) {
        if (sendMsgs.contains(msg)) {
            filterdMsgs.add(msg);
        } else {
            filterNum++;
        }
    }

    logger.info(String.format("[%s] messages is filterd!", filterNum));
    return filterdMsgs;
}
 
Example 7
protected Collection<String> getAnotherObject() {
    List<String> list = Collections.emptyList();
    return Collections.synchronizedCollection(list);
}
 
Example 8
protected Collection<String> getAnotherObject() {
    List<String> list = Collections.emptyList();
    return Collections.synchronizedCollection(list);
}
 
Example 9
Source Project: consulo   File: PsiReferenceProcessor.java    License: Apache License 2.0 4 votes vote down vote up
public CollectElements(Collection<PsiReference> collection) {
  myCollection = Collections.synchronizedCollection(collection);
}
 
Example 10
protected Collection<String> getAnotherObject() {
    List<String> list = Collections.emptyList();
    return Collections.synchronizedCollection(list);
}
 
Example 11
protected Collection<String> getObject() {
    List<String> list = Collections.singletonList("string");
    return Collections.synchronizedCollection(list);
}
 
Example 12
Source Project: consulo   File: PsiElementProcessor.java    License: Apache License 2.0 4 votes vote down vote up
public CollectElements(@Nonnull Collection<T> collection) {
  myCollection = Collections.synchronizedCollection(collection);
}
 
Example 13
@Test
public void testFailures() {
    final int itemCount = 2 * CAPACITY;
    val processedItems = Collections.synchronizedCollection(new HashSet<Integer>());
    val processFutures = Collections.synchronizedList(new ArrayList<CompletableFuture<Integer>>());
    Function<Integer, CompletableFuture<Integer>> itemProcessor = i -> {
        if (!processedItems.add(i)) {
            Assert.fail("Duplicate item detected: " + i);
        }

        CompletableFuture<Integer> result = new CompletableFuture<>();
        processFutures.add(result);
        return result;
    };

    val resultFutures = new ArrayList<CompletableFuture<Integer>>();
    @Cleanup
    val p = new TestProcessor(CAPACITY, itemProcessor, executorService());

    // Fill up to capacity, and beyond.
    for (int i = 0; i < itemCount; i++) {
        resultFutures.add(p.process(i));
    }

    // Fail an item.
    val failedIndex = CAPACITY / 2;
    processFutures.get(failedIndex).completeExceptionally(new IntentionalException());
    AssertExtensions.assertThrows(
            "Failed item did not have its result failed as well.",
            resultFutures.get(failedIndex)::join,
            ex -> ex instanceof IntentionalException);

    // Verify all queued-up items have been failed, but none of the initial ones (that have already begun processing)
    for (int i = CAPACITY; i < itemCount; i++) {
        AssertExtensions.assertThrows(
                "Queued-up item did not fail when a previous item failed.",
                resultFutures.get(i)::join,
                ex -> ex instanceof OrderedItemProcessor.ProcessingException && ex.getCause() instanceof IntentionalException);
    }

    for (int i = 0; i < CAPACITY; i++) {
        if (i != failedIndex) {
            Assert.assertFalse("Already-processing future was completed as well.", resultFutures.get(i).isDone());
        }
    }

    // Verify we can't add anything else ...
    AssertExtensions.assertThrows(
            "failure did not cause OrderedItemProcessor to close.",
            () -> p.process(Integer.MAX_VALUE),
            ex -> ex instanceof ObjectClosedException);
}
 
Example 14
protected Collection<String> getObject() {
    List<String> list = Collections.singletonList("string");
    return Collections.synchronizedCollection(list);
}
 
Example 15
PojoWithObjectCollectionFields fill()
{
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("zero");
    HashMap<String, Boolean> empty = newMap();

    TreeSet<String> ts = new TreeSet<String>();
    ts.add("two");

    EnumSet<Size> es = EnumSet.allOf(Size.class);

    emptySet = Collections.emptySet();
    emptyList = Collections.emptyList();
    singletonSet = Collections.singleton("three");
    singletonList = Collections.singletonList("four");
    setFromMap = Collections.newSetFromMap(empty);
    copiesList = Collections.nCopies(1, "five");

    unmodifiableCollection = Collections
            .unmodifiableCollection(Collections.emptyList()); // no
    // equals
    // impl
    unmodifiableSet = Collections.unmodifiableSet(Collections
            .emptySet());
    unmodifiableSortedSet = Collections.unmodifiableSortedSet(ts);
    unmodifiableList = Collections.unmodifiableList(ll);
    unmodifiableRandomAccessList = Collections
            .unmodifiableList(newList("six"));

    assertTrue(unmodifiableRandomAccessList.getClass().getName()
            .endsWith("RandomAccessList"));

    synchronizedCollection = Collections
            .synchronizedCollection(Collections.emptyList()); // no
    // equals
    // impl
    synchronizedSet = Collections.synchronizedSet(es);
    synchronizedSortedSet = Collections.synchronizedSortedSet(ts);
    synchronizedList = Collections.synchronizedList(ll);
    synchronizedRandomAccessList = Collections
            .synchronizedList(newList("seven"));

    assertTrue(synchronizedRandomAccessList.getClass().getName()
            .endsWith("RandomAccessList"));

    checkedCollection = Collections.checkedCollection(newList("eight"),
            String.class); // no equals impl
    checkedSet = Collections.checkedSet(es, Size.class);
    checkedSortedSet = Collections.checkedSortedSet(ts, String.class);
    checkedList = Collections.checkedList(ll, String.class);
    checkedRandomAccessList = Collections.checkedList(newList("nine"),
            String.class);

    assertTrue(checkedRandomAccessList.getClass().getName()
            .endsWith("RandomAccessList"));

    return this;
}
 
Example 16
protected Collection<String> getObject() {
    List<String> list = Collections.singletonList("string");
    return Collections.synchronizedCollection(list);
}
 
Example 17
/**
 * Tests a scenario where we add fewer items than the capacity allows. We want to verify that none of the items are
 * queued up.
 */
@Test
public void testCapacityNotExceeded() throws Exception {
    val processedItems = Collections.synchronizedCollection(new HashSet<Integer>());
    val processFutures = Collections.synchronizedList(new ArrayList<CompletableFuture<Integer>>());
    Function<Integer, CompletableFuture<Integer>> itemProcessor = i -> {
        if (!processedItems.add(i)) {
            Assert.fail("Duplicate item detected: " + i);
        }

        CompletableFuture<Integer> result = new CompletableFuture<>();
        processFutures.add(result);
        return result;
    };

    val resultFutures = new ArrayList<CompletableFuture<Integer>>();
    @Cleanup
    val p = new TestProcessor(CAPACITY, itemProcessor, executorService());
    for (int i = 0; i < CAPACITY; i++) {
        resultFutures.add(p.process(i));
        Assert.assertTrue("Item has not been immediately processed when under capacity: " + i, processedItems.contains(i));
    }

    // Finish up half the futures. We need a Semaphore so that we know when the OrderedItemProcessor actually
    // finished cleaning up after these completed tasks, as that happens asynchronously inside the processor and we
    // don't really have a hook into it, except by sub-classing it and intercepting 'executionComplete'.
    val half = CAPACITY / 2;
    val cleanupSignal = new Semaphore(half);
    cleanupSignal.acquire(half);
    p.setExecutionCompleteCallback(cleanupSignal::release);
    for (int i = 0; i < half; i++) {
        processFutures.get(i).complete(TRANSFORMER.apply(i));
    }

    cleanupSignal.acquire(half); // Wait until the processor finished internal cleanups.
    Futures.allOf(resultFutures.subList(0, half)).join();

    // Now add even more and make sure we are under capacity.
    for (int i = 0; i < CAPACITY / 2; i++) {
        val item = CAPACITY + i;
        resultFutures.add(p.process(item));
        Assert.assertTrue("Item has not been immediately processed when under capacity: " + item, processedItems.contains(item));
    }

    // Finish up the remaining futures.
    for (int i = 0; i < processFutures.size(); i++) {
        val f = processFutures.get(i);
        if (!f.isDone()) {
            f.complete(TRANSFORMER.apply(i));
        }
    }

    // Verify they have been executed in order.
    val results = Futures.allOfWithResults(resultFutures).join();
    for (int i = 0; i < results.size(); i++) {
        Assert.assertEquals("Unexpected result at index " + i, TRANSFORMER.apply(i), results.get(i));
    }
}
 
Example 18
protected Collection<String> getAnotherObject() {
    List<String> list = Collections.emptyList();
    return Collections.synchronizedCollection(list);
}
 
Example 19
@Override
public Object create(final Object sourceCollection) {
    return Collections.synchronizedCollection((Collection<?>) sourceCollection);
}
 
Example 20
protected Collection<String> getObject() {
    List<String> list = Collections.singletonList("string");
    return Collections.synchronizedCollection(list);
}