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.convention;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.modelmapper.spi.MatchingStrategy;
22 import org.modelmapper.spi.PropertyNameInfo;
23 import org.modelmapper.spi.Tokens;
24
25 /**
26  * See {@link MatchingStrategies#STANDARD}.
27  * 
28  * @author Jonathan Halterman
29  */

30 final class StandardMatchingStrategy implements MatchingStrategy {
31   @Override
32   public boolean matches(PropertyNameInfo propertyNameInfo) {
33     return new Matcher(propertyNameInfo).match();
34   }
35
36   static class Matcher extends InexactMatcher {
37     Matcher(PropertyNameInfo propertyNameInfo) {
38       super(propertyNameInfo);
39     }
40
41     boolean match() {
42       Set<Integer> matchSources = new HashSet<Integer>();
43       for (Tokens destTokens : propertyNameInfo.getDestinationPropertyTokens()) {
44         for (int destTokenIndex = 0; destTokenIndex < destTokens.size();) {
45           DestTokensMatcher matchedTokens = matchSourcePropertyName(destTokens, destTokenIndex);
46           if (matchedTokens.match()) {
47             destTokenIndex += matchedTokens.maxMatchTokens();
48             matchSources.addAll(matchedTokens.matchSources());
49           } else if (matchSourcePropertyType(destTokens.token(destTokenIndex))
50               || matchSourceClass(destTokens.token(destTokenIndex)))
51             destTokenIndex++;
52           else
53             return false;
54         }
55       }
56
57       return matchSources.size() == sourceTokens.size();
58     }
59   }
60
61   @Override
62   public boolean isExact() {
63     return false;
64   }
65
66   @Override
67   public String toString() {
68     return "Standard";
69   }
70 }
71