1 /*
2  * Copyright 2011 the original author or authors.
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 org.modelmapper.internal;
17
18 import java.util.Map;
19
20 import org.modelmapper.config.Configuration;
21 import org.modelmapper.spi.NameableType;
22
23 /**
24  * A TypeInfo implementation that lazily reflects members.
25  * 
26  * @author Jonathan Halterman
27  */

28 class TypeInfoImpl<T> implements TypeInfo<T> {
29   /** Source object from which memberNames are read. */
30   private final T source;
31   private final Class<T> type;
32   private final InheritingConfiguration configuration;
33   private volatile Map<String, Accessor> accessors;
34   private volatile Map<String, Mutator> mutators;
35
36   TypeInfoImpl(T source, Class<T> sourceType, InheritingConfiguration configuration) {
37     this.source = source;
38     this.type = sourceType;
39     this.configuration = configuration;
40   }
41
42   @Override
43   public boolean equals(Object obj) {
44     if (obj == this)
45       return true;
46     if (!(obj instanceof TypeInfo))
47       return false;
48     TypeInfo<?> typeInfo = (TypeInfo<?>) obj;
49     return type.equals(typeInfo.getType()) && configuration.equals(typeInfo.getConfiguration());
50   }
51
52   /**
53    * Lazily initializes and gets accessors.
54    */

55   public Map<String, Accessor> getAccessors() {
56     if (accessors == null)
57       synchronized (this) {
58         if (accessors == null)
59           accessors = PropertyInfoSetResolver.resolveAccessors(source, type, configuration);
60       }
61
62     return accessors;
63   }
64
65   public Configuration getConfiguration() {
66     return configuration;
67   }
68
69   /**
70    * Lazily initializes and gets mutators.
71    */

72   public Map<String, Mutator> getMutators() {
73     if (mutators == null)
74       synchronized (this) {
75         if (mutators == null)
76           mutators = PropertyInfoSetResolver.resolveMutators(type, configuration);
77       }
78
79     return mutators;
80   }
81
82   public Class<T> getType() {
83     return type;
84   }
85
86   @Override
87   public int hashCode() {
88     return type.hashCode() * 31 + configuration.hashCode();
89   }
90
91   @Override
92   public String toString() {
93     return type.toString();
94   }
95
96   Mutator mutatorForAccessorMethod(String accessorMethodName) {
97     return getMutators().get(
98         configuration.getSourceNameTransformer().transform(accessorMethodName, NameableType.METHOD));
99   }
100 }
101