@Test public void test() { assertTrue(sw.get(1) instanceof Impl1); assertTrue(sw.get(2) instanceof Impl2); }
public synchronized I get(int id) { String clzName = impls.get(id); if (clzName == null) { throw new IllegalArgumentException("Implementation class missing, ID " + id + ", interface " + interfaceClz.getName()); } @SuppressWarnings("unchecked") I result = (I) instances[id]; if (result == null) { try { result = (I) ClassUtil.newInstance(clzName); instances[id] = result; } catch (Exception ex) { logger.warn("Implementation missing " + clzName + " - " + ex); } } if (result == null) throw new IllegalArgumentException("Implementations missing, ID " + id + ", interface " + interfaceClz.getName()); return result; }
ImplementationSwitch { public synchronized I get(int id) { String clzName = impls.get(id); if (clzName == null) { throw new IllegalArgumentException("Implementation class missing, ID " + id + ", interface " + interfaceClz.getName()); } @SuppressWarnings("unchecked") I result = (I) instances[id]; if (result == null) { try { result = (I) ClassUtil.newInstance(clzName); instances[id] = result; } catch (Exception ex) { logger.warn("Implementation missing " + clzName + " - " + ex); } } if (result == null) throw new IllegalArgumentException("Implementations missing, ID " + id + ", interface " + interfaceClz.getName()); return result; } }
ImplementationSwitch { public synchronized I get(int id) { String clzName = impls.get(id); if (clzName == null) { throw new IllegalArgumentException("Implementation class missing, ID " + id + ", interface " + interfaceClz.getName()); } @SuppressWarnings("unchecked") I result = (I) instances[id]; if (result == null) { try { result = (I) ClassUtil.newInstance(clzName); instances[id] = result; } catch (Exception ex) { logger.warn("Implementation missing " + clzName + " - " + ex); } } if (result == null) throw new IllegalArgumentException("Implementations missing, ID " + id + ", interface " + interfaceClz.getName()); return result; } ImplementationSwitch(Map<Integer, String> impls, Class<I> interfaceClz); }
ImplementationSwitch { public synchronized I get(int id) { String clzName = impls.get(id); if (clzName == null) { throw new IllegalArgumentException("Implementation class missing, ID " + id + ", interface " + interfaceClz.getName()); } @SuppressWarnings("unchecked") I result = (I) instances[id]; if (result == null) { try { result = (I) ClassUtil.newInstance(clzName); instances[id] = result; } catch (Exception ex) { logger.warn("Implementation missing " + clzName + " - " + ex); } } if (result == null) throw new IllegalArgumentException("Implementations missing, ID " + id + ", interface " + interfaceClz.getName()); return result; } ImplementationSwitch(Map<Integer, String> impls, Class<I> interfaceClz); synchronized I get(int id); }
ImplementationSwitch { public synchronized I get(int id) { String clzName = impls.get(id); if (clzName == null) { throw new IllegalArgumentException("Implementation class missing, ID " + id + ", interface " + interfaceClz.getName()); } @SuppressWarnings("unchecked") I result = (I) instances[id]; if (result == null) { try { result = (I) ClassUtil.newInstance(clzName); instances[id] = result; } catch (Exception ex) { logger.warn("Implementation missing " + clzName + " - " + ex); } } if (result == null) throw new IllegalArgumentException("Implementations missing, ID " + id + ", interface " + interfaceClz.getName()); return result; } ImplementationSwitch(Map<Integer, String> impls, Class<I> interfaceClz); synchronized I get(int id);  }