View Javadoc
1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package ch.qos.cal10n;
24  
25  import java.util.Arrays;
26  
27  /**
28   * Holds data relevant for a deferred message lookup. This is useful when the
29   * appropriate locale is <em>unknown</em> at the time or place where the message
30   * key and message args are emitted. For example, a low level library might wish
31   * to emit a localized message but it is only at the UI (user interface) layer
32   * that the locale is known. As another example, imagine that the host where the
33   * localized messages are presented to the user is in a different locale, e.g.
34   * Japan, than the locale of the source host. e.g. US.
35   * 
36   * <p>
37   * Instances of this class are intended to be immutable, subject to the
38   * immutability of the supplied args.
39   * 
40   * @author Rick Beton
41   * @author Ceki G&uuml;lc&uuml;
42   * 
43   */
44  public class MessageParameterObj {
45  
46    private final Enum<?> key;
47    private final Object[] args;
48  
49    /**
50     * Constructs an instance.
51     * 
52     * @param key
53     *          the key for the localized message.
54     * @param args
55     *          any message parameters, as required.
56     */
57    public MessageParameterObj(Enum<?> key, Object... args) {
58      if (key == null) {
59        new IllegalArgumentException("Enum argument \"key\" can't be null");
60      }
61      this.key = key;
62      this.args = args;
63    }
64  
65    public Enum<?> getKey() {
66      return key;
67    }
68  
69    public Object[] getArgs() {
70      return args;
71    }
72  
73    @Override
74    public String toString() {
75      final StringBuilder b = new StringBuilder("MessageParameterObj(");
76      b.append(key.name());
77      b.append(", ");
78      b.append(Arrays.toString(args));
79      b.append(")");
80      return b.toString();
81    }
82  
83    @Override
84    public int hashCode() {
85      return key.hashCode();
86    }
87  
88    @Override
89    public boolean equals(Object obj) {
90      if (this == obj)
91        return true;
92      if (obj == null)
93        return false;
94      if (getClass() != obj.getClass())
95        return false;
96  
97      MessageParameterObj other = (MessageParameterObj) obj;
98      // key cannot be null by virtue of the check done within the constructor
99      if (!key.equals(other.key))
100       return false;
101 
102     return Arrays.equals(args, other.args);
103   }
104 }