Skip to content

Commit

Permalink
一顿狂修改 castors
Browse files Browse the repository at this point in the history
  • Loading branch information
zozoh committed Sep 25, 2013
1 parent a430837 commit aa9906b
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 179 deletions.
39 changes: 30 additions & 9 deletions src/org/nutz/castor/Castor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -21,8 +22,28 @@
public abstract class Castor<FROM, TO> {

protected Castor() {
fromClass = (Class<?>) Mirror.getTypeParams(getClass())[0];
toClass = (Class<?>) Mirror.getTypeParams(getClass())[1];
Class<?> myType = getClass();
Class<?> mySuper = myType.getSuperclass();

Type[] myParams = Mirror.getTypeParams(myType);
Type[] superParams = Mirror.getTypeParams(mySuper);

if (null != superParams && superParams.length == 2) {
Class<?>[] args = new Class<?>[2];
int n = 0;
for (int i = 0; i < superParams.length; i++) {
if (superParams[i] instanceof Class<?>)
args[i] = (Class<?>) superParams[i];
else
args[i] = (Class<?>) myParams[n++];
}
fromClass = args[0];
toClass = args[1];
} else {
fromClass = (Class<?>) myParams[0];
toClass = (Class<?>) myParams[1];
}

}

protected Class<?> fromClass;
Expand Down Expand Up @@ -56,29 +77,29 @@ protected static Collection<?> createCollection(Object src, Class<?> toType)
}
}
if (null == coll)
throw new FailToCastObjectException(String.format( "Castors don't know how to implement '%s'",
toType.getName()),
throw new FailToCastObjectException(String.format("Castors don't know how to implement '%s'",
toType.getName()),
Lang.unwrapThrow(e));
}
return coll;
}

public int hashCode() {
return toString().hashCode();
}

public boolean equals(Object obj) {
if(!(obj instanceof Castor)){
if (!(obj instanceof Castor)) {
return false;
}
Castor<?, ?> castor = (Castor<?, ?>) obj;
return toString().equals(castor.toString());
}

public String toString() {
return fromClass.getName() + "2" + toClass.getName();
}

public static final String key(Class<?> fromClass, Class<?> toClass) {
return fromClass.getName() + "2" + toClass.getName();
}
Expand Down
221 changes: 123 additions & 98 deletions src/org/nutz/castor/Castors.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/org/nutz/castor/castor/Number2Boolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Number2Boolean extends Castor<Number, Boolean> {

@Override
public Boolean cast(Number src, Class<?> toType, String... args) {
return src.toString().charAt(0) == '0' ? false : true;
return src.intValue() == 0 ? false : true;
}

}
12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Byte.java
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();
}

}
14 changes: 14 additions & 0 deletions src/org/nutz/castor/castor/Number2Char.java
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();
}

}
12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Double.java
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();
}

}
12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Float.java
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();
}

}
12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Integer.java
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();
}

}
12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Long.java
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();
}

}
22 changes: 0 additions & 22 deletions src/org/nutz/castor/castor/Number2Number.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/org/nutz/castor/castor/Number2Short.java
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();
}

}
26 changes: 8 additions & 18 deletions src/org/nutz/castor/castor/String2BigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

import java.math.BigDecimal;

import org.nutz.castor.Castor;
import org.nutz.castor.FailToCastObjectException;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
public class String2BigDecimal extends String2Number<BigDecimal> {

public class String2BigDecimal extends Castor<String, BigDecimal > {
@Override
protected BigDecimal getPrimitiveDefaultValue() {
return new BigDecimal(0);
}

public BigDecimal cast(String src, Class<?> toType, String... args)
throws FailToCastObjectException {
if (Strings.isBlank(src)) {
return null;
}
try {
return new BigDecimal(src);
}
catch (Exception e) {
throw new FailToCastObjectException(String.format( "Fail to cast '%s' to <%s>",
src,
toType.getName()), Lang.unwrapThrow(e));
}
@Override
protected BigDecimal valueOf(String str) {
return new BigDecimal(str);
}

}
7 changes: 3 additions & 4 deletions src/org/nutz/castor/castor/String2Boolean.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.nutz.castor.castor;

import org.nutz.castor.Castor;
import org.nutz.castor.FailToCastObjectException;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;

public class String2Boolean extends Castor<String, Boolean> {

@Override
public Boolean cast(String src, Class<?> toType, String... args)
throws FailToCastObjectException {
if (src.length() == 0)
public Boolean cast(String src, Class<?> toType, String... args) {
if (Strings.isBlank(src))
return false;
return Lang.parseBoolean(src);
}
Expand Down
15 changes: 15 additions & 0 deletions src/org/nutz/castor/castor/String2Byte.java
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);
}

}
15 changes: 15 additions & 0 deletions src/org/nutz/castor/castor/String2Double.java
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);
}

}
15 changes: 15 additions & 0 deletions src/org/nutz/castor/castor/String2Float.java
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);
}

}
15 changes: 15 additions & 0 deletions src/org/nutz/castor/castor/String2Integer.java
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);
}

}
15 changes: 15 additions & 0 deletions src/org/nutz/castor/castor/String2Long.java
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);
}

}
28 changes: 13 additions & 15 deletions src/org/nutz/castor/castor/String2Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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>",
Expand Down
Loading

0 comments on commit aa9906b

Please sign in to comment.