001    /*
002     * $Id: BasicMessage.java 366395 2006-01-06 02:42:19Z niallp $
003     * $Revision: 366395 $
004     * $Date: 2006-01-06 02:42:19 +0000 (Fri, 06 Jan 2006) $
005     *
006     * ====================================================================
007     *
008     *  Copyright 2003-2006 The Apache Software Foundation
009     *
010     *  Licensed under the Apache License, Version 2.0 (the "License");
011     *  you may not use this file except in compliance with the License.
012     *  You may obtain a copy of the License at
013     *
014     *      http://www.apache.org/licenses/LICENSE-2.0
015     *
016     *  Unless required by applicable law or agreed to in writing, software
017     *  distributed under the License is distributed on an "AS IS" BASIS,
018     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019     *  See the License for the specific language governing permissions and
020     *  limitations under the License.
021     *
022     */
023    
024    package org.apache.commons.resources.impl;
025    
026    import java.io.Serializable;
027    
028    import org.apache.commons.resources.Message;
029    
030    /**
031     * A basic implementation of the Message interface.
032     * <p>
033     * Orginally based on org.apache.struts.action.ActionMessage, Revision 49176.
034     */
035    public class BasicMessage implements Serializable, Message {
036    
037        /**
038         * The logical name of the {@link org.apache.commons.resources.Resources} 
039         * instance this message is associated with (optional).
040         */
041        private String resourcesName;
042        
043        /**
044         * The message key for this message.
045         */
046        private String key = null;
047    
048        /**
049         * The replacement values for this message.
050         */
051        private Object[] values = null;
052    
053        /**
054         * Default Constructor.
055         */
056        public BasicMessage() {
057            super();
058        }
059        
060        /**
061         * Construct a message with no replacement values.
062         *
063         * @param key Message key for this message
064         */
065        public BasicMessage(String key) {
066    
067            this.key = key;
068            this.values = null;
069    
070        }
071    
072        /**
073         * Construct a message with the specified replacement values.
074         *
075         * @param key Message key for this message
076         * @param value0 First replacement value
077         */
078        public BasicMessage(String key, Object value0) {
079    
080            this.key = key;
081            this.values = new Object[]{value0};
082    
083        }
084    
085        /**
086         * Construct a message with the specified replacement values.
087         *
088         * @param key Message key for this message
089         * @param value0 First replacement value
090         * @param value1 Second replacement value
091         */
092        public BasicMessage(String key, Object value0, Object value1) {
093    
094            this.key = key;
095            this.values = new Object[]{value0, value1};
096    
097        }
098    
099        /**
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    }