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

templates.StringTable.stg Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version


StringTable(packageName) ::=
<<
package ;

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

public class StringTable
{
   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 StringTable(String... start)
   {
      columnName = "A";
      columnMap.put(columnName, 0);
      for (String current : start)
      {
         ArrayList\ row = new ArrayList\<>();
         row.add(current);
         table.add(row);
      }
   }

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

   public String join(String seperator)
   {
      StringBuilder buf = new StringBuilder();
      boolean first = true;
      for (ArrayList\ row : table)
      {
         String value =  (String) row.get(columnMap.get(columnName));
         if ( ! first)
         {
            buf.append(seperator);
         }
         first = false;
         buf.append(value);
      }
      return buf.toString();
   }

   @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();
   }
}
>>