View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.config.cache;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.lang.ObjectUtils;
21  
22  import de.smartics.util.lang.Arg;
23  
24  /**
25   * A compound key consists of two key parts the virtual cache name and the name
26   * of the property key. A virtual cache is opened e.g. for configuration keys.
27   */
28  public class CompoundKey implements Serializable
29  {
30  
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     */
38    private static final long serialVersionUID = 1L;
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The name of the virtual cache (e.g. configuration key).
44     */
45    private String name;
46  
47    /**
48     * The real key.
49     */
50    private Object key;
51  
52    /**
53     * The internal string representation for this object.
54     */
55    private String stringRepresentation;
56  
57    // ****************************** Initializer *******************************
58  
59    // ****************************** Constructors ******************************
60  
61    /**
62     * Constructor to crate a compound key.
63     *
64     * @param name the name of the virtual cache (e.
65     * @param key the real key.
66     */
67    public CompoundKey(final String name, final Object key)
68    {
69      this.name = Arg.checkNotNull("name", name);
70      this.key = Arg.checkNotNull("key", key);
71      this.stringRepresentation = internaltoString(name, key);
72    }
73  
74    // ****************************** Inner Classes *****************************
75  
76    // ********************************* Methods ********************************
77  
78    // --- init -----------------------------------------------------------------
79  
80    // --- get&set --------------------------------------------------------------
81  
82    /**
83     * Returns the name of the virtual cache (e. g. configuration key).
84     *
85     * @return the name of the virtual cache (e.
86     */
87    public String getName()
88    {
89      return name;
90    }
91  
92    /**
93     * Returns the real key.
94     *
95     * @return the real key.
96     */
97    public Object getKey()
98    {
99      return key;
100   }
101 
102   // --- business -------------------------------------------------------------
103 
104   // --- object basics --------------------------------------------------------
105 
106   @Override
107   public int hashCode()
108   {
109     int result = 17;
110     result = 37 * result + ObjectUtils.hashCode(getName());
111     result = 37 * result + getKey().hashCode();
112     return result;
113   }
114 
115   @Override
116   public boolean equals(final Object object)
117   {
118     if (this == object)
119     {
120       return true;
121     }
122     else if (object == null || getClass() != object.getClass())
123     {
124       return false;
125     }
126 
127     final CompoundKey other = (CompoundKey) object;
128 
129     return ObjectUtils.equals(getName(), other.getName())
130            && getKey().equals(other.getKey());
131   }
132 
133   /**
134    * Returns the string representation of the object.
135    *
136    * @return the string representation of the object.
137    */
138   @Override
139   public String toString()
140   {
141     return this.stringRepresentation;
142   }
143 
144   private static String internaltoString(final String name, final Object key)
145   {
146     final StringBuilder buffer = new StringBuilder();
147     buffer.append(name).append('.');
148     buffer.append(key);
149     return buffer.toString().intern();
150   }
151 
152 }