1
|
 |
package com.instantiations.example.pattern;
|
2
|
|
|
3
|
|
import java.util.HashMap;
|
4
|
|
|
5
|
 |
/**
|
6
|
|
* Instances of the class <code>Priority</code> represent the priority of an
|
7
|
|
* issue.
|
8
|
|
* @author Albert Adams
|
9
|
|
*/
|
10
|
 |
public final class Priority
|
11
|
|
implements Comparable
|
12
|
|
{
|
13
|
|
/**
|
14
|
|
* The name of the priority.
|
15
|
|
*/
|
16
|
|
private final String printName;
|
17
|
|
|
18
|
|
/**
|
19
|
|
* The ordinal value of the priority, used for comparison purposes.
|
20
|
|
*/
|
21
|
|
private final int ordinal;
|
22
|
|
|
23
|
|
/**
|
24
|
|
* A table mapping the names of priorities to those priorities.
|
25
|
|
*/
|
26
|
     |
private static HashMap nameLookup = new HashMap(3);
|
27
|
|
|
28
|
|
/**
|
29
|
|
* The array of priorities.
|
30
|
|
*/
|
31
|
   |
private static Priority[] ordinalLookup = new Priority[3];
|
32
|
|
|
33
|
|
/**
|
34
|
|
* The object representing a low priority.
|
35
|
|
*/
|
36
|
 |
public static final Priority LOW = new Priority("low", 0);
|
37
|
|
|
38
|
|
/**
|
39
|
|
* The object representing a medium priority.
|
40
|
|
*/
|
41
|
 |
public static final Priority MEDIUM = new Priority("medium", 1);
|
42
|
|
|
43
|
|
/**
|
44
|
|
* The object representing a high priority.
|
45
|
|
*/
|
46
|
 |
public static final Priority HIGH = new Priority("high", 2);
|
47
|
|
|
48
|
|
/**
|
49
|
|
* Initialize a newly created priority. Prevent instances of this class from
|
50
|
|
* being created externally.
|
51
|
|
*
|
52
|
|
* @param name the name of the priority
|
53
|
|
* @param ordinal the ordinal value of the priority
|
54
|
|
*/
|
55
|
|
private Priority(String name, int ordinal)
|
56
|
|
{
|
57
|
 |
this.printName = name;
|
58
|
|
this.ordinal = ordinal;
|
59
|
|
nameLookup.put(name, this);
|
60
|
|
ordinalLookup[ordinal] = this;
|
61
|
|
}
|
62
|
|
|
63
|
|
/**
|
64
|
|
* Return the lowest priority.
|
65
|
|
*
|
66
|
|
* @return the lowest priority
|
67
|
|
*/
|
68
|
|
public static Priority first()
|
69
|
|
{
|
70
|
|
return ordinalLookup[0];
|
71
|
|
}
|
72
|
|
|
73
|
|
/**
|
74
|
|
* Return the highest priority
|
75
|
|
*
|
76
|
|
* @return the highest priority
|
77
|
|
*/
|
78
|
|
public static Priority last()
|
79
|
|
{
|
80
|
|
return ordinalLookup[ordinalLookup.length - 1];
|
81
|
|
}
|
82
|
|
|
83
|
|
/**
|
84
|
|
* Return the priority with the given name, or <code>null</code> if there is
|
85
|
|
* no priority with the given name.
|
86
|
|
*
|
87
|
|
* @param name the name of the priority to be returned
|
88
|
|
*
|
89
|
|
* @return the named priority
|
90
|
|
*/
|
91
|
|
public static Priority valueOf(String name)
|
92
|
|
{
|
93
|
|
return (Priority) nameLookup.get(name);
|
94
|
|
}
|
95
|
|
|
96
|
|
/**
|
97
|
|
* Return the priority preceding this priority, or <code>null</code> if this
|
98
|
|
* is the lowest priority.
|
99
|
|
*
|
100
|
|
* @return the priority preceding this priority
|
101
|
|
*/
|
102
|
|
public Priority predecessor()
|
103
|
|
{
|
104
|
  |
return (this == first()) ? null : ordinalLookup[ordinal - 1];
|
105
|
|
}
|
106
|
|
|
107
|
|
/**
|
108
|
|
* Return the priority following this priority, or <code>null</code> if this
|
109
|
|
* is the highest priority.
|
110
|
|
*
|
111
|
|
* @return the priority following this priority
|
112
|
|
*/
|
113
|
|
public Priority successor()
|
114
|
|
{
|
115
|
  |
return (this == last()) ? null : ordinalLookup[ordinal + 1];
|
116
|
|
}
|
117
|
|
|
118
|
|
/**
|
119
|
|
* Compare this priority to the given priority.
|
120
|
|
*
|
121
|
|
* @param priority the priority to compare this to
|
122
|
|
*
|
123
|
|
* @return the difference between the ordinal values
|
124
|
|
*
|
125
|
|
* @see java.lang.Comparable#compareTo(Object)
|
126
|
|
*/
|
127
|
|
public int compareTo(Object priority)
|
128
|
|
{
|
129
|
 |
return this.ordinal - ((Priority) priority).ordinal;
|
130
|
|
}
|
131
|
|
|
132
|
|
/**
|
133
|
|
* Return the string representation of this priority.
|
134
|
|
*
|
135
|
|
* @return the name of the priority
|
136
|
|
*/
|
137
|
|
public String toString()
|
138
|
|
{
|
139
|
|
return printName;
|
140
|
|
}
|
141
|
|
}
|