001package 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.
002
003import votorola.g.locale.*;
004
005
006/** A recorded atom of user, administrative or system activity,
007  * that would be of general interest to users.
008  */
009public abstract class ActivityEvent implements java.io.Serializable
010{
011
012    private static final long serialVersionUID = 0L;
013
014
015
016    /** Constructs an ActivityEvent, timestamped with the current clock time.
017      *
018      *     @see #timestamp()
019      */
020    public ActivityEvent() { this( System.currentTimeMillis() ); }
021
022
023
024    /** Constructs an ActivityEvent.
025      *
026      *     @see #timestamp()
027      */
028    public ActivityEvent( long _timestamp ) { timestamp = _timestamp; }
029
030
031
032   // ------------------------------------------------------------------------------------
033
034
035    /** Returns a string description of this event.  The <code>description</code> method
036      * for class ActivityEvent simply returns {@linkplain #toString() toString}().
037      */
038    public String description( BundleFormatter _bun ) { return toString(); }
039
040
041
042    /** Returns a human-readable description of the approximate time lapsed since the
043      * event occured, per {@linkplain #lapseToString(long,long,BundleFormatter)
044      * lapseToString}(time0,time1,bun).
045      */
046    public final String lapseToString( final BundleFormatter bun )
047    {
048        return lapseToString( timestamp, System.currentTimeMillis(), bun );
049    }
050
051
052
053    /** Returns a human-readable description of the approximate difference between time0
054      * and time1.  For example, "10 minutes", "3 hours", or "1 month".
055      */
056    public static String lapseToString( final long time0, final long time1,
057      final BundleFormatter bun )
058    {
059        float lapse = time1 - time0;
060        if( lapse < 0f ) lapse = 0f;
061
062        final String unit;
063        lapse = lapse / 1000f; /*ms per second*/
064        if( lapse < 100f ) unit = "seconds";
065        else
066        {
067            lapse = lapse / 60f; /*s per minute*/
068            if( lapse < 100f ) unit = "minutes";
069            else
070            {
071                lapse = lapse / 60f; /*minutes per hour*/
072                if( lapse < 37f ) unit = "hours";
073                else
074                {
075                    lapse = lapse / 24f; /*hours per day*/
076                    if( lapse < 13f ) unit = "days";
077                    else
078                    {
079                        lapse = lapse / 7f; /*days per week*/
080                        unit = "weeks";
081                    }
082                }
083            }
084        }
085        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
086    }
087
088
089
090    /** The time at which the event occured, in milliseconds since the Epoch.
091      *
092      *     @see System#currentTimeMillis()
093      */
094    public final long timestamp() { return timestamp; }
095
096
097        private final long timestamp;
098
099
100
101}