Skip to content

Commit

Permalink
Implement builder pattern in fed context
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-zack committed Nov 28, 2024
1 parent 9fea626 commit 2ddc132
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

public class FederationContext {
private final Federation activeFederation;
private Federation retiringFederation;
private Script lastRetiredFederationP2SHScript;
private final Federation retiringFederation;
private final Script lastRetiredFederationP2SHScript;

public FederationContext(Federation activeFederation) {
this.activeFederation = activeFederation;
private FederationContext(FederationContextBuilder federationContextBuilder) {
this.activeFederation = federationContextBuilder.activeFederation;
this.retiringFederation = federationContextBuilder.retiringFederation;
this.lastRetiredFederationP2SHScript = federationContextBuilder.lastRetiredFederationP2SHScript;
}

public Federation getActiveFederation() {
Expand All @@ -23,21 +25,43 @@ public Optional<Federation> getRetiringFederation() {
return Optional.ofNullable(retiringFederation);
}

public void setRetiringFederation(Federation retiringFederation) {
this.retiringFederation = retiringFederation;
}

public Optional<Script> getLastRetiredFederationP2SHScript() {
return Optional.ofNullable(lastRetiredFederationP2SHScript);
}

public void setLastRetiredFederationP2SHScript(Script lastRetiredFederationP2SHScript) {
this.lastRetiredFederationP2SHScript = lastRetiredFederationP2SHScript;
}

public List<Federation> getLiveFederations() {
return Stream.of(activeFederation, retiringFederation)
.filter(Objects::nonNull)
.toList();
}

public static FederationContextBuilder builder() {
return new FederationContextBuilder();
}

public static class FederationContextBuilder {
private Federation activeFederation;
private Federation retiringFederation;
private Script lastRetiredFederationP2SHScript;

public FederationContextBuilder setActiveFederation(Federation activeFederation) {
this.activeFederation = Objects.requireNonNull(activeFederation, "Active federation must not be null");
return this;
}

public FederationContextBuilder setRetiringFederation(Federation retiringFederation) {
this.retiringFederation = retiringFederation;
return this;
}

public FederationContextBuilder setLastRetiredFederationP2SHScript(Script lastRetiredFederationP2SHScript) {
this.lastRetiredFederationP2SHScript = lastRetiredFederationP2SHScript;
return this;
}

public FederationContext build() {
Objects.requireNonNull(activeFederation, "Active federation is required");
return new FederationContext(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,16 @@ public List<Federation> getLiveFederations() {

@Override
public FederationContext getFederationContext() {
FederationContext federationContext = new FederationContext(getActiveFederation());
FederationContext.FederationContextBuilder federationContextBuilder = FederationContext.builder();
federationContextBuilder.setActiveFederation(getActiveFederation());

Optional.ofNullable(getRetiringFederation())
.ifPresent(federationContext::setRetiringFederation);
.ifPresent(federationContextBuilder::setRetiringFederation);

provider.getLastRetiredFederationP2SHScript(activations)
.ifPresent(federationContext::setLastRetiredFederationP2SHScript);
.ifPresent(federationContextBuilder::setLastRetiredFederationP2SHScript);

return federationContext;
return federationContextBuilder.build();
}

@Override
Expand Down
Loading

0 comments on commit 2ddc132

Please sign in to comment.