1 23 24 package info.monitorenter.util.units; 25 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 44 public final class UnitFactory extends Object { 45 46 47 private static UnitFactory instance; 48 49 53 public static final AUnit UNCHANGED = new UnitUnchanged(); 54 55 56 private static final Map UNITSYSTEMS = new HashMap (); 57 58 64 public static UnitFactory getInstance() { 65 if (UnitFactory.instance == null) { 66 UnitFactory.instance = new UnitFactory(); 67 } 68 return UnitFactory.instance; 69 } 70 71 76 private UnitFactory() { 77 } 79 80 94 public AUnit getUnit(final double absoluteMax, final IUnitSystem units) { 95 List choice; 96 choice = (List ) UnitFactory.UNITSYSTEMS.get(units.getClass().getName()); 98 if (choice == null) { 99 choice = this.initUnitSystem(units); 100 } 101 Iterator it = choice.iterator(); 103 AUnit ret = null, old = null; 104 if (it.hasNext()) { 105 old = (AUnit) it.next(); 106 while (it.hasNext()) { 107 ret = (AUnit) it.next(); 108 if (absoluteMax < (ret.getFactor())) { 109 return old; 110 } 111 old = ret; 112 } 113 return old; 115 } 116 return UnitFactory.UNCHANGED; 118 } 119 120 131 public List getUnits(final IUnitSystem unitsystem) { 132 List choice = (List ) UnitFactory.UNITSYSTEMS.get(unitsystem.getClass().getName()); 133 if (choice == null) { 135 choice = this.initUnitSystem(unitsystem); 136 } 137 return choice; 138 139 } 140 141 150 private List initUnitSystem(final IUnitSystem units) { 151 List choice = new LinkedList (); 152 Class [] clazzs = units.getUnits(); 153 AUnit unit = null, previous = null; 154 for (int i = 0; i < clazzs.length; i++) { 155 if (!AUnit.class.isAssignableFrom(clazzs[i])) { 156 System.err.println("UnitFactory: wrong class " + clazzs[i].getName() + " delivered by " 157 + units.getClass().getName()); 158 continue; 159 } else { 160 try { 161 unit = (AUnit) clazzs[i].newInstance(); 162 choice.add(unit); 163 if (previous == null) { 164 previous = unit; 165 } else { 166 previous.m_nextHigherUnit = unit; 167 } 168 unit.m_nexLowerUnit = previous; 169 previous = unit; 170 } catch (InstantiationException e) { 171 System.err.println("UnitFactory encountered problems by instantiation of " 172 + clazzs[i].getName()); 173 } catch (IllegalAccessException f) { 174 System.err.println("UnitFactory has no access to " + clazzs[i].getName()); 175 } 176 } 177 } 178 unit.m_nextHigherUnit = unit; 179 double tmpfactori, tmpfactorj; 181 int min; 182 int stop = choice.size(); 183 for (int i = 0; i < stop - 1; i++) { 184 min = i; 185 tmpfactori = ((AUnit) choice.get(i)).getFactor(); 186 for (int j = i + 1; j < stop; j++) { 187 tmpfactorj = ((AUnit) choice.get(j)).getFactor(); 188 if (tmpfactorj < tmpfactori) { 189 tmpfactori = tmpfactorj; 190 min = j; 191 } 192 } 193 choice.add(i, choice.remove(min)); 194 } 195 UnitFactory.UNITSYSTEMS.put(units.getClass().getName(), choice); 196 return choice; 197 } 198 } 199 | Popular Tags |