Drawers { @SuppressWarnings("unchecked") @Pure public static synchronized <T> Drawer<T> getDrawerFor(Class<? extends T> type) { assert type != null : AssertMessages.notNullParameter(); try { assert getLock(); final Drawer<?> bufferedType = buffer.get(type); Drawer<T> choice = null; if (bufferedType != null) { choice = (Drawer<T>) bufferedType; } else { final Iterator<Drawer<?>> iterator = getAllDrawers(); Drawer<?> exactMatcherDrawer = null; Drawer<?> superMatcherDrawer = null; while (iterator.hasNext()) { final Drawer<?> drawer = iterator.next(); final Class<?> drawerType = drawer.getPrimitiveType(); if (exactMatcherDrawer != null) { if (drawerType.equals(type) && exactMatcherDrawer.getClass().isAssignableFrom(drawer.getClass())) { exactMatcherDrawer = drawer; } } else if (drawerType.equals(type)) { exactMatcherDrawer = drawer; } else if (drawerType.isAssignableFrom(type)) { if (superMatcherDrawer == null || (superMatcherDrawer.getPrimitiveType().isAssignableFrom(drawerType))) { superMatcherDrawer = drawer; } } } if (exactMatcherDrawer != null) { choice = (Drawer<T>) exactMatcherDrawer; } else if (superMatcherDrawer != null) { choice = (Drawer<T>) superMatcherDrawer; } if (choice != null) { buffer.put(type, choice); } } return choice; } finally { assert releaseLock(); } } private  Drawers(); static synchronized void reload(); @Pure static synchronized Iterator<Drawer<?>> getAllDrawers(); static synchronized void setBackedDrawers(Iterable<Drawer<?>> drawers); @SuppressWarnings("unchecked") @Pure static synchronized ServiceLoader<Drawer<?>> getDrawerProvidingService(); @SuppressWarnings("unchecked") @Pure static synchronized Drawer<T> getDrawerFor(Class<? extends T> type); @Pure @SuppressWarnings("unchecked") static synchronized Drawer<? super T> getDrawerFor(T instance);  }
@Test public void getDrawerForClass_01() { Drawer<String> d1 = Drawers.getDrawerFor(String.class); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer1); }
@Test public void getDrawerForClass_02() { Drawer<? extends Number> d1 = Drawers.getDrawerFor(Integer.class); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer2); }
@Test public void getDrawerForClass_03() { Drawer<? extends Number> d1 = Drawers.getDrawerFor(Double.class); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer3); }
@Test public void getDrawerForClass_04() { Drawer d1 = Drawers.getDrawerFor(Number.class); assertNull(d1); }
@Test public void getDrawerForObject_01() { Drawer<? super String> d1 = Drawers.getDrawerFor(""); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer1); }
@Test public void getDrawerForObject_02() { Drawer<? super Integer> d1 = Drawers.getDrawerFor(1); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer2); }
@Test public void getDrawerForObject_03() { Drawer<? super Double> d1 = Drawers.getDrawerFor(1d); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer3); }
@Test public void getDrawerForObject_04() { Drawer d1 = Drawers.getDrawerFor(1f); assertNull(d1); }
@Test public void getDrawerForObject_05() { DrawableObject obj = new DrawableObject(); Drawer d1 = Drawers.getDrawerFor(obj); assertNull(d1); obj.setDrawer(new MyDrawer1()); d1 = Drawers.getDrawerFor(obj); assertNotNull(d1); assertTrue(d1 instanceof MyDrawer1); }