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    /**
021     * BasicMessage to allow standard ORM
022     * configuration (no complex keys).
023     *
024     * @author James Mitchell
025     *
026     */
027    public class DatabaseBasicMessage extends BasicMessage {
028    
029        /**
030         * Default Constructor.
031         */
032        public DatabaseBasicMessage() {
033            super();
034        }
035    
036        /**
037         * Construct a message for a specified Locale
038         * with the specified replacement values.
039         *
040         * @param locale Locale for this message
041         * @param key Message key for this message
042         * @param values Array of replacement values
043         */
044        public DatabaseBasicMessage(String locale, String key, Object[] values) {
045            super(key, values);
046            this.locale = locale;
047    
048        }
049    
050        private String locale = null;
051    
052        /**
053         * Return the locale for the message.
054         *
055         * @return Returns the locale.
056         */
057        public String getLocale() {
058            return locale;
059        }
060    
061        /**
062         * Set the locale for the message.
063         *
064         * @param locale The locale to set.
065         */
066        public void setLocale(String locale) {
067            this.locale = locale;
068        }
069    
070        /**
071         * Set a replacement value for the message.
072         *
073         * @param value The replacement value.
074         */
075        public void setValue(String value) {
076            setValues(new String[]{value});
077        }
078    
079        /**
080         * Return the replacement value for the message.
081         *
082         * @return The replacement value.
083         */
084        public String getValue() {
085            Object[] values = getValues();
086            if (values == null || values.length < 1) {
087                throw new IllegalStateException("The retrived value for msg " +
088                        getKey() + "was null");
089            }
090            if (values.length > 1) {
091                throw new IllegalStateException("There were more than one values " +
092                        "retrived value for msg " +
093                        getKey());
094            }
095            return (String)getValues()[0];
096        }
097    
098        /**
099         * 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    }