View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.resources.impl;
19  
20  /**
21   * BasicMessage to allow standard ORM
22   * configuration (no complex keys).
23   *
24   * @author James Mitchell
25   *
26   */
27  public class DatabaseBasicMessage extends BasicMessage {
28  
29      /**
30       * Default Constructor.
31       */
32      public DatabaseBasicMessage() {
33          super();
34      }
35  
36      /**
37       * Construct a message for a specified Locale
38       * with the specified replacement values.
39       *
40       * @param locale Locale for this message
41       * @param key Message key for this message
42       * @param values Array of replacement values
43       */
44      public DatabaseBasicMessage(String locale, String key, Object[] values) {
45          super(key, values);
46          this.locale = locale;
47  
48      }
49  
50      private String locale = null;
51  
52      /**
53       * Return the locale for the message.
54       *
55       * @return Returns the locale.
56       */
57      public String getLocale() {
58          return locale;
59      }
60  
61      /**
62       * Set the locale for the message.
63       *
64       * @param locale The locale to set.
65       */
66      public void setLocale(String locale) {
67          this.locale = locale;
68      }
69  
70      /**
71       * Set a replacement value for the message.
72       *
73       * @param value The replacement value.
74       */
75      public void setValue(String value) {
76          setValues(new String[]{value});
77      }
78  
79      /**
80       * Return the replacement value for the message.
81       *
82       * @return The replacement value.
83       */
84      public String getValue() {
85          Object[] values = getValues();
86          if (values == null || values.length < 1) {
87              throw new IllegalStateException("The retrived value for msg " +
88                      getKey() + "was null");
89          }
90          if (values.length > 1) {
91              throw new IllegalStateException("There were more than one values " +
92                      "retrived value for msg " +
93                      getKey());
94          }
95          return (String)getValues()[0];
96      }
97  
98      /**
99       * Compare this message to another.
100      *
101      * @param obj The message to compare.
102      * @return 'true' if the messages are equal.
103      */
104     public boolean equals(Object obj) {
105         if (obj instanceof DatabaseBasicMessage){
106             if (((DatabaseBasicMessage)obj).getKey().equals(getKey()) &&
107                     ((DatabaseBasicMessage)obj).getLocale().equals(this.locale)){
108                 return true;
109             }
110         }
111         return false;
112     }
113 
114     /**
115      * Return the hashcode for the message.
116      *
117      * @return The message's hash code.
118      */
119     public int hashCode() {
120         return super.hashCode();
121     }
122 }