Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NeTEx any version #5983

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading