Code example for Formatter

Methods: format, out

0
    public CountPerTypeListener(ComplexEventListener complexEventListener) {
        super(complexEventListener);
    } 
 
    public void update(EventBean[] newEvents, EventBean[] oldEvents) {
        Formatter outFormatter = new Formatter(new StringBuffer());
        outFormatter.format("%10s | %10s\n", "type", "count");
        outFormatter.format("---------- | ----------\n");
 
        int total = 0;
        for (int i = 0; i < newEvents.length; i++) {
            String type = (String) newEvents[i].get("type");
            long count = (Long) newEvents[i].get("countPerType");
            outFormatter.format("%10s | %10s\n", type, count);
            total += count;
        } 
        outFormatter.format("%10s | %10s\n", "total =", total);
 
        complexEventListener.onComplexEvent("Current count per type:\n" + outFormatter.out().toString());
    } 
}