Crossfire JXClient, Trunk  R20561
NumberParser.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import java.io.IOException;
25 import org.jetbrains.annotations.NotNull;
26 
31 public class NumberParser {
32 
36  private NumberParser() {
37  }
38 
45  public static int parseInt(@NotNull final String string, final int defaultValue) {
46  try {
47  return Integer.parseInt(string);
48  } catch (final NumberFormatException ignored) {
49  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
50  return defaultValue;
51  }
52  }
53 
63  public static int parseInt(@NotNull final String string, final int defaultValue, final int minValue, final int maxValue) {
64  final int value = parseInt(string, defaultValue);
65  if (value < minValue || value > maxValue) {
66  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
67  return defaultValue;
68  }
69 
70  return value;
71  }
72 
79  public static long parseLong(@NotNull final String string, final long defaultValue) {
80  try {
81  return Long.parseLong(string);
82  } catch (final NumberFormatException ignored) {
83  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
84  return defaultValue;
85  }
86  }
87 
94  public static float parseFloat(@NotNull final String str) throws IOException {
95  try {
96  return Float.parseFloat(str);
97  } catch (final NumberFormatException ex) {
98  throw new IOException("invalid number: "+str, ex);
99  }
100  }
101 
108  public static boolean parseBoolean(@NotNull final String str) throws IOException {
109  try {
110  return Boolean.parseBoolean(str);
111  } catch (final NumberFormatException ex) {
112  throw new IOException("invalid boolean: "+str, ex);
113  }
114  }
115 
126  @NotNull
127  public static <T extends Enum<T>> T parseEnum(@NotNull final Class<T> class_, @NotNull final String name, @NotNull final String ident) throws IOException {
128  try {
129  return Enum.valueOf(class_, name);
130  } catch (final IllegalArgumentException ex) {
131  throw new IOException("no such "+ident+" type: "+name, ex);
132  }
133  }
134 
135 }
static< T extends Enum< T > T parseEnum(@NotNull final Class< T > class_, @NotNull final String name, @NotNull final String ident)
Parses an enum constant.
static long parseLong(@NotNull final String string, final long defaultValue)
Converts a string into a long value.
static float parseFloat(@NotNull final String str)
Parses a float constant.
static boolean parseBoolean(@NotNull final String str)
Parses a boolean constant.
static int parseInt(@NotNull final String string, final int defaultValue)
Converts a string into an int value.
Utility class for parsing strings into numbers.
static int parseInt(@NotNull final String string, final int defaultValue, final int minValue, final int maxValue)
Converts a string into an int value in the given bounds.
NumberParser()
Private constructor to prevent instantiation.