9 package javolution.util;
15 import java.io.IOException;
16 import java.io.Serializable;
17 import java.util.Collection;
18 import java.util.Comparator;
19 import java.util.ConcurrentModificationException;
20 import java.util.Iterator;
165 @DefaultTextFormat(FastCollection.Format.class)
168 private static final long serialVersionUID = 0x600L;
186 @
Parallelizable(mutexFree =
true, comment =
"Except for write operations, all read operations are mutex-free.")
200 @
Parallelizable(mutexFree =
false, comment =
"Use multiple-readers/single-writer lock.")
313 @SuppressWarnings(
"unchecked")
315 public
void perform(
Consumer<? extends Collection<E>> action) {
332 @SuppressWarnings(
"unchecked")
334 public
void update(
Consumer<? extends Collection<E>> action) {
346 public
void forEach(final
Consumer<? super E> consumer) {
347 perform(
new Consumer<Collection<E>>() {
348 public void accept(Collection<E> view) {
349 Iterator<E> it = view.iterator();
350 while (it.hasNext()) {
351 consumer.
accept(it.next());
368 public
boolean removeIf(final
Predicate<? super E> filter) {
369 final boolean[] removed =
new boolean[1];
370 update(
new Consumer<Collection<E>>() {
371 public void accept(Collection<E> view) {
372 Iterator<E> it = view.iterator();
373 while (it.hasNext()) {
374 if (filter.test(it.next())) {
398 return reducer.get();
407 @
Realtime(limit = LINEAR, comment =
"May have to search the whole collection (e.g. distinct view).")
408 public
boolean add(E element) {
409 return service().add(element);
414 @
Realtime(limit = LINEAR, comment =
"May actually iterates the whole collection (e.g. filtered view).")
415 public
boolean isEmpty() {
416 return iterator().hasNext();
421 @
Realtime(limit = LINEAR, comment =
"Potentially counts the elements.")
423 return service().size();
428 @
Realtime(limit = LINEAR, comment =
"Potentially removes elements one at a time.")
429 public
void clear() {
435 @
Realtime(limit = LINEAR, comment =
"Potentially searches the whole collection.")
436 public
boolean contains(Object searched) {
437 return service().contains(searched);
442 @
Realtime(limit = LINEAR, comment =
"Potentially searches the whole collection.")
443 public
boolean remove(Object searched) {
444 return service().remove(searched);
454 @
Realtime(limit = N_SQUARE, comment =
"Construction of the iterator may require sorting the elements (e.g. sorted view)")
455 public Iterator<E> iterator() {
456 return service().iterator();
462 public
boolean addAll(final Collection<? extends E> that) {
463 return service().addAll(that);
469 public
boolean containsAll(Collection<?> that) {
470 return service().containsAll(that);
476 public
boolean removeAll(final Collection<?> that) {
477 return service().removeAll(that);
483 public
boolean retainAll(final Collection<?> that) {
484 return service().retainAll(that);
490 public Object[] toArray() {
491 return service().toArray();
498 public <T> T[] toArray(final T[] array) {
499 return service().toArray(array);
513 @SuppressWarnings(
"unchecked")
515 public <T extends E> T any(Class<T> type) {
553 for (E e : elements) {
565 addAll((Collection<? extends E>) that);
575 return service().comparator();
585 public <T extends Collection<E>>
Immutable<T> toImmutable() {
587 @SuppressWarnings(
"unchecked")
588 final T value = (T) unmodifiable();
611 public
boolean equals(Object obj) {
612 return service().equals(obj);
625 public
int hashCode() {
626 return service().hashCode();
634 public String toString() {
667 throws IllegalArgumentException {
668 throw new UnsupportedOperationException();
675 Class<?> elementType =
null;
677 for (Object element : that) {
678 if (elementType ==
null) elementType = Void.class;
679 else dest.append(
", ");
680 if (element ==
null) {
684 Class<?> cls = element.getClass();
685 if (elementType.equals(cls)) {
686 format.
format(element, dest);
691 format.
format(element, dest);
693 return dest.append(
']');