IMI-Application/src/main/java/de/vanitasvitae/imi/codes/types/SampleTubeCode.java

82 lines
2.2 KiB
Java

package de.vanitasvitae.imi.codes.types;
import java.util.stream.IntStream;
/**
* Identification Code for Sample Tubes.
*/
public class SampleTubeCode implements CharSequence {
private final StudyNumber studyNumber;
private final SampleType sampleType;
private final int sampleNumber;
/**
* Create a new {@link SampleTubeCode} object.
* This constructor throws an {@link IllegalArgumentException}, if one of the following cases is happening:
* - {@code studyNumber} is null
* - {@code sampleType} is null
* - {@code sampleNumber is negative or greater than 9999}
*
* @param studyNumber code that identifies the associated study
* @param sampleType type of the sample
* @param sampleNumber number of the sample
*/
public SampleTubeCode(StudyNumber studyNumber, SampleType sampleType, int sampleNumber) {
if (studyNumber == null) {
throw new IllegalArgumentException("StudyNumber MUST NOT be null.");
}
if (sampleType == null) {
throw new IllegalArgumentException("SampleType MUST NOT be null.");
}
if (sampleNumber < 0 || sampleNumber > 9999) {
throw new IllegalArgumentException("SampleNumber MUST BE an integer between 0 and 9999 (inclusive).");
}
this.studyNumber = studyNumber;
this.sampleType = sampleType;
this.sampleNumber = sampleNumber;
}
@Override
public int length() {
return toString().length();
}
@Override
public char charAt(int i) {
return toString().charAt(i);
}
@Override
public CharSequence subSequence(int a, int b) {
return toString().subSequence(a, b);
}
@Override
public IntStream chars() {
return toString().chars();
}
@Override
public IntStream codePoints() {
return toString().codePoints();
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object o) {
return toString().equals(o.toString());
}
@Override
public String toString() {
String number = String.format("%04d", sampleNumber);
return studyNumber.toString() + sampleType.toString() + number;
}
}