1 /*
2 * TimeSpanUnit.java
3 *
4 * Created on March 26, 2003, 3:46 PM
5 */
6 /* ====================================================================
7 *
8 * The JavaRanch Software License, Version 1.0
9 *
10 * Copyright (c) 2003 JavaRanch. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
13 * following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided with the distribution.
20 *
21 * 3. The name JavaRanch must not be used to endorse or promote products derived from this software without prior written
22 * permission.
23 *
24 * 4. Products derived from this software may not be called "JavaRanch" nor may "JavaRanch" appear in their names without
25 * prior written permission of JavaRanch.
26 *
27 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JAVARANCH OR ITS
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE. ====================================================================
34 *
35 */
36
37 package com.melloware.jukes.util;
38
39 /** The TimeSpanUnit defines a standard set of units to be used with the TimeSpan
40 * class. The methods of the TimeSpan class require the use of TimeSpanUnit
41 * objects for certain methods. The purpose of this class is to provide a
42 * typesafe enum. (see <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/logging/Level.html">
43 * java.util.logging.Level</a> for an example from the Java API.)
44 * <p>
45 * The TimeSpanUnit objects are:
46 * <ul>
47 * <li>MILLISECONDS</li>
48 * <li>SECONDS</li>
49 * <li>MINUTES</li>
50 * <li>HOURS</li>
51 * <li>DAYS</li>
52 * </UL>
53 *
54 * @author Thomas Paul
55 */
56 public final class TimeSpanUnit {
57
58 /** MILLISECONDS indicates that milliseconds are being used */
59 public static final TimeSpanUnit MILLISECONDS = new TimeSpanUnit("MILLISECONDS", TimeSpanConstants.MILLISECONDS);
60
61 /** SECONDS indicates that seconds are being used */
62 public static final TimeSpanUnit SECONDS = new TimeSpanUnit("SECONDS", TimeSpanConstants.SECONDS);
63
64 /** MINUTES indicates that minutes are being used */
65 public static final TimeSpanUnit MINUTES = new TimeSpanUnit("MINUTES", TimeSpanConstants.MINUTES);
66
67 /** HOURS indicates that hours are being used */
68 public static final TimeSpanUnit HOURS = new TimeSpanUnit("HOURS", TimeSpanConstants.HOURS);
69
70 /** DAYS indicates that days are being used */
71 public static final TimeSpanUnit DAYS = new TimeSpanUnit("DAYS", TimeSpanConstants.DAYS);
72 private final int value;
73
74 private final String name;
75
76 /** No TimeSpanUnit objects should be created outside of this class */
77 private TimeSpanUnit(String name, int value) {
78 this.name = name;
79 this.value = value;
80 }
81
82 /** Gets the name of this TimeSpanUnit.
83 *
84 * @return the name of this TimeSpanUnit.
85 */
86 public int getName() {
87 return value;
88 }
89
90 /** Gets the number of milliseconds this TimeSpanUnit represents.
91 *
92 * @return the number of milliseconds.
93 */
94 public int getValue() {
95 return value;
96 }
97
98 /** Returns a hash code value for the object. This method is
99 * supported for the benefit of hashtables such as those provided by
100 * <code>java.util.Hashtable</code>. The method returns the value
101 * of the TimeSpanUnit.
102 *
103 * @return a hash code value for this object.
104 *
105 * @see java.lang.Object#equals(java.lang.Object)
106 * @see java.util.Hashtable
107 *
108 */
109 public int hashCode() {
110 return value;
111 }
112
113 /** Returns a string representation of the object in the format
114 * "name:value"
115 *
116 * @return a string containing the name and value of this TimeSpanUnit object.
117 *
118 */
119 public String toString() {
120 return name + ":" + value;
121 }
122
123 interface TimeSpanConstants {
124
125 /** Constant for milliseconds unit and conversion */
126 int MILLISECONDS = 1;
127
128 /** Constant for seconds unit and conversion */
129 int SECONDS = MILLISECONDS * 1000;
130
131 /** Constant for minutes unit and conversion */
132 int MINUTES = SECONDS * 60;
133
134 /** Constant for hours unit and conversion */
135 int HOURS = MINUTES * 60;
136
137 /** Constant for days unit and conversion */
138 int DAYS = HOURS * 24;
139 }
140
141 /* (non-Javadoc)
142 * @see java.lang.Object#equals(java.lang.Object)
143 */
144 @Override
145 public boolean equals(Object aObj) { //NOPMD
146 return super.equals(aObj);
147 }
148 }