/interpreter/tags/at2dist130208/src/edu/vub/at/objects/ATTable.java

http://ambienttalk.googlecode.com/ · Java · 198 lines · 19 code · 17 blank · 162 comment · 0 complexity · 6fc539e37d5075675cd083caa524f412 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * ATTable.java created on 26-jul-2006 at 15:19:44
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.objects.grammar.ATExpression;
  31. /**
  32. * ATTable is the public interface to a native AmtientTalk table (an array).
  33. * Extends the ATExpression interface as a Table may also be output by the parser as a literal.
  34. *
  35. * An important distinction between ATTables and other languages such as Java is that
  36. * ATTable objects are indexed from [1..size]
  37. *
  38. * @author tvcutsem
  39. */
  40. public interface ATTable extends ATExpression {
  41. // base-level interface
  42. /**
  43. * Returns the length of the table
  44. *
  45. * @return the length of the table as an {@link ATNumber}.
  46. */
  47. public ATNumber base_length() throws InterpreterException;
  48. /**
  49. * Returns the value of the element at the index passed as argument.
  50. *
  51. * @param index a position in the table.
  52. * @return ATObject representing the value of the element at the specified index.
  53. * @throws XTypeMismatch if the index is not an {@link ATNumber}.
  54. * @throws XIndexOutOfBounds if the index is negative or if it is greater than the length of the table.
  55. */
  56. public ATObject base_at(ATNumber index) throws InterpreterException;
  57. /**
  58. * Sets the value of the element at the specified index to the new value passed as argument.
  59. *
  60. * @param index a position in the table at which the given element is to be stored.
  61. * @param value the element to be stored.
  62. * @return value an {@link ATObject} representing the value stored in the table at the given index - i.e. the value argument.
  63. * @throws XTypeMismatch if the index is not an {@link ATNumber} or if the value is not an {@link ATObject}.
  64. * @throws XIndexOutOfBounds if the index is negative or if it is greater than to the length of the table
  65. */
  66. public ATObject base_atPut(ATNumber index, ATObject value) throws InterpreterException;
  67. /**
  68. * Checks if the table is empty
  69. *
  70. * @return true if the size of the table is zero. Otherwise, false.
  71. */
  72. public ATBoolean base_isEmpty() throws InterpreterException;
  73. /**
  74. * Applies a closure to each element of the table.
  75. * <p>
  76. * Usage example:
  77. * <code>[1,2,3].each: { |i| system.println(i) }</code>
  78. *
  79. * @param code a closure that takes one argument and is applied to each element of the table.
  80. * @return nil
  81. * @throws InterpreterException if raised inside the code closure.
  82. */
  83. public ATNil base_each_(ATClosure code) throws InterpreterException;
  84. /**
  85. * Maps a closure over each element of the table, resulting in a new table.
  86. * <p>
  87. * Usage example:
  88. * <code>[1,2,3].map: { |i| i + 1 } </code> returns <code>"[2, 3, 4]"</code>
  89. *
  90. * @param code a closure that takes one argument and is applied to each element of the table.
  91. * @return nil
  92. * @throws InterpreterException if raised inside the code closure.
  93. */
  94. public ATTable base_map_(ATClosure code) throws InterpreterException;
  95. /**
  96. * Collects all elements of the table by combining them using the given closure.
  97. * The first time closure is called, the given initializing element is passed as first argument.
  98. * <p>
  99. * Usage example:
  100. * <code>result := [1,2,3].inject: 0 into: { |total, next| total + next }</code> where the value of <code>result</code> is 6.
  101. *
  102. * @param init an {@link ATObject} passed as first argument the first time the closure is called.
  103. * @param code a closure that takes one argument and is applied to each element of the table.
  104. * @return {@link ATObject} representing the value of the last applied closure.
  105. * @throws XIndexOutOfBounds if the index is negative or if it is greater than the length of the table
  106. * @throws InterpreterException if raised inside the code closure.
  107. */
  108. public ATObject base_inject_into_(ATObject init, ATClosure code) throws InterpreterException;
  109. //result := [ tabl ].filter: { |elt| booleanCondition(elt) }
  110. /**
  111. * Returns a new table containing only those elements of the table for which the closure evaluates to true.
  112. * <p>
  113. * Usage example:
  114. * <code>[1,2,3].filter: {|e| e != 2 }</code> returns <code>[1, 3]</code>
  115. *
  116. * @param code a closure that takes one argument and is applied to each element of the table.
  117. * @return ATTable containing those elements of the table for which the closure evaluates to true.
  118. * @throws InterpreterException if raised inside the code closure.
  119. */
  120. public ATTable base_filter_(ATClosure code) throws InterpreterException;
  121. //result := [ tabl ].find: { |elt| booleanCondition(elt) }
  122. /**
  123. * Returns the index of the first element for which the given predicate returns true.
  124. * <p>
  125. * Returns nil if no element satisfying the closure can be found.
  126. * <p>
  127. * Usage example:
  128. * <code>[`a, `b, `c].find: { |e| e == `d }</code> returns <code>nil</code>
  129. *
  130. * @param code a closure that takes one argument and is applied to each element of the table.
  131. * @return {@link ATNumber} representing the index of the first element for which the given closure evaluates to true. Otherwise, nil.
  132. * @throws InterpreterException if raised inside the code closure.
  133. */
  134. public ATObject base_find_(ATClosure code) throws InterpreterException;
  135. /**
  136. * Returns true if and only if there exists an element e in the table for which
  137. * 'obj == e' evaluates to true.
  138. * <p>
  139. * Usage example:
  140. * <code>[`a, `b, `c].contains(`d)</code> returns <code>false</code>
  141. *
  142. * @param obj the element to find in the table
  143. * @return true if exists an element in the table for which 'obj == e' evaluates to true. Otherwise, false.
  144. */
  145. public ATBoolean base_contains(ATObject obj) throws InterpreterException;
  146. /**
  147. * Implodes the receiver table of characters into a text string.
  148. *
  149. * @return the {@link ATText} resulting of the implosion of the table.
  150. */
  151. public ATText base_implode() throws InterpreterException;
  152. /**
  153. * Joins all the text elements of the receiver table into a text string where the given text is used as a separator.
  154. *
  155. * @param sep a text used as separator in the resulting text string.
  156. * @return an {@link ATText} resulting of the join of all the text elements of the receiver using the sep text as separator.
  157. *
  158. */
  159. public ATText base_join(ATText sep) throws InterpreterException;
  160. /**
  161. * Selects the subrange of the table specified by the given limits.
  162. * <p>
  163. * Usage example:
  164. * <code>[a, b, c, d, e].select(2,4)</code> returns <code>[b, c, d]</code>
  165. *
  166. * @param start a number representing the lower limit of the range.
  167. * @param stop a number representing the upper limit of the range.
  168. * @return an ATTable resulting of the selection.
  169. */
  170. public ATTable base_select(ATNumber start, ATNumber stop) throws InterpreterException;
  171. /**
  172. * Concatenation infix operator. Returns the concatenation of the receiver table and the given table.
  173. * <p>
  174. * Usage example:
  175. * <code>[1,2,3] + [4,5] </code> returns <code>[1,2,3,4,5]</code>
  176. *
  177. * @param other a table to concatenate to the receiver table.
  178. * @return an ATTable containing the elements of the receiver table and then the elements of the other table.
  179. */
  180. public ATTable base__oppls_(ATTable other) throws InterpreterException;
  181. }