mirror of https://github.com/apache/openjpa.git
OPENJPA-2662 get rid of serp.util.Strings.parse
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1759518 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c4323ac563
commit
10110aadf0
|
@ -42,7 +42,6 @@ import org.apache.openjpa.meta.ClassMetaData;
|
|||
import org.apache.openjpa.meta.JavaTypes;
|
||||
import org.apache.openjpa.util.InternalException;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
import serp.util.Strings;
|
||||
|
||||
/**
|
||||
* Helper methods for dealing with query filters.
|
||||
|
@ -935,8 +934,7 @@ public class Filters {
|
|||
/**
|
||||
* Set the value of the property named by the hint key.
|
||||
*/
|
||||
public static void hintToSetter(Object target, String hintKey,
|
||||
Object value) {
|
||||
public static void hintToSetter(Object target, String hintKey, Object value) {
|
||||
if (target == null || hintKey == null)
|
||||
return;
|
||||
|
||||
|
@ -946,7 +944,7 @@ public class Filters {
|
|||
value = null;
|
||||
else {
|
||||
try {
|
||||
value = Strings.parse((String) value, setter.getParameterTypes()[0]);
|
||||
value = StringUtil.parse((String) value, setter.getParameterTypes()[0]);
|
||||
} catch (Exception e) {
|
||||
throw new UserException(_loc.get("bad-setter-hint-arg",
|
||||
hintKey, value, setter.getParameterTypes()[0])).
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.apache.openjpa.kernel.Filters;
|
|||
import org.apache.openjpa.lib.util.Localizer;
|
||||
import org.apache.openjpa.lib.util.StringDistance;
|
||||
import org.apache.openjpa.lib.util.Localizer.Message;
|
||||
import org.apache.openjpa.lib.util.StringUtil;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.meta.FieldMetaData;
|
||||
import org.apache.openjpa.meta.JavaTypes;
|
||||
|
@ -37,7 +38,6 @@ import org.apache.openjpa.util.InternalException;
|
|||
import org.apache.openjpa.util.OpenJPAException;
|
||||
import org.apache.openjpa.util.UnsupportedException;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
import serp.util.Strings;
|
||||
|
||||
/**
|
||||
* Abstract base class to help build expressions. Provides
|
||||
|
@ -515,13 +515,13 @@ public abstract class AbstractExpressionBuilder {
|
|||
if (t1 == TYPE_STRING && val1 instanceof Literal
|
||||
&& ((Literal) val1).getParseType() == Literal.TYPE_STRING) {
|
||||
String s = (String) ((Literal) val1).getValue();
|
||||
((Literal) val1).setValue(Strings.parse(s, Filters.wrap(t2)));
|
||||
((Literal) val1).setValue(StringUtil.parse(s, Filters.wrap(t2)));
|
||||
val1.setImplicitType(t2);
|
||||
}
|
||||
if (t2 == TYPE_STRING && val2 instanceof Literal
|
||||
&& ((Literal) val2).getParseType() == Literal.TYPE_STRING) {
|
||||
String s = (String) ((Literal) val2).getValue();
|
||||
((Literal) val2).setValue(Strings.parse(s, Filters.wrap(t1)));
|
||||
((Literal) val2).setValue(StringUtil.parse(s, Filters.wrap(t1)));
|
||||
val2.setImplicitType(t1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,16 +21,26 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public final class StringUtil {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
|
||||
private static final Byte BYTE_ZERO = Byte.valueOf((byte) 0);
|
||||
private static final Character CHAR_ZERO = Character.valueOf((char) 0);
|
||||
private static final Double DOUBLE_ZERO = Double.valueOf(0.0d);
|
||||
private static final Float FLOAT_ZERO = Float.valueOf(0.0f);
|
||||
private static final Integer INTEGER_ZERO = Integer.valueOf(0);
|
||||
private static final Long LONG_ZERO = Long.valueOf(0);
|
||||
private static final Short SHORT_ZERO = Short.valueOf((short) 0);
|
||||
|
||||
private StringUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the given string on the given token. Follows the semantics
|
||||
* of the Java 1.4 {@link String#split(String,int)} method, but does
|
||||
* of the Java 1.4 {@link String#split(String, int)} method, but does
|
||||
* not treat the given token as a regular expression.
|
||||
*/
|
||||
public static String[] split(String str, String token, int max) {
|
||||
|
@ -57,7 +67,8 @@ public final class StringUtil {
|
|||
}
|
||||
if (start < len) {
|
||||
ret.add(str.substring(start));
|
||||
} else if (start == len) {
|
||||
}
|
||||
else if (start == len) {
|
||||
ret.add("");
|
||||
}
|
||||
|
||||
|
@ -71,14 +82,15 @@ public final class StringUtil {
|
|||
while (ret.get(--size).isEmpty()) {
|
||||
ret.remove(size);
|
||||
}
|
||||
} else if (max > 0 && ret.size() > max) {
|
||||
}
|
||||
else if (max > 0 && ret.size() > max) {
|
||||
// move all splits over max into the last split
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
sb.append(ret.get(max-1));
|
||||
ret.remove(max-1);
|
||||
sb.append(ret.get(max - 1));
|
||||
ret.remove(max - 1);
|
||||
while (ret.size() >= max) {
|
||||
sb.append(token).append(ret.get(max-1));
|
||||
ret.remove(max-1);
|
||||
sb.append(token).append(ret.get(max - 1));
|
||||
ret.remove(max - 1);
|
||||
}
|
||||
ret.add(sb.toString());
|
||||
}
|
||||
|
@ -89,9 +101,9 @@ public final class StringUtil {
|
|||
* Replace all instances of <code>from</code> in <code>str</code>
|
||||
* with <code>to</code>.
|
||||
*
|
||||
* @param str the candidate string to replace
|
||||
* @param str the candidate string to replace
|
||||
* @param from the token to replace
|
||||
* @param to the new token
|
||||
* @param to the new token
|
||||
* @return the string with all the replacements made
|
||||
*/
|
||||
public static String replace(String str, String from, String to) {
|
||||
|
@ -102,4 +114,83 @@ public final class StringUtil {
|
|||
return StringUtils.join(split, to);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given
|
||||
*
|
||||
* @param val value to parse
|
||||
* @param type the target type of the the parsed value
|
||||
* @return the converted value
|
||||
*/
|
||||
public static <T> T parse(String val, Class<T> type) {
|
||||
if (type == null) {
|
||||
throw new NullPointerException("target type must not be null");
|
||||
}
|
||||
|
||||
// handle primitives
|
||||
if (type == byte.class) {
|
||||
return (T) (val == null ? BYTE_ZERO : Byte.valueOf(val));
|
||||
}
|
||||
if (type == char.class) {
|
||||
return (T) (val == null ? CHAR_ZERO : parseCharString(val));
|
||||
}
|
||||
if (type == double.class) {
|
||||
return (T) (val == null ? DOUBLE_ZERO : Double.valueOf(val));
|
||||
}
|
||||
if (type == float.class) {
|
||||
return (T) (val == null ? FLOAT_ZERO : Float.valueOf(val));
|
||||
}
|
||||
if (type == int.class) {
|
||||
return (T) (val == null ? INTEGER_ZERO : Integer.valueOf(val));
|
||||
}
|
||||
if (type == long.class) {
|
||||
return (T) (val == null ? LONG_ZERO : Long.valueOf(val));
|
||||
}
|
||||
if (type == short.class) {
|
||||
return (T) (val == null ? SHORT_ZERO : Short.valueOf(val));
|
||||
}
|
||||
if (type == boolean.class) {
|
||||
return (T) (val == null ? Boolean.FALSE : Boolean.valueOf(val));
|
||||
}
|
||||
if (type == void.class) {
|
||||
throw new IllegalStateException("Cannot parse void type");
|
||||
}
|
||||
|
||||
// handle wrapper types
|
||||
if (type == Byte.class) {
|
||||
return (T) (val == null ? null : Byte.valueOf(val));
|
||||
}
|
||||
if (type == Character.class) {
|
||||
return (T) (val == null ? null : parseCharString(val));
|
||||
}
|
||||
if (type == Double.class) {
|
||||
return (T) (val == null ? null : Double.valueOf(val));
|
||||
}
|
||||
if (type == Float.class) {
|
||||
return (T) (val == null ? null : Float.valueOf(val));
|
||||
}
|
||||
if (type == Integer.class) {
|
||||
return (T) (val == null ? null : Integer.valueOf(val));
|
||||
}
|
||||
if (type == Long.class) {
|
||||
return (T) (val == null ? null : Long.valueOf(val));
|
||||
}
|
||||
if (type == Short.class) {
|
||||
return (T) (val == null ? null : Short.valueOf(val));
|
||||
}
|
||||
if (type == Boolean.class) {
|
||||
return (T) (val == null ? null : Boolean.valueOf(val));
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unsupported type: " + type.getCanonicalName());
|
||||
}
|
||||
|
||||
private static Character parseCharString(String val) {
|
||||
if (val.length() == 0) {
|
||||
return Character.valueOf((char) 0);
|
||||
}
|
||||
if (val.length() == 1) {
|
||||
return val.charAt(0);
|
||||
}
|
||||
throw new IllegalArgumentException("'" + val + "' is longer than one character.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,6 +57,87 @@ public class StringUtilTest {
|
|||
Assert.assertArrayEquals(ssplit, new String[]{"a", "B", "C", ""});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringParse() {
|
||||
|
||||
try {
|
||||
StringUtil.parse(null, null);
|
||||
Assert.fail("NullPointerException expected");
|
||||
}
|
||||
catch (NullPointerException npe) {
|
||||
// all fine
|
||||
}
|
||||
|
||||
|
||||
// test null representation for primitives
|
||||
Assert.assertEquals(0, (int) StringUtil.parse(null, byte.class));
|
||||
Assert.assertEquals(0, (char) StringUtil.parse(null, char.class));
|
||||
Assert.assertEquals(0, (double) StringUtil.parse(null, double.class), 0);
|
||||
Assert.assertEquals(0f, (float) StringUtil.parse(null, float.class), 0);
|
||||
Assert.assertEquals(0, (int) StringUtil.parse(null, int.class));
|
||||
Assert.assertEquals(0L, (long) StringUtil.parse(null, long.class));
|
||||
Assert.assertEquals(0, (short) StringUtil.parse(null, short.class));
|
||||
Assert.assertEquals(false, StringUtil.parse(null, boolean.class));
|
||||
|
||||
// special fun:
|
||||
try {
|
||||
StringUtil.parse(null, void.class);
|
||||
Assert.fail("IllegalStateException expected");
|
||||
}
|
||||
catch (IllegalStateException ise) {
|
||||
// all fine
|
||||
}
|
||||
|
||||
Assert.assertNull(StringUtil.parse(null, Character.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Double.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Float.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Integer.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Long.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Short.class));
|
||||
Assert.assertNull(StringUtil.parse(null, Boolean.class));
|
||||
|
||||
try {
|
||||
StringUtil.parse(null, char[].class);
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
// all fine
|
||||
}
|
||||
|
||||
Assert.assertEquals('C', (char) StringUtil.parse("C", char.class));
|
||||
Assert.assertEquals(35.2345, (double) StringUtil.parse("35.2345", double.class), 0.000001);
|
||||
Assert.assertEquals(35.2345, (float) StringUtil.parse("35.2345", float.class), 0.000001);
|
||||
Assert.assertEquals(42, (int) StringUtil.parse("42", int.class));
|
||||
Assert.assertEquals(42L, (long) StringUtil.parse("42", long.class));
|
||||
Assert.assertEquals(42, (short) StringUtil.parse("42", short.class));
|
||||
Assert.assertEquals(true, StringUtil.parse("true", boolean.class));
|
||||
Assert.assertEquals(true, StringUtil.parse("TRUE", boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("false", boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("FALSE", boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("bla", boolean.class));
|
||||
|
||||
Assert.assertEquals('C', (char) StringUtil.parse("C", Character.class));
|
||||
Assert.assertEquals(35.2345, (double) StringUtil.parse("35.2345", Double.class), 0.000001);
|
||||
Assert.assertEquals(35.2345, (float) StringUtil.parse("35.2345", Float.class), 0.000001);
|
||||
Assert.assertEquals(42, (int) StringUtil.parse("42", Integer.class));
|
||||
Assert.assertEquals(42L, (long) StringUtil.parse("42", Long.class));
|
||||
Assert.assertEquals(42, (short) StringUtil.parse("42", Short.class));
|
||||
Assert.assertEquals(true, StringUtil.parse("true", Boolean.class));
|
||||
Assert.assertEquals(true, StringUtil.parse("TRUE", Boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("false", Boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("FALSE", Boolean.class));
|
||||
Assert.assertEquals(false, StringUtil.parse("bla", Boolean.class));
|
||||
|
||||
try {
|
||||
StringUtil.parse(null, StringUtilTest.class);
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
// all fine
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("only needed for manual performance tests")
|
||||
public void stringSplitPerformanceTest() {
|
||||
|
@ -103,4 +184,4 @@ public class StringUtilTest {
|
|||
long stop = System.nanoTime();
|
||||
System.out.println("took: " + TimeUnit.NANOSECONDS.toMillis(stop - start));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -175,7 +175,6 @@ import org.apache.openjpa.util.MetaDataException;
|
|||
import org.apache.openjpa.util.UnsupportedException;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
|
||||
import serp.util.Strings;
|
||||
|
||||
/**
|
||||
* Persistence annotation metadata parser. Currently does not parse
|
||||
|
|
|
@ -82,6 +82,7 @@ import org.apache.openjpa.kernel.jpql.JPQLParser;
|
|||
import org.apache.openjpa.lib.log.Log;
|
||||
import org.apache.openjpa.lib.util.Closeable;
|
||||
import org.apache.openjpa.lib.util.Localizer;
|
||||
import org.apache.openjpa.lib.util.StringUtil;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.meta.FieldMetaData;
|
||||
import org.apache.openjpa.meta.MetaDataRepository;
|
||||
|
@ -99,7 +100,6 @@ import org.apache.openjpa.util.ImplHelper;
|
|||
import org.apache.openjpa.util.RuntimeExceptionTranslator;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
|
||||
import serp.util.Strings;
|
||||
|
||||
/**
|
||||
* Implementation of {@link EntityManager} interface.
|
||||
|
@ -1980,7 +1980,7 @@ public class EntityManagerImpl
|
|||
if (!String.class.equals(targetType) && (parenIndex > 0)) {
|
||||
val = val.substring(0, parenIndex);
|
||||
}
|
||||
return Strings.parse(val, targetType);
|
||||
return StringUtil.parse(val, targetType);
|
||||
}
|
||||
} else if (value instanceof AutoDetachType) {
|
||||
EnumSet<AutoDetachType> autoDetachFlags = EnumSet.noneOf(AutoDetachType.class);
|
||||
|
|
Loading…
Reference in New Issue