All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.intTable.stg Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version


intTable(packageName,primitiveType,objectType) ::=
<<
package ;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;

public class Table
{
   private ArrayList\ > table = new ArrayList\<>();

   public ArrayList\ > getTable()
   {
      return table;
   }

   public void setTable(ArrayList\ > table)
   {
      this.table = table;
   }

   private String columnName = null;

   public String getColumnName()
   {
      return columnName;
   }

   public void setColumnName(String columnName)
   {
      this.columnName = columnName;
   }

   LinkedHashMap\ columnMap = new LinkedHashMap\<>();

   public void setColumnMap(LinkedHashMap\ columnMap)
   {
      this.columnMap = columnMap;
   }

   public Table(... start)
   {
      columnName = "A";
      columnMap.put(columnName, 0);
      for ( current : start)
      {
         ArrayList\ row = new ArrayList\<>();
         row.add(current);
         table.add(row);
      }
   }


   public  sum ()
   {
       result = 0;
      for (ArrayList\ row : table)
      {
         result += () row.get(columnMap.get(columnName));
      }
      return result;
   }


   public  min ()
   {
       result = .MAX_VALUE;
      for (ArrayList\ row : table)
      {
          value =  () row.get(columnMap.get(columnName));
         if (value \< result)
            result = value;
      }
      return result;
   }


   public  max ()
   {
       result = .MIN_VALUE;
      for (ArrayList\ row : table)
      {
          value =  () row.get(columnMap.get(columnName));
         if (value > result)
            result = value;
      }
      return result;
   }


   public  median ()
   {
      ArrayList\<  > list = this.toList();
      Collections.sort(list);
      int index = list.size() / 2;
       result = list.get(index);
      return result;
   }


   public ArrayList\<  > toList()
   {
      ArrayList\<  > result = new ArrayList\<>();
      for (ArrayList\ row : table)
      {
          value =  () row.get(columnMap.get(columnName));
         result.add(value);
      }
      return result;
   }

   @Override
   public String toString()
   {
      StringBuilder buf = new StringBuilder();
      for (String key : columnMap.keySet())
      {
         buf.append(key).append(" \t");
      }
      buf.append("\n");
      for (ArrayList\ row : table)
      {
         for (Object cell : row)
         {
            buf.append(cell).append(" \t");
         }
         buf.append("\n");
      }
      buf.append("\n");
      return buf.toString();
   }
}
>>