001package votorola.s.gwt.scene.feed; // 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 com.google.gwt.core.client.*;
004import votorola.g.lang.*;
005
006
007/** A person implemented as a JavaScript overlay type.
008  */
009public final class PersonJS extends JavaScriptObject implements Person
010{
011
012
013    protected PersonJS() {} // "precisely one constructor... protected, empty, and no-argument"
014
015
016
017    /** Initializes the native object, and sets <code>p.initJSCalled</code> to true.
018      */
019    private static native void initJS( Object p ) // "JSNI methods may not refer to instance methods defined within a JavaScriptObject"
020    /*-{
021        if( p.voteTrace ) // fill in detail excluded from serialization:
022        {
023            p.voteTrace.unshift( p );
024            if( p.voteTraceProjected ) p.voteTraceProjected.unshift( p );
025        }
026        p.initJSCalled = true;
027    }-*/;
028
029
030
031   // ------------------------------------------------------------------------------------
032
033
034    /** Answers whether or not this person has the specified other person as an immediate
035      * candidate.  One's immediate candidate is the first recipient of one's vote.
036      *
037      *     @throws NoSuchItem if this person's vote trace or the other's username is
038      *       unknown.
039      */
040    public boolean hasImmediateCandidate( final PersonJS other ) throws NoSuchItem
041    {
042        final JsArray<PersonJS> trace = voteTrace();
043        if( trace == null ) throw new NoSuchItem( "unknown vote trace for person: " + username() );
044
045        final String otherUsername = other.username();
046        if( otherUsername == null ) throw new NoSuchItem( "unknown username for other person" );
047
048        if( trace.length() < 2 ) return false; // this person's vote not actually cast
049
050        return trace.get(1).username().equals( otherUsername );
051    }
052
053
054
055   // - P e r s o n ----------------------------------------------------------------------
056
057
058    public native byte dartSector() /*-{ return this.dartSector? this.dartSector: -1; }-*/;
059
060
061
062    public native ResidenceJS residence() /*-{ return this.residence; }-*/;
063
064
065
066    public native String username() /*-{ return this.username; }-*/;
067
068
069
070    public native JsArray<PersonJS> voteTrace()
071    /*-{
072        if( !this.initJSCalled ) @votorola.s.gwt.scene.feed.PersonJS::initJS(Ljava/lang/Object;)( this );
073        return this.voteTrace;
074    }-*/;
075
076
077
078    public native JsArray<PersonJS> voteTraceProjected()
079    /*-{
080        if( !this.initJSCalled ) @votorola.s.gwt.scene.feed.PersonJS::initJS(Ljava/lang/Object;)( this );
081        return this.voteTraceProjected;
082    }-*/;
083
084
085
086}