下面列出了java.util.Queue#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> a, Queue<T> q) {
if (cancelled) {
q.clear();
actual = null;
return true;
}
if (d && empty) {
Throwable e = error;
actual = null;
if (e != null) {
a.onError(e);
} else {
a.onComplete();
}
return true;
}
return false;
}
@Override
public final ProcessingDirective process(final Queue<Item<AvroRecordBuffer>> batch) {
final int batchSize = batch.size();
final ProcessingDirective result;
switch (batchSize) {
case 0:
logger.warn("Ignoring empty batch of events.");
result = CONTINUE;
break;
case 1:
result = process(batch.remove());
break;
default:
logger.debug("Processing batch of {} events.", batchSize);
final List<T> messages =
batch.stream()
.map(i -> i.payload)
.map(this::buildRecord)
.collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
// Clear the messages now; on failure they'll be retried as part of our
// pending operation.
batch.clear();
result = flush(messages);
}
return result;
}
boolean checkTerminated(boolean d,
boolean empty,
Subscriber<?> a,
Queue<GroupedFlux<K, V>> q) {
if (d) {
Throwable e = error;
if (e != null && e != Exceptions.TERMINATED) {
q.clear();
signalAsyncError();
return true;
}
else if (empty) {
a.onComplete();
return true;
}
}
return false;
}
/**
* places a key, value pair in each context map of a list of components
*
* @param elements the list of elements
* @param contextName a value to be used as a key to retrieve the object
* @param contextValue the value to be placed in the context
*/
public static void pushObjectToContext(Collection<? extends LifecycleElement> elements, String contextName,
Object contextValue) {
if (elements == null || elements.isEmpty()) {
return;
}
Queue<LifecycleElement> elementQueue = new LinkedList<LifecycleElement>();
try {
elementQueue.addAll(elements);
while (!elementQueue.isEmpty()) {
LifecycleElement currentElement = elementQueue.poll();
if (currentElement == null) {
continue;
}
if (currentElement instanceof Component) {
((Component) currentElement).pushObjectToContext(contextName, contextValue);
}
elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
}
} finally {
elementQueue.clear();
RecycleUtils.recycle(elementQueue);
}
}
final void terminateReceiver(@Nullable Queue<?> q, CoreSubscriber<?> a) {
if (q != null) {
q.clear();
}
Throwable ex = inboundError;
receiver = null;
if (ex != null) {
//parent.listener.onReceiveError(channel, ex);
a.onError(ex);
}
else {
a.onComplete();
}
}
void drainFused() {
int missed = 1;
final Subscriber<? super Flux<T>> a = actual;
final Queue<Flux<T>> q = queue;
for (; ; ) {
if (cancelled != 0) {
q.clear();
return;
}
boolean d = done;
a.onNext(null);
if (d) {
Throwable ex = error;
if (ex != null) {
signalAsyncError();
}
else {
a.onComplete();
}
return;
}
missed = WIP.addAndGet(this, -missed);
if (missed == 0) {
break;
}
}
}
/**
* places a all entries from a map into each context map of a list of components
*
* @param components The list components.
* @param sourceContext The source context map.
*/
public static void pushAllToContext(List<? extends Component> components, Map<String, Object> sourceContext) {
if (components == null || components.isEmpty()) {
return;
}
@SuppressWarnings("unchecked") Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(
LinkedList.class);
try {
elementQueue.addAll(components);
while (!elementQueue.isEmpty()) {
LifecycleElement currentElement = elementQueue.poll();
if (currentElement == null) {
continue;
}
if (currentElement instanceof Component) {
((Component) currentElement).pushAllToContext(sourceContext);
}
elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
}
} finally {
elementQueue.clear();
RecycleUtils.recycle(elementQueue);
}
}
void drainFused(Subscriber<? super T> a) {
int missed = 1;
final Queue<T> q = queue;
for (;;) {
if (cancelled) {
q.clear();
actual = null;
return;
}
boolean d = done;
a.onNext(null);
if (d) {
actual = null;
Throwable ex = error;
if (ex != null) {
a.onError(ex);
} else {
a.onComplete();
}
return;
}
missed = WIP.addAndGet(this, -missed);
if (missed == 0) {
break;
}
}
}
@Test
public void testClear() {
Queue<Integer> q = PriorityQueue.of(universe1);
q.clear();
assertEquals(q.size(), 0);
assertTrue(q.isEmpty());
assertNull(q.peek());
assertNull(q.poll());
for (Integer i : universe1)
assertFalse(q.contains(i));
}
@SuppressWarnings("unchecked")
void drainLoop() {
final Queue<Object> q = queue;
final MultiSubscriber<? super Multi<T>> actual = downstream;
UnicastProcessor<T> processor = current;
int missed = 1;
for (;;) {
for (;;) {
if (terminated) {
super.cancel();
q.clear();
timer.cancel();
return;
}
boolean d = done;
Object o = q.poll();
boolean empty = o == null;
boolean isTick = o instanceof WindowTimeoutSubscriber.Tick;
if (d && (empty || isTick)) {
current = null;
q.clear();
Throwable err = failure;
if (err != null) {
processor.onError(err);
} else {
processor.onComplete();
}
timer.cancel();
return;
}
if (empty) {
break;
}
if (isTick) {
processor.onComplete();
processor = UnicastProcessor.create();
current = processor;
long requests = requested.get();
if (requests != 0L) {
actual.onItem(processor);
if (requests != Long.MAX_VALUE) {
requested.decrementAndGet();
}
} else {
current = null;
queue.clear();
actual.onError(new BackPressureFailure("no requests"));
timer.cancel();
return;
}
continue;
}
processor.onNext((T) o);
}
missed = wip.addAndGet(-missed);
if (missed == 0) {
break;
}
}
}
/**
* Traverse a component tree, setting a property on all components for which the property is writable.
*
* @param <T> component type
* @param <T> component type
* @param components The components to traverse.
* @param propertyPath The property path to set.
* @param propertyValue The property value to set.
* @see ObjectPropertyUtils#isWritableProperty(Object, String)
* @see ObjectPropertyUtils#setPropertyValue(Object, String, Object)
*/
public static <T extends Component> void setComponentsPropertyDeep(List<T> components, String propertyPath,
Object propertyValue) {
if (components == null || components.isEmpty()) {
return;
}
Set<Class<?>> skipTypes = null;
@SuppressWarnings("unchecked") Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(
LinkedList.class);
elementQueue.addAll(components);
try {
while (!elementQueue.isEmpty()) {
LifecycleElement currentElement = elementQueue.poll();
if (currentElement == null) {
continue;
}
elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
Class<?> componentClass = currentElement.getClass();
if (skipTypes != null && skipTypes.contains(componentClass)) {
continue;
}
if (!ObjectPropertyUtils.isWritableProperty(currentElement, propertyPath)) {
if (skipTypes == null) {
skipTypes = new HashSet<Class<?>>();
}
skipTypes.add(componentClass);
continue;
}
ObjectPropertyUtils.setPropertyValue(currentElement, propertyPath, propertyValue, true);
}
} finally {
elementQueue.clear();
RecycleUtils.recycle(elementQueue);
}
}
/**
* Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
* collection contains only a single policy instance, no intersection is performed and the instance is directly returned
* as a method call result.
*
* @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
* @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
*
* @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
*/
public Policy intersect(final Policy... policies) {
if (policies == null || policies.length == 0) {
throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
} else if (policies.length == 1) {
return policies[0];
}
// check for "null" and "empty" policy: if such policy is found return "null" policy,
// or if all policies are "empty", return "empty" policy
boolean found = false;
boolean allPoliciesEmpty = true;
NamespaceVersion latestVersion = null;
for (Policy tested : policies) {
if (tested.isEmpty()) {
found = true;
} else {
if (tested.isNull()) {
found = true;
}
allPoliciesEmpty = false;
}
if (latestVersion == null) {
latestVersion = tested.getNamespaceVersion();
} else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) {
latestVersion = tested.getNamespaceVersion();
}
if (found && !allPoliciesEmpty) {
return Policy.createNullPolicy(latestVersion, null, null);
}
}
latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion();
if (allPoliciesEmpty) {
return Policy.createEmptyPolicy(latestVersion, null, null);
}
// simple tests didn't lead to final answer => let's performe some intersecting ;)
final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent());
final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
for (int i = 1; i < policies.length; i++) {
final Collection<AssertionSet> currentAlternatives = policies[i].getContent();
testedAlternatives.clear();
testedAlternatives.addAll(finalAlternatives);
finalAlternatives.clear();
AssertionSet testedAlternative;
while ((testedAlternative = testedAlternatives.poll()) != null) {
for (AssertionSet currentAlternative : currentAlternatives) {
if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) {
alternativesToMerge.add(testedAlternative);
alternativesToMerge.add(currentAlternative);
finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
alternativesToMerge.clear();
}
}
}
}
return Policy.createPolicy(latestVersion, null, null, finalAlternatives);
}
/**
* Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
* collection contains only a single policy instance, no intersection is performed and the instance is directly returned
* as a method call result.
*
* @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
* @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
*
* @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
*/
public Policy intersect(final Policy... policies) {
if (policies == null || policies.length == 0) {
throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
} else if (policies.length == 1) {
return policies[0];
}
// check for "null" and "empty" policy: if such policy is found return "null" policy,
// or if all policies are "empty", return "empty" policy
boolean found = false;
boolean allPoliciesEmpty = true;
NamespaceVersion latestVersion = null;
for (Policy tested : policies) {
if (tested.isEmpty()) {
found = true;
} else {
if (tested.isNull()) {
found = true;
}
allPoliciesEmpty = false;
}
if (latestVersion == null) {
latestVersion = tested.getNamespaceVersion();
} else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) {
latestVersion = tested.getNamespaceVersion();
}
if (found && !allPoliciesEmpty) {
return Policy.createNullPolicy(latestVersion, null, null);
}
}
latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion();
if (allPoliciesEmpty) {
return Policy.createEmptyPolicy(latestVersion, null, null);
}
// simple tests didn't lead to final answer => let's performe some intersecting ;)
final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent());
final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
for (int i = 1; i < policies.length; i++) {
final Collection<AssertionSet> currentAlternatives = policies[i].getContent();
testedAlternatives.clear();
testedAlternatives.addAll(finalAlternatives);
finalAlternatives.clear();
AssertionSet testedAlternative;
while ((testedAlternative = testedAlternatives.poll()) != null) {
for (AssertionSet currentAlternative : currentAlternatives) {
if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) {
alternativesToMerge.add(testedAlternative);
alternativesToMerge.add(currentAlternative);
finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
alternativesToMerge.clear();
}
}
}
}
return Policy.createPolicy(latestVersion, null, null, finalAlternatives);
}
@Test(dataProvider = "populated")
public void clear_whenPopulated(Queue<?> queue) {
queue.clear();
assertThat(queue, is(deeplyEmpty()));
}
/**
* Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
* collection contains only a single policy instance, no intersection is performed and the instance is directly returned
* as a method call result.
*
* @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
* @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
*
* @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
*/
public Policy intersect(final Policy... policies) {
if (policies == null || policies.length == 0) {
throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
} else if (policies.length == 1) {
return policies[0];
}
// check for "null" and "empty" policy: if such policy is found return "null" policy,
// or if all policies are "empty", return "empty" policy
boolean found = false;
boolean allPoliciesEmpty = true;
NamespaceVersion latestVersion = null;
for (Policy tested : policies) {
if (tested.isEmpty()) {
found = true;
} else {
if (tested.isNull()) {
found = true;
}
allPoliciesEmpty = false;
}
if (latestVersion == null) {
latestVersion = tested.getNamespaceVersion();
} else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) {
latestVersion = tested.getNamespaceVersion();
}
if (found && !allPoliciesEmpty) {
return Policy.createNullPolicy(latestVersion, null, null);
}
}
latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion();
if (allPoliciesEmpty) {
return Policy.createEmptyPolicy(latestVersion, null, null);
}
// simple tests didn't lead to final answer => let's performe some intersecting ;)
final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent());
final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
for (int i = 1; i < policies.length; i++) {
final Collection<AssertionSet> currentAlternatives = policies[i].getContent();
testedAlternatives.clear();
testedAlternatives.addAll(finalAlternatives);
finalAlternatives.clear();
AssertionSet testedAlternative;
while ((testedAlternative = testedAlternatives.poll()) != null) {
for (AssertionSet currentAlternative : currentAlternatives) {
if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) {
alternativesToMerge.add(testedAlternative);
alternativesToMerge.add(currentAlternative);
finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
alternativesToMerge.clear();
}
}
}
}
return Policy.createPolicy(latestVersion, null, null, finalAlternatives);
}
@Test
public void testOrdering() throws Exception {
Queue<Object> actual;
Queue<Object> expected;
// emit some out of order events for a given key.
// numerous events are emitted for timestamp 2 to validate support for having
// more than one event at a given timestamp.
testHarness.processElement(record(K1, 1L));
testHarness.processElement(record(K1, 3L));
testHarness.processElement(record(K1, 2L));
testHarness.processElement(record(K1, 2L));
testHarness.processElement(record(K1, 4L));
// advance to timestamp 3, expecting a subset of elements to be emitted.
testHarness.processWatermark(3L);
actual = testHarness.getOutput();
expected = new ConcurrentLinkedQueue<>();
expected.add(record(K1, 1L));
expected.add(record(K1, 2L));
expected.add(record(K1, 2L));
expected.add(record(K1, 3L));
expected.add(watermark(3L));
TestHarnessUtil.assertOutputEquals("Unexpected output", expected, actual);
actual.clear();
// advance to timestamp 4, expecting the final element to be emitted.
testHarness.processWatermark(4L);
actual = testHarness.getOutput();
expected = new ConcurrentLinkedQueue<>();
expected.add(record(K1, 4L));
expected.add(watermark(4L));
TestHarnessUtil.assertOutputEquals("Unexpected output", expected, actual);
actual.clear();
// advance to timestamp 5, expecting no elements to be emitted.
testHarness.processWatermark(5L);
actual = testHarness.getOutput();
expected = new ConcurrentLinkedQueue<>();
expected.add(watermark(5L));
TestHarnessUtil.assertOutputEquals("Unexpected output", expected, actual);
actual.clear();
}
/**
* Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
* collection contains only a single policy instance, no intersection is performed and the instance is directly returned
* as a method call result.
*
* @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
* @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
*
* @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
*/
public Policy intersect(final Policy... policies) {
if (policies == null || policies.length == 0) {
throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
} else if (policies.length == 1) {
return policies[0];
}
// check for "null" and "empty" policy: if such policy is found return "null" policy,
// or if all policies are "empty", return "empty" policy
boolean found = false;
boolean allPoliciesEmpty = true;
NamespaceVersion latestVersion = null;
for (Policy tested : policies) {
if (tested.isEmpty()) {
found = true;
} else {
if (tested.isNull()) {
found = true;
}
allPoliciesEmpty = false;
}
if (latestVersion == null) {
latestVersion = tested.getNamespaceVersion();
} else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) {
latestVersion = tested.getNamespaceVersion();
}
if (found && !allPoliciesEmpty) {
return Policy.createNullPolicy(latestVersion, null, null);
}
}
latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion();
if (allPoliciesEmpty) {
return Policy.createEmptyPolicy(latestVersion, null, null);
}
// simple tests didn't lead to final answer => let's performe some intersecting ;)
final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent());
final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
for (int i = 1; i < policies.length; i++) {
final Collection<AssertionSet> currentAlternatives = policies[i].getContent();
testedAlternatives.clear();
testedAlternatives.addAll(finalAlternatives);
finalAlternatives.clear();
AssertionSet testedAlternative;
while ((testedAlternative = testedAlternatives.poll()) != null) {
for (AssertionSet currentAlternative : currentAlternatives) {
if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) {
alternativesToMerge.add(testedAlternative);
alternativesToMerge.add(currentAlternative);
finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
alternativesToMerge.clear();
}
}
}
}
return Policy.createPolicy(latestVersion, null, null, finalAlternatives);
}
/**
* Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
* collection contains only a single policy instance, no intersection is performed and the instance is directly returned
* as a method call result.
*
* @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
* @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
*
* @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
*/
public Policy intersect(final Policy... policies) {
if (policies == null || policies.length == 0) {
throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
} else if (policies.length == 1) {
return policies[0];
}
// check for "null" and "empty" policy: if such policy is found return "null" policy,
// or if all policies are "empty", return "empty" policy
boolean found = false;
boolean allPoliciesEmpty = true;
NamespaceVersion latestVersion = null;
for (Policy tested : policies) {
if (tested.isEmpty()) {
found = true;
} else {
if (tested.isNull()) {
found = true;
}
allPoliciesEmpty = false;
}
if (latestVersion == null) {
latestVersion = tested.getNamespaceVersion();
} else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) {
latestVersion = tested.getNamespaceVersion();
}
if (found && !allPoliciesEmpty) {
return Policy.createNullPolicy(latestVersion, null, null);
}
}
latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion();
if (allPoliciesEmpty) {
return Policy.createEmptyPolicy(latestVersion, null, null);
}
// simple tests didn't lead to final answer => let's performe some intersecting ;)
final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent());
final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
for (int i = 1; i < policies.length; i++) {
final Collection<AssertionSet> currentAlternatives = policies[i].getContent();
testedAlternatives.clear();
testedAlternatives.addAll(finalAlternatives);
finalAlternatives.clear();
AssertionSet testedAlternative;
while ((testedAlternative = testedAlternatives.poll()) != null) {
for (AssertionSet currentAlternative : currentAlternatives) {
if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) {
alternativesToMerge.add(testedAlternative);
alternativesToMerge.add(currentAlternative);
finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
alternativesToMerge.clear();
}
}
}
}
return Policy.createPolicy(latestVersion, null, null, finalAlternatives);
}
void cancelAndCleanup(Queue<?> q) {
s.cancel();
cancelInner();
q.clear();
}
/**
* Default nodesToLearnSelectionStrategy that returns all nodes from queue.
*
* @param queue Queue.
* @return List of nodes to learn.
*/
private List<TreeNode> defaultNodesToLearnSelectionStrgy(Queue<TreeNode> queue) {
List<TreeNode> res = new ArrayList<>(queue);
queue.clear();
return res;
}