001package votorola.a.count.gwt; // Copyright 2011, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.*;
004import votorola.g.web.gwt.*;
005
006
007/** A resource superaccount implemented as a JavaScript overlay type.
008  *
009  *     @see <a href='http://reluk.ca/w/Category:Account#Superaccounts'
010  *       target='_top'>Category:Account#Superaccounts</a>
011  */
012public class SacJS extends JavaScriptObjectX
013{
014
015
016    protected SacJS() {} // "precisely one constructor... protected, empty, and no-argument"
017
018
019
020    /** Initializes a superaccount that was received on the wire.
021      *
022      *     @throws CountingMethodJS.NoSuchMethod if the counting method is unrecognized.
023      */
024    final void init( final String countingMethodName, final String accountName )
025      throws CountingMethodJS.NoSuchMethod
026    {
027        if( countingMethodName == null || accountName == null ) throw new NullPointerException();
028
029        if( countingMethodName() != null && accountName() != null )
030        {
031            throw new IllegalStateException();
032        }
033
034        _set( "countingMethodName", countingMethodName );
035        _set( "countingMethodMnemonic", CountingMethodJS.SwitchMnemonic.valueOfFullName(
036          countingMethodName ));
037        _set( "accountName", accountName );
038    }
039
040
041
042   // ------------------------------------------------------------------------------------
043
044
045    /** The account name for this superaccount.
046      *
047      *     @see <a href='http://reluk.ca/w/Property:Account_name'
048      *       target='_top'>Property:Account_name</a>
049      */
050    public final native String accountName() /*-{ return this.accountName; }-*/;
051
052
053
054    /** Appends the value for the 'a' switch that designates this superaccount.
055      *
056      *     @param sac the superaccount, or null for the null account.
057      *     @return the same string builder.
058      *
059      *     @see SacSelection#aSacSwitch()
060      */
061    public static final StringBuilder appendSwitch( final SacJS sac, final StringBuilder b )
062    {
063        if( sac == null ) return b.append( "nulNull" );
064
065        b.append( sac.countingMethodMnemonic().name() );
066        final String accountName = sac.accountName();
067        final char c = accountName.charAt( 0 );
068        if( c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c == '\'' ) b.append( '\'' );
069        b.append( accountName );
070        return b;
071    }
072
073
074
075    /** The switch mnemonic of the counting method.
076      */
077    public final native CountingMethodJS.SwitchMnemonic countingMethodMnemonic()
078    /*-{
079        return this.countingMethodMnemonic;
080    }-*/;
081
082
083
084    /** The name of the counting method.
085      *
086      *     @see <a href='http://reluk.ca/w/Category:Counting_method'
087      *       target='_top'>Category:Counting method</a>
088      */
089    public final native String countingMethodName() /*-{ return this.countingMethodName; }-*/;
090
091
092
093    /** A lexical comparator that sorts (1) by account name, and (2) by counting method.
094      *
095      *     @see #accountName()
096      *     @see #countingMethodName()
097      */
098    public static final Comparator<SacJS> NAME_COMPARATOR = new Comparator<SacJS>()
099    {
100        public int compare( final SacJS s1, final SacJS s2 )
101        {
102            int signum = s1.accountName().compareTo( s2.accountName() );
103            if( signum == 0 ) signum = s1.countingMethodName().compareTo( s2.countingMethodName() );
104            assert signum != 0 || s1.equals(s2): "comparison consistent with equality";
105            return signum;
106        }
107    };
108
109
110
111}