@Test void testSelect() { ServerSelector ssArt = new ServerSelector(() -> new AdaptiveRadixTree<>(APIVersionBinaryComparable.INSTANCE)); ServerSelector ssRbt = new ServerSelector(() -> new TreeMap<>(APIVersionComparator.INSTANCE)); ssArt.addServer(new APIVersion(1, 1), "hosta:port"); ssArt.addServer(new APIVersion(1, 2), "hostb:port"); ssArt.addServer(new APIVersion(1, 3), "hostc:port"); ssArt.addServer(new APIVersion(2, 0), "hostd:port"); ssRbt.map().putAll(ssArt.map()); assertEquals("hosta:port", ssArt.select("v1.1").orElse("")); assertEquals("hostc:port", ssArt.select("v1").orElse("")); assertEquals("hostd:port", ssArt.select("").orElse("")); Iterator<Map.Entry<APIVersion, String>> it = ssArt.map().entrySet().iterator(); for (Map.Entry<APIVersion, String> e : ssRbt.map().entrySet()) { assertEquals(it.next(), e); } }
Optional<String> select(String version) { if (version.isEmpty()) { return Optional.ofNullable(m.lastEntry().getValue()); } version = version.substring(1); int separatorIndex = version.indexOf("."); if (separatorIndex != -1) { String[] mm = version.split("[.]"); int major = Integer.parseInt(mm[0]); int minor = Integer.parseInt(mm[1]); return Optional.ofNullable(m.get(new APIVersion(major, minor))); } int major = Integer.parseInt(version); Map.Entry<APIVersion, String> entry = m.floorEntry(new APIVersion(major, Integer.MAX_VALUE)); if (entry == null || entry.getKey().major != major) { return Optional.empty(); } return Optional.of(entry.getValue()); }
ServerSelector { Optional<String> select(String version) { if (version.isEmpty()) { return Optional.ofNullable(m.lastEntry().getValue()); } version = version.substring(1); int separatorIndex = version.indexOf("."); if (separatorIndex != -1) { String[] mm = version.split("[.]"); int major = Integer.parseInt(mm[0]); int minor = Integer.parseInt(mm[1]); return Optional.ofNullable(m.get(new APIVersion(major, minor))); } int major = Integer.parseInt(version); Map.Entry<APIVersion, String> entry = m.floorEntry(new APIVersion(major, Integer.MAX_VALUE)); if (entry == null || entry.getKey().major != major) { return Optional.empty(); } return Optional.of(entry.getValue()); } }
ServerSelector { Optional<String> select(String version) { if (version.isEmpty()) { return Optional.ofNullable(m.lastEntry().getValue()); } version = version.substring(1); int separatorIndex = version.indexOf("."); if (separatorIndex != -1) { String[] mm = version.split("[.]"); int major = Integer.parseInt(mm[0]); int minor = Integer.parseInt(mm[1]); return Optional.ofNullable(m.get(new APIVersion(major, minor))); } int major = Integer.parseInt(version); Map.Entry<APIVersion, String> entry = m.floorEntry(new APIVersion(major, Integer.MAX_VALUE)); if (entry == null || entry.getKey().major != major) { return Optional.empty(); } return Optional.of(entry.getValue()); } ServerSelector(Supplier<NavigableMap<APIVersion, String>> s); }
ServerSelector { Optional<String> select(String version) { if (version.isEmpty()) { return Optional.ofNullable(m.lastEntry().getValue()); } version = version.substring(1); int separatorIndex = version.indexOf("."); if (separatorIndex != -1) { String[] mm = version.split("[.]"); int major = Integer.parseInt(mm[0]); int minor = Integer.parseInt(mm[1]); return Optional.ofNullable(m.get(new APIVersion(major, minor))); } int major = Integer.parseInt(version); Map.Entry<APIVersion, String> entry = m.floorEntry(new APIVersion(major, Integer.MAX_VALUE)); if (entry == null || entry.getKey().major != major) { return Optional.empty(); } return Optional.of(entry.getValue()); } ServerSelector(Supplier<NavigableMap<APIVersion, String>> s);  }
ServerSelector { Optional<String> select(String version) { if (version.isEmpty()) { return Optional.ofNullable(m.lastEntry().getValue()); } version = version.substring(1); int separatorIndex = version.indexOf("."); if (separatorIndex != -1) { String[] mm = version.split("[.]"); int major = Integer.parseInt(mm[0]); int minor = Integer.parseInt(mm[1]); return Optional.ofNullable(m.get(new APIVersion(major, minor))); } int major = Integer.parseInt(version); Map.Entry<APIVersion, String> entry = m.floorEntry(new APIVersion(major, Integer.MAX_VALUE)); if (entry == null || entry.getKey().major != major) { return Optional.empty(); } return Optional.of(entry.getValue()); } ServerSelector(Supplier<NavigableMap<APIVersion, String>> s);   }