[stringtemplate-interest] Use case supported by ST?

Joshua Royalty royalpeasantry at gmail.com
Fri Feb 5 10:40:43 PST 2010


I'm new, but given that ST only allows you to check for not null/null or
true/false I don't believe you can generate it from the EnumSet.  I believe
you need to do some preprocessing and build a map from CheckListItem to
Boolean for the contains part.

I'm pretty sure this was a deliberate design decision to enforce strict
model-view separation.

EX:
public class App {
    public enum ChecklistItem {
        Test1, Test2, Test3, Test4, Test5;

        public String getTitle() {
            return name();
        }
    }

    public static void main(String[] args) throws Exception {
        final EnumSet<ChecklistItem> checklist =
EnumSet.of(ChecklistItem.Test1, ChecklistItem.Test3);
        final EnumMap<ChecklistItem, Boolean> checklistContents = new
EnumMap<ChecklistItem, Boolean>(ChecklistItem.class);
        for (ChecklistItem item : ChecklistItem.values()) {
            checklistContents.put(item, checklist.contains(item));
        }
        StringTemplate checks = new StringTemplate("<checklist.keys:{ k |
<k.title>: <if(checklist.(k))>Yes<else>No<endif>\n}>",
AngleBracketTemplateLexer.class);
        checks.setAttribute("checklist", checklistContents);
        System.out.println(checks.toString());
    }
}

A slightly more compact form of synax, ST will create the map for you but I
think it is slightly less efficient because I think this ends up being a
List<Map<String, Object>> instead of an EnumMap<CheckListItem, Boolean>, so
it takes up alot more space plus it takes computation time when parsing the
attribute.

public class App {
    public enum ChecklistItem {
        Test1, Test2, Test3, Test4, Test5;

        public String getTitle() {
            return name();
        }
    }

    public static void main(String[] args) throws Exception {
        final EnumSet<ChecklistItem> checklist =
EnumSet.of(ChecklistItem.Test1, ChecklistItem.Test3);
        StringTemplate checks = new StringTemplate("<checklist:{ v |
<v.item.title>: <if(v.contains)>Yes<else>No<endif>\n}>",
AngleBracketTemplateLexer.class);
        for (ChecklistItem item : ChecklistItem.values()) {
            checks.setAttribute("checklist.{item, contains}", item,
checklist.contains(item));
        }
        System.out.println(checks.toString());
    }
}

Look at http://www.antlr.org/wiki/display/ST/Expressions for more
information.
-Josh

On Fri, Feb 5, 2010 at 12:27 AM, Ronald Muller <ronald.k.muller at gmail.com>wrote:

> In the set are enums. I have to print "yes" if the set contains the value,
> "No" otherwise. So in java code:
>
> final EnumSet<ChecklistItem> checklist = ....
> for (ChecklistItem item : ChecklistItem.values()) {
>       println(item.getTitle() + ": " + checklist.contains(item) ? "Yes" :
> "No");
> }
>
> Regards,
>
> Ronald
>
> 2010/2/3 Terence Parr <parrt at cs.usfca.edu>
>
>>
>> On Feb 3, 2010, at 2:05 AM, Ronald Muller wrote:
>>
>> > Hi,
>> >
>> > I have the following use case:
>> >
>> > I have to list items and display "Yes" or "No" if they are present in a
>> set (Checklist items in my case, modeled as enums), So:
>> >
>> > Task1: Yes
>> > Task2: No
>> > Task3: No
>> > ...
>>
>> Hi.  What's in the set? must be an object with string/boolean?  wouldn't
>> this work then
>>
>> $yourdomainobject.checkList:{o | $o.str$: $o.bool$}$
>>
>> Ter
>>
>>
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20100205/cba999a8/attachment.html 


More information about the stringtemplate-interest mailing list