< prev index next >

src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java

Print this page




1552         s.defaultReadObject();
1553 
1554         // Read in elements until trailing null sentinel found
1555         Node<E> h = null, t = null;
1556         for (Object item; (item = s.readObject()) != null; ) {
1557             @SuppressWarnings("unchecked")
1558             Node<E> newNode = newNode((E) item);
1559             if (h == null)
1560                 h = t = newNode;
1561             else {
1562                 NEXT.set(t, newNode);
1563                 PREV.set(newNode, t);
1564                 t = newNode;
1565             }
1566         }
1567         initHeadTail(h, t);
1568     }
1569 
1570     /**
1571      * @throws NullPointerException {@inheritDoc}

1572      */
1573     public boolean removeIf(Predicate<? super E> filter) {
1574         Objects.requireNonNull(filter);
1575         return bulkRemove(filter);
1576     }
1577 
1578     /**
1579      * @throws NullPointerException {@inheritDoc}

1580      */
1581     public boolean removeAll(Collection<?> c) {
1582         Objects.requireNonNull(c);
1583         return bulkRemove(e -> c.contains(e));
1584     }
1585 
1586     /**
1587      * @throws NullPointerException {@inheritDoc}

1588      */
1589     public boolean retainAll(Collection<?> c) {
1590         Objects.requireNonNull(c);
1591         return bulkRemove(e -> !c.contains(e));
1592     }
1593 
1594     /** Implementation of bulk remove methods. */
1595     private boolean bulkRemove(Predicate<? super E> filter) {
1596         boolean removed = false;
1597         for (Node<E> p = first(), succ; p != null; p = succ) {
1598             succ = succ(p);
1599             final E item;
1600             if ((item = p.item) != null
1601                 && filter.test(item)
1602                 && ITEM.compareAndSet(p, item, null)) {
1603                 unlink(p);
1604                 removed = true;
1605             }
1606         }
1607         return removed;
1608     }
1609 
1610     /**
1611      * @throws NullPointerException {@inheritDoc}

1612      */
1613     public void forEach(Consumer<? super E> action) {
1614         Objects.requireNonNull(action);
1615         E item;
1616         for (Node<E> p = first(); p != null; p = succ(p))
1617             if ((item = p.item) != null)
1618                 action.accept(item);
1619     }
1620 
1621     // VarHandle mechanics
1622     private static final VarHandle HEAD;
1623     private static final VarHandle TAIL;
1624     private static final VarHandle PREV;
1625     private static final VarHandle NEXT;
1626     private static final VarHandle ITEM;
1627     static {
1628         PREV_TERMINATOR = new Node<Object>();
1629         PREV_TERMINATOR.next = PREV_TERMINATOR;
1630         NEXT_TERMINATOR = new Node<Object>();
1631         NEXT_TERMINATOR.prev = NEXT_TERMINATOR;


1552         s.defaultReadObject();
1553 
1554         // Read in elements until trailing null sentinel found
1555         Node<E> h = null, t = null;
1556         for (Object item; (item = s.readObject()) != null; ) {
1557             @SuppressWarnings("unchecked")
1558             Node<E> newNode = newNode((E) item);
1559             if (h == null)
1560                 h = t = newNode;
1561             else {
1562                 NEXT.set(t, newNode);
1563                 PREV.set(newNode, t);
1564                 t = newNode;
1565             }
1566         }
1567         initHeadTail(h, t);
1568     }
1569 
1570     /**
1571      * @throws NullPointerException {@inheritDoc}
1572      * @since 9
1573      */
1574     public boolean removeIf(Predicate<? super E> filter) {
1575         Objects.requireNonNull(filter);
1576         return bulkRemove(filter);
1577     }
1578 
1579     /**
1580      * @throws NullPointerException {@inheritDoc}
1581      * @since 9
1582      */
1583     public boolean removeAll(Collection<?> c) {
1584         Objects.requireNonNull(c);
1585         return bulkRemove(e -> c.contains(e));
1586     }
1587 
1588     /**
1589      * @throws NullPointerException {@inheritDoc}
1590      * @since 9
1591      */
1592     public boolean retainAll(Collection<?> c) {
1593         Objects.requireNonNull(c);
1594         return bulkRemove(e -> !c.contains(e));
1595     }
1596 
1597     /** Implementation of bulk remove methods. */
1598     private boolean bulkRemove(Predicate<? super E> filter) {
1599         boolean removed = false;
1600         for (Node<E> p = first(), succ; p != null; p = succ) {
1601             succ = succ(p);
1602             final E item;
1603             if ((item = p.item) != null
1604                 && filter.test(item)
1605                 && ITEM.compareAndSet(p, item, null)) {
1606                 unlink(p);
1607                 removed = true;
1608             }
1609         }
1610         return removed;
1611     }
1612 
1613     /**
1614      * @throws NullPointerException {@inheritDoc}
1615      * @since 9
1616      */
1617     public void forEach(Consumer<? super E> action) {
1618         Objects.requireNonNull(action);
1619         E item;
1620         for (Node<E> p = first(); p != null; p = succ(p))
1621             if ((item = p.item) != null)
1622                 action.accept(item);
1623     }
1624 
1625     // VarHandle mechanics
1626     private static final VarHandle HEAD;
1627     private static final VarHandle TAIL;
1628     private static final VarHandle PREV;
1629     private static final VarHandle NEXT;
1630     private static final VarHandle ITEM;
1631     static {
1632         PREV_TERMINATOR = new Node<Object>();
1633         PREV_TERMINATOR.next = PREV_TERMINATOR;
1634         NEXT_TERMINATOR = new Node<Object>();
1635         NEXT_TERMINATOR.prev = NEXT_TERMINATOR;
< prev index next >