Skip to content

Commit

Permalink
add intercept for method and param names
Browse files Browse the repository at this point in the history
  • Loading branch information
holmeszyx committed Aug 1, 2016
1 parent dd60f72 commit dcc6ef5
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 30 deletions.
31 changes: 31 additions & 0 deletions src/main/java/z/hol/spgen/ConstNameIntercept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package z.hol.spgen;

/**
* const name's transformation
* Created by holmes on 16-8-1.
*/
public final class ConstNameIntercept implements TextIntercept {

public static final ConstNameIntercept INSTANCE = new ConstNameIntercept();

private ConstNameIntercept() {
}

public String intercept(String src) {
String result;
final String name = src;
int length = name.length();
StringBuilder sb = new StringBuilder(length + 8);
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = Character.toLowerCase(c);
sb.append("_");
}
sb.append(c);
}
result = sb.toString();
return result;
}

}
76 changes: 47 additions & 29 deletions src/main/java/z/hol/spgen/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ public class Entity {
private String mComment;
private String mConstName;

public Entity(String key) {
private TextIntercept mParamKeyIntercept;
private TextIntercept mConstKeyIntercept;


public Entity(String key, TextIntercept paramKeyIntercept) {
mKey = key;
mParamKeyIntercept = paramKeyIntercept;
getParamName();
}

Expand All @@ -68,44 +73,57 @@ public String getKey() {
return mKey;
}

public TextIntercept getParamKeyIntercept() {
return mParamKeyIntercept;
}

/**
* set the intercept for the param name in getter/setter method
* @param paramKeyIntercept
*/
public Entity setParamKeyIntercept(TextIntercept paramKeyIntercept) {
if (mParamKeyIntercept != paramKeyIntercept) {
mParamKeyIntercept = paramKeyIntercept;
mParamName = null;
getParamName();
}
return this;
}

public TextIntercept getConstKeyIntercept() {
return mConstKeyIntercept;
}

/**
* set the intercept for const name of preference name
* @param constKeyIntercept
*/
public Entity setConstKeyIntercept(TextIntercept constKeyIntercept) {
mConstKeyIntercept = constKeyIntercept;
return this;
}

public String getParamName() {
if (mParamName == null) {
// 去掉_以及-, 并将下个字母的小字转大小
final String name = mKey;
int length = name.length();
StringBuilder sb = new StringBuilder(length);
boolean nextCap = false;
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (c == '_' || c == '-') {
nextCap = true;
continue;
}
if (sb.length() > 0 && nextCap && c >= 'a' && c <= 'z') {
c = Character.toUpperCase(c);
nextCap = false;
}
sb.append(c);
TextIntercept intercept = mParamKeyIntercept;
if (intercept == null) {
intercept = ParamNameIntercept.INSTANCE;
}
mParamName = intercept.intercept(mKey);
if (mParamName == null) {
throw new IllegalArgumentException("the return for param name should be not null.");
}
mParamName = sb.toString();
}
return mParamName;
}

public String getConstName() {
if (mConstName == null) {
final String name = mParamName;
int length = name.length();
StringBuilder sb = new StringBuilder(length + 8);
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = Character.toLowerCase(c);
sb.append("_");
}
sb.append(c);
TextIntercept intercept = mConstKeyIntercept;
if (intercept == null) {
intercept = ConstNameIntercept.INSTANCE;
}
mConstName = sb.toString();
mConstName = intercept.intercept(mParamName);
}
return mConstName;
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/z/hol/spgen/ParamNameIntercept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package z.hol.spgen;

/**
* parameter name's transformation
* Created by holmes on 16-8-1.
*/
public final class ParamNameIntercept implements TextIntercept {

public static final ParamNameIntercept INSTANCE = new ParamNameIntercept();

private ParamNameIntercept() {
}

public String intercept(String src) {
String result;
// 去掉_以及-, 并将下个字母的小字转大小
final String name = src;
int length = name.length();
StringBuilder sb = new StringBuilder(length);
boolean nextCap = false;
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (c == '_' || c == '-') {
nextCap = true;
continue;
}
if (sb.length() > 0 && nextCap && c >= 'a' && c <= 'z') {
c = Character.toUpperCase(c);
nextCap = false;
}
sb.append(c);
}
result = sb.toString();
return result;
}

}
22 changes: 21 additions & 1 deletion src/main/java/z/hol/spgen/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class Rule {

private boolean mCanClear = false;

private TextIntercept mParamKeyIntercept = ParamNameIntercept.INSTANCE;
private TextIntercept mConstKeyIntercept = ConstNameIntercept.INSTANCE;

/**
* SharedPreferences
* @param clssName 操作SharedPreferences的类(不包括包名)
Expand All @@ -29,11 +32,28 @@ public Rule(String clssName, String settingName) {
}

public Entity addEntity(String name) {
Entity entity = new Entity(name);
Entity entity = new Entity(name, mParamKeyIntercept);
entity.setConstKeyIntercept(mConstKeyIntercept);
mEntities.add(entity);
return entity;
}

/**
* the default parameter intercept for all entity in this rule
* @param paramKeyIntercept
*/
public void setParamKeyIntercept(TextIntercept paramKeyIntercept) {
mParamKeyIntercept = paramKeyIntercept;
}

/**
* the default const intercept for all entity in this rule
* @param constKeyIntercept
*/
public void setConstKeyIntercept(TextIntercept constKeyIntercept) {
mConstKeyIntercept = constKeyIntercept;
}

public String getClssName() {
return clssName;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/z/hol/spgen/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public List<Rule> getRules() {
return mRules;
}

/**
* Rule class name suffix.
* if don't need it, set suffix to null.
* notice: If you set suffix to null, you must call it before addRule.
* @param ruleClassSuffix
*/
public void setRuleClassSuffix(String ruleClassSuffix) {
mRuleClassSuffix = ruleClassSuffix;
}

public boolean isUseClassSuffix() {
return mRuleClassSuffix != null && mRuleClassSuffix.length() > 0;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/z/hol/spgen/TextIntercept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package z.hol.spgen;

/**
* To handle the transformation between text;
* Created by holmes on 16-8-1.
*/
public interface TextIntercept {

/**
* transforming the text
* @param src origin text
* @return
*/
String intercept(String src);

}

0 comments on commit dcc6ef5

Please sign in to comment.