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 20 interface Collection(E) : IObject, Iterable!E { 21 // Query Operations 22 23 /** 24 * Returns the number of elements in this collection. If this collection 25 * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns 26 * <tt>Integer.MAX_VALUE</tt>. 27 * 28 * @return the number of elements in this collection 29 */ 30 int size(); 31 32 /** 33 * Returns <tt>true</tt> if this collection contains no elements. 34 * 35 * @return <tt>true</tt> if this collection contains no elements 36 */ 37 bool isEmpty(); 38 39 /** 40 * Returns <tt>true</tt> if this collection contains the specified element. 41 * More formally, returns <tt>true</tt> if and only if this collection 42 * contains at least one element <tt>e</tt> such that 43 * <tt>(o==null ? e==null : o.equals(e))</tt>. 44 * 45 * @param o element whose presence in this collection is to be tested 46 * @return <tt>true</tt> if this collection contains the specified 47 * element 48 * @throws ClassCastException if the type of the specified element 49 * is incompatible with this collection 50 * (<a href="#optional-restrictions">optional</a>) 51 * @throws NullPointerException if the specified element is null and this 52 * collection does not permit null elements 53 * (<a href="#optional-restrictions">optional</a>) 54 */ 55 bool contains(E o); 56 57 /** 58 * Returns an iterator over the elements in this collection. There are no 59 * guarantees concerning the order in which the elements are returned 60 * (unless this collection is an instance of some class that provides a 61 * guarantee). 62 * 63 * @return an <tt>Iterator</tt> over the elements in this collection 64 */ 65 InputRange!E iterator(); 66 67 /** 68 * Returns an array containing all of the elements in this collection. 69 * If this collection makes any guarantees as to what order its elements 70 * are returned by its iterator, this method must return the elements in 71 * the same order. 72 * 73 * <p>The returned array will be "safe" in that no references to it are 74 * maintained by this collection. (In other words, this method must 75 * allocate a new array even if this collection is backed by an array). 76 * The caller is thus free to modify the returned array. 77 * 78 * <p>This method acts as bridge between array-based and collection-based 79 * APIs. 80 * 81 * @return an array containing all of the elements in this collection 82 */ 83 E[] toArray(); 84 85 /** 86 * Returns an array containing all of the elements in this collection; 87 * the runtime type of the returned array is that of the specified array. 88 * If the collection fits in the specified array, it is returned therein. 89 * Otherwise, a new array is allocated with the runtime type of the 90 * specified array and the size of this collection. 91 * 92 * <p>If this collection fits in the specified array with room to spare 93 * (i.e., the array has more elements than this collection), the element 94 * in the array immediately following the end of the collection is set to 95 * <tt>null</tt>. (This is useful in determining the length of this 96 * collection <i>only</i> if the caller knows that this collection does 97 * not contain any <tt>null</tt> elements.) 98 * 99 * <p>If this collection makes any guarantees as to what order its elements 100 * are returned by its iterator, this method must return the elements in 101 * the same order. 102 * 103 * <p>Like the {@link #toArray()} method, this method acts as bridge between 104 * array-based and collection-based APIs. Further, this method allows 105 * precise control over the runtime type of the output array, and may, 106 * under certain circumstances, be used to save allocation costs. 107 * 108 * <p>Suppose <tt>x</tt> is a collection known to contain only strings. 109 * The following code can be used to dump the collection into a newly 110 * allocated array of <tt>string</tt>: 111 * 112 * <pre> 113 * string[] y = x.toArray(new string[0]);</pre> 114 * 115 * Note that <tt>toArray(new Object[0])</tt> is identical in function to 116 * <tt>toArray()</tt>. 117 * 118 * @param (T) the runtime type of the array to contain the collection 119 * @param a the array into which the elements of this collection are to be 120 * stored, if it is big enough; otherwise, a new array of the same 121 * runtime type is allocated for this purpose. 122 * @return an array containing all of the elements in this collection 123 * @throws ArrayStoreException if the runtime type of the specified array 124 * is not a supertype of the runtime type of every element in 125 * this collection 126 * @throws NullPointerException if the specified array is null 127 */ 128 // T[] toArray(T)(T[] a); 129 130 // Modification Operations 131 132 /** 133 * Ensures that this collection contains the specified element (optional 134 * operation). Returns <tt>true</tt> if this collection changed as a 135 * result of the call. (Returns <tt>false</tt> if this collection does 136 * not permit duplicates and already contains the specified element.)<p> 137 * 138 * Collections that support this operation may place limitations on what 139 * elements may be added to this collection. In particular, some 140 * collections will refuse to add <tt>null</tt> elements, and others will 141 * impose restrictions on the type of elements that may be added. 142 * Collection classes should clearly specify in their documentation any 143 * restrictions on what elements may be added.<p> 144 * 145 * If a collection refuses to add a particular element for any reason 146 * other than that it already contains the element, it <i>must</i> throw 147 * an exception (rather than returning <tt>false</tt>). This preserves 148 * the invariant that a collection always contains the specified element 149 * after this call returns. 150 * 151 * @param e element whose presence in this collection is to be ensured 152 * @return <tt>true</tt> if this collection changed as a result of the 153 * call 154 * @throws UnsupportedOperationException if the <tt>add</tt> operation 155 * is not supported by this collection 156 * @throws ClassCastException if the class of the specified element 157 * prevents it from being added to this collection 158 * @throws NullPointerException if the specified element is null and this 159 * collection does not permit null elements 160 * @throws IllegalArgumentException if some property of the element 161 * prevents it from being added to this collection 162 * @throws IllegalStateException if the element cannot be added at this 163 * time due to insertion restrictions 164 */ 165 bool add(E e); 166 167 /** 168 * Removes a single instance of the specified element from this 169 * collection, if it is present (optional operation). More formally, 170 * removes an element <tt>e</tt> such that 171 * <tt>(o==null ? e==null : o.equals(e))</tt>, if 172 * this collection contains one or more such elements. Returns 173 * <tt>true</tt> if this collection contained the specified element (or 174 * equivalently, if this collection changed as a result of the call). 175 * 176 * @param o element to be removed from this collection, if present 177 * @return <tt>true</tt> if an element was removed as a result of this call 178 * @throws ClassCastException if the type of the specified element 179 * is incompatible with this collection 180 * (<a href="#optional-restrictions">optional</a>) 181 * @throws NullPointerException if the specified element is null and this 182 * collection does not permit null elements 183 * (<a href="#optional-restrictions">optional</a>) 184 * @throws UnsupportedOperationException if the <tt>remove</tt> operation 185 * is not supported by this collection 186 */ 187 bool remove(E o); 188 189 190 // Bulk Operations 191 192 /** 193 * Returns <tt>true</tt> if this collection contains all of the elements 194 * in the specified collection. 195 * 196 * @param c collection to be checked for containment in this collection 197 * @return <tt>true</tt> if this collection contains all of the elements 198 * in the specified collection 199 * @throws ClassCastException if the types of one or more elements 200 * in the specified collection are incompatible with this 201 * collection 202 * (<a href="#optional-restrictions">optional</a>) 203 * @throws NullPointerException if the specified collection contains one 204 * or more null elements and this collection does not permit null 205 * elements 206 * (<a href="#optional-restrictions">optional</a>), 207 * or if the specified collection is null. 208 * @see #contains(Object) 209 */ 210 bool containsAll(Collection!E c); 211 212 /** 213 * Adds all of the elements in the specified collection to this collection 214 * (optional operation). The behavior of this operation is undefined if 215 * the specified collection is modified while the operation is in progress. 216 * (This implies that the behavior of this call is undefined if the 217 * specified collection is this collection, and this collection is 218 * nonempty.) 219 * 220 * @param c collection containing elements to be added to this collection 221 * @return <tt>true</tt> if this collection changed as a result of the call 222 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation 223 * is not supported by this collection 224 * @throws ClassCastException if the class of an element of the specified 225 * collection prevents it from being added to this collection 226 * @throws NullPointerException if the specified collection contains a 227 * null element and this collection does not permit null elements, 228 * or if the specified collection is null 229 * @throws IllegalArgumentException if some property of an element of the 230 * specified collection prevents it from being added to this 231 * collection 232 * @throws IllegalStateException if not all the elements can be added at 233 * this time due to insertion restrictions 234 * @see #add(Object) 235 */ 236 bool addAll(Collection!E c); 237 bool addAll(E[] c); 238 239 /** 240 * Removes all of this collection's elements that are also contained in the 241 * specified collection (optional operation). After this call returns, 242 * this collection will contain no elements in common with the specified 243 * collection. 244 * 245 * @param c collection containing elements to be removed from this collection 246 * @return <tt>true</tt> if this collection changed as a result of the 247 * call 248 * @throws UnsupportedOperationException if the <tt>removeAll</tt> method 249 * is not supported by this collection 250 * @throws ClassCastException if the types of one or more elements 251 * in this collection are incompatible with the specified 252 * collection 253 * (<a href="#optional-restrictions">optional</a>) 254 * @throws NullPointerException if this collection contains one or more 255 * null elements and the specified collection does not support 256 * null elements 257 * (<a href="#optional-restrictions">optional</a>), 258 * or if the specified collection is null 259 * @see #remove(Object) 260 * @see #contains(Object) 261 */ 262 bool removeAll(Collection!E c); 263 264 /** 265 * Removes all of the elements of this collection that satisfy the given 266 * predicate. Errors or runtime exceptions thrown during iteration or by 267 * the predicate are relayed to the caller. 268 * 269 * @implSpec 270 * The default implementation traverses all elements of the collection using 271 * its {@link #iterator}. Each matching element is removed using 272 * {@link Iterator#remove()}. If the collection's iterator does not 273 * support removal then an {@code UnsupportedOperationException} will be 274 * thrown on the first matching element. 275 * 276 * @param filter a predicate which returns {@code true} for elements to be 277 * removed 278 * @return {@code true} if any elements were removed 279 * @throws NullPointerException if the specified filter is null 280 * @throws UnsupportedOperationException if elements cannot be removed 281 * from this collection. Implementations may throw this exception if a 282 * matching element cannot be removed or if, in general, removal is not 283 * supported. 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 */ 426 // override 427 // final Spliterator!E spliterator() { 428 // return Spliterators.spliterator(this, 0); 429 // } 430 431 /** 432 * Returns a sequential {@code Stream} with this collection as its source. 433 * 434 * <p>This method should be overridden when the {@link #spliterator()} 435 * method cannot return a spliterator that is {@code IMMUTABLE}, 436 * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} 437 * for details.) 438 * 439 * @implSpec 440 * The default implementation creates a sequential {@code Stream} from the 441 * collection's {@code Spliterator}. 442 * 443 * @return a sequential {@code Stream} over the elements in this collection 444 */ 445 // final Stream!E stream() { 446 // return StreamSupport.stream(spliterator(), false); 447 // } 448 449 /** 450 * Returns a possibly parallel {@code Stream} with this collection as its 451 * source. It is allowable for this method to return a sequential stream. 452 * 453 * <p>This method should be overridden when the {@link #spliterator()} 454 * method cannot return a spliterator that is {@code IMMUTABLE}, 455 * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} 456 * for details.) 457 * 458 * @implSpec 459 * The default implementation creates a parallel {@code Stream} from the 460 * collection's {@code Spliterator}. 461 * 462 * @return a possibly parallel {@code Stream} over the elements in this 463 * collection 464 */ 465 // final Stream!E parallelStream() { 466 // return StreamSupport.stream(spliterator(), true); 467 // } 468 469 470 } 471 472