1 23 package info.monitorenter.util.collections; 24 25 import java.math.BigDecimal ; 26 import java.math.BigInteger ; 27 import java.util.Comparator ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 32 60 public class TreeSetGreedy extends TreeSet implements Set { 61 62 87 private static final class NumberPropertyComparator implements Comparator { 88 89 93 private IComparableProperty m_resort = null; 94 95 129 public int compare(final Object o1, final Object o2) throws ClassCastException { 130 if (o1 == o2) { 132 return 0; 133 } 134 IComparableProperty prop1 = (IComparableProperty) o1; 135 IComparableProperty prop2 = (IComparableProperty) o2; 136 double i1 = prop1.getComparableProperty().doubleValue(); 137 double i2 = prop2.getComparableProperty().doubleValue(); 138 139 if (i1 < i2) { 141 return -1; 142 } else if (i1 == i2) { 143 prop2.setComparableProperty(createIncreasedNumber(prop2.getComparableProperty())); 146 this.m_resort = prop2; 147 return 1; 148 } else { 149 return 1; 150 } 151 152 } 153 154 163 private Number createIncreasedNumber(final Number n) { 164 Class c = n.getClass(); 165 if (c == Integer .class) { 166 return new Integer (n.intValue() - 1); 167 } else if (c == Double .class) { 168 return new Double (n.doubleValue() + 1); 169 } else if (c == Float .class) { 170 return new Float (n.floatValue() + 1); 171 } else if (c == Short .class) { 172 return new Short ((short) (n.shortValue() + 1)); 173 } else if (c == Byte .class) { 174 return new Byte ((byte) (n.byteValue() + 1)); 175 } else if (c == Long .class) { 176 return new Long (n.longValue() + 1); 177 } else if (c == BigDecimal .class) { 178 BigDecimal bd = new BigDecimal (n.toString()); 179 bd.add(new BigDecimal (1)); 180 return bd; 181 } else { 182 BigInteger bi = new BigInteger (n.toString()); 183 bi.add(new BigInteger ("1")); 184 return bi; 185 186 } 187 } 188 } 189 190 193 private static final long serialVersionUID = 3258130237048173623L; 194 195 196 private NumberPropertyComparator m_comparator; 197 198 202 public TreeSetGreedy() { 203 super(new NumberPropertyComparator()); 204 this.m_comparator = (NumberPropertyComparator) this.comparator(); 205 } 206 207 210 public synchronized boolean add(final Object o) { 211 boolean ret = this.addInternal(o); 212 this.packComparableProperties(); 213 return ret; 214 } 215 216 225 private boolean addInternal(final Object o) { 226 boolean ret = super.add(o); 227 boolean success = false; 228 IComparableProperty resort = this.m_comparator.m_resort; 229 this.m_comparator.m_resort = null; 230 if (resort != null) { 231 success = this.remove(resort); 232 if (!success) { 233 System.err.println("Could not remove: " + resort); 234 } 235 success = this.addInternal(resort); 238 } 239 return ret; 240 } 241 242 267 private void packComparableProperties() { 268 Iterator it = this.iterator(); 269 IComparableProperty prop; 270 int i = 0; 271 while (it.hasNext()) { 272 prop = (IComparableProperty) it.next(); 273 prop.setComparableProperty(new Integer (i)); 274 i++; 275 } 276 } 277 } 278 | Popular Tags |