Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.joda.convert.JDKStringConverter Maven / Gradle / Ivy
package org.joda.convert;
import java.io.File ;
import java.math.BigDecimal ;
import java.math.BigInteger ;
import java.net.InetAddress ;
import java.net.MalformedURLException ;
import java.net.URI ;
import java.net.URL ;
import java.net.UnknownHostException ;
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.Calendar ;
import java.util.Currency ;
import java.util.Date ;
import java.util.GregorianCalendar ;
import java.util.Locale ;
import java.util.TimeZone ;
import java.util.UUID ;
import java.util.concurrent.atomic.AtomicBoolean ;
import java.util.concurrent.atomic.AtomicInteger ;
import java.util.concurrent.atomic.AtomicLong ;
import javax.xml.bind.DatatypeConverter ;
enum JDKStringConverter implements StringConverter {
STRING (String .class ) {
public Object convertFromString(Class cls, String str) {
return str;
}
},
CHAR_SEQUENCE (CharSequence .class ) {
public Object convertFromString(Class cls, String str) {
return str;
}
},
STRING_BUFFER (StringBuffer .class ) {
public Object convertFromString(Class cls, String str) {
return new StringBuffer (str);
}
},
STRING_BUILDER (StringBuilder .class ) {
public Object convertFromString(Class cls, String str) {
return new StringBuilder (str);
}
},
LONG (Long .class ) {
public Object convertFromString(Class cls, String str) {
return new Long (str);
}
},
INTEGER (Integer .class ) {
public Object convertFromString(Class cls, String str) {
return new Integer (str);
}
},
SHORT (Short .class ) {
public Object convertFromString(Class cls, String str) {
return new Short (str);
}
},
BYTE (Byte .class ) {
public Object convertFromString(Class cls, String str) {
return new Byte (str);
}
},
BYTE_ARRAY (byte[].class ) {
@Override
public String convertToString(Object object ) {
return DatatypeConverter .printBase64Binary((byte[]) object ) ;
}
public Object convertFromString(Class cls, String str) {
return DatatypeConverter .parseBase64Binary(str);
}
},
CHARACTER (Character .class ) {
public Object convertFromString(Class cls, String str) {
if (str.length() != 1 ) {
throw new IllegalArgumentException ("Character value must be a string length 1" );
}
return new Character (str.charAt(0 ));
}
},
CHAR_ARRAY (char[].class ) {
@Override
public String convertToString(Object object ) {
return new String ((char[]) object ) ;
}
public Object convertFromString(Class cls, String str) {
return str.toCharArray();
}
},
BOOLEAN (Boolean .class ) {
public Object convertFromString(Class cls, String str) {
if ("true" .equalsIgnoreCase(str)) {
return Boolean .TRUE ;
}
if ("false" .equalsIgnoreCase(str)) {
return Boolean .FALSE ;
}
throw new IllegalArgumentException ("Boolean value must be 'true' or 'false', case insensitive" );
}
},
DOUBLE (Double .class ) {
public Object convertFromString(Class cls, String str) {
return new Double (str);
}
},
FLOAT (Float .class ) {
public Object convertFromString(Class cls, String str) {
return new Float (str);
}
},
BIG_INTEGER (BigInteger .class ) {
public Object convertFromString(Class cls, String str) {
return new BigInteger (str);
}
},
BIG_DECIMAL (BigDecimal .class ) {
public Object convertFromString(Class cls, String str) {
return new BigDecimal (str);
}
},
ATOMIC_LONG (AtomicLong .class ) {
public Object convertFromString(Class cls, String str) {
long val = Long .parseLong(str);
return new AtomicLong (val );
}
},
ATOMIC_INTEGER (AtomicInteger .class ) {
public Object convertFromString(Class cls, String str) {
int val = Integer .parseInt(str);
return new AtomicInteger (val );
}
},
ATOMIC_BOOLEAN (AtomicBoolean .class ) {
public Object convertFromString(Class cls, String str) {
if ("true" .equalsIgnoreCase(str)) {
return new AtomicBoolean (true );
}
if ("false" .equalsIgnoreCase(str)) {
return new AtomicBoolean (false );
}
throw new IllegalArgumentException ("Boolean value must be 'true' or 'false', case insensitive" );
}
},
LOCALE (Locale .class ) {
public Object convertFromString(Class cls, String str) {
String [] split = str.split("_" , 3 );
switch (split.length) {
case 1 :
return new Locale (split[0 ]);
case 2 :
return new Locale (split[0 ], split[1 ]);
case 3 :
return new Locale (split[0 ], split[1 ], split[2 ]);
}
throw new IllegalArgumentException ("Unable to parse Locale: " + str);
}
},
CLASS (Class .class ) {
@Override
public String convertToString(Object object ) {
return ((Class ) object ).getName ( ) ;
}
public Object convertFromString(Class cls, String str) {
try {
return RenameHandler .INSTANCE .lookupType(str);
} catch (ClassNotFoundException ex) {
throw new RuntimeException ("Unable to create type: " + str, ex);
}
}
},
PACKAGE (Package .class ) {
@Override
public String convertToString(Object object ) {
return ((Package ) object ).getName ( ) ;
}
public Object convertFromString(Class cls, String str) {
return Package .getPackage(str);
}
},
CURRENCY (Currency .class ) {
public Object convertFromString(Class cls, String str) {
return Currency .getInstance(str);
}
},
TIME_ZONE (TimeZone .class ) {
@Override
public String convertToString(Object object ) {
return ((TimeZone ) object ).getID ( ) ;
}
public Object convertFromString(Class cls, String str) {
return TimeZone .getTimeZone(str);
}
},
UUID (UUID .class ) {
public Object convertFromString(Class cls, String str) {
return java.util.UUID .fromString(str);
}
},
URL (URL .class ) {
public Object convertFromString(Class cls, String str) {
try {
return new URL (str);
} catch (MalformedURLException ex) {
throw new RuntimeException (ex.getMessage(), ex);
}
}
},
URI (URI .class ) {
public Object convertFromString(Class cls, String str) {
return java.net.URI .create(str);
}
},
INET_ADDRESS (InetAddress .class ) {
@Override
public String convertToString(Object object ) {
return ((InetAddress ) object ).getHostAddress ( ) ;
}
public Object convertFromString(Class cls, String str) {
try {
return InetAddress .getByName(str);
} catch (UnknownHostException ex) {
throw new RuntimeException (ex);
}
}
},
FILE (File .class ) {
public Object convertFromString(Class cls, String str) {
return new File (str);
}
},
DATE (Date .class ) {
@Override
public String convertToString(Object object ) {
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
String str = f.format(object ) ;
return str.substring(0 , 26 ) + ":" + str.substring(26 );
}
public Object convertFromString(Class cls, String str) {
if (str.length() != 29 ) {
throw new IllegalArgumentException ("Unable to parse date: " + str);
}
str = str.substring(0 , 26 ) + str.substring(27 );
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
try {
return f.parseObject(str);
} catch (ParseException ex) {
throw new RuntimeException (ex);
}
}
},
CALENDAR (Calendar .class ) {
@Override
public String convertToString(Object object ) {
if (object instanceof GregorianCalendar == false ) {
throw new RuntimeException ("Unable to convert calendar as it is not a GregorianCalendar" );
}
GregorianCalendar cal = (GregorianCalendar ) object ;
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
f.setCalendar(cal);
String str = f.format(cal.getTime());
return str.substring(0 , 26 ) + ":" + str.substring(26 ) + "[" + cal.getTimeZone().getID() + "]" ;
}
public Object convertFromString(Class cls, String str) {
if (str.length() < 31 || str.charAt(26 ) != ':'
|| str.charAt(29 ) != '[' || str.charAt(str.length() - 1 ) != ']') {
throw new IllegalArgumentException ("Unable to parse date: " + str);
}
TimeZone zone = TimeZone .getTimeZone(str.substring(30 , str.length() - 1 ));
str = str.substring(0 , 26 ) + str.substring(27 , 29 );
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
GregorianCalendar cal = new GregorianCalendar (zone);
cal.setTimeInMillis(0 );
f.setCalendar(cal);
try {
f.parseObject(str);
return f.getCalendar();
} catch (ParseException ex) {
throw new RuntimeException (ex);
}
}
},
ENUM (Enum .class ) {
@SuppressWarnings ("rawtypes" )
public String convertToString(Object object ) {
return ((Enum ) object ).name ( ) ;
}
@SuppressWarnings ({ "unchecked" , "rawtypes" })
public Object convertFromString(Class cls, String str) {
return RenameHandler .INSTANCE .lookupEnum(cls, str);
}
},
;
private Class type ;
private JDKStringConverter (Class type ) {
this .type = type ;
}
Class getType() {
return type ;
}
public String convertToString(Object object ) {
return object .toString ( ) ;
}
}