Permalink
Browse files

refactor: Warehouse -> StdWarehouse

  • Loading branch information...
a-peyrard committed Jan 4, 2015
1 parent b22dd96 commit 76b034b9d7f94b10cdc638b67cef160c4fa4fdfc
@@ -22,7 +22,7 @@
import restx.factory.Name;
import restx.factory.NamedComponent;
import restx.factory.SingletonFactoryMachine;
import restx.factory.Warehouse;
import restx.factory.StdWarehouse;
import restx.http.HttpStatus;
import restx.security.RestxSessionCookieFilter;
import restx.server.WebServer;
@@ -188,9 +188,9 @@ public void route(final RestxRequest restxRequest, final RestxResponse restxResp
private class PerRequestFactoryLoader implements RestxMainRouter {
private final String serverId;
private final Warehouse warehouse;
private final StdWarehouse warehouse;
public PerRequestFactoryLoader(String serverId, Warehouse warehouse) {
public PerRequestFactoryLoader(String serverId, StdWarehouse warehouse) {
this.serverId = serverId;
this.warehouse = warehouse;
}
@@ -409,7 +409,7 @@ private RestxMainRouter build(final String serverId, Optional<String> baseUri) {
Thread.currentThread().setContextClassLoader(previous);
}
Warehouse warehouse = getLoadFactoryMode().equals("cleanrequest") ? new Warehouse() : factory.getWarehouse();
StdWarehouse warehouse = getLoadFactoryMode().equals("cleanrequest") ? new StdWarehouse() : factory.getWarehouse();
RestxMainRouter router = new PerRequestFactoryLoader(serverId, warehouse);
@@ -12,14 +12,14 @@
@Component
public class WarehouseRoute extends StdRoute {
private final Warehouse warehouse;
private final StdWarehouse warehouse;
@Inject
public WarehouseRoute(Factory factory) {
this(factory.getWarehouse());
}
public WarehouseRoute(Warehouse warehouse) {
public WarehouseRoute(StdWarehouse warehouse) {
super("WarehouseRoute", new StdRestxRequestMatcher("GET", "/@/warehouse"));
this.warehouse = warehouse;
}
@@ -259,7 +259,7 @@ public LocalMachines set(int priority, String name, Object component) {
public static class Builder {
private boolean usedServiceLoader;
private Multimap<String, FactoryMachine> machines = ArrayListMultimap.create();
private List<Warehouse> providers = new ArrayList<>();
private List<StdWarehouse> providers = new ArrayList<>();
public Builder addFromServiceLoader() {
machines.putAll(SERVICE_LOADER, FactoryMachinesServiceLoader.getMachines());
@@ -279,7 +279,7 @@ public Builder addMachine(FactoryMachine machine) {
return this;
}
public Builder addWarehouseProvider(Warehouse warehouse) {
public Builder addWarehouseProvider(StdWarehouse warehouse) {
providers.add(warehouse);
return this;
}
@@ -307,18 +307,18 @@ public Factory build() {
*/
Factory factory = new Factory(
usedServiceLoader, machines, ImmutableList.<ComponentCustomizerEngine>of(),
new Warehouse(ImmutableList.copyOf(providers)));
new StdWarehouse(ImmutableList.copyOf(providers)));
Map<Name<FactoryMachine>, MachineEngine<FactoryMachine>> toBuild = new LinkedHashMap<>();
ImmutableList<FactoryMachine> factoryMachines = buildFactoryMachines(factory, factory.machines, toBuild);
while (!factoryMachines.isEmpty()) {
machines.putAll("FactoryMachines", factoryMachines);
factory = new Factory(usedServiceLoader, machines,
ImmutableList.<ComponentCustomizerEngine>of(), new Warehouse());
ImmutableList.<ComponentCustomizerEngine>of(), new StdWarehouse());
factoryMachines = buildFactoryMachines(factory, factoryMachines, toBuild);
}
factory = new Factory(usedServiceLoader, machines,
buildCustomizerEngines(factory), new Warehouse(ImmutableList.copyOf(providers)));
buildCustomizerEngines(factory), new StdWarehouse(ImmutableList.copyOf(providers)));
return factory;
}
@@ -698,7 +698,7 @@ public boolean apply(FactoryMachine input) {
private final boolean usedServiceLoader;
private final ImmutableList<FactoryMachine> machines;
private final ImmutableMultimap<String, FactoryMachine> machinesByBuilder;
private final Warehouse warehouse;
private final StdWarehouse warehouse;
private final ImmutableList<ComponentCustomizerEngine> customizerEngines;
private final String id;
private final Object dumper = new Object() { public String toString() { return Factory.this.dump(); } };
@@ -709,7 +709,7 @@ public boolean apply(FactoryMachine input) {
private MetricRegistry metrics;
private Factory(boolean usedServiceLoader, Multimap<String, FactoryMachine> machines,
ImmutableList<ComponentCustomizerEngine> customizerEngines, Warehouse warehouse) {
ImmutableList<ComponentCustomizerEngine> customizerEngines, StdWarehouse warehouse) {
this.usedServiceLoader = usedServiceLoader;
this.customizerEngines = customizerEngines;
@@ -760,14 +760,14 @@ public Factory concat(FactoryMachine machine) {
machines.removeAll("WarehouseProvidersMachine");
machines.put("IndividualMachines", machine);
return new Factory(usedServiceLoader, machines, customizerEngines,
new Warehouse(warehouse.getProviders()));
new StdWarehouse(warehouse.getProviders()));
}
public String getId() {
return id;
}
public Warehouse getWarehouse() {
public StdWarehouse getWarehouse() {
return warehouse;
}
@@ -22,23 +22,23 @@
* Date: 1/31/13
* Time: 5:47 PM
*/
public class Warehouse implements AutoCloseable {
public class StdWarehouse implements AutoCloseable {
private static final AtomicLong ID = new AtomicLong();
public Warehouse() {
this(ImmutableList.<Warehouse>of());
public StdWarehouse() {
this(ImmutableList.<StdWarehouse>of());
}
public Warehouse(ImmutableList<Warehouse> providers) {
public StdWarehouse(ImmutableList<StdWarehouse> providers) {
this.providers = providers;
StringBuilder sb = new StringBuilder();
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
sb.append("<<").append(provider.getId());
}
this.id = String.format("%03d%s", ID.incrementAndGet(), sb.toString());
}
public ImmutableList<Warehouse> getProviders() {
public ImmutableList<StdWarehouse> getProviders() {
return providers;
}
@@ -69,29 +69,29 @@ public String toString() {
}
}
private static final Logger logger = LoggerFactory.getLogger(Warehouse.class);
private static final Logger logger = LoggerFactory.getLogger(StdWarehouse.class);
private final String id;
private final ConcurrentMap<Name<?>, StoredBox<?>> boxes = new ConcurrentHashMap<>();
private final ImmutableList<Warehouse> providers;
private final ImmutableList<StdWarehouse> providers;
public String getId() {
return id;
}
@SuppressWarnings("unchecked")
<T> Optional<StoredBox<T>> getStoredBox(Name<T> name) {
public <T> Optional<StoredBox<T>> getStoredBox(Name<T> name) {
return Optional.fromNullable((StoredBox<T>) boxes.get(name));
}
@SuppressWarnings("unchecked")
<T> Optional<NamedComponent<T>> checkOut(Name<T> name) {
public <T> Optional<NamedComponent<T>> checkOut(Name<T> name) {
StoredBox<T> storedBox = (StoredBox<T>) boxes.get(name);
if (storedBox != null) {
return storedBox.box.pick();
}
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
Optional<NamedComponent<T>> component = provider.checkOut(name);
if (component.isPresent()) {
return component;
@@ -101,7 +101,7 @@ public String getId() {
return Optional.absent();
}
<T> void checkIn(ComponentBox<T> componentBox, SatisfiedBOM satisfiedBOM) {
public <T> void checkIn(ComponentBox<T> componentBox, SatisfiedBOM satisfiedBOM) {
StoredBox<?> previousBox = boxes.put(componentBox.getName(), new StoredBox<>(componentBox, satisfiedBOM));
if (previousBox != null) {
try {
@@ -137,7 +137,7 @@ public void close() {
public Iterable<Name<?>> listNames() {
Set<Name<?>> names = new LinkedHashSet<>();
names.addAll(boxes.keySet());
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
Iterables.addAll(names, provider.listNames());
}
@@ -153,7 +153,7 @@ public void close() {
}
return deps;
} else {
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
Iterable<Name<?>> deps = provider.listDependencies(name);
if (!Iterables.isEmpty(deps)) {
return deps;
@@ -14,16 +14,16 @@
* Time: 21:24
*/
public class WarehouseProvidersMachine implements FactoryMachine {
private final ImmutableList<Warehouse> providers;
private final ImmutableList<StdWarehouse> providers;
public WarehouseProvidersMachine(ImmutableList<Warehouse> providers) {
public WarehouseProvidersMachine(ImmutableList<StdWarehouse> providers) {
this.providers = providers;
}
@Override
@SuppressWarnings("unchecked")
public boolean canBuild(Name<?> name) {
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
if (Iterables.tryFind(provider.listNames(),
(Predicate<? super Name<?>>) Predicates.equalTo(name)).isPresent()) {
return true;
@@ -73,8 +73,8 @@ public String toString() {
}
private <T> BillOfMaterials findBomFor(Name<T> name) {
for (Warehouse provider : providers) {
Optional<Warehouse.StoredBox<T>> storedBox = provider.getStoredBox(name);
for (StdWarehouse provider : providers) {
Optional<StdWarehouse.StoredBox<T>> storedBox = provider.getStoredBox(name);
if (storedBox.isPresent()) {
return storedBox.get().getSatisfiedBOM().getBom();
}
@@ -84,7 +84,7 @@ public String toString() {
}
private <T> Optional<NamedComponent<T>> findComponent(Name<T> name) {
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
Optional<NamedComponent<T>> componentOptional = provider.checkOut(name);
if (componentOptional.isPresent()) {
return componentOptional;
@@ -97,7 +97,7 @@ public String toString() {
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
Set<Name<T>> names = new LinkedHashSet<>();
for (Warehouse provider : providers) {
for (StdWarehouse provider : providers) {
for (Name<?> name : provider.listNames()) {
if (componentClass.isAssignableFrom(name.getClazz())) {
names.add((Name<T>) name);
@@ -11,11 +11,11 @@
* Date: 1/31/13
* Time: 6:34 PM
*/
public class WarehouseTest {
public class StdWarehouseTest {
@Test
public void should_checkin_and_checkout_from_disposable_box() throws Exception {
Warehouse warehouse = new Warehouse();
StdWarehouse warehouse = new StdWarehouse();
warehouse.checkIn(new DisposableComponentBox<>(NamedComponent.of(String.class, "name", "test")),
new SatisfiedBOM(BillOfMaterials.EMPTY, ImmutableMultimap.<Factory.Query<?>, NamedComponent<?>>of()));
@@ -32,7 +32,7 @@ public void should_checkin_and_checkout_from_disposable_box() throws Exception {
@Test
public void should_checkin_and_checkout_from_boundless_box() throws Exception {
Warehouse warehouse = new Warehouse();
StdWarehouse warehouse = new StdWarehouse();
warehouse.checkIn(new BoundlessComponentBox<>(NamedComponent.of(String.class, "name", "test")),
new SatisfiedBOM(BillOfMaterials.EMPTY, ImmutableMultimap.<Factory.Query<?>, NamedComponent<?>>of()));

0 comments on commit 76b034b

Please sign in to comment.