Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
binbin.hou
committed
Jun 4, 2020
1 parent
bc4bd1c
commit e7daa62
Showing
7 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
src/main/java/com/github/houbb/lombok/ex/annotation/ToString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package com.github.houbb.lombok.ex.annotation; | ||
|
||
import com.github.houbb.lombok.ex.constant.ToStringType; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 注解 | ||
* @author binbin.hou | ||
* @since 0.0.4 | ||
* @see com.github.houbb.lombok.ex.support.tostring.IToString 字符串构建策略,后续添加这个拓展 | ||
*/ | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target(ElementType.TYPE) | ||
@Documented | ||
public @interface ToString { | ||
|
||
/** | ||
* 转换为字符串的实现方式 | ||
* @return 实现方式 | ||
* @since 0.0.6 | ||
*/ | ||
ToStringType value() default ToStringType.CONCAT; | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/github/houbb/lombok/ex/constant/ToStringType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.houbb.lombok.ex.constant; | ||
|
||
/** | ||
* @author binbin.hou | ||
* @since 0.0.6 | ||
*/ | ||
public enum ToStringType { | ||
|
||
/** | ||
* 基于 fastJson 实现 | ||
* @since 0.0.6 | ||
*/ | ||
FAST_JSON, | ||
|
||
/** | ||
* 字符串拼接 | ||
* @since 0.0.6 | ||
*/ | ||
CONCAT, | ||
|
||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/github/houbb/lombok/ex/support/tostring/impl/ToStringDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.houbb.lombok.ex.support.tostring.impl; | ||
|
||
import com.github.houbb.heaven.annotation.ThreadSafe; | ||
import com.github.houbb.heaven.util.lang.StringUtil; | ||
import com.github.houbb.lombok.ex.support.tostring.IToString; | ||
|
||
/** | ||
* @author binbin.hou | ||
* @since 0.0.6 | ||
*/ | ||
@ThreadSafe | ||
public class ToStringDefault implements IToString { | ||
|
||
@Override | ||
public String toString(Object instance) { | ||
return StringUtil.EMPTY; | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/com/github/houbb/lombok/ex/support/tostring/impl/ToStringFastJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/com/github/houbb/lombok/ex/util/LombokExStringConcatUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.github.houbb.lombok.ex.util; | ||
|
||
import com.github.houbb.heaven.annotation.CommonEager; | ||
import com.github.houbb.heaven.constant.PunctuationConst; | ||
import com.github.houbb.heaven.support.tuple.impl.Pair; | ||
import com.github.houbb.heaven.util.common.ArgUtil; | ||
import com.github.houbb.heaven.util.guava.Guavas; | ||
import com.github.houbb.heaven.util.lang.StringUtil; | ||
import com.github.houbb.heaven.util.lang.reflect.ClassTypeUtil; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author binbin.hou | ||
* @since 0.0.6 | ||
*/ | ||
@Deprecated | ||
public class LombokExStringConcatUtil { | ||
|
||
private LombokExStringConcatUtil(){} | ||
|
||
|
||
/** | ||
* 进行字符串的连接 | ||
* | ||
* <pre> | ||
* return "ToStringConcatTest{" + | ||
* "name='" + name + '\'' + | ||
* ", age=" + age + | ||
* ", ints=" + Arrays.toString(ints) + | ||
* '}'; | ||
* </pre> | ||
* @param className 类名 | ||
* @param fieldPairs 字段列表 | ||
* @return 结果 | ||
* @since 0.0.6 | ||
*/ | ||
public static String concat(final String className, | ||
final List<Pair<String, Object>> fieldPairs){ | ||
|
||
ArgUtil.notEmpty(className, "className"); | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append(className).append("{"); | ||
|
||
// 具体细节可以不断优化,初期可以简单些。 | ||
List<String> fieldInfoList = Guavas.newArrayList(fieldPairs.size()); | ||
for(Pair<String, Object> pair : fieldPairs) { | ||
String fieldName = pair.getValueOne(); | ||
Object fieldValue = pair.getValueTwo(); | ||
|
||
String fieldValueStr = objectToConcatString(fieldValue); | ||
fieldInfoList.add(fieldName+"="+fieldValueStr); | ||
} | ||
String fieldInfoStr = StringUtil.join(fieldInfoList, PunctuationConst.COMMA); | ||
stringBuilder.append(fieldInfoStr); | ||
stringBuilder.append("}"); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
/** | ||
* 转换为 toString() 的方法 | ||
* @param object 对象 | ||
* @return 结果 | ||
* @since 0.0.5 | ||
*/ | ||
@CommonEager | ||
private static String objectToConcatString(final Object object) { | ||
if(null == object) { | ||
return "null"; | ||
} | ||
|
||
final Class type = object.getClass(); | ||
if(ClassTypeUtil.isArray(type)) { | ||
// 这里添加各种不同的类型处理 | ||
if(int[].class == type) { | ||
return Arrays.toString((boolean[]) object); | ||
} else if(byte[].class == type) { | ||
return Arrays.toString((boolean[]) object); | ||
} else if(char[].class == type) { | ||
return Arrays.toString((char[]) object); | ||
} else if(boolean[].class == type) { | ||
return Arrays.toString((boolean[]) object); | ||
} else if(float[].class == type) { | ||
return Arrays.toString((float[]) object); | ||
} else if(double[].class == type) { | ||
return Arrays.toString((double[]) object); | ||
} else if(short[].class == type) { | ||
return Arrays.toString((short[]) object); | ||
} else if(long[].class == type) { | ||
return Arrays.toString((long[]) object); | ||
} | ||
|
||
// 默认是对象数组 | ||
return Arrays.toString((Object[]) object); | ||
} | ||
|
||
return object.toString(); | ||
} | ||
|
||
} |