Skip to content

Commit

Permalink
Merge pull request #5983 from leonardehrenfried/italian-profile
Browse files Browse the repository at this point in the history
Handle NeTEx `any` version
  • Loading branch information
leonardehrenfried authored Jul 26, 2024
2 parents 0afcedd + d71fcc1 commit 3c2dd45
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,43 @@
*/
public class NetexVersionHelper {

/**
* @see NetexVersionHelper#versionOf(EntityInVersionStructure)
*/
private static final String ANY = "any";
/**
* A special value that represents an unknown version.
*/
private static final int UNKNOWN_VERSION = -1;

/**
* private constructor to prevent instantiation of utility class
*/
private NetexVersionHelper() {}

/**
* According to the <b>Norwegian Netex profile</b> the version number must be a positive
* increasing integer. A bigger value indicate a later version.
* increasing integer. A bigger value indicates a later version.
* However, the special value "any" is also supported and returns a constant meaning "unknown".
* The EPIP profile at
* http://netex.uk/netex/doc/2019.05.07-v1.1_FinalDraft/prCEN_TS_16614-PI_Profile_FV_%28E%29-2019-Final-Draft-v3.pdf (page 33)
* defines this as follows: "Use "any" if the VERSION is unknown (note that this will trigger NeTEx's
* XML automatic consistency check)."
*/
public static int versionOf(EntityInVersionStructure e) {
return Integer.parseInt(e.getVersion());
if (e.getVersion().equals(ANY)) {
return UNKNOWN_VERSION;
} else {
return Integer.parseInt(e.getVersion());
}
}

/**
* Return the latest (maximum) version number for the given {@code list} of elements. If no
* elements exist in the collection {@code -1} is returned.
*/
public static int latestVersionIn(Collection<? extends EntityInVersionStructure> list) {
return list.stream().mapToInt(NetexVersionHelper::versionOf).max().orElse(-1);
return list.stream().mapToInt(NetexVersionHelper::versionOf).max().orElse(UNKNOWN_VERSION);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,39 @@
import org.rutebanken.netex.model.EntityInVersionStructure;
import org.rutebanken.netex.model.ValidBetween;

public class NetexVersionHelperTest {
class NetexVersionHelperTest {

private static final EntityInVersionStructure E_VER_1 = new EntityInVersionStructure()
.withVersion("1");
private static final EntityInVersionStructure E_VER_2 = new EntityInVersionStructure()
.withVersion("2");
private static final EntityInVersionStructure E_VER_ANY = new EntityInVersionStructure()
.withVersion("any");

@Test
public void versionOfTest() {
void versionOfTest() {
assertEquals(1, versionOf(E_VER_1));
}

@Test
public void latestVersionInTest() {
void any() {
assertEquals(-1, versionOf(E_VER_ANY));
}

@Test
void latestVersionInTest() {
assertEquals(2, latestVersionIn(Arrays.asList(E_VER_1, E_VER_2)));
assertEquals(-1, latestVersionIn(Collections.emptyList()));
}

@Test
public void lastestVersionedElementInTest() {
void lastestVersionedElementInTest() {
assertEquals(E_VER_2, latestVersionedElementIn(Arrays.asList(E_VER_1, E_VER_2)));
assertNull(latestVersionedElementIn(Collections.emptyList()));
}

@Test
public void comparingVersionTest() {
void comparingVersionTest() {
// Given a comparator (subject under test)
Comparator<EntityInVersionStructure> subject = comparingVersion();
// And a entity with version as the E_VER_1 entity
Expand All @@ -62,7 +69,7 @@ public void comparingVersionTest() {
}

@Test
public void testFirstRelevantDateTime() {
void testFirstRelevantDateTime() {
var may1st = LocalDateTime.of(2021, MAY, 1, 14, 0);
var may2nd = LocalDateTime.of(2021, MAY, 2, 14, 0);
var may3rd = LocalDateTime.of(2021, MAY, 3, 14, 0);
Expand Down

0 comments on commit 3c2dd45

Please sign in to comment.