Javolution 6.0.0 java
javolution.util.internal.collection.MappedCollectionImpl< E, R > Class Template Reference
Inheritance diagram for javolution.util.internal.collection.MappedCollectionImpl< E, R >:
[legend]
Collaboration diagram for javolution.util.internal.collection.MappedCollectionImpl< E, R >:
[legend]

Classes

class  IteratorImpl
 

Public Member Functions

 MappedCollectionImpl (CollectionService< E > target, Function<? super E, ? extends R > function)
 
boolean add (R element)
 
void clear ()
 
Equality<? super R > comparator ()
 
boolean isEmpty ()
 
Iterator< R > iterator ()
 
int size ()
 
abstract boolean add (E element)
 
boolean addAll (Collection<? extends E > c)
 
CollectionView< E > clone ()
 
boolean contains (Object obj)
 
boolean containsAll (Collection<?> c)
 
boolean equals (Object o)
 
int hashCode ()
 
void perform (Consumer< CollectionService< E >> action, CollectionService< E > view)
 
boolean remove (Object obj)
 
boolean removeAll (Collection<?> c)
 
boolean retainAll (Collection<?> c)
 
CollectionService< E >[] split (int n)
 
CollectionService< E > threadSafe ()
 
Object[] toArray ()
 
void update (Consumer< CollectionService< E >> action, CollectionService< E > view)
 

Protected Member Functions

CollectionService< E > target ()
 
CollectionService< E > service ()
 

Protected Attributes

final Function<? super E, ? extends R > function
 

Package Functions

public< T > T[] toArray (T[] a)
 

Private Attributes

CollectionService< E > target
 

Static Private Attributes

static final long serialVersionUID = 0x600L
 

Detailed Description

A mapped view over a collection.

Definition at line 21 of file MappedCollectionImpl.java.

Constructor & Destructor Documentation

◆ MappedCollectionImpl()

javolution.util.internal.collection.MappedCollectionImpl< E, R >.MappedCollectionImpl ( CollectionService< E >  target,
Function<? super E, ? extends R >  function 
)

Definition at line 53 of file MappedCollectionImpl.java.

54  {
55  super((CollectionService<R>) target); // Beware target is of type <E>
56  this.function = function;
57  }

Member Function Documentation

◆ add() [1/2]

abstract boolean javolution.util.internal.collection.CollectionView< E >.add ( element)
abstractinherited

Adds the specified element to this collection

◆ add() [2/2]

Reimplemented in javolution.util.internal.set.MappedSetImpl< E, R >.

Definition at line 60 of file MappedCollectionImpl.java.

60  {
61  throw new UnsupportedOperationException(
62  "New elements cannot be added to mapped views");
63  }

◆ addAll()

boolean javolution.util.internal.collection.CollectionView< E >.addAll ( Collection<? extends E >  c)
inherited

Adds all the specified elements to this collection.

Definition at line 46 of file CollectionView.java.

46  {
47  boolean changed = false;
48  Iterator<? extends E> it = c.iterator();
49  while (it.hasNext()) {
50  if (add(it.next())) changed = true;
51  }
52  return changed;
53  }

◆ clear()

Definition at line 66 of file MappedCollectionImpl.java.

66  {
67  target().clear();
68  }

◆ clone()

Returns a copy of this collection; updates of the copy should not impact the original.

Definition at line 66 of file CollectionView.java.

66  {
67  try {
68  CollectionView<E> copy = (CollectionView<E>) super.clone();
69  if (target != null) { // Not a root class.
70  copy.target = target.clone();
71  }
72  return copy;
73  } catch (CloneNotSupportedException e) {
74  throw new Error("Should not happen since target is cloneable");
75  }
76  }

◆ comparator()

Reimplemented in javolution.util.internal.map.MapView< K, V >.Values, and javolution.util.internal.map.MapView< K, V >.KeySet.

Definition at line 71 of file MappedCollectionImpl.java.

71  {
72  return Equalities.STANDARD;
73  }

◆ contains()

boolean javolution.util.internal.collection.CollectionView< E >.contains ( Object  obj)
inherited

Indicates if this collection contains the specified element.

Definition at line 83 of file CollectionView.java.

83  {
84  Iterator<? extends E> it = iterator();
85  Equality<Object> cmp = (Equality<Object>) comparator();
86  while (it.hasNext()) {
87  if (cmp.areEqual(obj, it.next())) return true;
88  }
89  return false;
90  }

◆ containsAll()

boolean javolution.util.internal.collection.CollectionView< E >.containsAll ( Collection<?>  c)
inherited

