forked from nutzam/nutz
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
21 changed files
with
387 additions
and
179 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Byte extends Castor<Number, Byte> { | ||
|
||
@Override | ||
public Byte cast(Number src, Class<?> toType, String... args) { | ||
return src.byteValue(); | ||
} | ||
|
||
} |
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,14 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
import org.nutz.castor.FailToCastObjectException; | ||
|
||
public class Number2Char extends Castor<Number, Character> { | ||
|
||
@Override | ||
public Character cast(Number src, Class<?> toType, String... args) | ||
throws FailToCastObjectException { | ||
return (char) src.intValue(); | ||
} | ||
|
||
} |
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Double extends Castor<Number, Double> { | ||
|
||
@Override | ||
public Double cast(Number src, Class<?> toType, String... args) { | ||
return src.doubleValue(); | ||
} | ||
|
||
} |
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Float extends Castor<Number, Float> { | ||
|
||
@Override | ||
public Float cast(Number src, Class<?> toType, String... args) { | ||
return src.floatValue(); | ||
} | ||
|
||
} |
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Integer extends Castor<Number, Integer> { | ||
|
||
@Override | ||
public Integer cast(Number src, Class<?> toType, String... args) { | ||
return src.intValue(); | ||
} | ||
|
||
} |
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Long extends Castor<Number, Long> { | ||
|
||
@Override | ||
public Long cast(Number src, Class<?> toType, String... args) { | ||
return src.longValue(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
package org.nutz.castor.castor; | ||
|
||
import org.nutz.castor.Castor; | ||
|
||
public class Number2Short extends Castor<Number, Short> { | ||
|
||
@Override | ||
public Short cast(Number src, Class<?> toType, String... args) { | ||
return src.shortValue(); | ||
} | ||
|
||
} |
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
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
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,15 @@ | ||
package org.nutz.castor.castor; | ||
|
||
public class String2Byte extends String2Number<Byte> { | ||
|
||
@Override | ||
protected Byte getPrimitiveDefaultValue() { | ||
return (byte) 0; | ||
} | ||
|
||
@Override | ||
protected Byte valueOf(String str) { | ||
return Byte.valueOf(str); | ||
} | ||
|
||
} |
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,15 @@ | ||
package org.nutz.castor.castor; | ||
|
||
public class String2Double extends String2Number<Double> { | ||
|
||
@Override | ||
protected Double getPrimitiveDefaultValue() { | ||
return 0.0; | ||
} | ||
|
||
@Override | ||
protected Double valueOf(String str) { | ||
return Double.valueOf(str); | ||
} | ||
|
||
} |
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,15 @@ | ||
package org.nutz.castor.castor; | ||
|
||
public class String2Float extends String2Number<Float> { | ||
|
||
@Override | ||
protected Float getPrimitiveDefaultValue() { | ||
return 0.0f; | ||
} | ||
|
||
@Override | ||
protected Float valueOf(String str) { | ||
return Float.valueOf(str); | ||
} | ||
|
||
} |
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,15 @@ | ||
package org.nutz.castor.castor; | ||
|
||
public class String2Integer extends String2Number<Integer> { | ||
|
||
@Override | ||
protected Integer getPrimitiveDefaultValue() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
protected Integer valueOf(String str) { | ||
return Integer.valueOf(str); | ||
} | ||
|
||
} |
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,15 @@ | ||
package org.nutz.castor.castor; | ||
|
||
public class String2Long extends String2Number<Long> { | ||
|
||
@Override | ||
protected Long getPrimitiveDefaultValue() { | ||
return 0L; | ||
} | ||
|
||
@Override | ||
protected Long valueOf(String str) { | ||
return Long.valueOf(str); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import org.nutz.castor.Castor; | ||
import org.nutz.castor.FailToCastObjectException; | ||
import org.nutz.lang.Lang; | ||
import org.nutz.lang.Mirror; | ||
import org.nutz.lang.Strings; | ||
|
||
/** | ||
|
@@ -17,28 +16,27 @@ | |
* | ||
* @author zozoh([email protected]) | ||
*/ | ||
public class String2Number extends Castor<String, Number> { | ||
public abstract class String2Number<T> extends Castor<String, T> { | ||
|
||
protected boolean _isNull(String str) { | ||
return Strings.isBlank(str) || "null".equalsIgnoreCase(str); | ||
} | ||
|
||
protected abstract T getPrimitiveDefaultValue(); | ||
|
||
protected abstract T valueOf(String str); | ||
|
||
@Override | ||
public Number cast(String src, Class<?> toType, String... args) { | ||
if (Strings.isBlank(src)) { | ||
if (toType.isPrimitive()) { | ||
if (toType == float.class || toType == double.class) | ||
return 0.0f; | ||
return 0; | ||
} else { | ||
return null; | ||
} | ||
public T cast(String src, Class<?> toType, String... args) { | ||
if (Strings.isBlank(src) || "null".equalsIgnoreCase(src)) { | ||
return toType.isPrimitive() ? getPrimitiveDefaultValue() : null; | ||
} | ||
if (!toType.isPrimitive() | ||
&& ("null".equals(src) || "NULL".equals(src) || "Null".equals(src))) { | ||
return null; | ||
} | ||
try { | ||
return (Number) Mirror.me(toType) | ||
.getWrapperClass() | ||
.getConstructor(String.class) | ||
.newInstance(src); | ||
return valueOf(src); | ||
} | ||
catch (Exception e) { | ||
throw new FailToCastObjectException(String.format("Fail to cast '%s' to <%s>", | ||
|
Oops, something went wrong.