Commits

Bogdan Mart committed bfa74ac

added implementation of "at" method
fixed javadoc for two methods

  • Participants
  • Parent commits 420fb83

Comments (0)

Files changed (1)

File src/main/java/com/innahema/collections/query/queriables/Queryable.java

     /**
      * Leaves no diplicates in sequence.<br>
      * {@link Object#hashCode()} and {@link Object#equals(Object)} are used.<br>
-     * <font color="red"><b>Caution</b></font> this is not final api, in final variant there will be only {@link  Queryable#distinct()} method
+     * <b style="color: red">Caution</b> this is not final api, in final variant there will be only {@link  Queryable#distinct()} method
      */
     @Deprecated
     public Queryable<T> distinct2() {
     /**
      * Leaves no duplicates in sequence.<br>
      * {@link Object#hashCode()} and {@link Object#equals(Object)} are used.<br>
-     * <font color="red"><b>Caution</b></font> this is not final api, in final variant there will be only {@link  Queryable#distinct()} method
+     * <b style="color: red">Caution</b> this is not final api, in final variant there will be only {@link  Queryable#distinct()} method
      */
     @Deprecated
     public Queryable<T> distinct3() {
     }
 
     /**
+     * Return element at given index, if sequence has less elements, then {@link java.lang.IndexOutOfBoundsException}
+     * will be thrown
+     * @param index ...
+     * @return element at given index
+     * @throws java.lang.IndexOutOfBoundsException  if not 0 &le; <b>index</b> &lt; {@link Queryable#count()}
+     */
+    public T at(int index) {
+        if(index<0)
+            throw new IndexOutOfBoundsException("negative index: "+index);
+        int count=0;
+        for (T obj : this){
+            if(count==index)
+                return obj;
+            count++;
+        }
+        throw new IndexOutOfBoundsException("Index greater than inner sequence: "+index);
+    }
+    /**
+     * Return element at given index, if sequence has less elements, then <b>null</b> will be returned
+     * will be thrown
+     * @param index ...
+     * @return element at given index or <b>null</b> if no such element
+     * @throws java.lang.IndexOutOfBoundsException if <b>index</b> &lt; 0
+     */
+    @Nullable
+    public T atOrDefault(int index) {
+        if(index<0)
+            throw new IndexOutOfBoundsException("negative index: "+index);
+        int count=0;
+        for (T obj : this){
+            if(count==index)
+                return obj;
+            count++;
+        }
+        return null;
+    }
+    /**
      * Utilitary function for performing unchecked casts without need to mark whole methods ans unchecked
      */
     @SuppressWarnings("unchecked")