Package jestr.examples.example3

This example illustrates enumeration stringification.

See:
          Description

Class Summary
Account  
Balance  
RunIt  
 

Package jestr.examples.example3 Description

This example illustrates enumeration stringification. You can run it with "ant -f runexample3.xml" from the root of the distribution.

Suppose each account has a state, which is either Open, Closed, or Inactive, and that we use an integer to hold this state. Thus we modify the Account class as follows:


    public class Account {
        public static final int OPEN = 0;
        public static final int CLOSED = 1;
        public static final int INACTIVE = 2;
        private Balance currentBalance = new Balance(100.00, "USD", new Date());
        private Balance endingBalance = new Balance(62.44, "USD", new Date() );
        private Balance ledgerBalance = new Balance(89.75, "USD", new Date());
        private String id = "123";
        private int acctState = INACTIVE;
    }

Since acctState is an integer, it will log as just a number, in this case 2. However, the logs would be more readable if acctState could show up as its corresponding mnemonic. To accomplish this we declare a stringifier based on the predefined "enum" stringifier and define a predicate that matches attributes named "acctState":


    jestr.predicate.isAcctState=clone-of-standard
    jestr.predicate.isAcctState.name=acctState

    jestr.stringifier.acctState=clone-of-enum
    jestr.stringifier.acctState.predicates=isAcctState
    jestr.stringifier.acctState.classDeclaringConstants=jestr.examples.example3.Account

The "classDeclaringConstants" property tells our stringifier to look for fields that are static and final in class jestr.examples.example3.Account and interpret them as constants defining enumerated values for objects matching its predicate. The predicate "isAcctState" just looks for anything named "acctState". We will see more sophisticated predicates in later examples.

The output is as follows:


    Account(
        Balance currentBalance = Balance(100.0 USD as of Wed Mar 03 12:23:04 EST 2004)
        Balance endingBalance = Balance(62.44 USD as of Wed Mar 03 12:23:04 EST 2004)
        Balance ledgerBalance = Balance(89.75 USD as of Wed Mar 03 12:23:04 EST 2004)
        String id = "123"
        int acctState = INACTIVE(2)
    )

The "acctState" attribute now logs as its mnemonic, with the corresponding value in parentheses.

Instead of specifying a class via the classDeclaringConstants property, we could have explicitly listed the name/value pairs, via the constants property, like this:


    jestr.stringifier.acctState.constants=OPEN=0,CLOSED=1,INACTIVE=2

The output is the same as before:


    Account(
        Balance currentBalance = Balance(100.0 USD as of Wed Mar 03 12:23:04 EST 2004)
        Balance endingBalance = Balance(62.44 USD as of Wed Mar 03 12:23:04 EST 2004)
        Balance ledgerBalance = Balance(89.75 USD as of Wed Mar 03 12:23:04 EST 2004)
        String id = "123"
        int acctState = INACTIVE(2)
    )



Copyright (c) 2001-2003 - Apache Software Foundation