Skip to content

Adds a predicate to DataLoaderRegistry and a per dataloader map of pedicates is also possible #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 8, 2023
Merged
Prev Previous commit
Next Next commit
Adds a predicate to ScheduledDataLoaderRegistry - removed generic bui…
…lders
  • Loading branch information
bbakerman committed Oct 3, 2023
commit 69528f1c41464fe312431187f058132c7c830ace
17 changes: 6 additions & 11 deletions src/main/java/org/dataloader/DataLoaderRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DataLoaderRegistry {
public DataLoaderRegistry() {
}

protected DataLoaderRegistry(Builder<?> builder) {
private DataLoaderRegistry(Builder builder) {
this.dataLoaders.putAll(builder.dataLoaders);
}

Expand Down Expand Up @@ -179,15 +179,10 @@ public static Builder newRegistry() {
return new Builder();
}

public static class Builder<B extends Builder<B>> {
public static class Builder {

private final Map<String, DataLoader<?, ?>> dataLoaders = new HashMap<>();

protected B self() {
//noinspection unchecked
return (B) this;
}

/**
* This will register a new dataloader
*
Expand All @@ -196,9 +191,9 @@ protected B self() {
*
* @return this builder for a fluent pattern
*/
public B register(String key, DataLoader<?, ?> dataLoader) {
public Builder register(String key, DataLoader<?, ?> dataLoader) {
dataLoaders.put(key, dataLoader);
return self();
return this;
}

/**
Expand All @@ -209,9 +204,9 @@ public B register(String key, DataLoader<?, ?> dataLoader) {
*
* @return this builder for a fluent pattern
*/
public B registerAll(DataLoaderRegistry otherRegistry) {
public Builder registerAll(DataLoaderRegistry otherRegistry) {
dataLoaders.putAll(otherRegistry.dataLoaders);
return self();
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements A
private volatile boolean closed;

private ScheduledDataLoaderRegistry(Builder builder) {
super(builder);
super();
this.dataLoaders.putAll(builder.dataLoaders);
this.scheduledExecutorService = builder.scheduledExecutorService;
this.schedule = builder.schedule;
this.closed = false;
Expand Down Expand Up @@ -215,23 +216,35 @@ public static Builder newScheduledRegistry() {
return new Builder();
}

public static class Builder extends DataLoaderRegistry.Builder<ScheduledDataLoaderRegistry.Builder> {
public static class Builder {

private final Map<String, DataLoader<?, ?>> dataLoaders = new LinkedHashMap<>();
private final Map<DataLoader<?, ?>, DispatchPredicate> dataLoaderPredicates = new LinkedHashMap<>();
private DispatchPredicate dispatchPredicate = DispatchPredicate.DISPATCH_ALWAYS;
private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private Duration schedule = Duration.ofMillis(10);

private final Map<DataLoader<?, ?>, DispatchPredicate> dataLoaderPredicates = new ConcurrentHashMap<>();

private DispatchPredicate dispatchPredicate = DispatchPredicate.DISPATCH_ALWAYS;

public Builder scheduledExecutorService(ScheduledExecutorService executorService) {
this.scheduledExecutorService = nonNull(executorService);
return self();
return this;
}

public Builder schedule(Duration schedule) {
this.schedule = schedule;
return self();
return this;
}

/**
* This will register a new dataloader
*
* @param key the key to put the data loader under
* @param dataLoader the data loader to register
*
* @return this builder for a fluent pattern
*/
public Builder register(String key, DataLoader<?, ?> dataLoader) {
dataLoaders.put(key, dataLoader);
return this;
}


Expand All @@ -247,7 +260,7 @@ public Builder schedule(Duration schedule) {
public Builder register(String key, DataLoader<?, ?> dataLoader, DispatchPredicate dispatchPredicate) {
register(key, dataLoader);
dataLoaderPredicates.put(dataLoader, dispatchPredicate);
return self();
return this;
}

/**
Expand All @@ -259,12 +272,12 @@ public Builder register(String key, DataLoader<?, ?> dataLoader, DispatchPredica
* @return this builder for a fluent pattern
*/
public Builder registerAll(DataLoaderRegistry otherRegistry) {
super.registerAll(otherRegistry);
dataLoaders.putAll(otherRegistry.getDataLoadersMap());
if (otherRegistry instanceof ScheduledDataLoaderRegistry) {
ScheduledDataLoaderRegistry other = (ScheduledDataLoaderRegistry) otherRegistry;
dataLoaderPredicates.putAll(other.dataLoaderPredicates);
}
return self();
return this;
}

/**
Expand All @@ -277,7 +290,7 @@ public Builder registerAll(DataLoaderRegistry otherRegistry) {
*/
public Builder dispatchPredicate(DispatchPredicate dispatchPredicate) {
this.dispatchPredicate = dispatchPredicate;
return self();
return this;
}

/**
Expand Down