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