
nl.tno.bim.nmd.domain.NlsfbCode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bimnmdservice Show documentation
Show all versions of bimnmdservice Show documentation
provides a REST api for retrieving nmd data from various data sources
The newest version!
package nl.tno.bim.nmd.domain;
import java.util.Arrays;
import java.util.List;
import javax.validation.ValidationException;
public class NlsfbCode {
private Integer majorId;
Integer mediorId;
Integer minorId;
public NlsfbCode(String code) {
if (!isNlsfbCode(code)) {
System.err.println("Invalid NlsfbCode");
majorId = -1;
mediorId = -1;
minorId = -1;
} else {
String[] split = code.split("\\.");
majorId = Integer.parseInt(split[0].trim());
if (split.length > 1) {
mediorId = Integer.parseInt(split[1].trim());
}
if (split.length > 2) {
minorId = Integer.parseInt(split[2].trim());
}
}
}
public NlsfbCode(Integer majorId, Integer mediorId, Integer minorId) {
if (majorId >= 0 && mediorId >= 0 && minorId >- 0) {
this.majorId = majorId;
this.mediorId = mediorId;
this.minorId = minorId;
} else {
throw new ValidationException("ids of NLsfbCode constructor are smaller than 0");
}
}
/**
* Check whether the string can be parsed to a NLsfb code
*
* @param code string in form i.j.k where i, j and k have to be left padded
* integer values
* @return a flag to indicate whether the above description of the code param
* holds.
*/
public static Boolean isNlsfbCode(String code) {
if (code == null) {
return false;
}
// split on periods in string
List split = Arrays.asList(code.split("\\."));
// make sure that each split is an integer value.
return split.size() > 0 && split.size() <= 3 && split.stream().anyMatch(i -> i.matches("-?([0-9]{1,3})"));
}
/**
* Check that the input code is an overlapping category of this code.
*
* example 1: this = 22.1 code = 22. will evaluate to true. example 2: this =
* 22.2 code = 22.1 will evaluate to false. example 3: this = 22.2 code = 22.2
* will evaluate to true.
*
* @param code the potential parent category
* @return flag to indicate that this NLSfb object is a (sub) category of the
* input Nlsfb object
*/
public Boolean isSubCategoryOf(NlsfbCode code) {
return this.majorId == code.getMajorId() && compareSubCodes(code.getMediorId(), this.getMediorId());
}
/**
* Should return true when an integer B is a derived (sub)code of integer A
* example: (1,11) will evaluate to true example (1,21) will evaluate to false
* example (1,1) will evaluate to true
*
* @param codeA 'parent' code
* @param codeB possible 'child' code
* @return flag to indicate b is a child of a
*/
private Boolean compareSubCodes(Integer codeA, Integer codeB) {
if (codeA == null && codeB != null) {
return true;
}
int[] digitsA = String.valueOf(codeA).chars().toArray();
int[] digitsB = String.valueOf(codeB).chars().toArray();
if (digitsA.length > digitsB.length) {
return false;
}
for (int i = 0; i < digitsA.length; i++) {
if (digitsA[i] != digitsB[i]) {
return false;
}
}
return true;
}
public String print() {
String res = "";
if (getMajorId() != null) {
res += getMajorId().toString();
if (getMediorId() != null && getMediorId() > 0) {
res += "." + mediorId.toString();
if (getMinorId() != null && getMinorId() > 0) {
res += "." + getMinorId().toString();
}
}
}
return res;
}
public Integer getMajorId() {
return majorId;
}
public Integer getMediorId() {
return mediorId;
}
public Integer getMinorId() {
return minorId;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy