public void testCompareTo() throws Exception { ClassWorld cw = new ClassWorld(); ClassRealm r = cw.newRealm("test1"); Entry entry1 = new Entry(r, "org.test"); Entry entry2 = new Entry(r, "org.test.impl"); assertTrue("org.test > org.test.impl", entry1.compareTo(entry2) > 0); }
/** * Determines if two objects are equal to each other. * * @param other The other Object to compare to * @return True if they are equal, false if not */ public boolean equals(Object other) { if (other == null) { throw new NullPointerException(); } if (other instanceof Entry) { Entry e = (Entry) other; if (entry.compareTo(e) == 0) { return true; } } else { throw new ClassCastException(); } return false; }
public void add(Comparable key, int index) { if (-1 < index) { int table = ((null == key) ? (0) : (Math.abs(key.hashCode()) % this.size)); Entry[] list = this.table[table]; if (null == list) { list = new Entry[] {new Entry(key, index)}; this.table[table] = list; } else { int len = list.length, term = (len - 1), comp; Entry entry, copier[]; for (int cc = 0; cc < len; cc++) { entry = list[cc]; comp = entry.compareTo(key); if (0 == comp) { list[cc] = new Entry(key, index); /*(assume index or key instance change)*/ return; } else if (-1 != comp) { copier = new Entry[len + 1]; if (0 == cc) { copier = new Entry[len + 1]; System.arraycopy(list, 0, copier, 1, len); copier[0] = new Entry(key, index); this.table[table] = copier; return; } else { copier = new Entry[len + 1]; System.arraycopy(list, 0, copier, 0, cc); copier[cc] = new Entry(key, index); System.arraycopy(list, cc, copier, (cc + 1), (len - cc)); this.table[table] = copier; return; } } } copier = new Entry[len + 1]; System.arraycopy(list, 0, copier, 0, len); copier[len] = new Entry(key, index); this.table[table] = copier; } } else throw new IllegalArgumentException(); }
public int get(Comparable key) { int table = ((null == key) ? (0) : (Math.abs(key.hashCode()) % this.size)); Entry[] list = this.table[table]; if (null == list) return -1; else { Entry entry; scan: for (int cc = 0, len = list.length; cc < len; cc++) { entry = list[cc]; switch (entry.compareTo(key)) { case 0: return entry.index; case -1: continue scan; default: return -1; } } return -1; } }