Commit 2ca4b967 authored by Rob's avatar Rob

Implement Comparable, #equals(Object), and #hashCode()

parent a094e9ff
......@@ -33,6 +33,7 @@
package edu.unc.cscc.crxrest.model;
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
......@@ -59,6 +60,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
*
*/
public final class NDC
implements Comparable<NDC>
{
/**
......@@ -390,5 +392,52 @@ public final class NDC
return true;
}
public boolean
equals(Object obj)
{
if (obj == null || ! (obj instanceof NDC))
{
return false;
}
NDC other = (NDC) obj;
return Objects.equals(this.labelerCode, other.labelerCode)
&& Objects.equals(this.productCode, other.productCode)
&& Objects.equals(this.packageCode, other.packageCode);
}
public int
hashCode()
{
return Objects.hash(Arrays.hashCode(this.labelerCode),
Arrays.hashCode(this.productCode),
Arrays.hashCode(this.packageCode));
}
@Override
public int compareTo(NDC other)
{
int x = Arrays.compare(this.labelerCode, other.labelerCode);
if (x != 0)
{
return x;
}
x = Arrays.compare(this.productCode, other.productCode);
if (x != 0)
{
return x;
}
x = Arrays.compare(this.packageCode, other.packageCode);
return x;
}
}
......@@ -32,6 +32,8 @@
*/
package edu.unc.cscc.crxrest.model.product;
import java.util.Objects;
import javax.validation.constraints.NotNull;
import com.fasterxml.jackson.annotation.JsonCreator;
......@@ -49,10 +51,13 @@ import edu.unc.cscc.crxrest.model.Drug;
*/
@JsonTypeName("product-component")
public final class ProductComponent
implements Comparable<ProductComponent>
{
private final Drug drug;
private final Dose dose;
private final String _str;
/**
* Create a new component consisting of a given dose of a given drug.
*
......@@ -75,6 +80,8 @@ public final class ProductComponent
this.drug = drug;
this.dose = dose;
this._str = (drug.canonicalName() + " " + dose.name());
}
/**
......@@ -102,4 +109,27 @@ public final class ProductComponent
{
return this.drug;
}
public boolean equals(Object obj)
{
if (obj == null || ! (obj instanceof ProductComponent))
{
return false;
}
ProductComponent other = (ProductComponent) obj;
return Objects.equals(this.dose, other.dose)
&& Objects.equals(this.drug, other.drug);
}
public int hashCode()
{
return this._str.hashCode();
}
public int compareTo(ProductComponent other)
{
return this._str.compareTo(other._str);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment