00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.util;
00023
00024 import java.io.IOException;
00025 import org.jetbrains.annotations.NotNull;
00026
00031 public class NumberParser {
00032
00036 private NumberParser() {
00037 }
00038
00045 public static int parseInt(@NotNull final String string, final int defaultValue) {
00046 try {
00047 return Integer.parseInt(string);
00048 } catch (final NumberFormatException ignored) {
00049 System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
00050 return defaultValue;
00051 }
00052 }
00053
00063 public static int parseInt(@NotNull final String string, final int defaultValue, final int minValue, final int maxValue) {
00064 final int value = parseInt(string, defaultValue);
00065 if (value < minValue || value > maxValue) {
00066 System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
00067 return defaultValue;
00068 }
00069
00070 return value;
00071 }
00072
00079 public static long parseLong(@NotNull final String string, final long defaultValue) {
00080 try {
00081 return Long.parseLong(string);
00082 } catch (final NumberFormatException ignored) {
00083 System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
00084 return defaultValue;
00085 }
00086 }
00087
00094 public static float parseFloat(@NotNull final String str) throws IOException {
00095 try {
00096 return Float.parseFloat(str);
00097 } catch (final NumberFormatException ex) {
00098 throw new IOException("invalid number: "+str, ex);
00099 }
00100 }
00101
00108 public static boolean parseBoolean(@NotNull final String str) throws IOException {
00109 try {
00110 return Boolean.parseBoolean(str);
00111 } catch (final NumberFormatException ex) {
00112 throw new IOException("invalid boolean: "+str, ex);
00113 }
00114 }
00115
00125 @NotNull
00126 public static <T extends Enum<T>> T parseEnum(@NotNull final Class<T> class_, @NotNull final String name, @NotNull final String ident) throws IOException {
00127 try {
00128 return Enum.valueOf(class_, name);
00129 } catch (final IllegalArgumentException ex) {
00130 throw new IOException("no such "+ident+" type: "+name, ex);
00131 }
00132 }
00133
00134 }