2
* Copyright 2002,2004 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.struts.faces.util;
20
import java.util.Collection;
21
import java.util.Locale;
25
import org.apache.struts.util.MessageResources;
29
* <p>A limited immutable <code>Map</code> implementation that wraps the
30
* <code>MessageResources</code> instance for the specified
31
* <code>Locale</code>. Exposing the messages as a <code>Map</code>
32
* makes them easily accessible via value binding expressions, as
33
* well as JSP 2.0 expression language expressions.
36
public class MessagesMap implements Map {
39
// ------------------------------------------------------------ Constructors
43
* <p>Construct a new {@link MessagesMap} instance that wraps the
44
* specified <code>MessageResources</code> instance, and returns messages
45
* for the specified <code>Locale</code>.</p>
47
* @param messages <code>MessageResources</code> instance to wrap
48
* @param locale <code>Locale</code> for which to retrieve messages,
49
* or <code>null</code> for the system default <code>Locale</code>
51
* @exception NullPointerException if <code>messages</code>
52
* is <code>null</code>
54
public MessagesMap(MessageResources messages, Locale locale) {
57
if (messages == null) {
58
throw new NullPointerException();
60
this.messages = messages;
66
// ------------------------------------------------------ Instance Variables
70
* <p>The <code>Locale</code> for which to return messages, or
71
* <code>null</code> for the system default <code>Locale</code>.</p>
73
private Locale locale = null;
77
* <p>The <code>MessageResources</code> being wrapped by this
78
* {@link MessagesMap}.</p>
80
private MessageResources messages = null;
83
// ---------------------------------------------------------- Public Methods
87
* <p>The <code>clear()</code> method is not supported.</p>
91
throw new UnsupportedOperationException();
97
* <p>Return <code>true</code> if there is a message for the
100
* @param key Message key to evaluate
102
public boolean containsKey(Object key) {
107
return (messages.isPresent(locale, key.toString()));
114
* <p>The <code>containsValue()</code> method is not supported.</p>
116
* @param value Value to evaluate
118
public boolean containsValue(Object value) {
120
throw new UnsupportedOperationException();
126
* <p>The <code>entrySet()</code> method is not supported.</p>
128
public Set entrySet() {
130
throw new UnsupportedOperationException();
136
* <p>The <code>equals</code> method checks whether equal
137
* <code>MessageResources</code> and <code>Locale</code> are
140
* @param o The object to be compared
142
public boolean equals(Object o) {
144
if (!(o instanceof MessagesMap)) {
147
MessagesMap other = (MessagesMap) o;
148
if (!messages.equals(other.getMessages())) {
151
if (locale == null) {
152
return (other.getLocale() == null);
154
return (locale.equals(other.getLocale()));
161
* <p>Return the message string for the specified key.</p>
163
* @param key Key for message to return
165
public Object get(Object key) {
170
return (messages.getMessage(locale, key.toString()));
177
* <p>The <code>hashCode()</code> method returns values that will
178
* be identical if the <code>equals</code> method returns <code>true</code>.
181
public int hashCode() {
183
int value = messages.hashCode();
184
if (locale != null) {
185
value = value ^ locale.hashCode();
193
* <p>The <code>isEmpty()</code> method returns <code>false</code>, on the
194
* assumption that there is always at least one message available.</p>
196
public boolean isEmpty() {
204
* <p>The <code>keySet()</code> method is not supported.</p>
206
public Set keySet() {
208
throw new UnsupportedOperationException();
214
* <p>The <code>put()</code> method is not supported.</p>
216
* @param key Key to store
217
* @param value Value to store
219
public Object put(Object key, Object value) {
221
throw new UnsupportedOperationException();
227
* <p>The <code>putAll()</code> method is not supported.</p>
229
* @param map Keys and values to store
231
public void putAll(Map map) {
233
throw new UnsupportedOperationException();
239
* <p>The <code>remove()</code> method is not supported.</p>
241
* @param key Key to remove
243
public Object remove(Object key) {
245
throw new UnsupportedOperationException();
251
* <p>The <code>size()</code> method is not supported.</p>
255
throw new UnsupportedOperationException();
261
* <p>The <code>values()</code> method is not supported.</p>
263
public Collection values() {
265
throw new UnsupportedOperationException();
270
// --------------------------------------------------------- Package Methods
274
* <p>Return the <code>Locale</code> we object we are wrapping.</p>
278
return (this.locale);
284
* <p>Return the <code>MessageResources</code> object we are wrapping.</p>
286
MessageResources getMessages() {
288
return (this.messages);