00001 package gov.nasa.arc.ase.util;
00002
00003
00004
00005
00006
00007 import java.util.Vector;
00008
00009 public class Category implements Comparable {
00010 private String[] categories;
00011 private int length;
00012 private int delta;
00013
00014 public Category() {
00015 categories = new String[0];
00016 length = 0;
00017 delta = 1;
00018 }
00019 public Category(int size) {
00020 if(size < 0) throw new IllegalArgumentException("size < 0");
00021
00022 categories = new String[size];
00023 length = 0;
00024 delta = 1;
00025 }
00026 public Category(int size, int delta) {
00027 if(size < 0) throw new IllegalArgumentException("size < 0");
00028 if(delta <= 0) throw new IllegalArgumentException("delta <= 0");
00029
00030 categories = new String[size];
00031 length = 0;
00032 this.delta = delta;
00033 }
00034 public Category(Category c) {
00035 int l = c.categories.length;
00036 categories = new String[l];
00037 System.arraycopy(c.categories, 0, categories, 0, l);
00038 length = l;
00039 delta = 1;
00040 }
00041 public Category(Category c, int delta) {
00042 if(delta <= 0) throw new IllegalArgumentException("delta <= 0");
00043
00044 int l = c.categories.length;
00045 categories = new String[l];
00046 System.arraycopy(c.categories, 0, categories, 0, l);
00047 length = l;
00048 this.delta = delta;
00049 }
00050 public Category(String s) {
00051 parseFromString(s);
00052 }
00053 public int compareTo(Object o) {
00054 Category c = (Category)o;
00055 String[] o1 = categories;
00056 String[] o2 = c.categories;
00057 int l1 = o1.length;
00058 int l2 = o2.length;
00059 for(int i = 0; i < l1 & i < l2; i++) {
00060 int r = o1[i].compareTo(o2[i]);
00061 if(r != 0) return r;
00062 }
00063 return l1 - l2;
00064 }
00065 public void down(String o) {
00066 if(length >= categories.length)
00067 grow();
00068
00069 categories[length++] = o;
00070 }
00071 public boolean equals(Object o) {
00072 Category c = (Category)o;
00073 String[] o1 = categories;
00074 String[] o2 = c.categories;
00075 int l1 = o1.length;
00076 int l2 = o2.length;
00077 for(int i = 0; i < l1 & i < l2; i++)
00078 if(!o1[i].equals(o2[i])) return false;
00079
00080 return true;
00081 }
00082 protected void grow() {
00083 String[] copy = new String[length + delta];
00084 System.arraycopy(categories, 0, copy, 0, length);
00085 categories = copy;
00086 }
00087 public int hashCode() {
00088 int hashcode = 0;
00089
00090 for(int i = 0; i < length; i++)
00091 hashcode = hashcode + categories[i].hashCode();
00092
00093 return hashcode;
00094 }
00095 protected void parseFromString(String s) {
00096 if (!s.startsWith("/")) throw new IllegalArgumentException("category should start with /");
00097
00098 int si = 1, len = s.length();
00099 Vector strings = new Vector();
00100
00101 while(si < len) {
00102 int ei = s.indexOf('/', si);
00103 if (ei == -1) ei = len;
00104
00105 strings.addElement(s.substring(si, ei));
00106
00107 si = ei + 1;
00108 }
00109
00110 length = strings.size();
00111 categories = new String[length];
00112
00113
00114
00115
00116
00117 strings.toArray(categories);
00118
00119 delta = 1;
00120 }
00121 public void print(Category last) {
00122 boolean started = false;
00123
00124 if(length == 0) {
00125 Debug.print(Debug.WARNING, "/");
00126 return;
00127 }
00128
00129 for(int i = 0; i < length; i++) {
00130 if(i == length-1) {
00131 for(int j = 0; j < i; j++)
00132 Debug.print(Debug.WARNING, " ");
00133 Debug.print(Debug.WARNING, categories[i].toString());
00134 } else if(started || last == null) {
00135 for(int j = 0; j < i; j++)
00136 Debug.print(Debug.WARNING, " ");
00137 Debug.println(Debug.WARNING, categories[i].toString());
00138 } else if(!categories[i].equals(last.categories[i])) {
00139 started = true;
00140 for(int j = 0; j < i; j++)
00141 Debug.print(Debug.WARNING, " ");
00142 Debug.println(Debug.WARNING, categories[i].toString());
00143 }
00144 }
00145 }
00146 public void save(java.io.PrintStream out, Category last) {
00147 boolean started = false;
00148
00149 if(length == 0) {
00150 out.print("/");
00151 return;
00152 }
00153
00154 for(int i = 0; i < length; i++) {
00155 if(i == length-1) {
00156 for(int j = 0; j < i; j++)
00157 out.print(" ");
00158 out.print(categories[i].toString());
00159 } else if(started || last == null) {
00160 for(int j = 0; j < i; j++)
00161 out.print(" ");
00162 out.println(categories[i].toString());
00163 } else if(i >= last.length || !categories[i].equals(last.categories[i])) {
00164 started = true;
00165 for(int j = 0; j < i; j++)
00166 out.print(" ");
00167 out.println(categories[i].toString());
00168 }
00169 }
00170 }
00171 public String toString() {
00172 if(length == 0) return "/";
00173
00174 StringBuffer sb = new StringBuffer();
00175 for(int i = 0; i < length; i++) {
00176 sb.append("/");
00177 sb.append(categories[i]);
00178 }
00179
00180 return sb.toString();
00181 }
00182 public String up() {
00183 if(length == 0) throw new IndexOutOfBoundsException("length == 0");
00184
00185 return categories[--length];
00186 }
00187 }