Indicates if this collection contains all the specified elements.

Definition at line 93 of file CollectionView.java.

93  {
94  for (Object e : c) {
95  if (!contains(e)) return false;
96  }
97  return true;
98  }

◆ equals()

boolean javolution.util.internal.collection.CollectionView< E >.equals ( Object  o)
inherited

Compares the specified object with this collection for equality. This method follows the Collection#equals(Object) specification if this collection comparator is Equalities#STANDARD (default). Otherwise, only collections using the same comparator can be considered equals.

Parameters
objthe object to be compared for equality with this collection
Returns
true if both collections are considered equals; false otherwise.

Definition at line 102 of file CollectionView.java.

102  {
103  // Follow Collection.equals specification if this comparator is standard.
104  if (this == o) return true;
105  // Check comparators consistency.
106  if (o instanceof CollectionService) {
107  if (!comparator().equals(((CollectionService<E>) o).comparator())) return false; // Different comparators.
108  } else {
109  if (!comparator().equals(Equalities.STANDARD)) return false;
110  }
111  // Collection.equals contract.
112  if (this instanceof Set) {
113  if (!(o instanceof Set)) return false;
114  Set<E> set = (Set<E>) o;
115  return (size() == set.size()) && containsAll(set);
116  } else if (this instanceof List) {
117  if (!(o instanceof List)) return false;
118  List<E> list = (List<E>) o;
119  if (size() != list.size()) return false; // Short-cut.
120  Equality<? super E> cmp = comparator();
121  Iterator<E> it1 = this.iterator();
122  Iterator<E> it2 = list.iterator();
123  while (it1.hasNext()) {
124  if (!it2.hasNext()) return false;
125  if (!cmp.areEqual(it1.next(), it2.next())) return false;
126  }
127  if (it2.hasNext()) return false;
128  return true;
129  } else {
130  return false;
131  }
132  }

◆ hashCode()

Returns the hash code of this collection. This method follows the Collection#hashCode() specification if this collection comparator is Equalities#STANDARD.

Returns
this collection hash code.

Definition at line 135 of file CollectionView.java.

135  {
136  // Follow Collection.equals specification if this comparator is standard.
137  Equality<? super E> cmp = comparator();
138  Iterator<E> it = this.iterator();
139  int hash = 0;
140  if (this instanceof Set) {
141  while (it.hasNext()) {
142  hash += cmp.hashCodeOf(it.next());
143  }
144  } else if (this instanceof List) {
145  while (it.hasNext()) {
146  hash += 31 * hash + cmp.hashCodeOf(it.next());
147  }
148  } else {
149  hash = super.hashCode();
150  }
151  return hash;
152  }

◆ isEmpty()

Definition at line 76 of file MappedCollectionImpl.java.

76  {
77  return target().isEmpty();
78  }

◆ iterator()

Definition at line 81 of file MappedCollectionImpl.java.

81  {
82  return new IteratorImpl();
83  }

◆ perform()

void javolution.util.internal.collection.CollectionView< E >.perform ( Consumer< CollectionService< E >>  action,
CollectionService< E >  view 
)
inherited

Definition at line 163 of file CollectionView.java.

163  {
164  if (target == null) {
165  action.accept(view);
166  } else {
167  target.perform(action, view);
168  }
169  }

◆ remove()

boolean javolution.util.internal.collection.CollectionView< E >.remove ( Object  obj)
inherited

Removes the specified element from this collection.

Definition at line 173 of file CollectionView.java.

173  {
174  Iterator<? extends E> it = iterator();
175  Equality<Object> cmp = (Equality<Object>) comparator();
176  while (it.hasNext()) {
177  if (cmp.areEqual(obj, it.next())) {
178  it.remove();
179  return true;
180  }
181  }
182  return false;
183  }

◆ removeAll()

boolean javolution.util.internal.collection.CollectionView< E >.removeAll ( Collection<?>  c)
inherited

Removes all the specified element from this collection.

Definition at line 186 of file CollectionView.java.

186  {
187  boolean changed = false;
188  Iterator<? extends E> it = iterator();
189  while (it.hasNext()) {
190  if (c.contains(it.next())) {
191  it.remove();
192  changed = true;
193  }
194  }
195  return changed;
196  }

◆ retainAll()

boolean javolution.util.internal.collection.CollectionView< E >.retainAll ( Collection<?>  c)
inherited

Removes all the elements except those in the specified collection.

Definition at line 199 of file CollectionView.java.

