/plugins/LogViewer/tags/rel-0-8/src/java/logviewer/EnumeratedProperties.java

# · Java · 83 lines · 29 code · 8 blank · 46 comment · 5 complexity · 8d717dcf07ab3ba6676be9e87b56c7cd MD5 · raw file

  1. /*
  2. * Copyright (C) 2003 Don Brown (mrdon@techie.com)
  3. * Copyright (C) 2000, 2001 Greg Merrill (greghmerrill@yahoo.com)
  4. * This file is part of Log Viewer, a plugin for jEdit (http://www.jedit.org).
  5. * It is heavily based off Follow (http://follow.sf.net).
  6. * Log Viewer is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. * Log Viewer 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. * You should have received a copy of the GNU General Public License
  14. * along with Log Viewer; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. package logviewer;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Properties;
  21. /**
  22. * Extension of {@link java.util.Properties} which allows one to specify
  23. * property values which are Lists of Strings.
  24. *
  25. * @author <a href="mailto:mrdon@techie.com">Don Brown</a>
  26. * @author <a href="mailto:greghmerrill@yahoo.com">Greg Merrill</a>
  27. */
  28. public class EnumeratedProperties {
  29. /** Delimiter between property name & list member index */
  30. protected static char delimiter = '.';
  31. /**
  32. * Returns the List value of the property with the supplied key. Note that
  33. * one can call getEnumeratedProperty() for a given key successfully if and
  34. * only if setEnumeratedProperty() for that key was called some time
  35. * beforehand. All members of the list returned will be Strings.
  36. *
  37. * @param key lookup of the enumerated property to be retrieved.
  38. * @return list containing String values
  39. */
  40. public List getEnumeratedProperty(String key) {
  41. List values = new ArrayList();
  42. int i = 0;
  43. String value;
  44. while ((value = LogViewer.getProperty(key + delimiter + i++)) != null) {
  45. values.add(value);
  46. }
  47. return values;
  48. }
  49. /**
  50. * Assigns the supplied array of String values to the supplied key.
  51. *
  52. * @param key property lookup
  53. * @param values values to be associated with the property lookup
  54. */
  55. public void setEnumeratedProperty(String key, String[] values) {
  56. int i;
  57. for (i = 0; i < values.length; i++) {
  58. LogViewer.setProperty(key + delimiter + i, values[i]);
  59. }
  60. while (LogViewer.getProperty(key + delimiter + i) != null) {
  61. LogViewer.setProperty(key + delimiter + i, null);
  62. i++;
  63. }
  64. }
  65. /**
  66. * Convenience method; equivalent to calling setEnumeratedProperty(key,
  67. * (String[])values.toArray(new String[] {}));
  68. *
  69. * @param key The new enumeratedProperty value
  70. * @param values The new enumeratedProperty value
  71. */
  72. public void setEnumeratedProperty(String key, List values) {
  73. this.setEnumeratedProperty(key, (String[]) values.toArray(new String[]{}));
  74. }
  75. }