Java Code Examples for java.util.ArrayDeque#offer()

The following examples show how to use java.util.ArrayDeque#offer() . 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: NullAwayNativeModels.java    From NullAway with MIT License 6 votes vote down vote up
static void arrayDequeStuff() {
  ArrayDeque<Object> d = new ArrayDeque<>();
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.add(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.addFirst(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.addLast(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offerFirst(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offerLast(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offer(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.push(null);
  Object[] o = null;
  // BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
  d.toArray(o);
  // this should be fine
  d.toArray();
}
 
Example 2
Source File: QueueDrainHelperTest.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Test
public void postCompleteWithRequest() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {
        @Override
        public boolean getAsBoolean() throws Exception {
            return false;
        }
    };

    ts.onSubscribe(new BooleanSubscription());
    queue.offer(1);
    state.getAndIncrement();

    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);

    ts.assertResult(1);
}
 
Example 3
Source File: QueueDrainHelperTest.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Test
public void postCompleteCancelled() {
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {
        @Override
        public boolean getAsBoolean() throws Exception {
            return ts.isCancelled();
        }
    };

    ts.onSubscribe(new BooleanSubscription());
    queue.offer(1);
    state.getAndIncrement();
    ts.cancel();

    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);

    ts.assertEmpty();
}
 
Example 4
Source File: QueueDrainHelperTest.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Test
public void postCompleteCancelledAfterOne() {
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            cancel();
        }
    };
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {
        @Override
        public boolean getAsBoolean() throws Exception {
            return ts.isCancelled();
        }
    };

    ts.onSubscribe(new BooleanSubscription());
    queue.offer(1);
    state.getAndIncrement();

    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);

    ts.assertValue(1).assertNoErrors().assertNotComplete();
}
 
Example 5
Source File: ClientStateIndicationTest.java    From tigase-extension with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testMessagesToInactive() throws XMPPException, TigaseStringprepException {
    String recipient = "recipient-1@localhost";
    JID recp1 = JID.jidInstanceNS(recipient + "/res1");
    JID connId1 = JID.jidInstanceNS("c2s@localhost/recipient1-res1");
    XMPPResourceConnection session1 = getSession(connId1, recp1);

    enableCSI(session1);

    ArrayDeque<Packet> results = new ArrayDeque<>();
    Packet p = Packet.packetInstance("message", "sender-1@localhost/res1", recp1.toString(), StanzaType.chat);
    p.setPacketTo(connId1);
    results.offer(p);
    Packet[] expected = results.toArray(new Packet[results.size()]);
    csi.filter(p, session1, null, results);
    Packet[] processed = results.toArray(new Packet[results.size()]);
    Assert.assertArrayEquals(expected, processed);
}
 
Example 6
Source File: ClientStateIndicationTest.java    From tigase-extension with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testPresenceToInactive() throws XMPPException, TigaseStringprepException {
    String recipient = "recipient-1@localhost";
    JID recp1 = JID.jidInstanceNS(recipient + "/res1");
    JID connId1 = JID.jidInstanceNS("c2s@localhost/recipient1-res1");
    XMPPResourceConnection session1 = getSession(connId1, recp1);

    enableCSI(session1);

    ArrayDeque<Packet> results = new ArrayDeque<>();
    Packet p = Packet.packetInstance("presence", "sender-1@localhost/res1", recp1.toString(), StanzaType.available);
    p.setPacketTo(connId1);
    results.offer(p);
    Packet[] expected = new Packet[0];
    csi.filter(p, session1, null, results);
    Packet[] processed = results.toArray(new Packet[results.size()]);
    Assert.assertArrayEquals(expected, processed);
}
 
Example 7
Source File: QueueDrainHelperTest.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Test
public void completeRequestRace() {
    for (int i = 0; i < 500; i++) {
        final TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
        final ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
        final AtomicLong state = new AtomicLong();
        final BooleanSupplier isCancelled = new BooleanSupplier() {
            @Override
            public boolean getAsBoolean() throws Exception {
                return false;
            }
        };

        ts.onSubscribe(new BooleanSubscription());
        queue.offer(1);

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                QueueDrainHelper.postCompleteRequest(1, ts, queue, state, isCancelled);
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                QueueDrainHelper.postComplete(ts, queue, state, isCancelled);
            }
        };

        TestCommonHelper.race(r1, r2);

        ts.assertResult(1);
    }
}
 
Example 8
Source File: ArrayDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * offer(null) throws NPE
 */
public void testOfferNull() {
    ArrayDeque q = new ArrayDeque();
    try {
        q.offer(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 9
Source File: ByteBufProxy.java    From lmdbjava with Apache License 2.0 5 votes vote down vote up
@Override
protected void deallocate(final ByteBuf buff) {
  final ArrayDeque<ByteBuf> queue = BUFFERS.get();
  if (!queue.offer(buff)) {
    buff.release();
  }
}
 
Example 10
Source File: SchemaModifier.java    From requery with Apache License 2.0 5 votes vote down vote up
private ArrayList<Type<?>> sortTypes() {
    // sort the types in table creation order to avoid referencing not created table via a
    // reference (could also add constraints at the end but SQLite doesn't support that)
    ArrayDeque<Type<?>> queue = new ArrayDeque<>(model.getTypes());
    ArrayList<Type<?>> sorted = new ArrayList<>();
    while (!queue.isEmpty()) {
        Type<?> type = queue.poll();

        if (type.isView()) {
            continue;
        }

        Set<Type<?>> referencing = referencedTypesOf(type);
        for (Type<?> referenced : referencing) {
            Set<Type<?>> backReferences = referencedTypesOf(referenced);
            if (backReferences.contains(type)) {
                throw new CircularReferenceException("circular reference detected between "
                    + type.getName() + " and " + referenced.getName());
            }
        }
        if (referencing.isEmpty() || sorted.containsAll(referencing)) {
            sorted.add(type);
            queue.remove(type);
        } else {
            queue.offer(type); // put back
        }
    }
    return sorted;
}
 
Example 11
Source File: PublisherTakeLast.java    From reactive-streams-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(T t) {
    ArrayDeque<T> bs = buffer;

    if (bs.size() == n) {
        bs.poll();
    }
    bs.offer(t);
}
 
Example 12
Source File: ArrayDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * offer(null) throws NPE
 */
public void testOfferNull() {
    ArrayDeque q = new ArrayDeque();
    try {
        q.offer(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 13
Source File: ClientStateIndicationTest.java    From tigase-extension with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFlushStopping() throws XMPPException, TigaseStringprepException {
    String recipient = "recipient-1@localhost";
    JID recp1 = JID.jidInstanceNS(recipient + "/res1");
    JID connId1 = JID.jidInstanceNS("c2s@localhost/recipient1-res1");
    XMPPResourceConnection session1 = getSession(connId1, recp1);

    enableCSI(session1);

    ArrayDeque<Packet> results = new ArrayDeque<>();
    Packet p = Packet.packetInstance("presence", "sender-1@localhost/res1", recp1.toString(), StanzaType.available);
    p.setPacketTo(connId1);
    csi.filter(p, session1, null, results);

    results.clear();
    Packet m = Packet.packetInstance("message", "sender-1@localhost/res1", recp1.toString(), StanzaType.chat);
    m.getElement().addChild(new Element("received", new String[]{ "xmlns" }, new String[] { "urn:xmpp:receipts" }));
    m.setPacketTo(connId1);
    results.offer(m);
    csi.filter(m, session1, null, results);

    results.clear();
    results.offer(m);
    results.clear();
    csi.stopped(session1, results, new HashMap<>());
    Packet[] processed = results.toArray(new Packet[results.size()]);
    Assert.assertEquals(0, processed.length);
}
 
Example 14
Source File: ByteBufferProxy.java    From lmdbjava with Apache License 2.0 4 votes vote down vote up
@Override
protected final void deallocate(final ByteBuffer buff) {
  buff.order(BIG_ENDIAN);
  final ArrayDeque<ByteBuffer> queue = BUFFERS.get();
  queue.offer(buff);
}
 
Example 15
Source File: OrderedCancellableSpliterator.java    From streamex with Apache License 2.0 4 votes vote down vote up
@Override
public boolean tryAdvance(Consumer<? super A> action) {
    Spliterator<T> source = this.source;
    if (source == null || localCancelled) {
        this.source = null;
        return false;
    }
    A acc = supplier.get();
    try {
        source.forEachRemaining(t -> {
            accumulator.accept(acc, t);
            if (cancelPredicate.test(acc)) {
                cancelSuffix();
                throw new CancelException();
            }
            if (localCancelled) {
                throw new CancelException();
            }
        });
    } catch (CancelException ex) {
        if (localCancelled) {
            return false;
        }
    }
    this.source = null;
    A result = acc;
    while (true) {
        if (prefix == null && suffix == null) {
            action.accept(result);
            return true;
        }
        ArrayDeque<A> res = new ArrayDeque<>();
        res.offer(result);
        synchronized (lock) {
            if (localCancelled)
                return false;
            OrderedCancellableSpliterator<T, A> s = prefix;
            while (s != null) {
                if (s.payload == null)
                    break;
                res.offerFirst(s.payload);
                s = s.prefix;
            }
            prefix = s;
            if (s != null) {
                s.suffix = this;
            }
            s = suffix;
            while (s != null) {
                if (s.payload == null)
                    break;
                res.offerLast(s.payload);
                s = s.suffix;
            }
            suffix = s;
            if (s != null) {
                s.prefix = this;
            }
            if (res.size() == 1) {
                if (prefix == null && suffix == null) {
                    action.accept(result);
                    return true;
                }
                this.payload = result;
                break;
            }
        }
        result = res.pollFirst();
        while (!res.isEmpty()) {
            result = combiner.apply(result, res.pollFirst());
            if (cancelPredicate.test(result)) {
                cancelSuffix();
            }
        }
    }
    return false;
}
 
Example 16
Source File: PublisherSkipLast.java    From reactive-streams-commons with Apache License 2.0 3 votes vote down vote up
@Override
public void onNext(T t) {

    ArrayDeque<T> bs = buffer;

    if (bs.size() == n) {
        T v = bs.poll();

        actual.onNext(v);
    }
    bs.offer(t);

}