001 /** 002 * This software is provided under the terms of the Minecraft Forge Public 003 * License v1.0. 004 */ 005 006 package net.minecraftforge.common; 007 008 public class Property 009 { 010 private String name; 011 public String value; 012 public String comment; 013 014 /** 015 * Returns the value in this property as a integer, 016 * if the value is not a valid integer, it will return -1. 017 * 018 * @return The value 019 */ 020 public int getInt() 021 { 022 return getInt(-1); 023 } 024 025 /** 026 * Returns the value in this property as a integer, 027 * if the value is not a valid integer, it will return the 028 * provided default. 029 * 030 * @param _default The default to provide if the current value is not a valid integer 031 * @return The value 032 */ 033 public int getInt(int _default) 034 { 035 try 036 { 037 return Integer.parseInt(value); 038 } 039 catch (NumberFormatException e) 040 { 041 return _default; 042 } 043 } 044 045 /** 046 * Checks if the current value stored in this property can be converted to an integer. 047 * @return True if the vslue can be converted to an integer 048 */ 049 public boolean isIntValue() 050 { 051 try 052 { 053 Integer.parseInt(value); 054 return true; 055 } 056 catch (NumberFormatException e) 057 { 058 return false; 059 } 060 } 061 062 /** 063 * Returns the value in this property as a boolean, 064 * if the value is not a valid boolean, it will return the 065 * provided default. 066 * 067 * @param _default The default to provide 068 * @return The value as a boolean, or the default 069 */ 070 public boolean getBoolean(boolean _default) 071 { 072 if (isBooleanValue()) 073 { 074 return Boolean.parseBoolean(value); 075 } 076 else 077 { 078 return _default; 079 } 080 } 081 082 /** 083 * Checks if the current value held by this property is a valid boolean value. 084 * @return True if it is a boolean value 085 */ 086 public boolean isBooleanValue() 087 { 088 return ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())); 089 } 090 091 public String getName() 092 { 093 return name; 094 } 095 096 public void setName(String name) 097 { 098 for (char c : name.toCharArray()) 099 { 100 if (!Character.isLetterOrDigit(c) && Configuration.ALLOWED_CHARS.indexOf(c) == -1 && !Character.isWhitespace(c)) 101 { 102 throw new IllegalArgumentException("Invalid property name: \"" + name + "\""); 103 } 104 } 105 this.name = name; 106 } 107 }