KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > EnumerationFactory


1 /*
2 * Copyright 1999-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 //----------------------------------------------------------------------
17
// This software is provided as is in the hope that it might be found
18
// useful.
19
// You may use and modify it freely provided you keep this copyright
20
// notice unchanged and mark modifications appropriately.
21
//
22
// Bug reports and proposals for improvements are welcome. Please send
23
// them to the eMail address below.
24
//
25
// Christoph Karl Walter Grein
26
// Hauptstr. 42
27
// D-86926 Greifenberg
28
// Germany
29
//
30
// eMail: Christ-Usch.Grein@T-Online.de
31
//
32
// Copyright (c) 1998 Christoph Karl Walter Grein
33
//----------------------------------------------------------------------
34

35 //====================================================================
36
// Author Christoph Grein <Christ-Usch.Grein@T-Online.de>
37
// Version 1.1
38
// Date 7 February 1998
39
//====================================================================
40
// A factory for the creation of enumeration types (missing in Java).
41
// The same operations are provided as for Ada, only representation
42
// specification is missing (which does not seem to make much sense
43
// in Java).
44
//
45
// The idea behind this implementation model is the following:
46
//
47
// Each enumeration object [or literal as called in Ada] is internally
48
// represented by its position number [like in Ada] starting with 0
49
// for the first one. For external representation, an image string
50
// may also be defined.
51
// All operations on enumerations (order relations between objects,
52
// iterators [in the Ada sense for getting the successor and predeces-
53
// sor of an object]) are implemented on the internal position number.
54
//
55
// Implementing the Ada 'Pos attribute as a function getPos () is
56
// straight forward as is the implementation of 'Image as a function
57
// toString () [*], whereas their reverse operations 'Val and 'Value
58
// present a bit of a problem.
59
// In order to be able to access all objects created for the given
60
// class, we define a vector and let the constructors add each object
61
// upon creation to this vector. Thus each object's position number
62
// is also its vector index. So getVal [Ada's 'Val] simply returns
63
// the object stored at the given place; getObject [Ada's 'Value]
64
// loops thru the vector until it finds the object with the given
65
// string.
66
//
67
// [*] The name toString has deliberately been chosen because of
68
// Java's convention of calling an operation with this name whenever
69
// an object's name appears in string context.
70
//
71
// There is one last point to take care of. An enumeration class has
72
// only a fixed number of objects, so we must inhibit the creation of
73
// new objects (with the "new" operator) outside of the enumeration
74
// class. However, in order to make this EnumerationFactory class
75
// work, the constructors need to be public. Therefore we set up the
76
// requirement that upon derivation from class EnumerationFactory all
77
// objects be created in the derivation class (using all capital
78
// letters according to Java's convention) and the constructors be
79
// made private.
80
//
81
// For this class at work, see the example classes EnumerationExample
82
// and Test_EnumerationExample, which present a few objects and opera-
83
// tions on them.
84
//
85
// Comments and improvements are welcome, see e-mail address above.
86
//====================================================================
87
// History:
88
// Author Version Date Reason for Change
89
// C.G. 1.0 03.02.1998
90
// C.G. 1.1 07.02.1998 getObject returns null if nothing found
91
//====================================================================
92

93 package org.apache.cocoon.util;
94
95 import java.util.Vector JavaDoc;
96
97 /**
98  * A factory for the creation of enumeration types (missing in Java).
99  * The same operations are provided as for Ada, only representation
100  * specification is missing (which does not seem to make much sense
101  * in Java).
102  * Enumeration classes are to be derived from this class thereby
103  * making the constructors private to inhibit creation outside of
104  * the derived class.
105  *
106  * @author Christoph Grein
107  * @version CVS $Id: EnumerationFactory.java 156964 2005-03-10 17:21:25Z cziegeler $
108  */

109 public class EnumerationFactory {
110
111     private static Vector JavaDoc allObjects = // must be here JDK 1.1.3
112
new Vector JavaDoc (0, 1); // empty, increment by 1
113

114     private int pos;
115     private String JavaDoc image;
116
117     /**
118      * Constructors with and without a string representation (image).
119      * Make constructors private upon derivation.
120      * Be careful: No check is made that the image string is unique!
121      * @param image
122      */

123     public EnumerationFactory(String JavaDoc image) {
124         this.pos = allObjects.size();
125         this.image = image;
126         allObjects.addElement(this);
127     }
128
129     public EnumerationFactory() {
130         this ("");
131     }
132
133     //--------------------------------------------------------------------------
134
// Order relations:
135

136     /**
137      * Order relations Object.op (OtherObject) representing the relation
138      * Object op OtherObject.
139      * @param e the right operand
140      */

141     public boolean lt(EnumerationFactory e) { // "<"
142
return this.getPos() < e.getPos();
143     }
144
145     public boolean le(EnumerationFactory e) { // "<="
146
return this.getPos() <= e.getPos();
147     }
148
149     public boolean gt(EnumerationFactory e) { // ">"
150
return this.getPos() > e.getPos();
151     }
152
153     public boolean ge(EnumerationFactory e) { // ">="
154
return this.getPos() >= e.getPos ();
155     }
156
157     // "==" and "equals" are inherited.
158

159     //--------------------------------------------------------------------------
160
// Numeric representation:
161

162     public int getPos() { // Ada'Pos
163
return pos;
164     }
165
166     /**
167      * Access to the numeric representation.
168      * @param value the numeric value
169      */

170     public static EnumerationFactory getVal(int value) { // Ada'Val
171
return (EnumerationFactory)allObjects.elementAt(value);
172     }
173
174     //--------------------------------------------------------------------------
175
// Iterator:
176

177     public static EnumerationFactory getFirst() { // Ada'First
178
return getVal(0);
179     }
180
181     public static EnumerationFactory getLast() { // Ada'Last
182
return getVal(allObjects.size() - 1);
183     }
184
185     public EnumerationFactory getNext () { // Ada'Succ
186
return getVal(this.getPos() + 1);
187     }
188
189     public EnumerationFactory getPrev () { // Ada'Pred
190
return getVal(this.getPos() - 1);
191     }
192
193     //--------------------------------------------------------------------------
194
// String representation:
195

196     public String JavaDoc toString() { // Ada'Image
197
return image;
198     }
199
200     public static EnumerationFactory getObject(String JavaDoc image) { // Ada'Value
201
EnumerationFactory found;
202         // Linear search seems good enough because there presumably
203
// will not be too many literals.
204
for (int i = 0 ; i < allObjects.size() ; i++) {
205             found = (EnumerationFactory) allObjects.elementAt(i);
206             if (found.toString().equals(image)) {
207                 return found;
208             }
209         }
210         return null;
211     }
212 }
213
Popular Tags