Settings.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Settings
  2. //
  3. // Douglas Thrift
  4. //
  5. // Settings.java
  6. /* Copyright 2011 Douglas Thrift
  7. *
  8. * This file is part of Big Screen Bot.
  9. *
  10. * Big Screen Bot is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Big Screen Bot is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Big Screen Bot. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package net.douglasthrift.bigscreenbot;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileReader;
  26. import java.io.FileWriter;
  27. import java.io.IOException;
  28. import java.util.Arrays;
  29. import java.util.Collection;
  30. import java.util.List;
  31. import java.util.Properties;
  32. import org.apache.commons.lang3.StringUtils;
  33. public class Settings extends Properties
  34. {
  35. private static final String SETTINGS = "bigscreenbot.properties";
  36. public boolean getBooleanProperty(String key, boolean defaultValue)
  37. {
  38. String value = getProperty(key);
  39. if (value != null)
  40. return Boolean.parseBoolean(value);
  41. else
  42. return defaultValue;
  43. }
  44. public int getIntegerProperty(String key, int defaultValue)
  45. {
  46. String value = getProperty(key);
  47. if (value != null)
  48. return Integer.parseInt(value);
  49. else
  50. return defaultValue;
  51. }
  52. public List<String> getListProperty(String key)
  53. {
  54. String value = getProperty(key);
  55. if (value != null)
  56. return Arrays.asList(StringUtils.split(value));
  57. else
  58. return null;
  59. }
  60. public List<String> getListProperty(String key, List<String> defaultValue)
  61. {
  62. String value = getProperty(key);
  63. if (value != null)
  64. return Arrays.asList(StringUtils.split(value));
  65. else
  66. return defaultValue;
  67. }
  68. public List<String> setListProperty(String key, Collection<String> value)
  69. {
  70. String oldValue = (String)setProperty(key, StringUtils.join(value, ' '));
  71. if (oldValue != null)
  72. return Arrays.asList(StringUtils.split(oldValue));
  73. else
  74. return null;
  75. }
  76. public void load() throws FileNotFoundException, IOException
  77. {
  78. FileReader reader = null;
  79. try
  80. {
  81. reader = new FileReader(SETTINGS);
  82. load(reader);
  83. }
  84. finally
  85. {
  86. if (reader != null)
  87. reader.close();
  88. }
  89. }
  90. public void store() throws IOException
  91. {
  92. FileWriter writer = null;
  93. try
  94. {
  95. writer = new FileWriter(SETTINGS);
  96. store(writer, null);
  97. }
  98. finally
  99. {
  100. if (writer != null)
  101. writer.close();
  102. }
  103. }
  104. }
  105. // vim: expandtab