package org.test;

import org.nlogo.api.*;

import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;

public class ComputeMean extends DefaultReporter{

    /** way 1 using repeatableType
    public Syntax getSyntax() {
        return Syntax.reporterSyntax(new int[]{Syntax.NumberType() | Syntax.RepeatableType()},Syntax.NumberType());
    } */

    /** way 2 using listType **/
    public Syntax getSyntax() {
        return Syntax.reporterSyntax(new int[]{Syntax.ListType()},Syntax.NumberType());
    }


    public double average(ArrayList<Double> numbers)
    {
        double average = 0.0;
        for (int i = 0; i < numbers.size(); i++)  {
            average += numbers.get(i);
        }

        return average/numbers.size();
    }

    /** Corersponding call in netlogo : show (my-extension:get-mean 5.0 5.0 10.0)
    public Object report(Argument args[], Context context) throws ExtensionException, LogoException {

        ArrayList<Double> numbers = new ArrayList<Double>();

        try {

            for (int i = 0; i < args.length; i += 1) {
                numbers.add(args[i].getDoubleValue());
            }
        } catch (LogoException e) {
        throw new ExtensionException(e.getMessage());
        }

        return average(numbers);

    }*/

    /* Corersponding call in netlogo : print my-extension:get-mean [5.0 5.0 10.0] **/
    private LogoList getListOrNull(Argument args[]) throws ExtensionException, LogoException  {
        try {
            return args[0].getList();
        } catch (LogoException e) {
            return null;
        }
    }

    public Object report(Argument args[], Context context) throws ExtensionException, LogoException   {

        final LogoList logoListNumbers = getListOrNull(args);
        Double[] logoDouble = null;
        try {
            Object[] objectArray = logoListNumbers.toArray();
            logoDouble = Arrays.copyOf(objectArray, objectArray.length, Double[].class);
        }catch (ClassCastException e){}

        ArrayList<Double> nunbers = new ArrayList<Double>(Arrays.asList(logoDouble));

        return average(nunbers);

    }
}

@@ -62,16 +62,39 @@
 Arrays;%0A
+import java.util.List;%0A
 import j
@@ -379,24 +379,519 @@
 ;%0A    %7D */%0A%0A
+    /** way 1 Corresponding call in netlogo : show (my-extension:get-mean 5.0 5.0 10.0)%0A     public Object report(Argument args%5B%5D, Context context) throws ExtensionException, LogoException %7B%0A%0A     ArrayList%3CDouble%3E numbers = new ArrayList%3CDouble%3E();%0A%0A     try %7B%0A%0A     for (int i = 0; i %3C args.length; i += 1) %7B%0A     numbers.add(args%5Bi%5D.getDoubleValue());%0A     %7D%0A     %7D catch (LogoException e) %7B%0A     throw new ExtensionException(e.getMessage());%0A     %7D%0A%0A     return average(numbers);%0A%0A     %7D*/%0A%0A
     /** way 
@@ -1038,17 +1038,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1127,16 +1127,17 @@
  = 0.0;%0A
+%0A
         
@@ -1145,49 +1145,30 @@
 or (
-int i = 0; i %3C numbers.size(); i++) 
+Double d : numbers)
  %7B%0A
-  
     
@@ -1188,22 +1188,9 @@
  += 
-numbers.get(i)
+d
 ;%0A  
@@ -1248,621 +1248,78 @@
  %7D%0A%0A
+%0A
     /*
-* Corersponding call in netlogo : show (my-extension:get-mean 5.0 5.0 10.0)%0A    public Object report(Argument args%5B%5D, Context context) throws ExtensionException, LogoException %7B%0A%0A        ArrayList%3CDouble%3E numbers = new ArrayList%3CDouble%3E();%0A%0A        try %7B%0A%0A            for (int i = 0; i %3C args.length; i += 1) %7B%0A                numbers.add(args%5Bi%5D.getDoubleValue());%0A            %7D%0A        %7D catch (LogoException e) %7B%0A        throw new ExtensionException(e.getMessage());%0A        %7D%0A%0A        return average(numbers);%0A%0A    %7D*/%0A%0A    /* Corersponding call in netlogo : print my-extension:get-mean %5B5.0 5.0 10.0%5D *
+ Utility method to safely cast LogoList into ArrayList%3CDouble%3E 
 */%0A 
@@ -1535,32 +1535,126 @@
        %7D%0A    %7D%0A%0A
+    /* Way 2 : Corresponding call in netlogo : print my-extension:get-mean %5B5.0 5.0 10.0%5D **/%0A
     public Objec
@@ -1803,16 +1803,104 @@
 l(args);
+%0A%0A        // LogoList return an array of Object, so we need to cast to ArrayList%5BDouble%5D
 %0A       
@@ -2134,16 +2134,17 @@
 n e)%7B%7D%0A%0A
+%0A
         

fix computeMean method