Top Description Fields Constructors Methods
org.stringtemplate.v4.misc

public Class Aggregate

extends Object
Class Inheritance
Imports
java.util.HashMap

An automatically created aggregate of properties.

I often have lists of things that need to be formatted, but the list items are actually pieces of data that are not already in an object. I need ST to do something like:

Ter=3432
Tom=32234
....

using template:

$items:{it.name$=$it.type$}$

This example will call getName() on the objects in items attribute, but what if they aren't objects? I have perhaps two parallel arrays instead of a single array of objects containing two fields. One solution is allow Maps to be handled like properties so that it.name would fail getName() but then see that it's a Map and do it.get("name") instead.

This very clean approach is espoused by some, but the problem is that it's a hole in my separation rules. People can put the logic in the view because you could say: "go get bob's data" in the view:

Bob's Phone: $db.bob.phone$

A view should not be part of the program and hence should never be able to go ask for a specific person's data.

After much thought, I finally decided on a simple solution. I've added setAttribute variants that pass in multiple property values, with the property names specified as part of the name using a special attribute name syntax: "name.{propName1,propName2,...}". This object is a special kind of HashMap that hopefully prevents people from passing a subclass or other variant that they have created as it would be a loophole. Anyway, the AggregateModelAdaptor#getProperty method looks for Aggregate as a special case and does a get instead of getPropertyName.

Field Summary

Modifier and TypeField and Description
public HashMap<String, Object>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
get(String propName)

protected void
put(String propName, Object propValue)

Allow StringTemplate to add values, but prevent the end user from doing so.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

propertiesback to summary
public HashMap<String, Object> properties

Constructor Detail

Aggregateback to summary
public Aggregate()

Method Detail

getback to summary
public Object get(String propName)
putback to summary
protected void put(String propName, Object propValue)

Allow StringTemplate to add values, but prevent the end user from doing so.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override