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.ArrayList;
19 import java.util.List;
20 import org.modelmapper.Converter;
21 import org.modelmapper.Provider;
22 import org.modelmapper.internal.ExplicitMappingBuilder.MappingOptions;
23 import org.modelmapper.internal.util.Strings;
24 import org.modelmapper.spi.PropertyInfo;
25 import org.modelmapper.spi.PropertyMapping;
26
27 /**
28  * @author Jonathan Halterman
29  */

30 class PropertyMappingImpl extends MappingImpl implements PropertyMapping {
31   private final List<PropertyInfo> sourceAccessors;
32   boolean cyclic;
33
34   /**
35    * Creates an implicit PropertyMapping.
36    */

37   PropertyMappingImpl(List<? extends PropertyInfo> sourceAccessors,
38       List<? extends PropertyInfo> destinationMutators, boolean cyclic) {
39     super(destinationMutators);
40     this.sourceAccessors = new ArrayList<PropertyInfo>(sourceAccessors);
41     this.cyclic = cyclic;
42   }
43
44   /**
45    * Creates an implicit merged PropertyMapping to a converter.
46    */

47   PropertyMappingImpl(List<? extends PropertyInfo> sourceAccessors,
48       List<? extends PropertyInfo> destinationMutators, Provider<?> provider,
49       Converter<?, ?> converter) {
50     super(destinationMutators);
51     this.provider = provider;
52     this.converter = converter;
53     this.sourceAccessors = new ArrayList<PropertyInfo>(sourceAccessors);
54   }
55
56   /**
57    * Creates an explicit PropertyMapping.
58    */

59   PropertyMappingImpl(List<? extends PropertyInfo> sourceAccessors,
60       List<? extends PropertyInfo> destinationMutators, MappingOptions options) {
61     super(destinationMutators, options);
62     this.sourceAccessors = new ArrayList<PropertyInfo>(sourceAccessors);
63   }
64
65   /**
66    * Creates a merged PropertyMapping.
67    */

68   private PropertyMappingImpl(PropertyMappingImpl mapping, List<? extends PropertyInfo> mergedAccessors,
69       List<? extends PropertyInfo> mergedMutators) {
70     super(mapping, mergedMutators);
71     sourceAccessors = new ArrayList<PropertyInfo>(mapping.sourceAccessors.size() + mergedAccessors.size());
72     sourceAccessors.addAll(mergedAccessors);
73     sourceAccessors.addAll(mapping.sourceAccessors);
74     cyclic = mapping.cyclic;
75   }
76
77   @Override
78   public PropertyInfo getLastSourceProperty() {
79     return sourceAccessors == null || sourceAccessors.isEmpty() ? null
80         : sourceAccessors.get(sourceAccessors.size() - 1);
81   }
82
83   @Override
84   public List<? extends PropertyInfo> getSourceProperties() {
85     return sourceAccessors;
86   }
87
88   @Override
89   public String toString() {
90     return String.format("PropertyMapping[%s -> %s]", Strings.joinWithFirstType(sourceAccessors),
91         Strings.joinWithFirstType(getDestinationProperties()));
92   }
93
94   @Override
95   public InternalMapping createMergedCopy(List<? extends PropertyInfo> mergedAccessors,
96       List<? extends PropertyInfo> mergedMutators) {
97     return new PropertyMappingImpl(this, mergedAccessors, mergedMutators);
98   }
99
100   @Override
101   public Class<?> getSourceType() {
102     return getLastSourceProperty().getType();
103   }
104 }
105