package votorola.a.voter; // Copyright 2008-2009, 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 votorola.g.locale.*; /** A recorded atom of user, administrative or system activity, * that would be of general interest to users. */ public abstract class ActivityEvent implements java.io.Serializable { private static final long serialVersionUID = 0L; /** Constructs an ActivityEvent, timestamped with the current clock time. * * @see #timestamp() */ public ActivityEvent() { this( System.currentTimeMillis() ); } /** Constructs an ActivityEvent. * * @see #timestamp() */ public ActivityEvent( long _timestamp ) { timestamp = _timestamp; } // ------------------------------------------------------------------------------------ /** Returns a string description of this event. The description method * for class ActivityEvent simply returns {@linkplain #toString() toString}(). */ public String description( BundleFormatter _bun ) { return toString(); } /** Returns a human-readable description of the approximate time lapsed since the * event occured, per {@linkplain #lapseToString(long,long,BundleFormatter) * lapseToString}(time0,time1,bun). */ public final String lapseToString( final BundleFormatter bun ) { return lapseToString( timestamp, System.currentTimeMillis(), bun ); } /** Returns a human-readable description of the approximate difference between time0 * and time1. For example, "10 minutes", "3 hours", or "1 month". */ public static String lapseToString( final long time0, final long time1, final BundleFormatter bun ) { float lapse = time1 - time0; if( lapse < 0f ) lapse = 0f; final String unit; lapse = lapse / 1000f; /*ms per second*/ if( lapse < 100f ) unit = "seconds"; else { lapse = lapse / 60f; /*s per minute*/ if( lapse < 100f ) unit = "minutes"; else { lapse = lapse / 60f; /*minutes per hour*/ if( lapse < 37f ) unit = "hours"; else { lapse = lapse / 24f; /*hours per day*/ if( lapse < 13f ) unit = "days"; else { lapse = lapse / 7f; /*days per week*/ unit = "weeks"; } } } } return Math.round( lapse ) + " " + bun.l( "a.voter.ActivityEvent.lapse." + unit ); // always > 1, except for 0 or 1 second, so no need to worry much about singular unit } /** The time at which the event occured, in milliseconds since the Epoch. * * @see System#currentTimeMillis() */ public final long timestamp() { return timestamp; } private final long timestamp; }