KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > DefaultMessageSourceResolvable


1 /*
2  * Copyright 2002-2005 the original author or authors.
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 package org.springframework.context.support;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.springframework.context.MessageSourceResolvable;
22 import org.springframework.util.ObjectUtils;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * Default implementation of the MessageSourceResolvable interface.
27  * Offers an easy way to store all the necessary values needed to
28  * resolve a message via a MessageSource.
29  *
30  * @author Juergen Hoeller
31  * @since 13.02.2004
32  * @see org.springframework.context.MessageSource#getMessage(MessageSourceResolvable, java.util.Locale)
33  */

34 public class DefaultMessageSourceResolvable implements MessageSourceResolvable, Serializable JavaDoc {
35
36     private final String JavaDoc[] codes;
37
38     private final Object JavaDoc[] arguments;
39
40     private final String JavaDoc defaultMessage;
41
42
43     /**
44      * Create a new DefaultMessageSourceResolvable.
45      * @param code the code to be used to resolve this message
46      */

47     public DefaultMessageSourceResolvable(String JavaDoc code) {
48         this(new String JavaDoc[] {code}, null, null);
49     }
50
51     /**
52      * Create a new DefaultMessageSourceResolvable.
53      * @param codes the codes to be used to resolve this message
54      */

55     public DefaultMessageSourceResolvable(String JavaDoc[] codes) {
56         this(codes, null, null);
57     }
58
59     /**
60      * Create a new DefaultMessageSourceResolvable.
61      * @param codes the codes to be used to resolve this message
62      * @param defaultMessage the default message to be used to resolve this message
63      */

64     public DefaultMessageSourceResolvable(String JavaDoc[] codes, String JavaDoc defaultMessage) {
65         this(codes, null, defaultMessage);
66     }
67
68     /**
69      * Create a new DefaultMessageSourceResolvable.
70      * @param codes the codes to be used to resolve this message
71      * @param arguments the array of arguments to be used to resolve this message
72      */

73     public DefaultMessageSourceResolvable(String JavaDoc[] codes, Object JavaDoc[] arguments) {
74         this(codes, arguments, null);
75     }
76
77     /**
78      * Create a new DefaultMessageSourceResolvable.
79      * @param codes the codes to be used to resolve this message
80      * @param arguments the array of arguments to be used to resolve this message
81      * @param defaultMessage the default message to be used to resolve this message
82      */

83     public DefaultMessageSourceResolvable(String JavaDoc[] codes, Object JavaDoc[] arguments, String JavaDoc defaultMessage) {
84         this.codes = codes;
85         this.arguments = arguments;
86         this.defaultMessage = defaultMessage;
87     }
88
89     /**
90      * Copy constructor: Create a new instance from another resolvable.
91      * @param resolvable the resolvable to copy from
92      */

93     public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
94         this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
95     }
96
97
98     public String JavaDoc[] getCodes() {
99         return codes;
100     }
101
102     /**
103      * Return the default code of this resolvable, that is,
104      * the last one in the codes array.
105      */

106     public String JavaDoc getCode() {
107         return (this.codes != null && this.codes.length > 0) ? this.codes[this.codes.length - 1] : null;
108     }
109
110     public Object JavaDoc[] getArguments() {
111         return arguments;
112     }
113
114     public String JavaDoc getDefaultMessage() {
115         return defaultMessage;
116     }
117
118
119     /**
120      * Build a default String representation for this MessageSourceResolvable:
121      * including codes, arguments, and default message.
122      */

123     protected final String JavaDoc resolvableToString() {
124         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
125         buf.append("codes [").append(StringUtils.arrayToDelimitedString(this.codes, ","));
126         buf.append("]; arguments [" + StringUtils.arrayToDelimitedString(this.arguments, ","));
127         buf.append("]; default message [").append(this.defaultMessage).append(']');
128         return buf.toString();
129     }
130
131     /**
132      * Default implementation exposes the attributes of this MessageSourceResolvable.
133      * To be overridden in more specific subclasses, potentially including the
134      * resolvable content through <code>resolvableToString()</code>.
135      * @see #resolvableToString()
136      */

137     public String JavaDoc toString() {
138         return getClass().getName() + ": " + resolvableToString();
139     }
140
141
142     public boolean equals(Object JavaDoc other) {
143         if (this == other) {
144             return true;
145         }
146         if (!(other instanceof MessageSourceResolvable)) {
147             return false;
148         }
149         MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other;
150         return ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes());
151     }
152
153     public int hashCode() {
154         return ObjectUtils.nullSafeHashCode(getCodes());
155     }
156
157 }
158
Popular Tags