Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.util;
24 
25 import java.io.IOException;
26 import org.jetbrains.annotations.NotNull;
27 
32 public class NumberParser {
33 
37  private NumberParser() {
38  }
39 
46  public static int parseInt(@NotNull final String string, final int defaultValue) {
47  try {
48  return Integer.parseInt(string);
49  } catch (final NumberFormatException ignored) {
50  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
51  return defaultValue;
52  }
53  }
54 
64  public static int parseInt(@NotNull final String string, final int defaultValue, final int minValue, final int maxValue) {
65  final int value = parseInt(string, defaultValue);
66  if (value < minValue || value > maxValue) {
67  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
68  return defaultValue;
69  }
70 
71  return value;
72  }
73 
80  public static long parseLong(@NotNull final String string, final long defaultValue) {
81  try {
82  return Long.parseLong(string);
83  } catch (final NumberFormatException ignored) {
84  System.err.println("Warning: invalid value "+string+", using "+defaultValue+" instead.");
85  return defaultValue;
86  }
87  }
88 
95  public static float parseFloat(@NotNull final String str) throws IOException {
96  try {
97  return Float.parseFloat(str);
98  } catch (final NumberFormatException ex) {
99  throw new IOException("invalid number: "+str, ex);
100  }
101  }
102 
109  public static boolean parseBoolean(@NotNull final String str) throws IOException {
110  try {
111  return Boolean.parseBoolean(str);
112  } catch (final NumberFormatException ex) {
113  throw new IOException("invalid boolean: "+str, ex);
114  }
115  }
116 
127  @NotNull
128  public static <T extends Enum<T>> T parseEnum(@NotNull final Class<T> class_, @NotNull final String name, @NotNull final String ident) throws IOException {
129  try {
130  return Enum.valueOf(class_, name);
131  } catch (final IllegalArgumentException ex) {
132  throw new IOException("no such "+ident+" type: "+name, ex);
133  }
134  }
135 
136 }
com.realtime.crossfire.jxclient.util.NumberParser.parseLong
static long parseLong(@NotNull final String string, final long defaultValue)
Definition: NumberParser.java:80
com.realtime.crossfire.jxclient.util.NumberParser.parseInt
static int parseInt(@NotNull final String string, final int defaultValue, final int minValue, final int maxValue)
Definition: NumberParser.java:64
com.realtime.crossfire.jxclient.util.NumberParser.parseFloat
static float parseFloat(@NotNull final String str)
Definition: NumberParser.java:95
com.realtime.crossfire.jxclient.util.NumberParser.parseEnum
static< T extends Enum< T > T parseEnum(@NotNull final Class< T > class_, @NotNull final String name, @NotNull final String ident)
Definition: NumberParser.java:128
com.realtime.crossfire.jxclient.util.NumberParser.parseInt
static int parseInt(@NotNull final String string, final int defaultValue)
Definition: NumberParser.java:46
com.realtime.crossfire.jxclient.util.NumberParser.parseBoolean
static boolean parseBoolean(@NotNull final String str)
Definition: NumberParser.java:109
com.realtime.crossfire.jxclient.util.NumberParser.NumberParser
NumberParser()
Definition: NumberParser.java:37
com.realtime.crossfire.jxclient.util.NumberParser
Definition: NumberParser.java:32