PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/briljant-core/src/main/java/org/briljantframework/data/series/Storage.java

https://gitlab.com/briljant/briljant
Java | 171 lines | 29 code | 17 blank | 125 comment | 0 complexity | 88933441a86889d5d37552c38a93e90e MD5 | raw file
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2016 Isak Karlsson
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  7. * associated documentation files (the "Software"), to deal in the Software without restriction,
  8. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  9. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or
  13. * substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  16. * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. package org.briljantframework.data.series;
  22. import java.util.List;
  23. import java.util.function.Supplier;
  24. import org.apache.commons.math3.complex.Complex;
  25. import org.briljantframework.ComplexSequence;
  26. import org.briljantframework.DoubleSequence;
  27. import org.briljantframework.IntSequence;
  28. /**
  29. * This class provides location based indexing of {@link Series}. // TODO: 4/28/16 removeAll
  30. *
  31. * @author Isak Karlsson
  32. */
  33. public interface Storage extends DoubleSequence, ComplexSequence, IntSequence, List<Object> {
  34. default Object get(int i) {
  35. return get(Object.class, i);
  36. }
  37. /**
  38. * Returns the value at {@code index} as an instance of {@code T}. If value at {@code index} is
  39. * not an instance of {@code cls}, returns an appropriate {@code NA} value. For references types
  40. * (apart from {@code Complex} and {@code Logical}) this means {@code null} and for
  41. * {@code primitive} types their respective {@code NA} value. Hence, checking for {@code null}
  42. * does not always work. Instead {@link org.briljantframework.data.Is#NA(Object)} should be used.
  43. *
  44. * <pre>
  45. * {@code
  46. * Series v = new ObjectSeries(Date.class, Arrays.asList(new Date(), new Date());
  47. * Date date = v.get(Date.class, 0);
  48. * if(Is.NA(date)) { // or date == null
  49. * // got a NA value
  50. * }
  51. *
  52. * Series v = ...; // for example an IntSeries
  53. * int value = v.get(Integer.class, 32);
  54. * if(Is.NA(value)) { // or value == IntSeries.NA (but not value == null)
  55. * // got a NA value
  56. * }}
  57. * </pre>
  58. *
  59. * <p>
  60. * {@link java.lang.ClassCastException} should not be thrown, instead {@code NA} should be
  61. * returned
  62. *
  63. * @param cls the class
  64. * @param i the index
  65. * @param <T> the type
  66. * @return a value of type; returns {@code NA} if value is not an instance of {@code cls}
  67. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  68. */
  69. <T> T get(Class<T> cls, int i);
  70. @Override
  71. Object set(int index, Object element);
  72. double setDouble(int index, double value);
  73. /**
  74. * Returns value as {@code double} if applicable. Otherwise returns
  75. * {@link org.briljantframework.data.Na#DOUBLE}.
  76. *
  77. * @param i the index
  78. * @return a double
  79. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  80. */
  81. double getDouble(int i);
  82. int setInt(int index, int value);
  83. /**
  84. * Returns value as {@code int} if applicable. Otherwise returns
  85. * {@link org.briljantframework.data.Na#INT}
  86. *
  87. * @param i the index
  88. * @return an int
  89. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  90. */
  91. int getInt(int i);
  92. /**
  93. * Get the value at the specified index. If the value is {@code NA}, the supplied default value is
  94. * returned.
  95. *
  96. * @param cls the class
  97. * @param index the index
  98. * @param defaultValue the default value supplier
  99. * @param <T> the type
  100. * @return the value at the specified index or the default value
  101. */
  102. <T> T get(Class<T> cls, int index, Supplier<T> defaultValue);
  103. @Override
  104. default Complex getComplex(int i) {
  105. return get(Complex.class, i);
  106. }
  107. /**
  108. * Returns true if value at {@code index} is NA
  109. *
  110. * @param i the index
  111. * @return true or false
  112. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  113. */
  114. boolean isNA(int i);
  115. /**
  116. * Follows the conventions from {@link Comparable#compareTo(Object)}.
  117. *
  118. * <p>
  119. * Returns value {@code < 0} if value at index {@code a} is less than {@code b}. value {@code > 0}
  120. * if value at index {@code b} is larger than {@code a} and 0 if they are equal.
  121. *
  122. * @param a the index a
  123. * @param b the index b
  124. * @return comparing int
  125. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  126. * @see Comparable#compareTo(Object)
  127. */
  128. int compare(int a, int b);
  129. /**
  130. * Compare value at {@code a} in {@code this} to value at {@code b} in {@code ba}. Equivalent to
  131. * {@code this.get(a).compareTo(other.get(b))}, but in most circumstances with greater
  132. * performance.
  133. *
  134. * @param a the index in {@code this}
  135. * @param other the other series
  136. * @param b the index in {@code other}
  137. * @return the comparison
  138. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  139. * @see java.lang.Comparable#compareTo(Object)
  140. */
  141. int compare(int a, Series other, int b);
  142. /**
  143. * Returns true if element at {@code a} in {@code this} equals element at {@code b} in
  144. * {@code other}.
  145. *
  146. * @param a the index in this
  147. * @param other the other series
  148. * @param b the index in other
  149. * @return true if values are equal
  150. * @throws java.lang.IndexOutOfBoundsException if {@code index < 0 || index > size()}
  151. */
  152. boolean equals(int a, Storage other, int b);
  153. void setFrom(int to, Storage source, int from);
  154. }