package votorola.a.voter; // Copyright 2008, 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 java.io.*; import java.util.*; import votorola.g.lang.*; import votorola.g.util.*; /** A rolling list of activity events, beginning with the most recent. New events that * are added to the list will roll over (overwrite) old ones, so the size of the list * will never exceed its fixed capacity. */ public class ActivityList extends AbstractList implements java.io.Serializable { private static final long serialVersionUID = 1L; /** Inserts a new event at the beginning of this list. If the list was already * at capacity size, then the oldest event is removed from the end. * * @param e event to be inserted; or null, to insert no event */ public void log( ActivityEvent e ) { if( e == null ) return; int actualIndexOfLogicalZero = offsetter.actualIndex( -1 ); // rolling backwards offsetter.setActualIndexOfLogicalZero( actualIndexOfLogicalZero ); eventArray[actualIndexOfLogicalZero] = e; if( size < eventArray.length ) ++size; } // - C o l l e c t i o n -------------------------------------------------------------- public final @Override int size() { return size; } private int size; // - L i s t -------------------------------------------------------------------------- public final @Override ActivityEvent get( int index ) { return eventArray[offsetter.actualIndex(index)]; } // ==================================================================================== /** An activity list that allows for thread-safe logging. * Its log(e) method synchronizes on monitor lock TL.this. */ public static final class TL extends ActivityList { public @ThreadSafe synchronized @Override void log( ActivityEvent e ) { super.log( e ); } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** The maximum size (number of events) for a newly constructed activity list. */ private static final int CAPACITY = 100; // Changing? Increment serialVersionUID to prevent deserialization of old lists, having the old size private final ActivityEvent[] eventArray = new ActivityEvent[CAPACITY]; private final IndexOffsetter offsetter = new IndexOffsetter( CAPACITY ); }