0

I have this class over here, and I was wondering how would one go about creating a value object for it. Please note I want to be able to compare two reports, so that two reports are only equal if and only if all the contents inside the arrays, name and commission are also equal.

So my value object would need to override the equals method.

Concrete Class

public class ReportImpl implements Report {

private String name;
private double commissionPerEmployee;
private double[] legalData;
private double[] cashFlowData;
private double[] mergesData;
private double[] tallyingData;
private double[] deductionsData;

public ReportImpl(String name,
                  double commissionPerEmployee,
                  double[] legalData,
                  double[] cashFlowData,
                  double[] mergesData,
                  double[] tallyingData,
                  double[] deductionsData) {
    this.name = name;
    this.commissionPerEmployee = commissionPerEmployee;
    this.legalData = legalData;
    this.cashFlowData = cashFlowData;
    this.mergesData = mergesData;
    this.tallyingData = tallyingData;
    this.deductionsData = deductionsData;
}

@Override
public String getReportName() {
    return name;
}

@Override
public double getCommission() {
    return commissionPerEmployee;
}


    @Override
public double[] getLegalData() {
        return legalData;
}

@Override
public double[] getCashFlowData() {
    return cashFlowData;

}

@Override
public double[] getMergesData() {
    return mergesData;

}

@Override
public double[] getTallyingData() {
    return tallyingData;
}

@Override
public double[] getDeductionsData() {
    return deductionsData;

}

@Override
public String toString() {

    return String.format("%s", name);
}


}


public interface Report {

/*
 * Note from Tim: You would think you could use reportName as an id, but there's more than
 * 1700 cases of duplicate name in the database where the marketing guys decided to call
 * literally the same accounting work 10 different things (with different prices)
 */
String getReportName();
double getCommission();
double[] getLegalData();
double[] getCashFlowData();
double[] getMergesData();
double[] getTallyingData();
double[] getDeductionsData();
}
Kein
  • 348
  • 1
  • 3
  • 15
  • Note the arrays need to be immutable – TheNotoriousCoder May 20 '20 at 05:41
  • There is no [Immutable array in Java](https://stackoverflow.com/questions/3700971/immutable-array-in-java). – jaco0646 May 20 '20 at 13:05
  • You can make the internal members essentially immutable by cloning prior to assignment in constructor, and returning clones from getters. It doesn't prevent calling code from mutating the clones, but protects the internal state. That's as close as you can get with arrays, to my knowledge. – Taylor May 20 '20 at 16:20

0 Answers0