View Javadoc

1   /*
2    * $Id: DatabaseBasicMessage.java 348369 2005-11-23 04:51:04Z niallp $
3    * $Revision: 348369 $
4    * $Date: 2005-11-23 04:51:04 +0000 (Wed, 23 Nov 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2005 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  
27  
28  
29  
30  /**
31   * BasicMessage to allow standard ORM 
32   * configuration (no complex keys).
33   * 
34   * @author James Mitchell
35   *
36   */
37  public class DatabaseBasicMessage extends BasicMessage {
38  
39      /**
40       * Default Constructor.
41       */
42      public DatabaseBasicMessage() {
43          super();
44      }
45  
46      /**
47       * Construct a message for a specified Locale
48       * with the specified replacement values.
49       *
50       * @param locale Locale for this message
51       * @param key Message key for this message
52       * @param values Array of replacement values
53       */
54      public DatabaseBasicMessage(String locale, String key, Object[] values) {
55          super(key, values);
56          this.locale = locale;
57          
58      }
59      
60      private String locale = null;
61  
62      /**
63       * Return the locale for the message.
64       *
65       * @return Returns the locale.
66       */
67      public String getLocale() {
68          return locale;
69      }
70  
71      /**
72       * Set the locale for the message.
73       *
74       * @param locale The locale to set.
75       */
76      public void setLocale(String locale) {
77          this.locale = locale;
78      }
79  
80      /**
81       * Set a replacement value for the message.
82       *
83       * @param value The replacement value.
84       */
85      public void setValue(String value) {
86          setValues(new String[]{value});
87      }
88  
89      /**
90       * Return the replacement value for the message.
91       *
92       * @return The replacement value.
93       */
94      public String getValue() {
95          Object[] values = getValues();
96          if (values == null || values.length < 1) {
97              throw new IllegalStateException("The retrived value for msg " + 
98                      getKey() + "was null");
99          }
100         if (values.length > 1) {
101             throw new IllegalStateException("There were more than one values " +
102                     "retrived value for msg " + 
103                     getKey());
104         }
105         return (String)getValues()[0];
106     }
107     
108     /**
109      * Compare this message to another.
110      *
111      * @param obj The message to compare.
112      * @return 'true' if the messages are equal.
113      */
114     public boolean equals(Object obj) {
115         if (obj instanceof DatabaseBasicMessage){
116             if (((DatabaseBasicMessage)obj).getKey().equals(getKey()) && 
117                     ((DatabaseBasicMessage)obj).getLocale().equals(this.locale)){
118                 return true;
119             }
120         }
121         return false;
122     }
123 
124     /**
125      * Return the hashcode for the message.
126      *
127      * @return The message's hash code.
128      */
129     public int hashCode() {
130         return super.hashCode();
131     }
132 }