300 */
301 public void addLast(E e) {
302 if (e == null)
303 throw new NullPointerException();
304 final Object[] es = elements;
305 es[tail] = e;
306 if (head == (tail = inc(tail, es.length)))
307 grow(1);
308 }
309
310 /**
311 * Adds all of the elements in the specified collection at the end
312 * of this deque, as if by calling {@link #addLast} on each one,
313 * in the order that they are returned by the collection's
314 * iterator.
315 *
316 * @param c the elements to be inserted into this deque
317 * @return {@code true} if this deque changed as a result of the call
318 * @throws NullPointerException if the specified collection or any
319 * of its elements are null
320 */
321 public boolean addAll(Collection<? extends E> c) {
322 final int s, needed;
323 if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
324 grow(needed);
325 c.forEach(this::addLast);
326 return size() > s;
327 }
328
329 /**
330 * Inserts the specified element at the front of this deque.
331 *
332 * @param e the element to add
333 * @return {@code true} (as specified by {@link Deque#offerFirst})
334 * @throws NullPointerException if the specified element is null
335 */
336 public boolean offerFirst(E e) {
337 addFirst(e);
338 return true;
339 }
858 E e = nonNullElementAt(es, i);
859 cursor = inc(i, es.length);
860 action.accept(e);
861 return true;
862 }
863
864 public long estimateSize() {
865 return sub(getFence(), cursor, elements.length);
866 }
867
868 public int characteristics() {
869 return Spliterator.NONNULL
870 | Spliterator.ORDERED
871 | Spliterator.SIZED
872 | Spliterator.SUBSIZED;
873 }
874 }
875
876 /**
877 * @throws NullPointerException {@inheritDoc}
878 */
879 public void forEach(Consumer<? super E> action) {
880 Objects.requireNonNull(action);
881 final Object[] es = elements;
882 for (int i = head, end = tail, to = (i <= end) ? end : es.length;
883 ; i = 0, to = end) {
884 for (; i < to; i++)
885 action.accept(elementAt(es, i));
886 if (to == end) {
887 if (end != tail) throw new ConcurrentModificationException();
888 break;
889 }
890 }
891 }
892
893 /**
894 * @throws NullPointerException {@inheritDoc}
895 */
896 public boolean removeIf(Predicate<? super E> filter) {
897 Objects.requireNonNull(filter);
898 return bulkRemove(filter);
899 }
900
901 /**
902 * @throws NullPointerException {@inheritDoc}
903 */
904 public boolean removeAll(Collection<?> c) {
905 Objects.requireNonNull(c);
906 return bulkRemove(e -> c.contains(e));
907 }
908
909 /**
910 * @throws NullPointerException {@inheritDoc}
911 */
912 public boolean retainAll(Collection<?> c) {
913 Objects.requireNonNull(c);
914 return bulkRemove(e -> !c.contains(e));
915 }
916
917 /** Implementation of bulk remove methods. */
918 private boolean bulkRemove(Predicate<? super E> filter) {
919 final Object[] es = elements;
920 // Optimize for initial run of survivors
921 for (int i = head, end = tail, to = (i <= end) ? end : es.length;
922 ; i = 0, to = end) {
923 for (; i < to; i++)
924 if (filter.test(elementAt(es, i)))
925 return bulkRemoveModified(filter, i);
926 if (to == end) {
927 if (end != tail) throw new ConcurrentModificationException();
928 break;
929 }
930 }
|
300 */
301 public void addLast(E e) {
302 if (e == null)
303 throw new NullPointerException();
304 final Object[] es = elements;
305 es[tail] = e;
306 if (head == (tail = inc(tail, es.length)))
307 grow(1);
308 }
309
310 /**
311 * Adds all of the elements in the specified collection at the end
312 * of this deque, as if by calling {@link #addLast} on each one,
313 * in the order that they are returned by the collection's
314 * iterator.
315 *
316 * @param c the elements to be inserted into this deque
317 * @return {@code true} if this deque changed as a result of the call
318 * @throws NullPointerException if the specified collection or any
319 * of its elements are null
320 * @since 9
321 */
322 public boolean addAll(Collection<? extends E> c) {
323 final int s, needed;
324 if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
325 grow(needed);
326 c.forEach(this::addLast);
327 return size() > s;
328 }
329
330 /**
331 * Inserts the specified element at the front of this deque.
332 *
333 * @param e the element to add
334 * @return {@code true} (as specified by {@link Deque#offerFirst})
335 * @throws NullPointerException if the specified element is null
336 */
337 public boolean offerFirst(E e) {
338 addFirst(e);
339 return true;
340 }
859 E e = nonNullElementAt(es, i);
860 cursor = inc(i, es.length);
861 action.accept(e);
862 return true;
863 }
864
865 public long estimateSize() {
866 return sub(getFence(), cursor, elements.length);
867 }
868
869 public int characteristics() {
870 return Spliterator.NONNULL
871 | Spliterator.ORDERED
872 | Spliterator.SIZED
873 | Spliterator.SUBSIZED;
874 }
875 }
876
877 /**
878 * @throws NullPointerException {@inheritDoc}
879 * @since 9
880 */
881 public void forEach(Consumer<? super E> action) {
882 Objects.requireNonNull(action);
883 final Object[] es = elements;
884 for (int i = head, end = tail, to = (i <= end) ? end : es.length;
885 ; i = 0, to = end) {
886 for (; i < to; i++)
887 action.accept(elementAt(es, i));
888 if (to == end) {
889 if (end != tail) throw new ConcurrentModificationException();
890 break;
891 }
892 }
893 }
894
895 /**
896 * @throws NullPointerException {@inheritDoc}
897 * @since 9
898 */
899 public boolean removeIf(Predicate<? super E> filter) {
900 Objects.requireNonNull(filter);
901 return bulkRemove(filter);
902 }
903
904 /**
905 * @throws NullPointerException {@inheritDoc}
906 * @since 9
907 */
908 public boolean removeAll(Collection<?> c) {
909 Objects.requireNonNull(c);
910 return bulkRemove(e -> c.contains(e));
911 }
912
913 /**
914 * @throws NullPointerException {@inheritDoc}
915 * @since 9
916 */
917 public boolean retainAll(Collection<?> c) {
918 Objects.requireNonNull(c);
919 return bulkRemove(e -> !c.contains(e));
920 }
921
922 /** Implementation of bulk remove methods. */
923 private boolean bulkRemove(Predicate<? super E> filter) {
924 final Object[] es = elements;
925 // Optimize for initial run of survivors
926 for (int i = head, end = tail, to = (i <= end) ? end : es.length;
927 ; i = 0, to = end) {
928 for (; i < to; i++)
929 if (filter.test(elementAt(es, i)))
930 return bulkRemoveModified(filter, i);
931 if (to == end) {
932 if (end != tail) throw new ConcurrentModificationException();
933 break;
934 }
935 }
|