PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/xxlcore/src/xxl/core/collections/bags/Bag.java

http://xxl.googlecode.com/
Java | 187 lines | 30 code | 15 blank | 142 comment | 0 complexity | 9a369a0d48b4d89fdc3df0f84848544a MD5 | raw file
  1. /* XXL: The eXtensible and fleXible Library for data processing
  2. Copyright (C) 2000-2011 Prof. Dr. Bernhard Seeger
  3. Head of the Database Research Group
  4. Department of Mathematics and Computer Science
  5. University of Marburg
  6. Germany
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 3 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; If not, see <http://www.gnu.org/licenses/>.
  17. http://code.google.com/p/xxl/
  18. */
  19. package xxl.core.collections.bags;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import xxl.core.cursors.Cursor;
  23. import xxl.core.functions.AbstractFunction;
  24. import xxl.core.functions.Function;
  25. import xxl.core.predicates.Predicate;
  26. /**
  27. * The interface bag represents a data type that is able to contain any kind of
  28. * object and performs no duplicate detection. More formally, this interface
  29. * models a kind of mathematical <i>multiset</i> abstraction.
  30. *
  31. * <p>The bag only provides methods for insertion of objetcs and removal of all
  32. * elements, but for accessing or removing <i>single</i> elements, a cursor
  33. * iterating over all elements of the bag must be created by calling
  34. * <code>cursor()</code>. The sequence of elements delivered by this cursor has
  35. * no predefined order. However, a subclass may define such an order.</p>
  36. *
  37. * @param <E> the type of the elements this bag is able to store.
  38. * @see Cursor
  39. * @see Function
  40. * @see Iterator
  41. */
  42. public interface Bag<E> {
  43. /**
  44. * A factory method to create a default bag. Each concrete implementation
  45. * of bag except for ListBag should have a function FACTORY_METHOD that
  46. * implements three variants of <code>invoke</code>
  47. * <ul>
  48. * <dl>
  49. * <dt><li><code>Bag invoke()</code>:</dt>
  50. * <dd>returns <code>new Bag()</code>.</dd>
  51. * <dt><li><code>Bag invoke(Object iterator)</code>:</dt>
  52. * <dd>returns <code>new Bag(iterator)</code>.</dd>
  53. * <dt><li><code>Bag invoke(List<? extends Object> internalDataStructure)</code>:</dt>
  54. * <dd>returns <code>new Bag((&lt;<i>InternalDataStructure&gt;</i>)internalDataStructure.get(0))</code>.</dd>
  55. * </dl>
  56. * </ul>
  57. * This factory method creates a new ListBag. It may be invoked with a
  58. * <i>parameter list</i> (for further details see {@link Function}) of
  59. * lists, an iterator or without any parameters. A <i>parameter list</i> of
  60. * lists will be used to initialize the internally used list with the list
  61. * at index 0 and an iterator will be used to insert the contained elements
  62. * into the new ListBag.
  63. *
  64. * @see Function
  65. */
  66. public static final Function<Object,ListBag<Object>> FACTORY_METHOD = new AbstractFunction<Object,ListBag<Object>> () {
  67. @Override
  68. public ListBag<Object> invoke() {
  69. return new ListBag<Object>();
  70. }
  71. @Override
  72. public ListBag<Object> invoke(Object iterator) {
  73. return new ListBag<Object>((Iterator<?>)iterator);
  74. }
  75. @Override
  76. public ListBag<Object> invoke(List<? extends Object> list) {
  77. return new ListBag<Object>((List<Object>)list.get(0));
  78. }
  79. };
  80. /**
  81. * Removes all of the elements from this bag. The bag will be empty after
  82. * this call so that <code>size() == 0</code>.
  83. */
  84. public abstract void clear();
  85. /**
  86. * Closes this bag and releases any system resources associated with it.
  87. * This operation is idempotent, i.e., multiple calls of this method take
  88. * the same effect as a single call. When needed, a closed bag can be
  89. * implicit reopened by a consecutive call to one of its methods. Because
  90. * of having an unspecified behavior when this bag is closed every cursor
  91. * iterating over the elements of this bag must be closed.
  92. *
  93. * <p><b>Note:</b> This method is very important for bags using external
  94. * resources like files or JDBC resources.</p>
  95. */
  96. public abstract void close();
  97. /**
  98. * Returns a cursor to iterate over the elements in this bag without any
  99. * predefined sequence. The cursor is specifying a <i>view</i> on the
  100. * elements of this bag so that closing the cursor takes no effect on the
  101. * bag (e.g., not closing the bag). The behavior of the cursor is
  102. * unspecified if this bag is modified while the cursor is in progress in
  103. * any way other than by calling the methods of the cursor. So, when the
  104. * implementation of this cursor cannot guarantee that the cursor is in a
  105. * valid state after modifying the underlying bag every method of the cursor
  106. * except <code>close()</code> should throw a
  107. * <code>ConcurrentModificationException</code>.
  108. *
  109. * @return a cursor to iterate over the elements in this bag without any
  110. * predefined sequence.
  111. */
  112. public abstract Cursor<E> cursor();
  113. /**
  114. * Adds the specified element to this bag. This method does not perform any
  115. * kind of <i>duplicate detection</i>.
  116. *
  117. * @param object element to be added to this bag.
  118. */
  119. public abstract void insert(E object);
  120. /**
  121. * Adds all of the elements in the specified iterator to this bag. This
  122. * method does not perform any kind of <i>duplicate detection.</i> The
  123. * behavior of this operation is unspecified if the specified iterator is
  124. * modified while the operation is in progress.
  125. *
  126. * @param objects iterator whose elements are to be added to this bag.
  127. */
  128. public abstract void insertAll(Iterator<? extends E> objects);
  129. /**
  130. * Returns the number of elements in this bag (its cardinality). If this
  131. * bag contains more than <code>Integer.MAX_VALUE</code> elements,
  132. * <code>Integer.MAX_VALUE</code> is returned.
  133. *
  134. * @return the number of elements in this bag (its cardinality).
  135. */
  136. public abstract int size();
  137. /**
  138. * Returns a cursor to iterate over all elements in this bag for which the
  139. * given predicate returns <code>true</code>. This method is very similar
  140. * to the cursor method except that its result is determined by a
  141. * predicate. A possible implementation filters the result of the cursor
  142. * method using the following code
  143. * <code><pre>
  144. * return new Filter<E>(cursor(), predicate);
  145. * </pre></code>
  146. * Note, that this method is implemented in <i>AbstractBag</i>.
  147. *
  148. * <p>The default implementation of this method is not very interesting,
  149. * but the method is very import for some bags. When the data structure
  150. * that is internally used for storing the elements of this bag is able to
  151. * handle with queries, this method can be implemented very efficient by
  152. * passing the query to the data structure. For example a range query on a
  153. * bag that internally uses a R-tree to store its elements will be more
  154. * efficient when it is proceed on the R-tree itself.</p>
  155. *
  156. * <p>Like the cursor returned by the cursor method, this cursor's behavior
  157. * is unspecified if this bag is modified while the cursor is in progress
  158. * in any way other than by calling the methods of the cursor.</p>
  159. *
  160. * @param predicate a predicate that determines whether an element of this
  161. * bag should be returned or not.
  162. * @return a cursor to iterate over all elements in this bag for which the
  163. * given predicate returns <code>true</code>.
  164. */
  165. public abstract Cursor<E> query(Predicate<? super E> predicate);
  166. }