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.PropertyType;
19 import org.modelmapper.spi.NamingConvention;
20
21 /**
22  * {@link NamingConvention} implementations.
23  * 
24  * @author Jonathan Halterman
25  */

26 public class NamingConventions {
27   /**
28    * JavaBeans naming convention for accessors.
29    */

30   public static final NamingConvention JAVABEANS_ACCESSOR = new NamingConvention() {
31     public boolean applies(String propertyName, PropertyType propertyType) {
32       return PropertyType.FIELD.equals(propertyType)
33           || (propertyName.startsWith("get") && propertyName.length() > 3)
34           || (propertyName.startsWith("is") && propertyName.length() > 2);
35     }
36
37     @Override
38     public String toString() {
39       return "Javabeans Accessor";
40     }
41   };
42
43   /**
44    * JavaBeans naming convention for mutators.
45    */

46   public static final NamingConvention JAVABEANS_MUTATOR = new NamingConvention() {
47     public boolean applies(String propertyName, PropertyType propertyType) {
48       return PropertyType.FIELD.equals(propertyType)
49           || (propertyName.startsWith("set") && propertyName.length() > 3);
50     }
51
52     @Override
53     public String toString() {
54       return "Javabeans Mutator";
55     }
56   };
57
58   /**
59    * Creates NameTransformer for builder.
60    * @return a NamingConvention
61    */

62   public static NamingConvention builder() {
63     return builder("");
64   }
65
66   /**
67    * Creates NamingConvention for builder.
68    *
69    * @param prefix the prefix for the setter of the builder
70    * @return a NamingConvention
71    */

72   public static NamingConvention builder(String prefix) {
73     return new BuilderNamingConventions(prefix);
74   }
75
76   /**
77    * Naming convention for builder.
78    */

79   private static class BuilderNamingConventions implements NamingConvention {
80     private String prefix;
81
82     private BuilderNamingConventions(String prefix) {
83       this.prefix = prefix;
84     }
85
86     public boolean applies(String propertyName, PropertyType propertyType) {
87       return PropertyType.METHOD.equals(propertyType) && propertyName.startsWith(prefix);
88     }
89
90     @Override
91     public String toString() {
92       return "Builder(prefix=" + prefix + ")";
93     }
94   }
95
96   /**
97    * Represents no naming convention. This convention
98    * {@link NamingConvention#applies(String, PropertyType) applies} to all property names, allowing
99    * all properties to be eligible for matching.
100    */

101   public static final NamingConvention NONE = new NamingConvention() {
102     public boolean applies(String propertyName, PropertyType propertyType) {
103       return true;
104     }
105
106     @Override
107     public String toString() {
108       return "None";
109     }
110   };
111 }
112