/net.sourceforge.vrapper.core/src/net/sourceforge/vrapper/platform/SimpleConfiguration.java

https://github.com/luntain/vrapper · Java · 66 lines · 47 code · 10 blank · 9 comment · 8 complexity · b4a312ff8fedc6b77e2dac791976b009 MD5 · raw file

  1. package net.sourceforge.vrapper.platform;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class SimpleConfiguration implements Configuration {
  5. private String newLine = NewLine.SYSTEM.nl;
  6. private static final Map<Option<?>, Object> vars = new HashMap<Option<?>, Object>();
  7. @SuppressWarnings("unchecked")
  8. public <T> T get(Option<T> key) {
  9. if (vars.containsKey(key)) {
  10. return (T) vars.get(key);
  11. }
  12. return key.getDefaultValue();
  13. }
  14. public <T> void set(Option<T> key, T value) {
  15. if (value == null) {
  16. throw new NullPointerException("value must not be null");
  17. }
  18. vars.put(key, value);
  19. }
  20. /* (non-Javadoc)
  21. * @see net.sourceforge.vrapper.platform.Configuration#getNewLine()
  22. */
  23. public String getNewLine() {
  24. return newLine;
  25. }
  26. /* (non-Javadoc)
  27. * @see net.sourceforge.vrapper.platform.Configuration#setNewLine(java.lang.String)
  28. */
  29. public void setNewLine(String newLine) {
  30. this.newLine = newLine;
  31. }
  32. /* (non-Javadoc)
  33. * @see net.sourceforge.vrapper.platform.Configuration#setNewLine(net.sourceforge.vrapper.platform.SimpleConfiguration.NewLine)
  34. */
  35. public void setNewLine(NewLine newLine) {
  36. this.newLine = newLine.nl;
  37. }
  38. public static enum NewLine {
  39. MAC("\r"), UNIX("\n"), WINDOWS("\r\n"), SYSTEM(System
  40. .getProperty("line.separator")), UNKNOWN("\n");
  41. public final String nl;
  42. private NewLine(String nl) {
  43. this.nl = nl;
  44. }
  45. public static NewLine parse(String nl) {
  46. if (nl.startsWith(WINDOWS.nl)) {
  47. return WINDOWS;
  48. } else if (nl.startsWith(UNIX.nl)) {
  49. return UNIX;
  50. } else if (nl.startsWith(MAC.nl)) {
  51. return UNIX;
  52. }
  53. throw new IllegalArgumentException("string does not begin with a known newline");
  54. }
  55. }
  56. }