001 package org.rakeshv;
002
003 import java.util.Date;
004
005 /**
006 * A simple value object used to store statistics about usage of
007 * resources.
008 *
009 * <p>The following shows the easiest way to use this class.</p>
010 * <pre>
011 * import org.rakeshv.Statistics;
012 *
013 * ...
014 *
015 * // Instance member
016 * private Statistics statistics = new Statistics();
017 *
018 * ...
019 *
020 * // Within method
021 * statistics.update();
022 *
023 * ...
024 * </pre>
025 *
026 * <p>Copyright 2005 Rakesh Vidyadharan</p>
027 * @author Rakesh Vidyadharan 2005-11-20
028 * @version $Id: ConnectionFactory.java,v 1.2 2005/10/29 22:27:38 rakesh Exp $
029 */
030 public class Statistics extends Object
031 {
032 /**
033 * The total number of times the resource has been accessed.
034 *
035 * <p><b>Note:</b> No synchronisation is done while modifying the
036 * value of this field.</p>
037 */
038 private volatile int accessCount;
039
040 /**
041 * The time in milliseconds that represents the last time at which
042 * the resource was accessed.
043 *
044 * <p><b>Note:</b> No synchronisation is done while modifying the
045 * value of this field.</p>
046 */
047 private volatile long lastAccess;
048
049 /**
050 * Default constructor. Initialises the instance members with
051 * default values.
052 */
053 public Statistics()
054 {
055 this( 0, System.currentTimeMillis() );
056 }
057
058 /**
059 * Create a new instance of the class using the specified value for
060 * {@link #lastAccess}.
061 *
062 * @param lastAccess The millisecond value to use to initialise the
063 * {@link #lastAccess}.
064 */
065 public Statistics( long lastAccess )
066 {
067 this( 0, lastAccess );
068 }
069
070 /**
071 * Designated constructor. Create a new instance of the class using
072 * the specified values.
073 *
074 * @param accessCount The number of times the resource has been
075 * accessed.
076 * @param lastAccess The millisecond value to use to initialise the
077 * {@link #lastAccess}.
078 */
079 public Statistics( int accessCount, long lastAccess )
080 {
081 setAccessCount( accessCount );
082 setLastAccess( lastAccess );
083 }
084
085 /**
086 * Returns {@link #lastAccess} as a <code>Date</code> object.
087 *
088 * @return Date A date value that represents the {@link #lastAccess}
089 * time.
090 */
091 public final Date getLastAccess()
092 {
093 return new Date( lastAccess );
094 }
095
096 /**
097 * Returns {@link #accessCount}.
098 *
099 * @return int The value/reference of/to accessCount.
100 */
101 public final int getAccessCount()
102 {
103 return accessCount;
104 }
105
106 /**
107 * Increment the value of {@link #accessCount}
108 */
109 public final void incrementAccessCount()
110 {
111 ++accessCount;
112 }
113
114 /**
115 * Reset the {@link #accessCount} to <code>0</code>.
116 */
117 public final void resetAccessCount()
118 {
119 setAccessCount( 0 );
120 }
121
122 /**
123 * Set {@link #accessCount}.
124 *
125 * @param accessCount The value to set.
126 */
127 protected final void setAccessCount( int accessCount )
128 {
129 this.accessCount = accessCount;
130 }
131
132 /**
133 * Set the value of {@link #lastAccess} using the specified
134 * millisecond value.
135 *
136 * @param lastAccess The value in milliseconds to set.
137 */
138 public final void setLastAccess( long lastAccess )
139 {
140 this.lastAccess = lastAccess;
141 }
142
143 /**
144 * Set the value of {@link #lastAccess} using the specified date.
145 *
146 * @param date The date to use to set the value.
147 */
148 public final void setLastAccess( Date date )
149 {
150 lastAccess = date.getTime();
151 }
152
153 /**
154 * Update the {@link #lastAccess} to the current time.
155 */
156 public final void updateLastAccess()
157 {
158 setLastAccess( System.currentTimeMillis() );
159 }
160
161 /**
162 * Update the instance members to indicate that a resource
163 * has been accessed.
164 *
165 * @see #incrementAccessCount
166 * @see #updateLastAccess
167 */
168 public final void update()
169 {
170 incrementAccessCount();
171 updateLastAccess();
172 }
173 }