package 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. import com.google.gwt.core.client.*; import votorola.g.lang.*; /** A person implemented as a JavaScript overlay type. */ public final class PersonJS extends JavaScriptObject implements Person { protected PersonJS() {} // "precisely one constructor... protected, empty, and no-argument" /** Initializes the native object, and sets p.initJSCalled to true. */ private static native void initJS( Object p ) // "JSNI methods may not refer to instance methods defined within a JavaScriptObject" /*-{ if( p.voteTrace ) // fill in detail excluded from serialization: { p.voteTrace.unshift( p ); if( p.voteTraceProjected ) p.voteTraceProjected.unshift( p ); } p.initJSCalled = true; }-*/; // ------------------------------------------------------------------------------------ /** Answers whether or not this person has the specified other person as an immediate * candidate. One's immediate candidate is the first recipient of one's vote. * * @throws NoSuchItem if this person's vote trace or the other's username is * unknown. */ public boolean hasImmediateCandidate( final PersonJS other ) throws NoSuchItem { final JsArray trace = voteTrace(); if( trace == null ) throw new NoSuchItem( "unknown vote trace for person: " + username() ); final String otherUsername = other.username(); if( otherUsername == null ) throw new NoSuchItem( "unknown username for other person" ); if( trace.length() < 2 ) return false; // this person's vote not actually cast return trace.get(1).username().equals( otherUsername ); } // - P e r s o n ---------------------------------------------------------------------- public native byte dartSector() /*-{ return this.dartSector? this.dartSector: -1; }-*/; public native ResidenceJS residence() /*-{ return this.residence; }-*/; public native String username() /*-{ return this.username; }-*/; public native JsArray voteTrace() /*-{ if( !this.initJSCalled ) @votorola.s.gwt.scene.feed.PersonJS::initJS(Ljava/lang/Object;)( this ); return this.voteTrace; }-*/; public native JsArray voteTraceProjected() /*-{ if( !this.initJSCalled ) @votorola.s.gwt.scene.feed.PersonJS::initJS(Ljava/lang/Object;)( this ); return this.voteTraceProjected; }-*/; }