View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.config;
17  
18  import net.sf.ehcache.store.ElementValueComparator;
19  
20  /***
21   * @author Ludovic Orban
22   */
23  public class ElementValueComparatorConfiguration {
24  
25      private volatile String className = "net.sf.ehcache.store.DefaultElementValueComparator";
26      private ElementValueComparator comparator;
27  
28      /***
29       * Returns the fully qualified class name for the ElementValueComparator to use
30       * 
31       * @return FQCN to the ElementValueComparator implementation to use
32       */
33      public String getClassName() {
34          return className;
35      }
36  
37      /***
38       * Sets the fully qualified class name for the ElementValueComparator to use
39       * 
40       * @param className
41       *            FQCN
42       */
43      public void setClass(final String className) {
44          this.className = className;
45      }
46  
47      /***
48       * Get (and potentially) instantiate the instance
49       * 
50       * @return the instance
51       */
52      public synchronized ElementValueComparator getElementComparatorInstance() {
53          if (comparator == null) {
54              Class elementComparator = null;
55              try {
56                  elementComparator = Class.forName(className);
57                  comparator = (ElementValueComparator) elementComparator.newInstance();
58              } catch (ClassNotFoundException e) {
59                  throw new RuntimeException("Couldn't find the ElementValueComparator class!", e);
60              } catch (InstantiationException e) {
61                  throw new RuntimeException("Couldn't instantiate the ElementValueComparator instance!", e);
62              } catch (IllegalAccessException e) {
63                  throw new RuntimeException("Couldn't instantiate the ElementValueComparator instance!", e);
64              } catch (ClassCastException e) {
65                  throw new RuntimeException(elementComparator != null ? elementComparator.getSimpleName()
66                          + " doesn't implement net.sf.ehcache.store.ElementValueComparator" : "Error with ElementValueComparator", e);
67              }
68          }
69          return comparator;
70      }
71  
72      /***
73       * {@inheritDoc}
74       */
75      @Override
76      public int hashCode() {
77          final int prime = 31;
78          int result = 1;
79          result = prime * result + ((className == null) ? 0 : className.hashCode());
80          return result;
81      }
82  
83      /***
84       * {@inheritDoc}
85       */
86      @Override
87      public boolean equals(Object obj) {
88          if (this == obj) {
89              return true;
90          }
91          if (obj == null) {
92              return false;
93          }
94          if (getClass() != obj.getClass()) {
95              return false;
96          }
97          ElementValueComparatorConfiguration other = (ElementValueComparatorConfiguration) obj;
98          if (className == null) {
99              if (other.className != null) {
100                 return false;
101             }
102         } else if (!className.equals(other.className)) {
103             return false;
104         }
105         return true;
106     }
107 
108 }