From: <aki...@us...> - 2010-05-26 21:35:16
|
Revision: 8014 http://gridarta.svn.sourceforge.net/gridarta/?rev=8014&view=rev Author: akirschbaum Date: 2010-05-26 21:35:09 +0000 (Wed, 26 May 2010) Log Message: ----------- Extract duplicated code into NumberUtils. Modified Paths: -------------- trunk/atrinik/src/app/net/sf/gridarta/var/atrinik/model/io/MapArchObjectParser.java trunk/crossfire/src/app/net/sf/gridarta/var/crossfire/model/io/MapArchObjectParser.java trunk/daimonin/src/app/net/sf/gridarta/var/daimonin/model/io/MapArchObjectParser.java trunk/src/app/net/sf/gridarta/gui/newmap/AbstractMapsizeNewMapDialog.java trunk/src/app/net/sf/gridarta/gui/newmap/NewMapDialog.java trunk/src/app/net/sf/gridarta/mainactions/MainActions.java trunk/src/app/net/sf/gridarta/model/archetypetype/AttributeBitmask.java trunk/src/app/net/sf/gridarta/model/baseobject/AbstractBaseObject.java trunk/src/app/net/sf/gridarta/model/io/AbstractMapArchObjectParser.java trunk/src/app/net/sf/gridarta/script/parameter/DoubleParameter.java trunk/src/app/net/sf/gridarta/utils/ActionBuilderUtils.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/utils/NumberUtils.java Modified: trunk/atrinik/src/app/net/sf/gridarta/var/atrinik/model/io/MapArchObjectParser.java =================================================================== --- trunk/atrinik/src/app/net/sf/gridarta/var/atrinik/model/io/MapArchObjectParser.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/atrinik/src/app/net/sf/gridarta/var/atrinik/model/io/MapArchObjectParser.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Formatter; import net.sf.gridarta.model.io.AbstractMapArchObjectParser; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.var.atrinik.model.maparchobject.MapArchObject; import org.jetbrains.annotations.NotNull; @@ -129,72 +130,72 @@ @Override protected boolean parseLine(@NotNull final String line, @NotNull final MapArchObject mapArchObject) { if (line.startsWith("no_save ")) { - mapArchObject.setNoSave(parseInteger(line.substring(8)) != 0); + mapArchObject.setNoSave(NumberUtils.parseInt(line.substring(8)) != 0); return true; } if (line.startsWith("no_magic ")) { - mapArchObject.setNoMagic(parseInteger(line.substring(9)) != 0); + mapArchObject.setNoMagic(NumberUtils.parseInt(line.substring(9)) != 0); return true; } if (line.startsWith("no_priest ")) { - mapArchObject.setNoPriest(parseInteger(line.substring(10)) != 0); + mapArchObject.setNoPriest(NumberUtils.parseInt(line.substring(10)) != 0); return true; } if (line.startsWith("no_summon ")) { - mapArchObject.setNoSummon(parseInteger(line.substring(10)) != 0); + mapArchObject.setNoSummon(NumberUtils.parseInt(line.substring(10)) != 0); return true; } if (line.startsWith("no_harm ")) { - mapArchObject.setNoHarm(parseInteger(line.substring(8)) != 0); + mapArchObject.setNoHarm(NumberUtils.parseInt(line.substring(8)) != 0); return true; } if (line.startsWith("fixed_login ")) { - mapArchObject.setFixedLogin(parseInteger(line.substring(12)) != 0); + mapArchObject.setFixedLogin(NumberUtils.parseInt(line.substring(12)) != 0); return true; } if (line.startsWith("perm_death ")) { - mapArchObject.setPermDeath(parseInteger(line.substring(11)) != 0); + mapArchObject.setPermDeath(NumberUtils.parseInt(line.substring(11)) != 0); return true; } if (line.startsWith("ultra_death ")) { - mapArchObject.setUltraDeath(parseInteger(line.substring(12)) != 0); + mapArchObject.setUltraDeath(NumberUtils.parseInt(line.substring(12)) != 0); return true; } if (line.startsWith("ultimate_death ")) { - mapArchObject.setUltimateDeath(parseInteger(line.substring(15)) != 0); + mapArchObject.setUltimateDeath(NumberUtils.parseInt(line.substring(15)) != 0); return true; } if (line.startsWith("pvp ")) { - mapArchObject.setPvp(parseInteger(line.substring(4)) != 0); + mapArchObject.setPvp(NumberUtils.parseInt(line.substring(4)) != 0); return true; } if (line.startsWith("plugins ")) { - mapArchObject.setPlugins(parseInteger(line.substring(8)) != 0); + mapArchObject.setPlugins(NumberUtils.parseInt(line.substring(8)) != 0); return true; } if (line.startsWith("tileset_id ")) { - mapArchObject.setTilesetId(parseInteger(line.substring(11))); + mapArchObject.setTilesetId(NumberUtils.parseInt(line.substring(11))); return true; } if (line.startsWith("tileset_x ")) { - mapArchObject.setTilesetX(parseInteger(line.substring(10))); + mapArchObject.setTilesetX(NumberUtils.parseInt(line.substring(10))); return true; } if (line.startsWith("tileset_y ")) { - mapArchObject.setTilesetY(parseInteger(line.substring(10))); + mapArchObject.setTilesetY(NumberUtils.parseInt(line.substring(10))); return true; } Modified: trunk/crossfire/src/app/net/sf/gridarta/var/crossfire/model/io/MapArchObjectParser.java =================================================================== --- trunk/crossfire/src/app/net/sf/gridarta/var/crossfire/model/io/MapArchObjectParser.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/crossfire/src/app/net/sf/gridarta/var/crossfire/model/io/MapArchObjectParser.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -23,6 +23,7 @@ import java.io.IOException; import java.util.Formatter; import net.sf.gridarta.model.io.AbstractMapArchObjectParser; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.var.crossfire.model.maparchobject.MapArchObject; import org.jetbrains.annotations.NotNull; @@ -170,17 +171,17 @@ } if (line.startsWith("unique ")) { - mapArchObject.setUnique(parseInteger(line.substring(7)) != 0); + mapArchObject.setUnique(NumberUtils.parseInt(line.substring(7)) != 0); return true; } if (line.startsWith("template ")) { - mapArchObject.setTemplate(parseInteger(line.substring(9)) != 0); + mapArchObject.setTemplate(NumberUtils.parseInt(line.substring(9)) != 0); return true; } if (line.startsWith("nosmooth ")) { - mapArchObject.setNosmooth(parseInteger(line.substring(9)) != 0); + mapArchObject.setNosmooth(NumberUtils.parseInt(line.substring(9)) != 0); return true; } @@ -195,47 +196,47 @@ } if (line.startsWith("shopmin ")) { - mapArchObject.setShopMin(parseInteger(line.substring(8))); + mapArchObject.setShopMin(NumberUtils.parseInt(line.substring(8))); return true; } if (line.startsWith("shopmax ")) { - mapArchObject.setShopMax(parseInteger(line.substring(8))); + mapArchObject.setShopMax(NumberUtils.parseInt(line.substring(8))); return true; } if (line.startsWith("shopgreed ")) { - mapArchObject.setShopGreed(parseDouble(line.substring(10))); + mapArchObject.setShopGreed(NumberUtils.parseDouble(line.substring(10))); return true; } if (line.startsWith("temp ")) { - mapArchObject.setTemp(parseInteger(line.substring(5))); + mapArchObject.setTemp(NumberUtils.parseInt(line.substring(5))); return true; } if (line.startsWith("pressure ")) { - mapArchObject.setPressure(parseInteger(line.substring(9))); + mapArchObject.setPressure(NumberUtils.parseInt(line.substring(9))); return true; } if (line.startsWith("humid ")) { - mapArchObject.setHumid(parseInteger(line.substring(6))); + mapArchObject.setHumid(NumberUtils.parseInt(line.substring(6))); return true; } if (line.startsWith("windspeed ")) { - mapArchObject.setWindspeed(parseInteger(line.substring(10))); + mapArchObject.setWindspeed(NumberUtils.parseInt(line.substring(10))); return true; } if (line.startsWith("winddir ")) { - mapArchObject.setWinddir(parseInteger(line.substring(8))); + mapArchObject.setWinddir(NumberUtils.parseInt(line.substring(8))); return true; } if (line.startsWith("sky ")) { - mapArchObject.setSky(parseInteger(line.substring(4))); + mapArchObject.setSky(NumberUtils.parseInt(line.substring(4))); return true; } Modified: trunk/daimonin/src/app/net/sf/gridarta/var/daimonin/model/io/MapArchObjectParser.java =================================================================== --- trunk/daimonin/src/app/net/sf/gridarta/var/daimonin/model/io/MapArchObjectParser.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/daimonin/src/app/net/sf/gridarta/var/daimonin/model/io/MapArchObjectParser.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Formatter; import net.sf.gridarta.model.io.AbstractMapArchObjectParser; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.var.daimonin.model.maparchobject.MapArchObject; import org.jetbrains.annotations.NotNull; @@ -126,67 +127,67 @@ @Override protected boolean parseLine(@NotNull final String line, @NotNull final MapArchObject mapArchObject) { if (line.startsWith("no_save ")) { - mapArchObject.setNoSave(parseInteger(line.substring(8)) != 0); + mapArchObject.setNoSave(NumberUtils.parseInt(line.substring(8)) != 0); return true; } if (line.startsWith("no_magic ")) { - mapArchObject.setNoMagic(parseInteger(line.substring(9)) != 0); + mapArchObject.setNoMagic(NumberUtils.parseInt(line.substring(9)) != 0); return true; } if (line.startsWith("no_priest ")) { - mapArchObject.setNoPriest(parseInteger(line.substring(10)) != 0); + mapArchObject.setNoPriest(NumberUtils.parseInt(line.substring(10)) != 0); return true; } if (line.startsWith("no_summon ")) { - mapArchObject.setNoSummon(parseInteger(line.substring(10)) != 0); + mapArchObject.setNoSummon(NumberUtils.parseInt(line.substring(10)) != 0); return true; } if (line.startsWith("no_harm ")) { - mapArchObject.setNoHarm(parseInteger(line.substring(8)) != 0); + mapArchObject.setNoHarm(NumberUtils.parseInt(line.substring(8)) != 0); return true; } if (line.startsWith("fixed_login ")) { - mapArchObject.setFixedLogin(parseInteger(line.substring(12)) != 0); + mapArchObject.setFixedLogin(NumberUtils.parseInt(line.substring(12)) != 0); return true; } if (line.startsWith("perm_death ")) { - mapArchObject.setPermDeath(parseInteger(line.substring(11)) != 0); + mapArchObject.setPermDeath(NumberUtils.parseInt(line.substring(11)) != 0); return true; } if (line.startsWith("ultra_death ")) { - mapArchObject.setUltraDeath(parseInteger(line.substring(12)) != 0); + mapArchObject.setUltraDeath(NumberUtils.parseInt(line.substring(12)) != 0); return true; } if (line.startsWith("ultimate_death ")) { - mapArchObject.setUltimateDeath(parseInteger(line.substring(15)) != 0); + mapArchObject.setUltimateDeath(NumberUtils.parseInt(line.substring(15)) != 0); return true; } if (line.startsWith("pvp ")) { - mapArchObject.setPvp(parseInteger(line.substring(4)) != 0); + mapArchObject.setPvp(NumberUtils.parseInt(line.substring(4)) != 0); return true; } if (line.startsWith("tileset_id ")) { - mapArchObject.setTilesetId(parseInteger(line.substring(11))); + mapArchObject.setTilesetId(NumberUtils.parseInt(line.substring(11))); return true; } if (line.startsWith("tileset_x ")) { - mapArchObject.setTilesetX(parseInteger(line.substring(10))); + mapArchObject.setTilesetX(NumberUtils.parseInt(line.substring(10))); return true; } if (line.startsWith("tileset_y ")) { - mapArchObject.setTilesetY(parseInteger(line.substring(10))); + mapArchObject.setTilesetY(NumberUtils.parseInt(line.substring(10))); return true; } Modified: trunk/src/app/net/sf/gridarta/gui/newmap/AbstractMapsizeNewMapDialog.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/newmap/AbstractMapsizeNewMapDialog.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/gui/newmap/AbstractMapsizeNewMapDialog.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -28,6 +28,7 @@ import net.sf.gridarta.model.archetype.Archetype; import net.sf.gridarta.model.gameobject.GameObject; import net.sf.gridarta.model.map.maparchobject.MapArchObject; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.Size2D; import net.sf.japi.swing.action.ActionBuilder; import net.sf.japi.swing.action.ActionBuilderFactory; @@ -240,17 +241,9 @@ */ private int getMapWidth() { final String text = mapWidthField.getText(); - final int width; - try { - width = Integer.parseInt(text); - } catch (final NumberFormatException ignored) { - return -1; - } - if (width < 1) { - return -1; - } + final int width = NumberUtils.parseInt(text); + return width < 1 ? -1 : width; - return width; } /** @@ -259,17 +252,9 @@ */ private int getMapHeight() { final String text = mapHeightField.getText(); - final int height; - try { - height = Integer.parseInt(text); - } catch (final NumberFormatException ignored) { - return -1; - } - if (height < 1) { - return -1; - } + final int height = NumberUtils.parseInt(text); + return height < 1 ? -1 : height; - return height; } } // class AbstractMapsizeNewMapDialog Modified: trunk/src/app/net/sf/gridarta/gui/newmap/NewMapDialog.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/newmap/NewMapDialog.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/gui/newmap/NewMapDialog.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -31,6 +31,7 @@ import net.sf.gridarta.model.gameobject.GameObject; import net.sf.gridarta.model.map.maparchobject.MapArchObject; import net.sf.gridarta.model.map.maparchobject.MapArchObjectFactory; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.Size2D; import net.sf.japi.swing.action.ActionBuilder; import net.sf.japi.swing.action.ActionBuilderFactory; @@ -217,12 +218,7 @@ */ private int getDifficulty() { final String text = mapDifficultyField.getText(); - final int difficulty; - try { - difficulty = Integer.parseInt(text); - } catch (final NumberFormatException ignored) { - return -1; - } + final int difficulty = NumberUtils.parseInt(text); return difficulty >= 1 ? difficulty : -1; } Modified: trunk/src/app/net/sf/gridarta/mainactions/MainActions.java =================================================================== --- trunk/src/app/net/sf/gridarta/mainactions/MainActions.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/mainactions/MainActions.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -69,6 +69,7 @@ import net.sf.gridarta.utils.ActionUtils; import net.sf.gridarta.utils.Exiter; import net.sf.gridarta.utils.ExiterListener; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.Size2D; import net.sf.gridarta.validation.DelegatingMapValidator; import net.sf.japi.swing.action.ActionBuilder; @@ -1543,12 +1544,7 @@ return -1; } - int rand; - try { - rand = Integer.parseInt(input); - } catch (final NumberFormatException ignored) { - rand = -1; - } + final int rand = NumberUtils.parseInt(input); if (rand < 1 || rand > 100) { JOptionPane.showMessageDialog(parent, "The fill density must be between 1-100.", "Illegal Value.", JOptionPane.ERROR_MESSAGE); } else { Modified: trunk/src/app/net/sf/gridarta/model/archetypetype/AttributeBitmask.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/archetypetype/AttributeBitmask.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/model/archetypetype/AttributeBitmask.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.WrappingStringBuilder; import org.apache.log4j.Category; import org.apache.log4j.Logger; @@ -199,11 +200,7 @@ */ public int decodeValue(@NotNull final String encodedValue) { if (!isNamed) { - try { - return Integer.parseInt(encodedValue); - } catch (final NumberFormatException ignored) { - return 0; - } + return NumberUtils.parseInt(encodedValue); } if (encodedValue.length() == 0) { Modified: trunk/src/app/net/sf/gridarta/model/baseobject/AbstractBaseObject.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/baseobject/AbstractBaseObject.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/model/baseobject/AbstractBaseObject.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -29,6 +29,7 @@ import net.sf.gridarta.model.gameobject.GameObject; import net.sf.gridarta.model.gameobject.MultiArchData; import net.sf.gridarta.model.map.maparchobject.MapArchObject; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -295,18 +296,7 @@ */ @Override public int getAttributeInt(@NotNull final String attributeName, final boolean queryArchetype) { - final String str = getAttributeString(attributeName, queryArchetype); - if (str.length() <= 0) { - return 0; - } - - try { - return Integer.parseInt(str); - } catch (final NumberFormatException ignore) { - // ignore - } - - return 0; + return NumberUtils.parseInt(getAttributeString(attributeName, queryArchetype)); } /** @@ -322,13 +312,7 @@ */ @Override public long getAttributeLong(@NotNull final String attributeName, final boolean queryArchetype) { - long result = 0L; - try { - result = Long.parseLong(getAttributeString(attributeName, queryArchetype)); - } catch (final NumberFormatException ignore) { - // ignore - } - return result; + return NumberUtils.parseLong(getAttributeString(attributeName, queryArchetype)); } /** @@ -344,13 +328,7 @@ */ @Override public double getAttributeDouble(@NotNull final String attributeName, final boolean queryArchetype) { - double result = 0.0; - try { - result = Double.parseDouble(getAttributeString(attributeName, queryArchetype)); - } catch (final NumberFormatException ignore) { - // ignore - } - return result; + return NumberUtils.parseDouble(getAttributeString(attributeName, queryArchetype)); } /** Modified: trunk/src/app/net/sf/gridarta/model/io/AbstractMapArchObjectParser.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/io/AbstractMapArchObjectParser.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/model/io/AbstractMapArchObjectParser.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -22,6 +22,7 @@ import java.io.BufferedReader; import java.io.IOException; import net.sf.gridarta.model.map.maparchobject.MapArchObject; +import net.sf.gridarta.utils.NumberUtils; import net.sf.gridarta.utils.Size2D; import org.jetbrains.annotations.NotNull; @@ -77,27 +78,27 @@ } else if (line.startsWith("name ")) { mapArchObject.setMapName(line.substring(5)); } else if (line.startsWith("width ")) { - width = parseInteger(line.substring(6)); + width = NumberUtils.parseInt(line.substring(6)); } else if (line.startsWith("height ")) { - height = parseInteger(line.substring(7)); + height = NumberUtils.parseInt(line.substring(7)); } else if (line.startsWith("enter_x ")) { - mapArchObject.setEnterX(parseInteger(line.substring(8))); + mapArchObject.setEnterX(NumberUtils.parseInt(line.substring(8))); } else if (line.startsWith("enter_y ")) { - mapArchObject.setEnterY(parseInteger(line.substring(8))); + mapArchObject.setEnterY(NumberUtils.parseInt(line.substring(8))); } else if (line.startsWith("reset_timeout ")) { - mapArchObject.setResetTimeout(parseInteger(line.substring(14))); + mapArchObject.setResetTimeout(NumberUtils.parseInt(line.substring(14))); } else if (line.startsWith("swap_time ")) { - mapArchObject.setSwapTime(parseInteger(line.substring(10))); + mapArchObject.setSwapTime(NumberUtils.parseInt(line.substring(10))); } else if (line.startsWith("difficulty ")) { - mapArchObject.setDifficulty(parseInteger(line.substring(11))); + mapArchObject.setDifficulty(NumberUtils.parseInt(line.substring(11))); } else if (line.startsWith("darkness ")) { - mapArchObject.setDarkness(parseInteger(line.substring(9))); + mapArchObject.setDarkness(NumberUtils.parseInt(line.substring(9))); } else if (line.startsWith("fixed_resettime ")) { - if (parseInteger(line.substring(16)) != 0) { + if (NumberUtils.parseInt(line.substring(16)) != 0) { mapArchObject.setFixedReset(true); } } else if (line.startsWith("outdoor ")) { - if (parseInteger(line.substring(8)) != 0) { + if (NumberUtils.parseInt(line.substring(8)) != 0) { mapArchObject.setOutdoor(true); } } else if (line.startsWith("tile_path_")) { @@ -130,32 +131,6 @@ } /** - * Parse an integer string. - * @param s The string to parse. - * @return The integer value, or zero if value not readable. - */ - protected static int parseInteger(@NotNull final String s) { - try { - return Integer.parseInt(s); - } catch (final NumberFormatException ignored) { - return 0; - } - } - - /** - * Parse a double string. - * @param s The string to parse. - * @return The double value, or zero if value not readable. - */ - protected static double parseDouble(@NotNull final String s) { - try { - return Double.parseDouble(s); - } catch (final NumberFormatException ignored) { - return 0.0; - } - } - - /** * Parse a line for this editor type. * @param line The line to parse. * @param mapArchObject The map arch object to update. Modified: trunk/src/app/net/sf/gridarta/script/parameter/DoubleParameter.java =================================================================== --- trunk/src/app/net/sf/gridarta/script/parameter/DoubleParameter.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/script/parameter/DoubleParameter.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -22,6 +22,7 @@ import net.sf.gridarta.model.archetype.Archetype; import net.sf.gridarta.model.gameobject.GameObject; import net.sf.gridarta.model.map.maparchobject.MapArchObject; +import net.sf.gridarta.utils.NumberUtils; import org.jdom.Element; import org.jetbrains.annotations.NotNull; @@ -42,12 +43,7 @@ public void fromXML(@NotNull final Element e) { super.fromXML(e); final String val = e.getChildText("value"); - try { - final Double iVal = Double.parseDouble(val); - setValue(iVal); - } catch (final NumberFormatException ignored) { - setValue(0.0); - } + setValue(NumberUtils.parseDouble(val)); final DoubleConfig o = new DoubleConfig(); try { o.setMin(Double.parseDouble(e.getChildTextTrim("minimum"))); Modified: trunk/src/app/net/sf/gridarta/utils/ActionBuilderUtils.java =================================================================== --- trunk/src/app/net/sf/gridarta/utils/ActionBuilderUtils.java 2010-05-26 21:34:40 UTC (rev 8013) +++ trunk/src/app/net/sf/gridarta/utils/ActionBuilderUtils.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -58,11 +58,7 @@ return defaultValue; } - try { - return Integer.parseInt(value); - } catch (final NumberFormatException ignored) { - return defaultValue; - } + return NumberUtils.parseInt(value, defaultValue); } /** Added: trunk/src/app/net/sf/gridarta/utils/NumberUtils.java =================================================================== --- trunk/src/app/net/sf/gridarta/utils/NumberUtils.java (rev 0) +++ trunk/src/app/net/sf/gridarta/utils/NumberUtils.java 2010-05-26 21:35:09 UTC (rev 8014) @@ -0,0 +1,85 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2010 The Gridarta Developers. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.utils; + +import org.jetbrains.annotations.NotNull; + +/** + * Utility class for parsing strings into numbers. + * @author Andreas Kirschbaum + */ +public class NumberUtils { + + /** + * Private constructor to prevent instantiation. + */ + private NumberUtils() { + } + + /** + * Parses an integer string. + * @param s the string to parse + * @return the integer value or zero if value not readable + */ + public static int parseInt(@NotNull final String s) { + return parseInt(s, 0); + } + + /** + * Parses an integer string. + * @param s the string to parse + * @param defaultValue the default value + * @return the integer value or the default value if value not readable + */ + public static int parseInt(@NotNull final String s, final int defaultValue) { + try { + return Integer.parseInt(s); + } catch (final NumberFormatException ignored) { + return defaultValue; + } + } + + /** + * Parses a long string. + * @param s the string to parse + * @return the long value or zero if value not readable + */ + public static long parseLong(@NotNull final String s) { + try { + return Long.parseLong(s); + } catch (final NumberFormatException ignored) { + return 0L; + } + } + + /** + * Parses a double string. + * @param s the string to parse + * @return the double value or zero if value not readable + */ + public static double parseDouble(@NotNull final String s) { + try { + return Double.parseDouble(s); + } catch (final NumberFormatException ignored) { + return 0.0; + } + } + +} // class NumberUtils Property changes on: trunk/src/app/net/sf/gridarta/utils/NumberUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |