ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/LinkedTransferQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/LinkedTransferQueue.java (file contents):
Revision 1.81 by jsr166, Wed Feb 18 06:39:40 2015 UTC vs.
Revision 1.82 by jsr166, Fri Feb 20 03:09:08 2015 UTC

# Line 7 | Line 7
7   package java.util.concurrent;
8  
9   import java.util.AbstractQueue;
10 + import java.util.Arrays;
11   import java.util.Collection;
12   import java.util.Iterator;
13   import java.util.NoSuchElementException;
# Line 800 | Line 801 | public class LinkedTransferQueue<E> exte
801          }
802      }
803  
804 +    public String toString() {
805 +        String[] a = null;
806 +        restartFromHead: for (;;) {
807 +            int charLength = 0;
808 +            int size = 0;
809 +            for (Node p = head; p != null;) {
810 +                Object item = p.item;
811 +                if (p.isData) {
812 +                    if (item != null && item != p) {
813 +                        if (a == null)
814 +                            a = new String[4];
815 +                        else if (size == a.length)
816 +                            a = Arrays.copyOf(a, 2 * size);
817 +                        String s = item.toString();
818 +                        a[size++] = s;
819 +                        charLength += s.length();
820 +                    }
821 +                } else if (item == null)
822 +                    break;
823 +                if (p == (p = p.next))
824 +                    continue restartFromHead;
825 +            }
826 +
827 +            if (size == 0)
828 +                return "[]";
829 +
830 +            // Copy each string into a perfectly sized char[]
831 +            final char[] chars = new char[charLength + 2 * size];
832 +            chars[0] = '[';
833 +            int j = 1;
834 +            for (int i = 0; i < size; i++) {
835 +                if (i > 0) {
836 +                    chars[j++] = ',';
837 +                    chars[j++] = ' ';
838 +                }
839 +                String s = a[i];
840 +                int len = s.length();
841 +                s.getChars(0, len, chars, j);
842 +                j += len;
843 +            }
844 +            chars[j] = ']';
845 +            return new String(chars);
846 +        }
847 +    }
848 +
849 +    private Object[] toArrayInternal(Object[] a) {
850 +        Object[] x = a;
851 +        restartFromHead: for (;;) {
852 +            int size = 0;
853 +            for (Node p = head; p != null;) {
854 +                Object item = p.item;
855 +                if (p.isData) {
856 +                    if (item != null && item != p) {
857 +                        if (x == null)
858 +                            x = new Object[4];
859 +                        else if (size == x.length)
860 +                            x = Arrays.copyOf(x, 2 * (size + 4));
861 +                        x[size++] = item;
862 +                    }
863 +                } else if (item == null)
864 +                    break;
865 +                if (p == (p = p.next))
866 +                    continue restartFromHead;
867 +            }
868 +            if (x == null)
869 +                return new Object[0];
870 +            else if (a != null && size <= a.length) {
871 +                if (a != x)
872 +                    System.arraycopy(x, 0, a, 0, size);
873 +                if (size < a.length)
874 +                    a[size] = null;
875 +                return a;
876 +            }
877 +            return (size == x.length) ? x : Arrays.copyOf(x, size);
878 +        }
879 +    }
880 +
881 +    /**
882 +     * Returns an array containing all of the elements in this queue, in
883 +     * proper sequence.
884 +     *
885 +     * <p>The returned array will be "safe" in that no references to it are
886 +     * maintained by this queue.  (In other words, this method must allocate
887 +     * a new array).  The caller is thus free to modify the returned array.
888 +     *
889 +     * <p>This method acts as bridge between array-based and collection-based
890 +     * APIs.
891 +     *
892 +     * @return an array containing all of the elements in this queue
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 +     */
933 +    @SuppressWarnings("unchecked")
934 +    public <T> T[] toArray(T[] a) {
935 +        if (a == null) throw new NullPointerException();
936 +        return (T[]) toArrayInternal(a);
937 +    }
938 +
939      final class Itr implements Iterator<E> {
940          private Node nextNode;   // next node to return item for
941          private E nextItem;      // the corresponding item

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines