package votorola.s.gwt.scene.feed.ss; // Copyright 2011-2012, Michael Allan, Christian Weilbach. 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.gson.stream.JsonWriter; import java.io.IOException; import java.util.*; import votorola.g.util.ArrayListX; import static votorola.g.util.ArrayListX.EMPTY_LIST; /** A serial jig for a bite. */ public final class BiteJig implements votorola.s.gwt.scene.feed.Bite, SerialJig { // adding fields? ensure they are cleared in serialize() /** Removes all persons from the specified list and caches them for reuse. * * @see #uncachePerson() */ void encachePersons( ArrayList persons ) { encachePersons( persons, 0 ); } private ArrayList personCache = ArrayListX.emptyList(); // i.e. EMPTY_LIST /** Removes all persons from the specified list and caches the persons from * the start onwards for reuse. * * @see #uncachePerson() */ void encachePersons( final ArrayList persons, final int start ) { for( int p = start, pN = persons.size(); p < pN; ++p ) { personCache.add( persons.get( p )); } persons.clear(); } /** Fetches a person jig from the cache, or creates a new jig if the cache is * empty. * * @see #encachePersons(ArrayList) */ PersonJig uncachePerson() { if( personCache == EMPTY_LIST ) { personCache = new ArrayList( /*initial capacity*/20 ); // lots for PersonJig's vote traces } return personCache.size() > 0? personCache.remove(0): new PersonJig(BiteJig.this); } // - B i t e -------------------------------------------------------------------------- public DiffLookJig difference() { return difference; } private DiffLookJig difference; private void difference( final JsonWriter out ) throws IOException { if( difference == null ) return; out.name( "difference" ); difference.serialize( out ); difference = null; } private DiffLookJig differenceC; /** Ensures the difference is set to a jig and caches the jig for reuse. * * @return the jig that was set. */ public DiffLookJig setDifference() { if( differenceC == null ) differenceC = new DiffLookJig(); difference = differenceC; return differenceC; } public MessageJig message() { return messages.size() == 0? null: messages.get( 0 ); } public List messages() { return messages; } private ArrayList messages = ArrayListX.emptyList(); // i.e. EMPTY_LIST private void messages( final JsonWriter out ) throws IOException { if( messages.size() == 0 ) return; // that is the default in BiteJS out.name( "messages" ); out.beginArray(); for( MessageJig jig: messages ) jig.serialize( out ); out.endArray(); messagesC.addAll( messages ); messages.clear(); } private ArrayList messagesC = messages; /** Adds a message jig, caching it for reuse. * * @return the jig added. */ public MessageJig addMessage() { if( messages == EMPTY_LIST ) { messages = new ArrayList( /*initial capacity*/1 ); messagesC = new ArrayList( /*initial capacity*/1 ); } final MessageJig jig; if( messagesC.size() > 0 ) jig = messagesC.remove( 0 ); else jig = new MessageJig(); messages.add( jig ); return jig; } public char relation() { return relation; } private char relation = ' '; private void relation( final JsonWriter out ) throws IOException { if( relation == ' ' ) return; out.name("relation").value( relation ); relation = ' '; } /** Sets the relation of the first person to the other * in difference related messages. */ public void setRelation( final char c ) { relation = c; } public Date sentDate() { return sentDate; } private Date sentDate = null; private void sentDate( final JsonWriter out ) throws IOException { if( sentDate == null ) return; out.name( "sentDate" ).value( sentDate.getTime() ); sentDate = null; } /** Sets the date on which the bite was parsed. */ public void setSentDate( final Date pd ) { sentDate = pd; } // OPT a single Date instance should be cached for reuse; or better yet it should be a long public PersonJig person() { return persons.size() == 0? null: persons.get( 0 ); } public List persons() { return persons; } private ArrayList persons = ArrayListX.emptyList(); // i.e. EMPTY_LIST private void persons( final JsonWriter out ) throws IOException { if( persons.size() == 0 ) return; // that is the default in BiteJS out.name( "persons" ); out.beginArray(); for( PersonJig jig: persons ) jig.serialize( out ); out.endArray(); encachePersons( persons ); } /** Adds a person jig, caching it for reuse. * * @return the jig added. */ public PersonJig addPerson() { if( persons == EMPTY_LIST ) persons = new ArrayList( /*initial capacity*/2 ); final PersonJig jig = uncachePerson(); persons.add( jig ); return jig; } public PollJig poll() { return poll; } private PollJig poll; private void poll( final JsonWriter out ) throws IOException { if( poll == null ) return; out.name( "poll" ); poll.serialize( out ); poll = null; } private PollJig pollC; /** Ensures the poll is set to a jig and caches the jig for reuse. * * @return the jig that was set. */ public PollJig setPoll() { if( pollC == null ) pollC = new PollJig(); poll = pollC; return pollC; } // - S e r i a l - J i g -------------------------------------------------------------- public void serialize( final JsonWriter out ) throws IOException { out.beginObject(); difference( out ); messages( out ); relation( out ); sentDate( out ); persons( out ); poll( out ); out.endObject(); } }