001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.support;
018:
019: import java.io.Serializable;
020:
021: import org.springframework.context.MessageSourceResolvable;
022: import org.springframework.util.ObjectUtils;
023: import org.springframework.util.StringUtils;
024:
025: /**
026: * Default implementation of the MessageSourceResolvable interface.
027: * Offers an easy way to store all the necessary values needed to
028: * resolve a message via a MessageSource.
029: *
030: * @author Juergen Hoeller
031: * @since 13.02.2004
032: * @see org.springframework.context.MessageSource#getMessage(MessageSourceResolvable, java.util.Locale)
033: */
034: public class DefaultMessageSourceResolvable implements
035: MessageSourceResolvable, Serializable {
036:
037: private final String[] codes;
038:
039: private final Object[] arguments;
040:
041: private final String defaultMessage;
042:
043: /**
044: * Create a new DefaultMessageSourceResolvable.
045: * @param code the code to be used to resolve this message
046: */
047: public DefaultMessageSourceResolvable(String code) {
048: this (new String[] { code }, null, null);
049: }
050:
051: /**
052: * Create a new DefaultMessageSourceResolvable.
053: * @param codes the codes to be used to resolve this message
054: */
055: public DefaultMessageSourceResolvable(String[] codes) {
056: this (codes, null, null);
057: }
058:
059: /**
060: * Create a new DefaultMessageSourceResolvable.
061: * @param codes the codes to be used to resolve this message
062: * @param defaultMessage the default message to be used to resolve this message
063: */
064: public DefaultMessageSourceResolvable(String[] codes,
065: String defaultMessage) {
066: this (codes, null, defaultMessage);
067: }
068:
069: /**
070: * Create a new DefaultMessageSourceResolvable.
071: * @param codes the codes to be used to resolve this message
072: * @param arguments the array of arguments to be used to resolve this message
073: */
074: public DefaultMessageSourceResolvable(String[] codes,
075: Object[] arguments) {
076: this (codes, arguments, null);
077: }
078:
079: /**
080: * Create a new DefaultMessageSourceResolvable.
081: * @param codes the codes to be used to resolve this message
082: * @param arguments the array of arguments to be used to resolve this message
083: * @param defaultMessage the default message to be used to resolve this message
084: */
085: public DefaultMessageSourceResolvable(String[] codes,
086: Object[] arguments, String defaultMessage) {
087: this .codes = codes;
088: this .arguments = arguments;
089: this .defaultMessage = defaultMessage;
090: }
091:
092: /**
093: * Copy constructor: Create a new instance from another resolvable.
094: * @param resolvable the resolvable to copy from
095: */
096: public DefaultMessageSourceResolvable(
097: MessageSourceResolvable resolvable) {
098: this (resolvable.getCodes(), resolvable.getArguments(),
099: resolvable.getDefaultMessage());
100: }
101:
102: public String[] getCodes() {
103: return codes;
104: }
105:
106: /**
107: * Return the default code of this resolvable, that is,
108: * the last one in the codes array.
109: */
110: public String getCode() {
111: return (this .codes != null && this .codes.length > 0) ? this .codes[this .codes.length - 1]
112: : null;
113: }
114:
115: public Object[] getArguments() {
116: return arguments;
117: }
118:
119: public String getDefaultMessage() {
120: return defaultMessage;
121: }
122:
123: /**
124: * Build a default String representation for this MessageSourceResolvable:
125: * including codes, arguments, and default message.
126: */
127: protected final String resolvableToString() {
128: StringBuffer buf = new StringBuffer();
129: buf.append("codes [").append(
130: StringUtils.arrayToDelimitedString(this .codes, ","));
131: buf.append("]; arguments ["
132: + StringUtils.arrayToDelimitedString(this .arguments,
133: ","));
134: buf.append("]; default message [").append(this .defaultMessage)
135: .append(']');
136: return buf.toString();
137: }
138:
139: /**
140: * Default implementation exposes the attributes of this MessageSourceResolvable.
141: * To be overridden in more specific subclasses, potentially including the
142: * resolvable content through <code>resolvableToString()</code>.
143: * @see #resolvableToString()
144: */
145: public String toString() {
146: return getClass().getName() + ": " + resolvableToString();
147: }
148:
149: public boolean equals(Object other) {
150: if (this == other) {
151: return true;
152: }
153: if (!(other instanceof MessageSourceResolvable)) {
154: return false;
155: }
156: MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other;
157: return ObjectUtils.nullSafeEquals(getCodes(), otherResolvable
158: .getCodes());
159: }
160:
161: public int hashCode() {
162: return ObjectUtils.nullSafeHashCode(getCodes());
163: }
164:
165: }
|