Skip to content

Commit

Permalink
Minor lookup performance increase
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 7, 2015
1 parent 0bbe166 commit 096e288
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
*/
public class SimpleContainer
{
private static Map<Addon, Furnace> started = new ConcurrentHashMap<>();
private static Map<ClassLoader, ContainerEntry> started = new ConcurrentHashMap<>();

/**
* Used to retrieve an instance of {@link Furnace}.
*/
public static Furnace getFurnace(ClassLoader loader)
{
return started.get(getAddon(loader));
Assert.notNull(loader, "ClassLoader must not be null");
ContainerEntry containerEntry = started.get(loader);
return containerEntry != null ? containerEntry.furnace : null;
}

/**
Expand All @@ -44,14 +46,8 @@ public static Furnace getFurnace(ClassLoader loader)
public static Addon getAddon(ClassLoader loader)
{
Assert.notNull(loader, "ClassLoader must not be null");
for (Addon addon : started.keySet())
{
if (addon.getClassLoader() == loader)
{
return addon;
}
}
return null;
ContainerEntry containerEntry = started.get(loader);
return containerEntry != null ? containerEntry.addon : null;
}

/**
Expand All @@ -75,12 +71,26 @@ public static <T> Imported<T> getServices(ClassLoader classloader, Class<T> serv

static void start(Addon addon, Furnace furnace)
{
started.put(addon, furnace);
started.put(addon.getClassLoader(), new ContainerEntry(furnace, addon));
}

static void stop(Addon addon)
{
started.remove(addon);
started.remove(addon.getClassLoader());
}

private static class ContainerEntry
{
final Furnace furnace;
final Addon addon;

public ContainerEntry(Furnace furnace, Addon addon)
{
super();
this.furnace = furnace;
this.addon = addon;
}

}

}

0 comments on commit 096e288

Please sign in to comment.