package de.lmu.ifi.dbs.elki.result;

import java.util.ArrayList;

/**
 * MultiResult is a result collection class.
 * 
 * @author Erich Schubert
 */
public class MultiResult implements Result {
  /**
   * Store the actual results
   */
  private ArrayList<Result> results;
  
  /**
   * Constructor
   * 
   * @param results Array to use for results.
   */
  public MultiResult(ArrayList<Result> results) {
    super();
    this.results = results;
  }

  /**
   * Constructor
   */
  public MultiResult() {
    super();
    this.results = new ArrayList<Result>();
  }

  /**
   * Retrieve result array.
   * 
   * @return results list
   */
  public ArrayList<Result> getResults() {
    return results;
  }

  /**
   * Add a new result to the object
   * 
   * @param r new result
   */
  public void addResult(Result r) {
    this.results.add(r);
  }

  /**
   * Insert a new result at the beginning of the results list
   * 
   * @param r new result
   */
  public void prependResult(Result r) {
    this.results.add(0, r);
  }

  /**
   * Return only results of the given restriction class
   * 
   * @param <C>
   * @param restrictionClass
   * @return filtered results list
   */
  // We can't ensure that restrictionClass matches C.
  @SuppressWarnings("unchecked")
  public <C> ArrayList<C> filterResults(Class<?> restrictionClass) {
    ArrayList<C> res = new ArrayList<C>();
    for (Result result : results)
      try {
        res.add((C) restrictionClass.cast(result));
      } catch (ClassCastException e) {
        // skip non-matching items
      }
    return res;
  }
}

@@ -266,18 +266,16 @@
 esults;%0A
-  
 %0A  /**%0A 
@@ -1382,17 +1382,16 @@
 %0A    for
- 
 (Result 
@@ -1407,17 +1407,48 @@
 results)
-%0A
+ %7B%0A      if(result != null) %7B%0A  
       tr
@@ -1459,16 +1459,18 @@
         
+  
 res.add(
@@ -1515,16 +1515,25 @@
     
-%7D
+  %7D%0A       
  catch
- 
 (Cla
@@ -1549,24 +1549,26 @@
 eption e) %7B%0A
+  
         // s
@@ -1592,16 +1592,32 @@
 items%0A  
+ 
+     %7D%0A      %7D%0A
     %7D%0A  

Increase robustness wrt. null pointers.