001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: //----------------------------------------------------------------------
018: // This software is provided as is in the hope that it might be found
019: // useful.
020: // You may use and modify it freely provided you keep this copyright
021: // notice unchanged and mark modifications appropriately.
022: //
023: // Bug reports and proposals for improvements are welcome. Please send
024: // them to the eMail address below.
025: //
026: // Christoph Karl Walter Grein
027: // Hauptstr. 42
028: // D-86926 Greifenberg
029: // Germany
030: //
031: // eMail: Christ-Usch.Grein@T-Online.de
032: //
033: // Copyright (c) 1998 Christoph Karl Walter Grein
034: //----------------------------------------------------------------------
035: //====================================================================
036: // Author Christoph Grein <Christ-Usch.Grein@T-Online.de>
037: // Version 1.1
038: // Date 7 February 1998
039: //====================================================================
040: // A factory for the creation of enumeration types (missing in Java).
041: // The same operations are provided as for Ada, only representation
042: // specification is missing (which does not seem to make much sense
043: // in Java).
044: //
045: // The idea behind this implementation model is the following:
046: //
047: // Each enumeration object [or literal as called in Ada] is internally
048: // represented by its position number [like in Ada] starting with 0
049: // for the first one. For external representation, an image string
050: // may also be defined.
051: // All operations on enumerations (order relations between objects,
052: // iterators [in the Ada sense for getting the successor and predeces-
053: // sor of an object]) are implemented on the internal position number.
054: //
055: // Implementing the Ada 'Pos attribute as a function getPos () is
056: // straight forward as is the implementation of 'Image as a function
057: // toString () [*], whereas their reverse operations 'Val and 'Value
058: // present a bit of a problem.
059: // In order to be able to access all objects created for the given
060: // class, we define a vector and let the constructors add each object
061: // upon creation to this vector. Thus each object's position number
062: // is also its vector index. So getVal [Ada's 'Val] simply returns
063: // the object stored at the given place; getObject [Ada's 'Value]
064: // loops thru the vector until it finds the object with the given
065: // string.
066: //
067: // [*] The name toString has deliberately been chosen because of
068: // Java's convention of calling an operation with this name whenever
069: // an object's name appears in string context.
070: //
071: // There is one last point to take care of. An enumeration class has
072: // only a fixed number of objects, so we must inhibit the creation of
073: // new objects (with the "new" operator) outside of the enumeration
074: // class. However, in order to make this EnumerationFactory class
075: // work, the constructors need to be public. Therefore we set up the
076: // requirement that upon derivation from class EnumerationFactory all
077: // objects be created in the derivation class (using all capital
078: // letters according to Java's convention) and the constructors be
079: // made private.
080: //
081: // For this class at work, see the example classes EnumerationExample
082: // and Test_EnumerationExample, which present a few objects and opera-
083: // tions on them.
084: //
085: // Comments and improvements are welcome, see e-mail address above.
086: //====================================================================
087: // History:
088: // Author Version Date Reason for Change
089: // C.G. 1.0 03.02.1998
090: // C.G. 1.1 07.02.1998 getObject returns null if nothing found
091: //====================================================================
092: package org.apache.cocoon.util;
093:
094: import java.util.Vector;
095:
096: /**
097: * A factory for the creation of enumeration types (missing in Java).
098: * The same operations are provided as for Ada, only representation
099: * specification is missing (which does not seem to make much sense
100: * in Java).
101: * Enumeration classes are to be derived from this class thereby
102: * making the constructors private to inhibit creation outside of
103: * the derived class.
104: *
105: * @author Christoph Grein
106: * @version CVS $Id: EnumerationFactory.java 433543 2006-08-22 06:22:54Z crossley $
107: */
108: public class EnumerationFactory {
109:
110: private static Vector allObjects = // must be here JDK 1.1.3
111: new Vector(0, 1); // empty, increment by 1
112:
113: private int pos;
114: private String image;
115:
116: /**
117: * Constructors with and without a string representation (image).
118: * Make constructors private upon derivation.
119: * Be careful: No check is made that the image string is unique!
120: * @param image
121: */
122: public EnumerationFactory(String image) {
123: this .pos = allObjects.size();
124: this .image = image;
125: allObjects.addElement(this );
126: }
127:
128: public EnumerationFactory() {
129: this ("");
130: }
131:
132: //--------------------------------------------------------------------------
133: // Order relations:
134:
135: /**
136: * Order relations Object.op (OtherObject) representing the relation
137: * Object op OtherObject.
138: * @param e the right operand
139: */
140: public boolean lt(EnumerationFactory e) { // "<"
141: return this .getPos() < e.getPos();
142: }
143:
144: public boolean le(EnumerationFactory e) { // "<="
145: return this .getPos() <= e.getPos();
146: }
147:
148: public boolean gt(EnumerationFactory e) { // ">"
149: return this .getPos() > e.getPos();
150: }
151:
152: public boolean ge(EnumerationFactory e) { // ">="
153: return this .getPos() >= e.getPos();
154: }
155:
156: // "==" and "equals" are inherited.
157:
158: //--------------------------------------------------------------------------
159: // Numeric representation:
160:
161: public int getPos() { // Ada'Pos
162: return pos;
163: }
164:
165: /**
166: * Access to the numeric representation.
167: * @param value the numeric value
168: */
169: public static EnumerationFactory getVal(int value) { // Ada'Val
170: return (EnumerationFactory) allObjects.elementAt(value);
171: }
172:
173: //--------------------------------------------------------------------------
174: // Iterator:
175:
176: public static EnumerationFactory getFirst() { // Ada'First
177: return getVal(0);
178: }
179:
180: public static EnumerationFactory getLast() { // Ada'Last
181: return getVal(allObjects.size() - 1);
182: }
183:
184: public EnumerationFactory getNext() { // Ada'Succ
185: return getVal(this .getPos() + 1);
186: }
187:
188: public EnumerationFactory getPrev() { // Ada'Pred
189: return getVal(this .getPos() - 1);
190: }
191:
192: //--------------------------------------------------------------------------
193: // String representation:
194:
195: public String toString() { // Ada'Image
196: return image;
197: }
198:
199: public static EnumerationFactory getObject(String image) { // Ada'Value
200: EnumerationFactory found;
201: // Linear search seems good enough because there presumably
202: // will not be too many literals.
203: for (int i = 0; i < allObjects.size(); i++) {
204: found = (EnumerationFactory) allObjects.elementAt(i);
205: if (found.toString().equals(image)) {
206: return found;
207: }
208: }
209: return null;
210: }
211: }
|