KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > properties > MediaTypeEnum


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.properties;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.apache.commons.lang.builder.HashCodeBuilder;
19
20
21 /**
22  * Enumeration for media types.
23  * @author Fabrizio Giustina
24  * @version $Revision: 720 $ ($Author: fgiust $)
25  */

26 public final class MediaTypeEnum
27 {
28
29     /**
30      * Array containing all the export types.
31      */

32     private static final List JavaDoc ALL = new ArrayList JavaDoc();
33
34     /**
35      * media type HTML = 0.
36      */

37     public static final MediaTypeEnum HTML = new MediaTypeEnum(0, "html"); //$NON-NLS-1$
38

39     /**
40      * Media type CSV = 1.
41      */

42     public static final MediaTypeEnum CSV = new MediaTypeEnum(1, "csv"); //$NON-NLS-1$
43

44     /**
45      * media type EXCEL = 2.
46      */

47     public static final MediaTypeEnum EXCEL = new MediaTypeEnum(2, "excel"); //$NON-NLS-1$
48

49     /**
50      * media type XML = 3.
51      */

52     public static final MediaTypeEnum XML = new MediaTypeEnum(3, "xml"); //$NON-NLS-1$
53

54     /**
55      * Code; this is the primary key for these objects.
56      */

57     private final int enumCode;
58
59     /**
60      * description.
61      */

62     private final String JavaDoc enumName;
63
64     /**
65      * private constructor. Use only constants.
66      * @param code int code
67      * @param name description of media type
68      */

69     private MediaTypeEnum(int code, String JavaDoc name)
70     {
71         this.enumCode = code;
72         this.enumName = name;
73         ALL.add(this);
74     }
75
76     /**
77      * returns the int code.
78      * @return int code
79      */

80     public int getCode()
81     {
82         return this.enumCode;
83     }
84
85     /**
86      * returns the description.
87      * @return String description of the media type ("excel", "xml", "csv", "html")
88      */

89     public String JavaDoc getName()
90     {
91         return this.enumName;
92     }
93
94     /**
95      * lookup a media type by key.
96      * @param key int code
97      * @return MediaTypeEnum or null if no mediaType is found with the given key
98      */

99     public static MediaTypeEnum fromCode(int key)
100     {
101         // @todo optimization needed
102
for (int i = 0; i < ALL.size(); i++)
103         {
104             if (key == ((MediaTypeEnum) ALL.get(i)).getCode())
105             {
106                 return (MediaTypeEnum) ALL.get(i);
107             }
108         }
109         // lookup failed
110
return null;
111     }
112
113     /**
114      * lookup a media type by an Integer key.
115      * @param key Integer code - null safe: a null key returns a null Enum
116      * @return MediaTypeEnum or null if no mediaType is found with the given key
117      */

118     public static MediaTypeEnum fromCode(Integer JavaDoc key)
119     {
120         if (key == null)
121         {
122             return null;
123         }
124
125         return fromCode(key.intValue());
126     }
127
128     /**
129      * lookup a media type by an Integer key.
130      * @param key Integer code - null safe: a null key returns a null Enum
131      * @return MediaTypeEnum or null if no mediaType is found with the given key
132      * @deprecated use fromCode(Integer)
133      */

134     public static MediaTypeEnum fromIntegerCode(Integer JavaDoc key)
135     {
136         return fromCode(key);
137     }
138
139     /**
140      * Lookup a media type by a String key.
141      * @param code String code - null safe: a null key returns a null Enum
142      * @return MediaTypeEnum or null if no mediaType is found with the given key
143      */

144     public static MediaTypeEnum fromName(String JavaDoc code)
145     {
146         // @todo optimization needed
147
for (int i = 0; i < ALL.size(); i++)
148         {
149             if (((MediaTypeEnum) ALL.get(i)).getName().equals(code))
150             {
151                 return ((MediaTypeEnum) ALL.get(i));
152             }
153         }
154         // lookup failed
155
return null;
156     }
157
158     /**
159      * returns an iterator on all the media type.
160      * @return iterator
161      */

162     public static Iterator JavaDoc iterator()
163     {
164         return ALL.iterator();
165     }
166
167     /**
168      * Register a new MediaType. If <code>name</code> is already assigned the existing instance is returned, otherwise
169      * a new instance is created.
170      * @param name media name
171      * @return assigned MediaTypeEnum instance
172      */

173     public static synchronized MediaTypeEnum registerMediaType(String JavaDoc name)
174     {
175         MediaTypeEnum existing = fromName(name);
176         if (existing == null)
177         {
178             existing = new MediaTypeEnum(ALL.size() + 1, name);
179         }
180         return existing;
181     }
182
183     /**
184      * Returns the number of media type currently loaded.
185      * @return number of media types loaded
186      */

187     public static int getSize()
188     {
189         return ALL.size();
190     }
191
192     /**
193      * returns the media type description.
194      * @see java.lang.Object#toString()
195      */

196     public String JavaDoc toString()
197     {
198         return getName();
199     }
200
201     /**
202      * Only a single instance of a specific MediaTypeEnum can be created, so we can check using ==.
203      * @param o the object to compare to
204      * @return hashCode
205      */

206     public boolean equals(Object JavaDoc o)
207     {
208         if (this == o)
209         {
210             return true;
211         }
212
213         return false;
214     }
215
216     /**
217      * @see java.lang.Object#hashCode()
218      */

219     public int hashCode()
220     {
221         return new HashCodeBuilder(1188997057, -1289297553).append(this.enumCode).toHashCode();
222     }
223
224 }
Popular Tags