199  {
200  boolean changed = false;
201  Iterator<? extends E> it = iterator();
202  while (it.hasNext()) {
203  if (!c.contains(it.next())) {
204  it.remove();
205  changed = true;
206  }
207  }
208  return changed;
209  }

◆ service()

Returns the service implementation of this collection (for sub-classes).

Definition at line 274 of file CollectionView.java.

274  {
275  return this;
276  }

◆ size()

Definition at line 86 of file MappedCollectionImpl.java.

86  {
87  return target().size();
88  }

◆ split()

Returns

n

distinct parts of this object. This method may return an array of size less than

n

(e.g. an array of size one if this object cannot split).

Parameters
nthe number of parts.
Returns
the distinct parts (or views) for this object.
Exceptions
IllegalArgumentExceptionif
n <= 1

Definition at line 224 of file CollectionView.java.

224  {
225  if (target == null) return new CollectionService[] { this }; // No split.
226  CollectionService<E>[] subTargets = target.split(n);
227  CollectionService<E>[] result = new CollectionService[subTargets.length];
228  for (int i = 0; i < subTargets.length; i++) {
229  CollectionView<E> copy = this.clone();
230  copy.target = subTargets[i];
231  result[i] = copy;
232  }
233  return result;
234  }

◆ target()

Returns the actual target

Definition at line 279 of file CollectionView.java.

279  {
280  return target;
281  }

◆ threadSafe()

Returns a thread-safe version of this service (used during parallel updates).

Definition at line 237 of file CollectionView.java.

237  {
238  // We use shared collection as default (they can split).
239  return new SharedCollectionImpl<E>(this);
240  }

◆ toArray() [1/2]

Object [] javolution.util.internal.collection.CollectionView< E >.toArray
inherited

Returns an array holding this collection elements.

Definition at line 243 of file CollectionView.java.

243  {
244  return toArray(new Object[size()]);
245  }

◆ toArray() [2/2]

public<T> T [] javolution.util.internal.collection.CollectionView< E >.toArray ( T[]  a)
packageinherited

Returns the specified array holding this collection elements if enough capacity.

Definition at line 249 of file CollectionView.java.

249  {
250  final int size = size();
251  final T[] result = (size <= a.length) ? a
252  : (T[]) java.lang.reflect.Array.newInstance(a.getClass()
253  .getComponentType(), size);
254  int i = 0;
255  Iterator<E> it = iterator();
256  while (it.hasNext()) {
257  result[i++] = (T) it.next();
258  }
259  if (result.length > size) {
260  result[size] = null; // As per Collection contract.
261  }
262  return result;
263  }

◆ update()

void javolution.util.internal.collection.CollectionView< E >.update ( Consumer< CollectionService< E >>  action,
CollectionService< E >  view 
)
inherited

Definition at line 266 of file CollectionView.java.

266  {
267  if (target == null) {
268  action.accept(view);
269  } else {
270  target.perform(action, view);
271  }
272  }

Member Data Documentation

◆ function

final Function<? super E, ? extends R> javolution.util.internal.collection.MappedCollectionImpl< E, R >.function
protected

Definition at line 50 of file MappedCollectionImpl.java.

◆ serialVersionUID

final long javolution.util.internal.collection.MappedCollectionImpl< E, R >.serialVersionUID = 0x600L
staticprivate

Definition at line 49 of file MappedCollectionImpl.java.

◆ target

Definition at line 33 of file CollectionView.java.


The documentation for this class was generated from the following file:
javolution.util.internal.collection.CollectionView< R >::add
abstract boolean add(E element)
javolution.util.internal.collection.CollectionView< R >::contains
boolean contains(Object obj)
Definition: CollectionView.java:83
javolution.util.internal.collection.CollectionView< R >::comparator
abstract Equality<? super E > comparator()
javolution.util.internal.collection.CollectionView< R >::size
int size()
Definition: CollectionView.java:212
javolution.util.internal.collection.CollectionView< R >::target
CollectionService< E > target
Definition: CollectionView.java:33
javolution.util.internal.collection.CollectionView< R >::iterator
abstract Iterator< E > iterator()
javolution.util.internal.collection.CollectionView< R >::equals
boolean equals(Object o)
Definition: CollectionView.java:102
javolution.util.internal.collection.CollectionView< R >::toArray
Object[] toArray()
Definition: CollectionView.java:243
javolution.util.internal.collection.CollectionView< R >::clone
CollectionView< E > clone()
Definition: CollectionView.java:66
javolution.util.internal.collection.CollectionView< R >::containsAll
boolean containsAll(Collection<?> c)
Definition: CollectionView.java:93