1. package org.jr;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月20日
  6. * @author Cherami
  7. */
  8. import java.util.*;
  9. import org.jr.util.*;
  10. /**
  11. * 以字符分隔的属性的抽象类。
  12. * <p>其他具体的属性类的父类,封装共通的方法并完成最初的属性数组的解析。
  13. * 这个类将一个字符串表达的属性值字符串解析为一个字符串数组,各个子类定义每个值对应何属性。
  14. * @since 0.4
  15. */
  16. public abstract class CharSplitProperty {
  17. protected final String source; //保持未经解析的最原始的信息
  18. protected char splitChar; //信息的分隔符
  19. protected final String[] properties; //解析以后的信息数组
  20. protected static final HashMap numbers = new HashMap(); //未得到最少属性时需要使用的序号
  21. /**
  22. * 构造方法,根据原始信息解析得到需要的各个子信息,解析的分隔符为'*'。
  23. * source的一般形式为"value1*value2*value3"
  24. * @param source 未经解析的原始信息
  25. * @since 0.4
  26. */
  27. public CharSplitProperty(String source) {
  28. this(source, '*');
  29. }
  30. /**
  31. * 构造方法,根据原始信息解析得到需要的各个子信息。
  32. * @param source 未经解析的原始信息
  33. * @param splitChar 解析的分隔符
  34. * @since 0.4
  35. */
  36. public CharSplitProperty(String source, char splitChar) {
  37. this.source = source;
  38. this.splitChar = splitChar;
  39. PropertySerials ps = new PropertySerials(source);
  40. ps.setSplitChar(splitChar);
  41. properties = ps.getSerials();
  42. if (properties.length < getLeastPropertyCount() || properties.length == 0) {
  43. String className = this.getClass().getName();
  44. Integer number = (Integer) numbers.get(className);
  45. if (number == null) {
  46. number = new Integer(0);
  47. numbers.put(className, number);
  48. }
  49. numbers.put(className, new Integer(number.intValue() + 1));
  50. }
  51. }
  52. /**
  53. * 得到对象的字符串表示。
  54. * @return 对象的字符串表示
  55. * @since 0.4
  56. */
  57. public String toString() {
  58. return source;
  59. }
  60. /**
  61. * 得到信息解析时使用的分隔符。
  62. * @return 信息解析时使用的分隔符
  63. * @since 0.4
  64. */
  65. public char getSplitChar() {
  66. return splitChar;
  67. }
  68. /**
  69. * 得到解析后的属性数组。
  70. * @return 解析后的属性数组
  71. * @since 0.4
  72. */
  73. public String[] getProperties() {
  74. return (String[])properties.clone();
  75. }
  76. /**
  77. * 得到属性应有的最少个数。
  78. * 由于对于具体的的项目而言,其需要的最少个数是不同的,因此此方法被定义为抽象的。
  79. * @return 属性应有的最少个数
  80. * @since 0.4
  81. */
  82. public abstract int getLeastPropertyCount();
  83. /**
  84. * 得到属性应有的个数。
  85. * <p>由于对于具体的的项目而言,其需要的属性个数是不同的,因此此方法被定义为抽象的。
  86. * @return 应有的属性个数
  87. * @since 0.4
  88. */
  89. public abstract int getPropertyCount();
  90. /**
  91. * 得到属性数组中的指定索引的属性不存在时的返回值。
  92. * @param index 属性数组中的索引
  93. * @return 属性数组中的指定索引的属性不存在时的返回值
  94. * @since 0.4
  95. */
  96. public String getDefaultProperty(int index) {
  97. return "";
  98. }
  99. /**
  100. * 属性数组中的指定索引的属性。
  101. * <p>如果不存在时返回缺省值。
  102. * @param index 属性数组中的索引
  103. * @return 属性数组中的指定索引的属性
  104. * @see #getDefaultProperty(int index) getDefaultProperty(int index)
  105. * @since 0.4
  106. */
  107. public String getProperty(int index) {
  108. if (properties == null || index < 0 || index >= properties.length) {
  109. return getDefaultProperty(index);
  110. }
  111. else if (index == 0 && properties.length == 1 && properties[0].equals("")) {
  112. return getDefaultProperty(index);
  113. }
  114. else {
  115. return properties[index];
  116. }
  117. }
  118. /**
  119. * 得到属性未满足最少属性数时需要使用的顺序序号。
  120. * @return 序号
  121. * @since 0.4
  122. */
  123. public int getNumber() {
  124. String className = this.getClass().getName();
  125. Integer number = (Integer) numbers.get(className);
  126. if (number == null) {
  127. numbers.put(className, new Integer(1));
  128. return 1;
  129. }
  130. return number.intValue();
  131. }
  132. }