Skip to content
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

forge: add builder for HostUser #890

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -193,19 +193,17 @@ Optional<String> authId() {
return Optional.empty();
}

private String getFullName(String username) {
var details = user(username);
return details.get().fullName();
}

// Most GitHub API's return user information in this format
HostUser parseUserField(JSONValue json) {
return parseUserObject(json.get("user"));
}

HostUser parseUserObject(JSONValue json) {
return HostUser.create(json.get("id").asInt(), json.get("login").asString(),
() -> getFullName(json.get("login").asString()));
return HostUser.builder()
.id(json.get("id").asInt())
.username(json.get("login").asString())
.supplier(() -> user(json.get("login").asString()).orElseThrow())
.build();
}

@Override
@@ -284,7 +282,12 @@ private static HostUser asHostUser(JSONObject details) {
name = login;
}
var email = details.get("email").asString();
return HostUser.create(id, login, name, email);
return HostUser.builder()
.id(id)
.username(login)
.fullName(name)
.email(email)
.build();
}

@Override
@@ -149,7 +149,12 @@ HostUser parseAuthorObject(JSONObject o) {
var username = o.get("username").asString();
var name = o.get("name").asString();
var email = o.get("email").asString();
return HostUser.create(id, username, name, email);
return HostUser.builder()
.id(id)
.username(username)
.fullName(name)
.email(email)
.build();
}

@Override
@@ -27,90 +27,125 @@
import java.util.function.Supplier;

public class HostUser {
private final String id;
private final String username;
private final Supplier<String> nameSupplier;
private String name;
private String id;
private String username;
private String fullName;
private String email;
private final Supplier<HostUser> supplier;

public static class Builder {
private String id;
private String username;
private String fullName;
private String email;
private Supplier<HostUser> supplier;

public Builder id(int id) {
this.id = String.valueOf(id);
return this;
}

private HostUser(String id, String username, String name) {
this.id = id ;
this.username = username;
this.nameSupplier = null;
this.name = name;
this.email = null;
}
public Builder id(String id) {
this.id = id;
return this;
}

private HostUser(String id, String username, Supplier<String> nameSupplier) {
this.id = id ;
this.username = username;
this.nameSupplier = nameSupplier;
this.name = null;
this.email = null;
}
public Builder username(String username) {
this.username = username;
return this;
}

private HostUser(String id, String username, String name, String email) {
this.id = id ;
this.username = username;
this.nameSupplier = null;
this.name = name;
this.email = email;
}
public Builder fullName(String fullName) {
this.fullName = fullName;
return this;
}

public static HostUser create(int id, String username, Supplier<String> nameSupplier) {
return new HostUser(String.valueOf(id), username, nameSupplier);
public Builder email(String email) {
this.email = email;
return this;
}

public Builder supplier(Supplier<HostUser> supplier) {
this.supplier = supplier;
return this;
}

public HostUser build() {
return new HostUser(id, username, fullName, email, supplier);
}
}

public static HostUser create(int id, String username, String name, String email) {
return new HostUser(String.valueOf(id), username, name, email);
private HostUser(String id, String username, String fullName, String email, Supplier<HostUser> supplier) {
this.id = id;
this.username = username;
this.fullName = fullName;
this.email = email;
this.supplier = supplier;
}

public static HostUser create(String id, String username, String name, String email) {
return new HostUser(id, username, name, email);
public static Builder builder() {
return new Builder();
}

public static HostUser create(String id, String username, String name) {
return new HostUser(id, username, name);
public static HostUser create(String id, String username, String fullName) {
return builder().id(id).username(username).fullName(fullName).build();
}

public static HostUser create(int id, String username, String name) {
return new HostUser(String.valueOf(id), username, name);
public static HostUser create(int id, String username, String fullName) {
return builder().id(id).username(username).fullName(fullName).build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (other == null || getClass() != other.getClass()) {
return false;
}
HostUser that = (HostUser) o;
return id.equals(that.id) &&
Objects.equals(username, that.username);
HostUser o = (HostUser) other;
return Objects.equals(id(), o.id()) &&
Objects.equals(username(), o.username());
}

@Override
public int hashCode() {
return Objects.hash(id, username);
}

private void update() {
var result = supplier.get();
id = result.id;
username = result.username;
fullName = result.fullName;
email = result.email;
}

public String id() {
if (id == null) {
update();
}
return id;
}

public String username() {
if (username == null) {
update();
}
return username;
}

public String fullName() {
if (name == null) {
name = nameSupplier.get();
if (fullName == null) {
update();
}
return name;
return fullName;
}

public Optional<String> email() {
if (id == null || username == null || fullName == null) {
update();
}
return Optional.ofNullable(email);
}

@@ -119,7 +154,7 @@ public String toString() {
return "HostUserDetails{" +
"id=" + id +
", username='" + username + '\'' +
", name='" + name + '\'' +
", fullName='" + fullName + '\'' +
'}';
}
}
@@ -114,10 +114,12 @@ public Optional<HostUser> user(String username) {
public HostUser currentUser() {
if (currentUser == null) {
var data = request.get("myself").execute();
currentUser = HostUser.create(data.get("name").asString(),
data.get("name").asString(),
data.get("displayName").asString(),
data.get("emailAddress").asString());
currentUser = HostUser.builder()
.id(data.get("name").asString())
.username(data.get("name").asString())
.fullName(data.get("displayName").asString())
.email(data.get("emailAddress").asString())
.build();
}
return currentUser;
}
@@ -434,9 +434,12 @@ public Optional<HostUser> findUser(String findBy) {
return Optional.empty();
}
var data = user.asArray().get(0);
return Optional.of(HostUser.create(data.get("name").asString(),
data.get("name").asString(),
data.get("displayName").asString(),
data.get("emailAddress").asString()));
var hostUser = HostUser.builder()
.id(data.get("name").asString())
.username(data.get("name").asString())
.fullName(data.get("displayName").asString())
.email(data.get("emailAddress").asString())
.build();
return Optional.of(hostUser);
}
}