KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > EnumProperty


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

17
18 /* $Id: EnumProperty.java 488960 2006-12-20 08:34:28Z spepping $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.fo.FObj;
23 import org.apache.fop.fo.PropertyList;
24 import org.apache.fop.fo.expr.PropertyException;
25
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28
29 /**
30  * Superclass for properties that wrap an enumeration value
31  */

32 public class EnumProperty extends Property {
33
34     /**
35      * Inner class for creating EnumProperty instances
36      */

37     public static class Maker extends PropertyMaker {
38
39         /**
40          * @param propId the id of the property for which a Maker should be created
41          */

42         public Maker(int propId) {
43             super(propId);
44         }
45
46         /**
47          * Called by subclass if no match found.
48          * @param value string containing the value to be checked
49          * @return null (indicates that an appropriate match was not found)
50          */

51         public Property checkEnumValues(String JavaDoc value) {
52             //log.error("Unknown enumerated value for property '"
53
// + getPropName() + "': " + value);
54
return super.checkEnumValues(value);
55         }
56
57         public Property convertProperty(Property p,
58                                         PropertyList propertyList,
59                                         FObj fo) throws PropertyException {
60             if (p instanceof EnumProperty) {
61                 return p;
62             } else {
63                 return super.convertProperty(p, propertyList, fo);
64             }
65         }
66     }
67
68     private static final Map JavaDoc propertyCache = new WeakHashMap JavaDoc();
69
70     private final int value;
71     private final String JavaDoc text;
72
73     /**
74      * @param explicitValue enumerated value to be set for this property
75      * @param text the string value of the enum.
76      */

77     private EnumProperty(int explicitValue, String JavaDoc text) {
78         this.value = explicitValue;
79         this.text = text;
80     }
81
82     public static EnumProperty getInstance(int explicitValue, String JavaDoc text) {
83         EnumProperty ep = new EnumProperty(explicitValue, text);
84         EnumProperty cacheEntry = (EnumProperty)propertyCache.get(ep);
85         if (cacheEntry == null) {
86             propertyCache.put(ep, ep);
87             return ep;
88         } else {
89             return cacheEntry;
90         }
91     }
92
93     /**
94      * @return this.value
95      */

96     public int getEnum() {
97         return this.value;
98     }
99
100     /**
101      * @return this.value cast as an Object
102      */

103     public Object JavaDoc getObject() {
104         return text;
105     }
106
107     public boolean equals(Object JavaDoc obj) {
108         if (obj instanceof EnumProperty) {
109             EnumProperty ep = (EnumProperty)obj;
110             return ep.value == this.value &&
111                 ((ep.text == null && this.text == null)
112                  || ep.text.equals(this.text));
113         } else {
114             return false;
115         }
116     }
117
118     public int hashCode() {
119         return value + text.hashCode();
120     }
121 }
122
123
Popular Tags