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

templates.tablesSelectColumns.stg Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version

selectColumns(className) ::=
<<
   public Table selectColumns(String... columnNames)
   {
      LinkedHashMap\ oldColumnMap = (LinkedHashMap\) this.columnMap.clone();
      this.columnMap.clear();

      int i = 0;
      for (String name : columnNames)
      {
         if (oldColumnMap.get(name) == null)
            throw new IllegalArgumentException("unknown column name: " + name);
         this.columnMap.put(name, i);
         i++;
      }

      ArrayList\ > oldTable = (ArrayList\ >) this.table.clone();
      this.table.clear();

      LinkedHashSet\ > rowSet = new LinkedHashSet\<>();
      for (ArrayList row : oldTable)
      {
         ArrayList newRow = new ArrayList();
         for (String name : columnNames)
         {
            Object value = row.get(oldColumnMap.get(name));
            newRow.add(value);
         }
         if (rowSet.add(newRow))
            this.table.add(newRow);
      }

      return this;
   }
>>



dropColumns(className) ::=
<<
   public Table dropColumns(String... columnNames)
   {
      LinkedHashMap\ oldColumnMap = (LinkedHashMap\) this.columnMap.clone();
      this.columnMap.clear();

      LinkedHashSet\ dropNames = new LinkedHashSet\<>();
      dropNames.addAll(Arrays.asList(columnNames));
      int i = 0;
      for (String name : oldColumnMap.keySet())
      {
         if ( ! dropNames.contains(name))
         {
            this.columnMap.put(name, i);
            i++;
         }
      }

      ArrayList\ > oldTable = (ArrayList\ >) this.table.clone();
      this.table.clear();

      LinkedHashSet\ > rowSet = new LinkedHashSet\<>();
      for (ArrayList row : oldTable)
      {
         ArrayList newRow = new ArrayList();
         for (String name : this.columnMap.keySet())
         {
            Object value = row.get(oldColumnMap.get(name));
            newRow.add(value);
         }
         if (rowSet.add(newRow))
            this.table.add(newRow);
      }

      return this;
   }
>>


addColumn(className) ::=
<<
   public void addColumn(String columnName, java.util.function.Function\,Object> function)
   {
      int newColumnNumber = this.table.size() > 0 ? this.table.get(0).size() : 0;
      for (ArrayList\ row : this.table)
      {
         java.util.LinkedHashMap\ map = new java.util.LinkedHashMap\<>();
         for (String key : columnMap.keySet())
         {
            map.put(key, row.get(columnMap.get(key)));
         }
         Object result = function.apply(map);
         row.add(result);
      }
      this.columnMap.put(columnName, newColumnNumber);
   }
>>