Skip to content

Commit

Permalink
Merge pull request #400 from ncats/ep_csg_defalut_max
Browse files Browse the repository at this point in the history
Make the max and length parameters optional for the UniqueCodeGenerator
  • Loading branch information
alx652 authored Mar 4, 2025
2 parents aca08c3 + 4cf2894 commit 0e437e7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public void testSeqGenCheckDefaults() {
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
assertEquals(codeGenerator.getMax(), codeGenerator.DEFAULT_MAX);
assertEquals(codeGenerator.getMax(), (Long) Long.MAX_VALUE);

assertEquals(codeGenerator.getLen(),String.valueOf(codeGenerator.getMax()).length()+codeGenerator.getSuffix().length());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -37,16 +38,17 @@ public UniqueCodeGenerator(Map with) {
log.trace("UniqueCodeGenerator constructor with Map");
String name = (String) with.get("name");
codeSystem = (String) with.get("codesystem");
String codeSystemSuffix = (String) with.get("suffix");
int length = (Integer) with.get("length");
String codeSystemSuffix = (String) with.getOrDefault("suffix", "");
int length = (int) with.getOrDefault("length", 0);
boolean padding = (Boolean) with.get("padding");
Boolean useLegacy = (Boolean) with.get("useLegacy");
Map<Integer, String> groups = (Map<Integer, String>) with.get("groups");
Long maxValue;
try {
maxValue = Long.parseLong(String.valueOf(with.get("max")));
} catch (Exception e) {
maxValue = Long.MAX_VALUE;
int counterLen = length - codeSystemSuffix.length();
maxValue = counterLen < 1 ? Long.MAX_VALUE : Long.valueOf(String.join("", Collections.nCopies(counterLen, "9")));
}
final Long max = maxValue;
String msg = String.format("codeSystem: %s; codeSystemSuffix: %s; length: %d; padding: %b, max: %d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ix.ginas.models.v1.Substance;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
/*
Expand Down Expand Up @@ -49,8 +50,6 @@
@Component
public class CodeSequentialGenerator extends SequentialNumericIDGenerator<Substance> {

public static final Long DEFAULT_MAX = Long.MAX_VALUE;

@Autowired
private CodeRepository codeRepository;

Expand Down Expand Up @@ -88,10 +87,12 @@ public CodeSequentialGenerator( @JsonProperty("name") String name,
@JsonProperty("groups") Map<Integer, String> groups) {

super(len, suffix, padding);
if(max==null) { max=DEFAULT_MAX; }

this.max = max;
if(suffix==null) { this.suffix= "";}
if(max==null) {
int counterLen = len - this.suffix.length();
max = counterLen < 1 ? Long.MAX_VALUE : Long.valueOf(String.join("", Collections.nCopies(counterLen, "9")));
}
this.max = max;
this.groups = (groups != null) ? groups.values().stream().toArray(String[]::new) : null;
if(!this.getClass().equals(LegacyCodeSequentialGenerator.class)) {
// Legacy could be an extension of this class, and if so these checks aren't appropriate.
Expand Down

0 comments on commit 0e437e7

Please sign in to comment.