HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Organization data class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dataclassorganization

Problem

I believe I have made the following data class quite decently, and I'd like a thorough review on it.

The code is built on Java 8 and uses the Builder and Serialization Proxy pattern, it is used to store results from a web service call, and the only certainty I have is that the organization id will be an integer, as it's most likely used as a primary key in some database.

```
public class Organization implements Serializable {
private static final long serialVersionUID = 28484399283838343L;

private final int organizationId;

private final String accountNumber;
private final String address;
private final String branchCode;
private final String chamberOfCommerce;
private final String city;
private final String ibanNumber;
private final String organizationName;
private final String postalAddress;
private final String postalCity;
private final String postalZipCode;
private final String vatNumber;
private final String zipCode;

Organization(final int organizationId, final String accountNumber, final String address, final String branchCode, final String chamberOfCommerce, final String city, final String ibanNumber, final String organizationName, final String postalAddress, final String postalCity, final String postalZipCode, final String vatNumber, final String zipCode) {
this.organizationId = organizationId;
this.accountNumber = Objects.requireNonNull(accountNumber, "accountNumber");
this.address = Objects.requireNonNull(address, "address");
this.branchCode = Objects.requireNonNull(branchCode, "branchCode");
this.chamberOfCommerce = Objects.requireNonNull(chamberOfCommerce, "chamberOfCommerce");
this.city = Objects.requireNonNull(city, "city");
this.ibanNumber = Objects.requireNonNull(ibanNumber, "ibanNumber");
this.organizationName = Objects.requireNonNull(organizationName, "organizationName");
this.postalAddress = Objects.requireNonNull(

Solution

This code is begging for an EnumMap as the data store, rather than the individual variables.

Creating an Enum with values like:

public enum OrganizationField {ACCOUNT_NUMBER, ZIP_CODE, ....};


In your Organization you would have:

private final EnumMap fieldMap = new ...;


and methods like:

public String getAccountNumber() {
    return fieldMap.get(OrganizationField.ACCOUNT_NUMBER);
}


Then, your class can simply have an EnumMap and the organizationId as fields. The serialization should serialize the map, and then, if the fields change, the serialization does not need to change. All you have to do is add a new Enum member, and a new 'getter' that pulls that Enum's value from the map.

Similarly, the OrganizationBuilder can simply loop through the Enum to make sure all members are populated in the map before construction:

for (OrganizationField f : OrganizationField.values()) {
    if (!fieldMap.containsKey(f)) {
        throw new IllegalStateException("Field " + f + " has not been specified");
    }
}


which gives the added bonus of knowing which field was not populated.

Code Snippets

public enum OrganizationField {ACCOUNT_NUMBER, ZIP_CODE, ....};
private final EnumMap<OrganizationField,String> fieldMap = new ...;
public String getAccountNumber() {
    return fieldMap.get(OrganizationField.ACCOUNT_NUMBER);
}
for (OrganizationField f : OrganizationField.values()) {
    if (!fieldMap.containsKey(f)) {
        throw new IllegalStateException("Field " + f + " has not been specified");
    }
}

Context

StackExchange Code Review Q#55205, answer score: 10

Revisions (0)

No revisions yet.