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.regex.Pattern;
19
20 import org.modelmapper.spi.NameTokenizer;
21 import org.modelmapper.spi.NameableType;
22
23 /**
24  * {@link NameTokenizer} implementations.
25  * 
26  * @author Jonathan Halterman
27  */

28 public class NameTokenizers {
29   /**
30    * Tokenizes class and property names according to the CamelCase naming convention.
31    */

32   public static final NameTokenizer CAMEL_CASE = new CamelCaseNameTokenizer();
33
34   /**
35    * Tokenizes class and property names according to the underscore naming convention.
36    */

37   public static final NameTokenizer UNDERSCORE = new UnderscoreNameTokenizer();
38
39   private static class CamelCaseNameTokenizer implements NameTokenizer {
40     private static final Pattern camelCase = Pattern.compile("(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])");
41
42     public String[] tokenize(String name, NameableType nameableType) {
43       return camelCase.split(name);
44     }
45
46     @Override
47     public String toString() {
48       return "Camel Case";
49     }
50   }
51
52   private static class UnderscoreNameTokenizer implements NameTokenizer {
53     private static final Pattern underscore = Pattern.compile("_");
54
55     public String[] tokenize(String name, NameableType nameableType) {
56       return underscore.split(name);
57     }
58
59     @Override
60     public String toString() {
61       return "Underscore";
62     }
63   }
64 }
65