Skip to content

Commit

Permalink
parsing primitive values in a try/catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhat committed Nov 9, 2014
1 parent 8859c6d commit 5317a7f
Showing 1 changed file with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,70 @@ public static boolean isHex(String s) {
}

public static byte parseByte(String s) {
if (isHex(s)) {
return Byte.parseByte(s.substring(2), 16);
} else {
return Byte.parseByte(s);
try {
if (isHex(s)) {
return Byte.parseByte(s.substring(2), 16);
} else {
return Byte.parseByte(s);
}
}catch (Exception e){
return 0;
}
}

public static int parseInt(String s) {
if (isHex(s)) {
return Integer.parseInt(s.substring(2), 16);
} else {
return Integer.parseInt(s);
try {
if (isHex(s)) {
return Integer.parseInt(s.substring(2), 16);
} else {
return Integer.parseInt(s);
}
}catch (Exception e){
return 0;
}
}

public static short parseShort(String s) {
if (isHex(s)) {
return Short.parseShort(s.substring(2), 16);
} else {
return Short.parseShort(s);
try {
if (isHex(s)) {
return Short.parseShort(s.substring(2), 16);
} else {
return Short.parseShort(s);
}
}catch (Exception e){
return 0;
}
}

public static long parseLong(String s) {
if (isHex(s)) {
return Long.parseLong(s.substring(2), 16);
} else {
return Long.parseLong(s);
try {
if (isHex(s)) {
return Long.parseLong(s.substring(2), 16);
} else {
return Long.parseLong(s);
}
}catch (Exception e){
return 0;
}
}

public static Timestamp parseTimestamp(String s) {
Timestamp value;
if (s.indexOf(':') > 0) {
value = Timestamp.valueOf(s);
} else if (s.indexOf('.') >= 0) {
// it's a float
value = new Timestamp(
(long) ((double) (Double.parseDouble(s) * 1000)));
} else {
// integer
value = new Timestamp(Long.parseLong(s) * 1000);
try {
Timestamp value;
if (s.indexOf(':') > 0) {
value = Timestamp.valueOf(s);
} else if (s.indexOf('.') >= 0) {
// it's a float
value = new Timestamp(
(long) ((double) (Double.parseDouble(s) * 1000)));
} else {
// integer
value = new Timestamp(Long.parseLong(s) * 1000);
}
return value;
}catch (Exception e){
return null;
}
return value;
}

}

0 comments on commit 5317a7f

Please sign in to comment.