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 org.modelmapper.spi.MatchingStrategy;
19 import org.modelmapper.spi.PropertyNameInfo;
20 import org.modelmapper.spi.Tokens;
21
22 /**
23  * See {@link MatchingStrategies#LOOSE}.
24  * 
25  * @author Jonathan Halterman
26  */

27 final class LooseMatchingStrategy implements MatchingStrategy {
28   @Override
29   public boolean matches(PropertyNameInfo propertyNameInfo) {
30     return new Matcher(propertyNameInfo).match();
31   }
32
33   /**
34    * Since this strategy only requires matching the last source and destination properties, property
35    * iteration is done in reverse.
36    */

37   static class Matcher extends InexactMatcher {
38     Matcher(PropertyNameInfo propertyNameInfo) {
39       super(propertyNameInfo);
40     }
41
42     boolean match() {
43       if (!matchLastDestTokens())
44         return false;
45
46       for (Tokens destTokens : propertyNameInfo.getDestinationPropertyTokens()) {
47         for (int destTokenIndex = 0; destTokenIndex < destTokens.size(); destTokenIndex++) {
48           DestTokensMatcher matchedTokens = matchSourcePropertyName(destTokens, destTokenIndex);
49           if (matchedTokens.match(sourceTokens.size() - 1))
50             return true;
51         }
52       }
53
54       return false;
55     }
56
57     boolean matchLastDestTokens() {
58       Tokens tokens = destTokens.get(destTokens.size() - 1);
59       for (int destTokenIndex = 0; destTokenIndex < tokens.size(); destTokenIndex++) {
60         DestTokensMatcher matchedTokens = matchSourcePropertyName(tokens, destTokenIndex);
61         if (matchedTokens.match())
62           return true;
63         if (matchSourcePropertyType(tokens.token(destTokenIndex)) || matchSourceClass(tokens.token(destTokenIndex)))
64           return true;
65       }
66       return false;
67     }
68   }
69
70   @Override
71   public boolean isExact() {
72     return false;
73   }
74
75   @Override
76   public String toString() {
77     return "Loose";
78   }
79 }
80