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

27 public class NameTransformers {
28   /**
29    * Transforms accessor names to their simple property name according to the JavaBeans convention.
30    * Class and field names are unchanged.
31    */

32   public static final NameTransformer JAVABEANS_ACCESSOR = new NameTransformer() {
33     public String transform(String name, NameableType nameableType) {
34       if (NameableType.METHOD.equals(nameableType)) {
35         if (name.startsWith("get") && name.length() > 3)
36           return Strings.decapitalize(name.substring(3));
37         else if (name.startsWith("is") && name.length() > 2)
38           return Strings.decapitalize(name.substring(2));
39       }
40
41       return name;
42     }
43
44     @Override
45     public String toString() {
46       return "Javabeans Accessor";
47     }
48   };
49
50   /**
51    * Transforms mutator names to their simple property name according to the JavaBeans convention.
52    * Class and field names are unchanged.
53    */

54   public static final NameTransformer JAVABEANS_MUTATOR = new NameTransformer() {
55     public String transform(String name, NameableType nameableType) {
56       if (NameableType.METHOD.equals(nameableType) && name.startsWith("set") && name.length() > 3)
57         return Strings.decapitalize(name.substring(3));
58       return name;
59     }
60
61     @Override
62     public String toString() {
63       return "Javabeans Mutator";
64     }
65   };
66
67   /**
68    * Creates NameTransformer for builder.
69    * @return a NameTransformer
70    */

71   public static NameTransformer builder() {
72     return builder("");
73   }
74
75   /**
76    * Creates NameTransformer for builder.
77    *
78    * @param prefix the prefix for the setter of the builder
79    * @return a NameTransformer
80    */

81   public static NameTransformer builder(String prefix) {
82     return new BuilderNameTransformer(prefix);
83   }
84
85   /**
86    * Transforms the names to their simple property name according to the builder convention.
87    * Class and field names are unchanged.
88    */

89   private static class BuilderNameTransformer implements NameTransformer {
90     private String prefix;
91
92     private BuilderNameTransformer(String prefix) {
93       this.prefix = prefix;
94     }
95
96     public String transform(String name, NameableType nameableType) {
97       if (prefix.isEmpty())
98         return name;
99       if (name.startsWith(prefix))
100         return Strings.decapitalize(name.substring(prefix.length()));
101       return name;
102     }
103
104     @Override
105     public String toString() {
106       return "Builder(prefix=" + prefix + ")";
107     }
108   }
109 }
110