Enforce immutability of fields in request and response objects.

This commit is contained in:
Scott Frederick
2018-10-17 16:46:44 -05:00
parent 4ea4f2647d
commit deb051568d
12 changed files with 65 additions and 58 deletions

View File

@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
/**
* Fields common to all types of CredHub requests.
@@ -135,18 +136,14 @@ public class CredHubRequest<T> {
if (additionalPermissions != null ?
additionalPermissions.equals(that.additionalPermissions) : that.additionalPermissions == null) return false;
if (details != null ? !details.equals(that.details) : that.details != null) return false;
if (mode != null ? !mode.equals(that.mode) : that.mode != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (overwrite ? 1 : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (credentialType != null ? credentialType.hashCode() : 0);
result = 31 * result + (additionalPermissions != null ? additionalPermissions.hashCode() : 0);
result = 31 * result + (details != null ? details.hashCode() : 0);
return result;
return Objects.hash(overwrite, name, credentialType, additionalPermissions, details, mode);
}
/**

View File

@@ -19,6 +19,8 @@ package org.springframework.credhub.support;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.util.Objects;
/**
* The details of a credential that has been written to CredHub. Clients don't
* typically instantiate objects of this type, but will receive them in response
@@ -28,18 +30,21 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
* @author Scott Frederick
*/
public class CredentialDetails<T> extends CredentialSummary {
private String id;
private final String id;
@JsonProperty("type")
private CredentialType credentialType;
private final CredentialType credentialType;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
private T value;
private final T value;
/**
* Create a {@link CredentialDetails}.
*/
public CredentialDetails() {
this.id = null;
this.credentialType = null;
this.value = null;
}
/**
@@ -107,13 +112,7 @@ public class CredentialDetails<T> extends CredentialSummary {
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (credentialType != null ? credentialType.hashCode() : 0);
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result
+ (versionCreatedAt != null ? versionCreatedAt.hashCode() : 0);
return result;
return Objects.hash(id, name, credentialType, value, versionCreatedAt);
}
@Override

View File

@@ -20,6 +20,7 @@ package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A collection of {@link CredentialDetails}. Clients don't typically instantiate
@@ -29,12 +30,13 @@ import java.util.List;
* @author Scott Frederick
*/
public class CredentialDetailsData<T> {
private List<CredentialDetails<T>> data;
private final List<CredentialDetails<T>> data;
/**
* Create a {@link CredentialDetailsData}.
*/
public CredentialDetailsData() {
this.data = null;
}
/**
@@ -74,9 +76,7 @@ public class CredentialDetailsData<T> {
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
return Objects.hashCode(data);
}
@Override

View File

@@ -17,6 +17,7 @@
package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -32,7 +33,7 @@ import org.springframework.util.StringUtils;
*/
public class CredentialName {
@JsonIgnore
protected final String[] segments;
final String[] segments;
/**
* Create a name from the provided value. The name must consist of segments
@@ -93,6 +94,6 @@ public class CredentialName {
@Override
public int hashCode() {
return Arrays.hashCode(segments);
return Objects.hashCode(segments);
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.credhub.support;
import java.util.Objects;
/**
* The path to a credential that has been written to CredHub. Clients don't typically
* instantiate objects of this type, but will receive them in response to requests.
@@ -23,12 +25,14 @@ package org.springframework.credhub.support;
* @author Scott Frederick
*/
public class CredentialPath {
protected String path;
private final String path;
/**
* Create a {@link CredentialPath}. Intended for internal use.
*/
CredentialPath() {
@SuppressWarnings("unused")
private CredentialPath() {
this.path = null;
}
/**
@@ -65,7 +69,7 @@ public class CredentialPath {
@Override
public int hashCode() {
return path != null ? path.hashCode() : 0;
return Objects.hashCode(path);
}
@Override

View File

@@ -18,6 +18,7 @@ package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A collection of {@link CredentialPath}s. Clients don't typically instantiate
@@ -26,12 +27,14 @@ import java.util.List;
* @author Scott Frederick
*/
public class CredentialPathData {
private List<CredentialPath> paths;
private final List<CredentialPath> paths;
/**
* Create a {@link CredentialPathData}.
*/
CredentialPathData() {
@SuppressWarnings("unused")
private CredentialPathData() {
this.paths = null;
}
/**
@@ -71,9 +74,7 @@ public class CredentialPathData {
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (paths != null ? paths.hashCode() : 0);
return result;
return Objects.hashCode(paths);
}
@Override

View File

@@ -22,6 +22,7 @@ import org.springframework.credhub.support.permissions.CredentialPermission;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A collection of {@link CredentialPermission}s. Clients don't typically instantiate
@@ -31,13 +32,16 @@ import java.util.List;
* @author Scott Frederick
*/
public class CredentialPermissions {
private CredentialName credentialName;
private List<CredentialPermission> permissions;
private final CredentialName credentialName;
private final List<CredentialPermission> permissions;
/**
* Create a {@link CredentialPermissions}.
*/
public CredentialPermissions() {
@SuppressWarnings("unused")
private CredentialPermissions() {
this.credentialName = null;
this.permissions = null;
}
/**
@@ -93,8 +97,6 @@ public class CredentialPermissions {
@Override
public int hashCode() {
int result = credentialName != null ? credentialName.hashCode() : 0;
result = 31 * result + (permissions != null ? permissions.hashCode() : 0);
return result;
return Objects.hash(credentialName, permissions);
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.credhub.support;
import java.util.Date;
import java.util.Objects;
/**
* A summary of a credential that has been written to CredHub. Clients don't typically
@@ -26,13 +27,15 @@ import java.util.Date;
* @author Scott Frederick
*/
public class CredentialSummary {
protected CredentialName name;
protected Date versionCreatedAt;
protected final CredentialName name;
protected final Date versionCreatedAt;
/**
* Create a {@link CredentialSummary}. Intended for internal use.
*/
CredentialSummary() {
this.name = null;
this.versionCreatedAt = null;
}
/**
@@ -82,10 +85,7 @@ public class CredentialSummary {
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result
+ (versionCreatedAt != null ? versionCreatedAt.hashCode() : 0);
return result;
return Objects.hash(name, versionCreatedAt);
}
@Override

View File

@@ -18,6 +18,7 @@ package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A collection of {@link CredentialSummary}s. Clients don't typically instantiate
@@ -27,12 +28,14 @@ import java.util.List;
* @author Scott Frederick
*/
public class CredentialSummaryData {
private List<CredentialSummary> credentials;
private final List<CredentialSummary> credentials;
/**
* Create a {@link CredentialSummaryData}.
*/
CredentialSummaryData() {
@SuppressWarnings("unused")
private CredentialSummaryData() {
this.credentials = null;
}
/**
@@ -72,9 +75,7 @@ public class CredentialSummaryData {
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (credentials != null ? credentials.hashCode() : 0);
return result;
return Objects.hashCode(credentials);
}
@Override

View File

@@ -18,6 +18,8 @@ package org.springframework.credhub.support;
import org.springframework.util.Assert;
import java.util.Objects;
/**
* A base type for a credential that contains a single string value.
*
@@ -50,7 +52,7 @@ public class StringCredential {
@Override
public int hashCode() {
return value != null ? value.hashCode() : 0;
return Objects.hashCode(value);
}
@Override

View File

@@ -22,6 +22,8 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.util.Assert;
import java.util.Objects;
import static org.springframework.credhub.support.permissions.ActorType.APP;
import static org.springframework.credhub.support.permissions.ActorType.OAUTH_CLIENT;
import static org.springframework.credhub.support.permissions.ActorType.USER;
@@ -32,8 +34,8 @@ import static org.springframework.credhub.support.permissions.ActorType.USER;
* @author Scott Frederick
*/
public class Actor {
private ActorType authType;
private String primaryIdentifier;
private final ActorType authType;
private final String primaryIdentifier;
/**
* Create a new {@literal Actor}.
@@ -168,8 +170,6 @@ public class Actor {
@Override
public int hashCode() {
int result = authType.hashCode();
result = 31 * result + primaryIdentifier.hashCode();
return result;
return Objects.hash(authType, primaryIdentifier);
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* Permissions applied to a credential in CredHub. If provided when a
@@ -38,14 +39,15 @@ import java.util.List;
* @author Scott Frederick
*/
public class CredentialPermission {
private Actor actor;
private final Actor actor;
@JsonProperty
private List<Operation> operations;
private final List<Operation> operations;
/**
* Create a {@literal CredentialPermission}.
*/
@SuppressWarnings("unused")
private CredentialPermission() {
this.actor = null;
this.operations = null;
@@ -129,9 +131,7 @@ public class CredentialPermission {
@Override
public int hashCode() {
int result = actor != null ? actor.hashCode() : 0;
result = 31 * result + (operations != null ? operations.hashCode() : 0);
return result;
return Objects.hash(actor, operations);
}
@Override