1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.collection.Collection;
13 
14 import hunt.Functions;
15 import hunt.Object;
16 import hunt.util.Common;
17 import std.range;
18 
19 interface Collection(E) : Iterable!E, IObject {
20     // Query Operations
21 
22     /**
23      * Returns the number of elements in this collection.  If this collection
24      * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
25      * <tt>Integer.MAX_VALUE</tt>.
26      *
27      * @return the number of elements in this collection
28      */
29     int size();
30 
31     /**
32      * Returns <tt>true</tt> if this collection contains no elements.
33      *
34      * @return <tt>true</tt> if this collection contains no elements
35      */
36     bool isEmpty();
37 
38     /**
39      * Returns <tt>true</tt> if this collection contains the specified element.
40      * More formally, returns <tt>true</tt> if and only if this collection
41      * contains at least one element <tt>e</tt> such that
42      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
43      *
44      * @param o element whose presence in this collection is to be tested
45      * @return <tt>true</tt> if this collection contains the specified
46      *         element
47      * @throws ClassCastException if the type of the specified element
48      *         is incompatible with this collection
49      *         (<a href="#optional-restrictions">optional</a>)
50      * @throws NullPointerException if the specified element is null and this
51      *         collection does not permit null elements
52      *         (<a href="#optional-restrictions">optional</a>)
53      */
54     bool contains(E o);
55 
56     /**
57      * Returns an iterator over the elements in this collection.  There are no
58      * guarantees concerning the order in which the elements are returned
59      * (unless this collection is an instance of some class that provides a
60      * guarantee).
61      *
62      * @return an <tt>Iterator</tt> over the elements in this collection
63      */
64     InputRange!E iterator();
65 
66     /**
67      * Returns an array containing all of the elements in this collection.
68      * If this collection makes any guarantees as to what order its elements
69      * are returned by its iterator, this method must return the elements in
70      * the same order.
71      *
72      * <p>The returned array will be "safe" in that no references to it are
73      * maintained by this collection.  (In other words, this method must
74      * allocate a new array even if this collection is backed by an array).
75      * The caller is thus free to modify the returned array.
76      *
77      * <p>This method acts as bridge between array-based and collection-based
78      * APIs.
79      *
80      * @return an array containing all of the elements in this collection
81      */
82     E[] toArray();
83 
84     /**
85      * Returns an array containing all of the elements in this collection;
86      * the runtime type of the returned array is that of the specified array.
87      * If the collection fits in the specified array, it is returned therein.
88      * Otherwise, a new array is allocated with the runtime type of the
89      * specified array and the size of this collection.
90      *
91      * <p>If this collection fits in the specified array with room to spare
92      * (i.e., the array has more elements than this collection), the element
93      * in the array immediately following the end of the collection is set to
94      * <tt>null</tt>.  (This is useful in determining the length of this
95      * collection <i>only</i> if the caller knows that this collection does
96      * not contain any <tt>null</tt> elements.)
97      *
98      * <p>If this collection makes any guarantees as to what order its elements
99      * are returned by its iterator, this method must return the elements in
100      * the same order.
101      *
102      * <p>Like the {@link #toArray()} method, this method acts as bridge between
103      * array-based and collection-based APIs.  Further, this method allows
104      * precise control over the runtime type of the output array, and may,
105      * under certain circumstances, be used to save allocation costs.
106      *
107      * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
108      * The following code can be used to dump the collection into a newly
109      * allocated array of <tt>string</tt>:
110      *
111      * <pre>
112      *     string[] y = x.toArray(new string[0]);</pre>
113      *
114      * Note that <tt>toArray(new Object[0])</tt> is identical in function to
115      * <tt>toArray()</tt>.
116      *
117      * @param (T) the runtime type of the array to contain the collection
118      * @param a the array into which the elements of this collection are to be
119      *        stored, if it is big enough; otherwise, a new array of the same
120      *        runtime type is allocated for this purpose.
121      * @return an array containing all of the elements in this collection
122      * @throws ArrayStoreException if the runtime type of the specified array
123      *         is not a supertype of the runtime type of every element in
124      *         this collection
125      * @throws NullPointerException if the specified array is null
126      */
127     // T[] toArray(T)(T[] a);
128 
129     // Modification Operations
130 
131     /**
132      * Ensures that this collection contains the specified element (optional
133      * operation).  Returns <tt>true</tt> if this collection changed as a
134      * result of the call.  (Returns <tt>false</tt> if this collection does
135      * not permit duplicates and already contains the specified element.)<p>
136      *
137      * Collections that support this operation may place limitations on what
138      * elements may be added to this collection.  In particular, some
139      * collections will refuse to add <tt>null</tt> elements, and others will
140      * impose restrictions on the type of elements that may be added.
141      * Collection classes should clearly specify in their documentation any
142      * restrictions on what elements may be added.<p>
143      *
144      * If a collection refuses to add a particular element for any reason
145      * other than that it already contains the element, it <i>must</i> throw
146      * an exception (rather than returning <tt>false</tt>).  This preserves
147      * the invariant that a collection always contains the specified element
148      * after this call returns.
149      *
150      * @param e element whose presence in this collection is to be ensured
151      * @return <tt>true</tt> if this collection changed as a result of the
152      *         call
153      * @throws UnsupportedOperationException if the <tt>add</tt> operation
154      *         is not supported by this collection
155      * @throws ClassCastException if the class of the specified element
156      *         prevents it from being added to this collection
157      * @throws NullPointerException if the specified element is null and this
158      *         collection does not permit null elements
159      * @throws IllegalArgumentException if some property of the element
160      *         prevents it from being added to this collection
161      * @throws IllegalStateException if the element cannot be added at this
162      *         time due to insertion restrictions
163      */
164     bool add(E e);
165 
166     /**
167      * Removes a single instance of the specified element from this
168      * collection, if it is present (optional operation).  More formally,
169      * removes an element <tt>e</tt> such that
170      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
171      * this collection contains one or more such elements.  Returns
172      * <tt>true</tt> if this collection contained the specified element (or
173      * equivalently, if this collection changed as a result of the call).
174      *
175      * @param o element to be removed from this collection, if present
176      * @return <tt>true</tt> if an element was removed as a result of this call
177      * @throws ClassCastException if the type of the specified element
178      *         is incompatible with this collection
179      *         (<a href="#optional-restrictions">optional</a>)
180      * @throws NullPointerException if the specified element is null and this
181      *         collection does not permit null elements
182      *         (<a href="#optional-restrictions">optional</a>)
183      * @throws UnsupportedOperationException if the <tt>remove</tt> operation
184      *         is not supported by this collection
185      */
186     bool remove(E o);
187 
188 
189     // Bulk Operations
190 
191     /**
192      * Returns <tt>true</tt> if this collection contains all of the elements
193      * in the specified collection.
194      *
195      * @param  c collection to be checked for containment in this collection
196      * @return <tt>true</tt> if this collection contains all of the elements
197      *         in the specified collection
198      * @throws ClassCastException if the types of one or more elements
199      *         in the specified collection are incompatible with this
200      *         collection
201      *         (<a href="#optional-restrictions">optional</a>)
202      * @throws NullPointerException if the specified collection contains one
203      *         or more null elements and this collection does not permit null
204      *         elements
205      *         (<a href="#optional-restrictions">optional</a>),
206      *         or if the specified collection is null.
207      * @see    #contains(Object)
208      */
209     bool containsAll(Collection!E c);
210 
211     /**
212      * Adds all of the elements in the specified collection to this collection
213      * (optional operation).  The behavior of this operation is undefined if
214      * the specified collection is modified while the operation is in progress.
215      * (This implies that the behavior of this call is undefined if the
216      * specified collection is this collection, and this collection is
217      * nonempty.)
218      *
219      * @param c collection containing elements to be added to this collection
220      * @return <tt>true</tt> if this collection changed as a result of the call
221      * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
222      *         is not supported by this collection
223      * @throws ClassCastException if the class of an element of the specified
224      *         collection prevents it from being added to this collection
225      * @throws NullPointerException if the specified collection contains a
226      *         null element and this collection does not permit null elements,
227      *         or if the specified collection is null
228      * @throws IllegalArgumentException if some property of an element of the
229      *         specified collection prevents it from being added to this
230      *         collection
231      * @throws IllegalStateException if not all the elements can be added at
232      *         this time due to insertion restrictions
233      * @see #add(Object)
234      */
235     bool addAll(Collection!E c);
236     bool addAll(E[] c);
237 
238     /**
239      * Removes all of this collection's elements that are also contained in the
240      * specified collection (optional operation).  After this call returns,
241      * this collection will contain no elements in common with the specified
242      * collection.
243      *
244      * @param c collection containing elements to be removed from this collection
245      * @return <tt>true</tt> if this collection changed as a result of the
246      *         call
247      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
248      *         is not supported by this collection
249      * @throws ClassCastException if the types of one or more elements
250      *         in this collection are incompatible with the specified
251      *         collection
252      *         (<a href="#optional-restrictions">optional</a>)
253      * @throws NullPointerException if this collection contains one or more
254      *         null elements and the specified collection does not support
255      *         null elements
256      *         (<a href="#optional-restrictions">optional</a>),
257      *         or if the specified collection is null
258      * @see #remove(Object)
259      * @see #contains(Object)
260      */
261     bool removeAll(Collection!E c);
262 
263     /**
264      * Removes all of the elements of this collection that satisfy the given
265      * predicate.  Errors or runtime exceptions thrown during iteration or by
266      * the predicate are relayed to the caller.
267      *
268      * @implSpec
269      * The default implementation traverses all elements of the collection using
270      * its {@link #iterator}.  Each matching element is removed using
271      * {@link Iterator#remove()}.  If the collection's iterator does not
272      * support removal then an {@code UnsupportedOperationException} will be
273      * thrown on the first matching element.
274      *
275      * @param filter a predicate which returns {@code true} for elements to be
276      *        removed
277      * @return {@code true} if any elements were removed
278      * @throws NullPointerException if the specified filter is null
279      * @throws UnsupportedOperationException if elements cannot be removed
280      *         from this collection.  Implementations may throw this exception if a
281      *         matching element cannot be removed or if, in general, removal is not
282      *         supported.
283      * @since 1.8
284      */
285     bool removeIf(Predicate!E filter);
286 
287     /**
288      * Retains only the elements in this collection that are contained in the
289      * specified collection (optional operation).  In other words, removes from
290      * this collection all of its elements that are not contained in the
291      * specified collection.
292      *
293      * @param c collection containing elements to be retained in this collection
294      * @return <tt>true</tt> if this collection changed as a result of the call
295      * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
296      *         is not supported by this collection
297      * @throws ClassCastException if the types of one or more elements
298      *         in this collection are incompatible with the specified
299      *         collection
300      *         (<a href="#optional-restrictions">optional</a>)
301      * @throws NullPointerException if this collection contains one or more
302      *         null elements and the specified collection does not permit null
303      *         elements
304      *         (<a href="#optional-restrictions">optional</a>),
305      *         or if the specified collection is null
306      * @see #remove(Object)
307      * @see #contains(Object)
308      */
309     bool retainAll(Collection!E c);
310 
311     /**
312      * Removes all of the elements from this collection (optional operation).
313      * The collection will be empty after this method returns.
314      *
315      * @throws UnsupportedOperationException if the <tt>clear</tt> operation
316      *         is not supported by this collection
317      */
318     void clear();
319 
320     // string toString();
321 
322 
323     // Comparison and hashing
324 
325     /**
326      * Compares the specified object with this collection for equality. <p>
327      *
328      * While the <tt>Collection</tt> interface adds no stipulations to the
329      * general contract for the <tt>Object.equals</tt>, programmers who
330      * implement the <tt>Collection</tt> interface "directly" (in other words,
331      * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
332      * or a <tt>List</tt>) must exercise care if they choose to override the
333      * <tt>Object.equals</tt>.  It is not necessary to do so, and the simplest
334      * course of action is to rely on <tt>Object</tt>'s implementation, but
335      * the implementor may wish to implement a "value comparison" in place of
336      * the default "reference comparison."  (The <tt>List</tt> and
337      * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
338      *
339      * The general contract for the <tt>Object.equals</tt> method states that
340      * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
341      * only if <tt>b.equals(a)</tt>).  The contracts for <tt>List.equals</tt>
342      * and <tt>Set.equals</tt> state that lists are only equal to other lists,
343      * and sets to other sets.  Thus, a custom <tt>equals</tt> method for a
344      * collection class that implements neither the <tt>List</tt> nor
345      * <tt>Set</tt> interface must return <tt>false</tt> when this collection
346      * is compared to any list or set.  (By the same logic, it is not possible
347      * to write a class that correctly implements both the <tt>Set</tt> and
348      * <tt>List</tt> interfaces.)
349      *
350      * @param o object to be compared for equality with this collection
351      * @return <tt>true</tt> if the specified object is equal to this
352      * collection
353      *
354      * @see Object#equals(Object)
355      * @see Set#equals(Object)
356      * @see List#equals(Object)
357      */
358     // bool opEquals(Object o);
359 
360     /**
361      * Returns the hash code value for this collection.  While the
362      * <tt>Collection</tt> interface adds no stipulations to the general
363      * contract for the <tt>Object.toHash</tt> method, programmers should
364      * take note that any class that overrides the <tt>Object.equals</tt>
365      * method must also override the <tt>Object.toHash</tt> method in order
366      * to satisfy the general contract for the <tt>Object.toHash</tt> method.
367      * In particular, <tt>c1.equals(c2)</tt> implies that
368      * <tt>c1.toHash()==c2.toHash()</tt>.
369      *
370      * @return the hash code value for this collection
371      *
372      * @see Object#toHash()
373      * @see Object#equals(Object)
374      */
375     // size_t toHash() @trusted nothrow;
376 
377     /**
378      * Creates a {@link Spliterator} over the elements in this collection.
379      *
380      * Implementations should document characteristic values reported by the
381      * spliterator.  Such characteristic values are not required to be reported
382      * if the spliterator reports {@link Spliterator#SIZED} and this collection
383      * contains no elements.
384      *
385      * <p>The default implementation should be overridden by subclasses that
386      * can return a more efficient spliterator.  In order to
387      * preserve expected laziness behavior for the {@link #stream()} and
388      * {@link #parallelStream()}} methods, spliterators should either have the
389      * characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be
390      * <em><a href="Spliterator.html#binding">late-binding</a></em>.
391      * If none of these is practical, the overriding class should describe the
392      * spliterator's documented policy of binding and structural interference,
393      * and should override the {@link #stream()} and {@link #parallelStream()}
394      * methods to create streams using a {@code Supplier} of the spliterator,
395      * as in:
396      * <pre>{@code
397      *     Stream!E s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)
398      * }</pre>
399      * <p>These requirements ensure that streams produced by the
400      * {@link #stream()} and {@link #parallelStream()} methods will reflect the
401      * contents of the collection as of initiation of the terminal stream
402      * operation.
403      *
404      * @implSpec
405      * The default implementation creates a
406      * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
407      * from the collections's {@code Iterator}.  The spliterator inherits the
408      * <em>fail-fast</em> properties of the collection's iterator.
409      * <p>
410      * The created {@code Spliterator} reports {@link Spliterator#SIZED}.
411      *
412      * @implNote
413      * The created {@code Spliterator} additionally reports
414      * {@link Spliterator#SUBSIZED}.
415      *
416      * <p>If a spliterator covers no elements then the reporting of additional
417      * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},
418      * does not aid clients to control, specialize or simplify computation.
419      * However, this does enable shared use of an immutable and empty
420      * spliterator instance (see {@link Spliterators#emptySpliterator()}) for
421      * empty collections, and enables clients to determine if such a spliterator
422      * covers no elements.
423      *
424      * @return a {@code Spliterator} over the elements in this collection
425      * @since 1.8
426      */
427     // override
428     // final Spliterator!E spliterator() {
429     //     return Spliterators.spliterator(this, 0);
430     // }
431 
432     /**
433      * Returns a sequential {@code Stream} with this collection as its source.
434      *
435      * <p>This method should be overridden when the {@link #spliterator()}
436      * method cannot return a spliterator that is {@code IMMUTABLE},
437      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
438      * for details.)
439      *
440      * @implSpec
441      * The default implementation creates a sequential {@code Stream} from the
442      * collection's {@code Spliterator}.
443      *
444      * @return a sequential {@code Stream} over the elements in this collection
445      * @since 1.8
446      */
447     // final Stream!E stream() {
448     //     return StreamSupport.stream(spliterator(), false);
449     // }
450 
451     /**
452      * Returns a possibly parallel {@code Stream} with this collection as its
453      * source.  It is allowable for this method to return a sequential stream.
454      *
455      * <p>This method should be overridden when the {@link #spliterator()}
456      * method cannot return a spliterator that is {@code IMMUTABLE},
457      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
458      * for details.)
459      *
460      * @implSpec
461      * The default implementation creates a parallel {@code Stream} from the
462      * collection's {@code Spliterator}.
463      *
464      * @return a possibly parallel {@code Stream} over the elements in this
465      * collection
466      * @since 1.8
467      */
468     // final Stream!E parallelStream() {
469     //     return StreamSupport.stream(spliterator(), true);
470     // }
471 
472  
473 }
474 
475