Skip to content

Commit

Permalink
Use builder for ClientContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Reznik committed May 15, 2015
1 parent c100db2 commit dbe1d2e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/contentful/java/cda/CDAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public class CDAClient {
final Gson gson;
final SpaceWrapper spaceWrapper;
final Executor callbackExecutor;
final boolean skipLinks;

// Modules
final ModuleAssets moduleAssets;
final ModuleContentTypes moduleContentTypes;
final ModuleEntries moduleEntries;
final ModuleSpaces moduleSpaces;
final ModuleSync moduleSync;
final boolean nullifyUnresolved;

private CDAClient(Builder builder) {
if (builder.accessToken == null) {
Expand All @@ -82,19 +84,29 @@ private CDAClient(Builder builder) {
this.callbackExecutor = createCallbackExecutor(builder);
this.gson = createGson();
this.service = createRetrofitService(builder);
this.skipLinks = builder.skipLinks;
this.nullifyUnresolved = builder.nullifyUnresolved;

// Modules
ClientContext context =
new ClientContext(service, callbackExecutor, spaceKey, gson, spaceWrapper, classMap,
builder.skipLinks, builder.nullifyUnresolved);

ClientContext context = createContext();
this.moduleAssets = new ModuleAssets(context);
this.moduleContentTypes = new ModuleContentTypes(context);
this.moduleEntries = new ModuleEntries(context);
this.moduleSpaces = new ModuleSpaces(context);
this.moduleSync = new ModuleSync(context);
}

private ClientContext createContext() {
return ClientContext.builder()
.setService(service)
.setCallbackExecutor(callbackExecutor)
.setSpaceId(spaceKey)
.setGson(gson)
.setCustomTypesMap(classMap)
.setSkipLinks(skipLinks)
.setNullifyUnresolved(nullifyUnresolved)
.build();
}

/**
* Returns the {@code CDASpace} object associated with this client. Note that a Space is attached
* to a client only <b>after</b> it's first request was successfully executed.
Expand Down
86 changes: 75 additions & 11 deletions src/main/java/com/contentful/java/cda/ClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,80 @@ final class ClientContext {
final boolean skipLinks;
final boolean nullifyUnresolved;

public ClientContext(CDAService service, Executor callbackExecutor, String spaceId, Gson gson,
SpaceWrapper spaceWrapper, Map<String, Class<?>> customTypesMap, boolean skipLinks,
boolean nullifyUnresolved) {
this.service = service;
this.callbackExecutor = callbackExecutor;
this.spaceId = spaceId;
this.gson = gson;
this.spaceWrapper = spaceWrapper;
this.customTypesMap = customTypesMap;
this.skipLinks = skipLinks;
this.nullifyUnresolved = nullifyUnresolved;
private ClientContext() {
throw new AssertionError();
}

public ClientContext(Builder builder) {
this.service = builder.service;
this.callbackExecutor = builder.callbackExecutor;
this.spaceId = builder.spaceId;
this.gson = builder.gson;
this.spaceWrapper = builder.spaceWrapper;
this.customTypesMap = builder.customTypesMap;
this.skipLinks = builder.skipLinks;
this.nullifyUnresolved = builder.nullifyUnresolved;
}

static Builder builder() {
return new Builder();
}

static class Builder {
private CDAService service;
private Executor callbackExecutor;
private String spaceId;
private Gson gson;
private SpaceWrapper spaceWrapper;
private Map<String, Class<?>> customTypesMap;
private boolean skipLinks;
private boolean nullifyUnresolved;

private Builder() {
}

public Builder setService(CDAService service) {
this.service = service;
return this;
}

public Builder setCallbackExecutor(Executor callbackExecutor) {
this.callbackExecutor = callbackExecutor;
return this;
}

public Builder setSpaceId(String spaceId) {
this.spaceId = spaceId;
return this;
}

public Builder setGson(Gson gson) {
this.gson = gson;
return this;
}

public Builder setSpaceWrapper(SpaceWrapper spaceWrapper) {
this.spaceWrapper = spaceWrapper;
return this;
}

public Builder setCustomTypesMap(Map<String, Class<?>> customTypesMap) {
this.customTypesMap = customTypesMap;
return this;
}

public Builder setSkipLinks(boolean skipLinks) {
this.skipLinks = skipLinks;
return this;
}

public Builder setNullifyUnresolved(boolean nullifyUnresolved) {
this.nullifyUnresolved = nullifyUnresolved;
return this;
}

public ClientContext build() {
return new ClientContext(this);
}
}
}

0 comments on commit dbe1d2e

Please sign in to comment.