872 if (size < a.length)
873 a[size] = null;
874 return a;
875 }
876 return (size == x.length) ? x : Arrays.copyOf(x, size);
877 }
878 }
879
880 /**
881 * Returns an array containing all of the elements in this queue, in
882 * proper sequence.
883 *
884 * <p>The returned array will be "safe" in that no references to it are
885 * maintained by this queue. (In other words, this method must allocate
886 * a new array). The caller is thus free to modify the returned array.
887 *
888 * <p>This method acts as bridge between array-based and collection-based
889 * APIs.
890 *
891 * @return an array containing all of the elements in this queue
892 */
893 public Object[] toArray() {
894 return toArrayInternal(null);
895 }
896
897 /**
898 * Returns an array containing all of the elements in this queue, in
899 * proper sequence; the runtime type of the returned array is that of
900 * the specified array. If the queue fits in the specified array, it
901 * is returned therein. Otherwise, a new array is allocated with the
902 * runtime type of the specified array and the size of this queue.
903 *
904 * <p>If this queue fits in the specified array with room to spare
905 * (i.e., the array has more elements than this queue), the element in
906 * the array immediately following the end of the queue is set to
907 * {@code null}.
908 *
909 * <p>Like the {@link #toArray()} method, this method acts as bridge between
910 * array-based and collection-based APIs. Further, this method allows
911 * precise control over the runtime type of the output array, and may,
912 * under certain circumstances, be used to save allocation costs.
913 *
914 * <p>Suppose {@code x} is a queue known to contain only strings.
915 * The following code can be used to dump the queue into a newly
916 * allocated array of {@code String}:
917 *
918 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
919 *
920 * Note that {@code toArray(new Object[0])} is identical in function to
921 * {@code toArray()}.
922 *
923 * @param a the array into which the elements of the queue are to
924 * be stored, if it is big enough; otherwise, a new array of the
925 * same runtime type is allocated for this purpose
926 * @return an array containing all of the elements in this queue
927 * @throws ArrayStoreException if the runtime type of the specified array
928 * is not a supertype of the runtime type of every element in
929 * this queue
930 * @throws NullPointerException if the specified array is null
931 */
932 @SuppressWarnings("unchecked")
933 public <T> T[] toArray(T[] a) {
934 Objects.requireNonNull(a);
935 return (T[]) toArrayInternal(a);
936 }
937
938 /**
939 * Weakly-consistent iterator.
940 *
941 * Lazily updated ancestor is expected to be amortized O(1) remove(),
942 * but O(n) in the worst case, when lastRet is concurrently deleted.
943 */
944 final class Itr implements Iterator<E> {
945 private Node nextNode; // next node to return item for
946 private E nextItem; // the corresponding item
947 private Node lastRet; // last returned node, to support remove
948 private Node ancestor; // Helps unlink lastRet on remove()
949
950 /**
1602 throws java.io.IOException, ClassNotFoundException {
1603
1604 // Read in elements until trailing null sentinel found
1605 Node h = null, t = null;
1606 for (Object item; (item = s.readObject()) != null; ) {
1607 @SuppressWarnings("unchecked")
1608 Node newNode = new Node((E) item);
1609 if (h == null)
1610 h = t = newNode;
1611 else
1612 t.appendRelaxed(t = newNode);
1613 }
1614 if (h == null)
1615 h = t = new Node();
1616 head = h;
1617 tail = t;
1618 }
1619
1620 /**
1621 * @throws NullPointerException {@inheritDoc}
1622 */
1623 public boolean removeIf(Predicate<? super E> filter) {
1624 Objects.requireNonNull(filter);
1625 return bulkRemove(filter);
1626 }
1627
1628 /**
1629 * @throws NullPointerException {@inheritDoc}
1630 */
1631 public boolean removeAll(Collection<?> c) {
1632 Objects.requireNonNull(c);
1633 return bulkRemove(e -> c.contains(e));
1634 }
1635
1636 /**
1637 * @throws NullPointerException {@inheritDoc}
1638 */
1639 public boolean retainAll(Collection<?> c) {
1640 Objects.requireNonNull(c);
1641 return bulkRemove(e -> !c.contains(e));
1642 }
1643
1644 public void clear() {
1645 bulkRemove(e -> true);
1646 }
1647
1648 /**
1649 * Tolerate this many consecutive dead nodes before CAS-collapsing.
1650 * Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element.
1651 */
1652 private static final int MAX_HOPS = 8;
1653
1654 /** Implementation of bulk remove methods. */
1655 @SuppressWarnings("unchecked")
1656 private boolean bulkRemove(Predicate<? super E> filter) {
1657 boolean removed = false;
1700 final Object item;
1701 if ((item = p.item) != null) {
1702 if (p.isData) {
1703 action.accept((E) item);
1704 pred = p; p = q; continue;
1705 }
1706 }
1707 else if (!p.isData)
1708 break;
1709 for (Node c = p;; q = p.next) {
1710 if (q == null || !q.isMatched()) {
1711 pred = skipDeadNodes(pred, c, p, q); p = q; break;
1712 }
1713 if (p == (p = q)) { pred = null; p = head; break; }
1714 }
1715 }
1716 }
1717
1718 /**
1719 * @throws NullPointerException {@inheritDoc}
1720 */
1721 public void forEach(Consumer<? super E> action) {
1722 Objects.requireNonNull(action);
1723 forEachFrom(action, head);
1724 }
1725
1726 // VarHandle mechanics
1727 private static final VarHandle HEAD;
1728 private static final VarHandle TAIL;
1729 private static final VarHandle SWEEPVOTES;
1730 static final VarHandle ITEM;
1731 static final VarHandle NEXT;
1732 static final VarHandle WAITER;
1733 static {
1734 try {
1735 MethodHandles.Lookup l = MethodHandles.lookup();
1736 HEAD = l.findVarHandle(LinkedTransferQueue.class, "head",
1737 Node.class);
1738 TAIL = l.findVarHandle(LinkedTransferQueue.class, "tail",
1739 Node.class);
|
872 if (size < a.length)
873 a[size] = null;
874 return a;
875 }
876 return (size == x.length) ? x : Arrays.copyOf(x, size);
877 }
878 }
879
880 /**
881 * Returns an array containing all of the elements in this queue, in
882 * proper sequence.
883 *
884 * <p>The returned array will be "safe" in that no references to it are
885 * maintained by this queue. (In other words, this method must allocate
886 * a new array). The caller is thus free to modify the returned array.
887 *
888 * <p>This method acts as bridge between array-based and collection-based
889 * APIs.
890 *
891 * @return an array containing all of the elements in this queue
892 * @since 9
893 */
894 public Object[] toArray() {
895 return toArrayInternal(null);
896 }
897
898 /**
899 * Returns an array containing all of the elements in this queue, in
900 * proper sequence; the runtime type of the returned array is that of
901 * the specified array. If the queue fits in the specified array, it
902 * is returned therein. Otherwise, a new array is allocated with the
903 * runtime type of the specified array and the size of this queue.
904 *
905 * <p>If this queue fits in the specified array with room to spare
906 * (i.e., the array has more elements than this queue), the element in
907 * the array immediately following the end of the queue is set to
908 * {@code null}.
909 *
910 * <p>Like the {@link #toArray()} method, this method acts as bridge between
911 * array-based and collection-based APIs. Further, this method allows
912 * precise control over the runtime type of the output array, and may,
913 * under certain circumstances, be used to save allocation costs.
914 *
915 * <p>Suppose {@code x} is a queue known to contain only strings.
916 * The following code can be used to dump the queue into a newly
917 * allocated array of {@code String}:
918 *
919 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
920 *
921 * Note that {@code toArray(new Object[0])} is identical in function to
922 * {@code toArray()}.
923 *
924 * @param a the array into which the elements of the queue are to
925 * be stored, if it is big enough; otherwise, a new array of the
926 * same runtime type is allocated for this purpose
927 * @return an array containing all of the elements in this queue
928 * @throws ArrayStoreException if the runtime type of the specified array
929 * is not a supertype of the runtime type of every element in
930 * this queue
931 * @throws NullPointerException if the specified array is null
932 * @since 9
933 */
934 @SuppressWarnings("unchecked")
935 public <T> T[] toArray(T[] a) {
936 Objects.requireNonNull(a);
937 return (T[]) toArrayInternal(a);
938 }
939
940 /**
941 * Weakly-consistent iterator.
942 *
943 * Lazily updated ancestor is expected to be amortized O(1) remove(),
944 * but O(n) in the worst case, when lastRet is concurrently deleted.
945 */
946 final class Itr implements Iterator<E> {
947 private Node nextNode; // next node to return item for
948 private E nextItem; // the corresponding item
949 private Node lastRet; // last returned node, to support remove
950 private Node ancestor; // Helps unlink lastRet on remove()
951
952 /**
1604 throws java.io.IOException, ClassNotFoundException {
1605
1606 // Read in elements until trailing null sentinel found
1607 Node h = null, t = null;
1608 for (Object item; (item = s.readObject()) != null; ) {
1609 @SuppressWarnings("unchecked")
1610 Node newNode = new Node((E) item);
1611 if (h == null)
1612 h = t = newNode;
1613 else
1614 t.appendRelaxed(t = newNode);
1615 }
1616 if (h == null)
1617 h = t = new Node();
1618 head = h;
1619 tail = t;
1620 }
1621
1622 /**
1623 * @throws NullPointerException {@inheritDoc}
1624 * @since 9
1625 */
1626 public boolean removeIf(Predicate<? super E> filter) {
1627 Objects.requireNonNull(filter);
1628 return bulkRemove(filter);
1629 }
1630
1631 /**
1632 * @throws NullPointerException {@inheritDoc}
1633 * @since 9
1634 */
1635 public boolean removeAll(Collection<?> c) {
1636 Objects.requireNonNull(c);
1637 return bulkRemove(e -> c.contains(e));
1638 }
1639
1640 /**
1641 * @throws NullPointerException {@inheritDoc}
1642 * @since 9
1643 */
1644 public boolean retainAll(Collection<?> c) {
1645 Objects.requireNonNull(c);
1646 return bulkRemove(e -> !c.contains(e));
1647 }
1648
1649 public void clear() {
1650 bulkRemove(e -> true);
1651 }
1652
1653 /**
1654 * Tolerate this many consecutive dead nodes before CAS-collapsing.
1655 * Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element.
1656 */
1657 private static final int MAX_HOPS = 8;
1658
1659 /** Implementation of bulk remove methods. */
1660 @SuppressWarnings("unchecked")
1661 private boolean bulkRemove(Predicate<? super E> filter) {
1662 boolean removed = false;
1705 final Object item;
1706 if ((item = p.item) != null) {
1707 if (p.isData) {
1708 action.accept((E) item);
1709 pred = p; p = q; continue;
1710 }
1711 }
1712 else if (!p.isData)
1713 break;
1714 for (Node c = p;; q = p.next) {
1715 if (q == null || !q.isMatched()) {
1716 pred = skipDeadNodes(pred, c, p, q); p = q; break;
1717 }
1718 if (p == (p = q)) { pred = null; p = head; break; }
1719 }
1720 }
1721 }
1722
1723 /**
1724 * @throws NullPointerException {@inheritDoc}
1725 * @since 9
1726 */
1727 public void forEach(Consumer<? super E> action) {
1728 Objects.requireNonNull(action);
1729 forEachFrom(action, head);
1730 }
1731
1732 // VarHandle mechanics
1733 private static final VarHandle HEAD;
1734 private static final VarHandle TAIL;
1735 private static final VarHandle SWEEPVOTES;
1736 static final VarHandle ITEM;
1737 static final VarHandle NEXT;
1738 static final VarHandle WAITER;
1739 static {
1740 try {
1741 MethodHandles.Lookup l = MethodHandles.lookup();
1742 HEAD = l.findVarHandle(LinkedTransferQueue.class, "head",
1743 Node.class);
1744 TAIL = l.findVarHandle(LinkedTransferQueue.class, "tail",
1745 Node.class);
|