View Javadoc

1   /*
2    * $Id: BasicMessage.java 366395 2006-01-06 02:42:19Z niallp $
3    * $Revision: 366395 $
4    * $Date: 2006-01-06 02:42:19 +0000 (Fri, 06 Jan 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   *
22   */
23  
24  package org.apache.commons.resources.impl;
25  
26  import java.io.Serializable;
27  
28  import org.apache.commons.resources.Message;
29  
30  /**
31   * A basic implementation of the Message interface.
32   * <p>
33   * Orginally based on org.apache.struts.action.ActionMessage, Revision 49176.
34   */
35  public class BasicMessage implements Serializable, Message {
36  
37      /**
38       * The logical name of the {@link org.apache.commons.resources.Resources} 
39       * instance this message is associated with (optional).
40       */
41      private String resourcesName;
42      
43      /**
44       * The message key for this message.
45       */
46      private String key = null;
47  
48      /**
49       * The replacement values for this message.
50       */
51      private Object[] values = null;
52  
53      /**
54       * Default Constructor.
55       */
56      public BasicMessage() {
57          super();
58      }
59      
60      /**
61       * Construct a message with no replacement values.
62       *
63       * @param key Message key for this message
64       */
65      public BasicMessage(String key) {
66  
67          this.key = key;
68          this.values = null;
69  
70      }
71  
72      /**
73       * Construct a message with the specified replacement values.
74       *
75       * @param key Message key for this message
76       * @param value0 First replacement value
77       */
78      public BasicMessage(String key, Object value0) {
79  
80          this.key = key;
81          this.values = new Object[]{value0};
82  
83      }
84  
85      /**
86       * Construct a message with the specified replacement values.
87       *
88       * @param key Message key for this message
89       * @param value0 First replacement value
90       * @param value1 Second replacement value
91       */
92      public BasicMessage(String key, Object value0, Object value1) {
93  
94          this.key = key;
95          this.values = new Object[]{value0, value1};
96  
97      }
98  
99      /**
100      * Construct a message with the specified replacement values.
101      *
102      * @param key Message key for this message
103      * @param value0 First replacement value
104      * @param value1 Second replacement value
105      * @param value2 Third replacement value
106      */
107     public BasicMessage(String key, Object value0, Object value1,
108                         Object value2) {
109 
110         this.key = key;
111         this.values = new Object[]{value0, value1, value2};
112 
113     }
114 
115     /**
116      * Construct a message with the specified replacement values.
117      *
118      * @param key Message key for this message
119      * @param value0 First replacement value
120      * @param value1 Second replacement value
121      * @param value2 Third replacement value
122      * @param value3 Fourth replacement value
123      */
124     public BasicMessage(String key, Object value0, Object value1,
125                         Object value2, Object value3) {
126 
127         this.key = key;
128         this.values = new Object[]{value0, value1, value2, value3};
129     }
130 
131     /**
132      * Construct a message with the specified replacement values.
133      *
134      * @param key Message key for this message
135      * @param values Array of replacement values
136      */
137     public BasicMessage(String key, Object[] values) {
138         this.key = key;
139         this.values = values;
140     }
141 
142     /**
143      * <p>Return the logical name of the {@link org.apache.commons.resources.Resources} 
144      * instance this message is associated with.</p>
145      *
146      * @return The name of the resources instance.
147      */
148     public String getResourcesName() {
149         return resourcesName;
150     }
151 
152     /**
153      * <p>Set the logical name of the {@link org.apache.commons.resources.Resources} 
154      * instance this message is associated with.</p>
155      *
156      * @param resourcesName The name of the resources instance.
157      */
158     public void setResourcesName(String resourcesName) {
159         this.resourcesName = resourcesName;
160     }
161 
162     /**
163      * @return Get the message key for this message.
164      */
165     public String getKey() {
166         return (this.key);
167     }
168     
169     /**
170      * Set the key for the message.
171      *
172      * @param key The key to set.
173      */
174     public void setKey(String key) {
175         this.key = key;
176     }
177 
178     /**
179      * @return Get the replacement values for this message.
180      */
181     public Object[] getValues() {
182         return (this.values);
183     }
184 
185     /**
186      * @param values The replacement values for this message.
187      */
188     public void setValues(Object[] values) {
189         this.values = values;
190     }
191 
192     /**
193      * Returns a String in the format: resourcesName:key[value0, value1, etc]. 
194      * @see java.lang.Object#toString()
195      */
196     public String toString() {
197         StringBuffer buff = new StringBuffer();
198         if (this.resourcesName != null) {
199             buff.append(this.resourcesName);
200             buff.append(":"); 
201         }
202         buff.append(this.key);
203         buff.append("[");
204 
205         if (this.values != null) {
206 
207             for (int i = 0; i < this.values.length; i++) {
208 
209                 buff.append(this.values[i]);
210 
211                 // don't append comma to last entry
212                 if (i < this.values.length - 1) {
213                     buff.append(", ");
214                 }
215                 
216             }
217         }
218 
219         buff.append("]");
220 
221         return buff.toString();
222     }
223 
224 }