Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Category.java

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

Generated at Thu Feb 7 06:41:51 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001