PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/testcase/StringList.java

#
Java | 125 lines | 73 code | 18 blank | 34 comment | 10 complexity | 6022acad0e6d9a24dba20612ec6253f9 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * StringList.java - Helper functions for
  3. perl-like string lists.
  4. *
  5. * :tabSize=8:indentSize=8:noTabs=false:
  6. * :folding=explicit:collapseFolds=1:
  7. *
  8. * Copyright (C) 2005 Alan Ezust
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program 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 this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. package testcase;
  25. // {{{ imports
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. // }}}
  29. public class StringList extends ArrayList<String>
  30. {
  31. // {{{ StringList()
  32. public StringList()
  33. {
  34. }
  35. public StringList(Object[] array)
  36. {
  37. addAll(array);
  38. } // }}}
  39. // {{{ addAll()
  40. public void addAll(Object[] array)
  41. {
  42. for (int i = 0; i < array.length; ++i)
  43. {
  44. add(array[i].toString());
  45. }
  46. } // }}}
  47. // {{{ split()
  48. public static StringList split(String orig, Object delim)
  49. {
  50. if ((orig == null) || (orig.length() == 0))
  51. return new StringList();
  52. return new StringList(orig.split(delim.toString()));
  53. } // }}}
  54. // {{{ toString()
  55. public String toString()
  56. {
  57. return join("\n");
  58. }
  59. // }}}
  60. public String[] toArray() {
  61. int siz = size();
  62. String[] result = new String[siz];
  63. System.arraycopy(super.toArray(), 0, result, 0, siz);
  64. return result;
  65. }
  66. // {{{ join()
  67. public static String join(Collection c, String delim)
  68. {
  69. StringList sl = new StringList();
  70. for (Object o: c) {
  71. String s = o.toString();
  72. sl.add(s);
  73. }
  74. return sl.join(delim);
  75. }
  76. public static String join(Object[] arr, String delim) {
  77. StringList sl = new StringList();
  78. sl.addAll(arr);
  79. return sl.join(delim);
  80. }
  81. public String join(String delim)
  82. {
  83. int s = size();
  84. if (s < 1)
  85. return "";
  86. if (s == 1)
  87. return get(0).toString();
  88. else
  89. {
  90. StringBuffer retval = new StringBuffer();
  91. retval.append(get(0));
  92. for (int i = 1; i < s; ++i)
  93. retval.append(delim + get(i));
  94. return retval.toString();
  95. }
  96. }
  97. // }}}
  98. // {{{ main()
  99. public static void main(String args[])
  100. {
  101. String teststr = "a,b,c,d,e,f";
  102. StringList sl = StringList.split(teststr, ",");
  103. String joinstr = sl.join(",");
  104. // assert(teststr.equals(joinstr));
  105. System.out.println("Test Passed");
  106. }// }}}
  107. }