001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.resources.impl;
019
020 import java.io.Serializable;
021
022 import org.apache.commons.resources.Message;
023
024 /**
025 * A basic implementation of the Message interface.
026 * <p>
027 * Orginally based on org.apache.struts.action.ActionMessage, Revision 49176.
028 */
029 public class BasicMessage implements Serializable, Message {
030
031 /**
032 * The logical name of the {@link org.apache.commons.resources.Resources}
033 * instance this message is associated with (optional).
034 */
035 private String resourcesName;
036
037 /**
038 * The message key for this message.
039 */
040 private String key = null;
041
042 /**
043 * The replacement values for this message.
044 */
045 private Object[] values = null;
046
047 /**
048 * Default Constructor.
049 */
050 public BasicMessage() {
051 super();
052 }
053
054 /**
055 * Construct a message with no replacement values.
056 *
057 * @param key Message key for this message
058 */
059 public BasicMessage(String key) {
060
061 this.key = key;
062 this.values = null;
063
064 }
065
066 /**
067 * Construct a message with the specified replacement values.
068 *
069 * @param key Message key for this message
070 * @param value0 First replacement value
071 */
072 public BasicMessage(String key, Object value0) {
073
074 this.key = key;
075 this.values = new Object[]{value0};
076
077 }
078
079 /**
080 * Construct a message with the specified replacement values.
081 *
082 * @param key Message key for this message
083 * @param value0 First replacement value
084 * @param value1 Second replacement value
085 */
086 public BasicMessage(String key, Object value0, Object value1) {
087
088 this.key = key;
089 this.values = new Object[]{value0, value1};
090
091 }
092
093 /**
094 * Construct a message with the specified replacement values.
095 *
096 * @param key Message key for this message
097 * @param value0 First replacement value
098 * @param value1 Second replacement value
099 * @param value2 Third replacement value
100 */
101 public BasicMessage(String key, Object value0, Object value1,
102 Object value2) {
103
104 this.key = key;
105 this.values = new Object[]{value0, value1, value2};
106
107 }
108
109 /**
110 * Construct a message with the specified replacement values.
111 *
112 * @param key Message key for this message
113 * @param value0 First replacement value
114 * @param value1 Second replacement value
115 * @param value2 Third replacement value
116 * @param value3 Fourth replacement value
117 */
118 public BasicMessage(String key, Object value0, Object value1,
119 Object value2, Object value3) {
120
121 this.key = key;
122 this.values = new Object[]{value0, value1, value2, value3};
123 }
124
125 /**
126 * Construct a message with the specified replacement values.
127 *
128 * @param key Message key for this message
129 * @param values Array of replacement values
130 */
131 public BasicMessage(String key, Object[] values) {
132 this.key = key;
133 this.values = values;
134 }
135
136 /**
137 * <p>Return the logical name of the {@link org.apache.commons.resources.Resources}
138 * instance this message is associated with.</p>
139 *
140 * @return The name of the resources instance.
141 */
142 public String getResourcesName() {
143 return resourcesName;
144 }
145
146 /**
147 * <p>Set the logical name of the {@link org.apache.commons.resources.Resources}
148 * instance this message is associated with.</p>
149 *
150 * @param resourcesName The name of the resources instance.
151 */
152 public void setResourcesName(String resourcesName) {
153 this.resourcesName = resourcesName;
154 }
155
156 /**
157 * @return Get the message key for this message.
158 */
159 public String getKey() {
160 return (this.key);
161 }
162
163 /**
164 * Set the key for the message.
165 *
166 * @param key The key to set.
167 */
168 public void setKey(String key) {
169 this.key = key;
170 }
171
172 /**
173 * @return Get the replacement values for this message.
174 */
175 public Object[] getValues() {
176 return (this.values);
177 }
178
179 /**
180 * @param values The replacement values for this message.
181 */
182 public void setValues(Object[] values) {
183 this.values = values;
184 }
185
186 /**
187 * Returns a String in the format: resourcesName:key[value0, value1, etc].
188 * @see java.lang.Object#toString()
189 */
190 public String toString() {
191 StringBuffer buff = new StringBuffer();
192 if (this.resourcesName != null) {
193 buff.append(this.resourcesName);
194 buff.append(":");
195 }
196 buff.append(this.key);
197 buff.append("[");
198
199 if (this.values != null) {
200
201 for (int i = 0; i < this.values.length; i++) {
202
203 buff.append(this.values[i]);
204
205 // don't append comma to last entry
206 if (i < this.values.length - 1) {
207 buff.append(", ");
208 }
209
210 }
211 }
212
213 buff.append("]");
214
215 return buff.toString();
216 }
217
218 }