public static int shortestUniquePrefix(Collection<String> strings) { return shortestUniquePrefix(strings, 1); }
public AgentStatusRepresentationFactory(List<AgentStatus> agentStatuses, Repository repository) { this.shortIdPrefixSize = shortestUniquePrefix(transform(agentStatuses, idGetter()), MIN_PREFIX_SIZE); this.commonLocationParts = commonPrefixSegments('/', transform(agentStatuses, locationGetter("/")), MIN_LOCATION_SEGMENTS); this.repository = repository; }
@Test public void testTrim() { assertEquals(trimLeadingSegments("/a/b/c", '/', 0), "/a/b/c"); assertEquals(trimLeadingSegments("/a/b/c", '/', 1), "/b/c"); assertEquals(trimLeadingSegments("/a/b/c", '/', 2), "/c"); }
private static int shortestUniquePrefixStartingAt(List<String> strings, int charPosition) Preconditions.checkArgument(charPosition < Collections.max(transform(strings, lengthGetter())), "charPosition is beyond the size of all the provided strings"); if (index - sequenceStart > 1) { result = max(result, shortestUniquePrefixStartingAt(strings.subList(sequenceStart, index), charPosition + 1) + 1); result = max(result, shortestUniquePrefixStartingAt(strings.subList(sequenceStart, strings.size()), charPosition + 1) + 1);
public AgentStatusRepresentation create(AgentStatus status) { Builder<SlotStatusRepresentation> builder = ImmutableList.builder(); for (SlotStatus slot : status.getSlotStatuses()) { builder.add(SlotStatusRepresentation.from(slot, Integer.MAX_VALUE, repository)); } return new AgentStatusRepresentation( status.getAgentId(), safeTruncate(status.getAgentId(), shortIdPrefixSize), status.getInstanceId(), status.getState(), status.getInternalUri(), status.getExternalUri(), status.getLocation(), trimLeadingSegments(status.getLocation(), '/', commonLocationParts), status.getInstanceType(), builder.build(), status.getResources(), status.getVersion()); } }
public static int commonPrefixSegments(char separator, Collection<String> strings) { return commonPrefixSegments(separator, strings, 0); }
public static int shortestUniquePrefix(Collection<String> strings, int minSize) { Preconditions.checkNotNull(strings, "strings is null"); // remove nulls strings = ImmutableList.copyOf(Iterables.filter(strings, Predicates.notNull())); // must have at least two strings to calculate min size if (strings.size() < 2) { return minSize; } SortedSet<String> sorted = Sets.newTreeSet(strings); if (sorted.size() != strings.size()) { throw new IllegalArgumentException("Cannot compute unique prefix size for collection with duplicate entries"); } int prefix = shortestUniquePrefixStartingAt(ImmutableList.copyOf(sorted), 0); return max(prefix, minSize); }
public static int commonPrefixSegments(char separator, Collection<String> strings, int minSize) { Preconditions.checkNotNull(strings, "strings is null"); Preconditions.checkArgument(minSize >= 0, "minSize is negative"); if (strings.isEmpty()) { return 0; } int shortestNumberOfParts = Integer.MAX_VALUE; List<List<String>> stringsParts = newArrayList(); for (String string : strings) { List<String> parts = ImmutableList.copyOf(Splitter.on(separator).split(string)); if (parts.isEmpty() || !parts.get(0).isEmpty()) { throw new IllegalArgumentException("All strings must start with the separator character"); } parts = parts.subList(1, parts.size()); stringsParts.add(parts); shortestNumberOfParts = min(parts.size(), shortestNumberOfParts); } int maxNumberOfSharedParts = max(shortestNumberOfParts - minSize, 0); int commonParts = 0; while (commonParts < maxNumberOfSharedParts && isPartEqual(commonParts, stringsParts)) { commonParts++; } return commonParts; }
safeTruncate(slotStatus.getId().toString(), shortIdPrefixSize), slotStatus.getSelf(), slotStatus.getExternalUri(),
public CoordinatorStatusRepresentation create(CoordinatorStatus status) { return new CoordinatorStatusRepresentation( status.getCoordinatorId(), safeTruncate(status.getCoordinatorId(), shortIdPrefixSize), status.getInstanceId(), status.getState(), status.getInternalUri(), status.getExternalUri(), status.getLocation(), trimLeadingSegments(status.getLocation(), '/', commonLocationParts), status.getInstanceType(), status.getVersion()); } }
@Test public void testCommonSegment() { assertEquals(commonPrefixSegments('/', ImmutableList.<String>of()), 0); assertEquals(commonPrefixSegments('/', of("/")), 1); assertEquals(commonPrefixSegments('/', of("/a")), 1); assertEquals(commonPrefixSegments('/', of("/a", "/a")), 1); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a")), 3); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b")), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b")), 2); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*unique.*") public void testDuplicateStrings() { shortestUniquePrefix(of("aaa", "aaa")); }
public SlotStatusRepresentationFactory(List<SlotStatus> slotStatuses, Repository repository) { this.shortIdPrefixSize = shortestUniquePrefix(transform(slotStatuses, idGetter()), MIN_PREFIX_SIZE); this.commonLocationParts = commonPrefixSegments('/', transform(slotStatuses, locationGetter()), MIN_LOCATION_SEGMENTS); this.repository = repository; }
safeTruncate(status.getId().toString(), shortIdPrefixSize), status.getSelf(), status.getExternalUri(), status.getInstanceId(), status.getLocation(), trimLeadingSegments(status.getLocation(), '/', commonLocationParts), binary, shortBinary,
@Test public void testCommonSegmentWithMin() { assertEquals(commonPrefixSegments('/', ImmutableList.<String>of(), 2), 0); assertEquals(commonPrefixSegments('/', of("/"), 0), 1); assertEquals(commonPrefixSegments('/', of("/"), 1), 0); assertEquals(commonPrefixSegments('/', of("/"), 2), 0); assertEquals(commonPrefixSegments('/', of("/a"), 0), 1); assertEquals(commonPrefixSegments('/', of("/a"), 1), 0); assertEquals(commonPrefixSegments('/', of("/a"), 2), 0); assertEquals(commonPrefixSegments('/', of("/a", "/a"), 0), 1); assertEquals(commonPrefixSegments('/', of("/a", "/a"), 1), 0); assertEquals(commonPrefixSegments('/', of("/a", "/a"), 2), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a"), 0), 3); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a"), 1), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a"), 2), 1); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a"), 3), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a"), 4), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b"), 0), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b"), 1), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b"), 2), 1); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b"), 3), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/b"), 4), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b"), 0), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b"), 1), 2); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b"), 2), 1); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b"), 3), 0); assertEquals(commonPrefixSegments('/', of("/a/a/a", "/a/a/a", "/a/a/b"), 4), 0); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*unique.*") public void testDuplicateStrings2() { shortestUniquePrefix(of("aaa", "aa", "aaa")); }
public CoordinatorStatusRepresentationFactory(List<CoordinatorStatus> coordinatorStatuses) { this.shortIdPrefixSize = shortestUniquePrefix(transform(coordinatorStatuses, idGetter()), MIN_PREFIX_SIZE); this.commonLocationParts = commonPrefixSegments('/', transform(coordinatorStatuses, locationGetter("/")), MIN_LOCATION_SEGMENTS); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*unique.*") public void testDuplicateStrings3() { shortestUniquePrefix(of("aa", "aaa", "aa", "aa")); } }
@Test public void testShortestUniquePrefix() { assertEquals(shortestUniquePrefix(of("aa", "aaa")), 3); assertEquals(shortestUniquePrefix(of("a")), 1); assertEquals(shortestUniquePrefix(of("aaaaa")), 1); assertEquals(shortestUniquePrefix(of("a", "b", "c")), 1); assertEquals(shortestUniquePrefix(of("axxxxx", "b", "c")), 1); assertEquals(shortestUniquePrefix(of("ax", "bx", "cx")), 1); assertEquals(shortestUniquePrefix(of("ax", "ay", "cx")), 2); assertEquals(shortestUniquePrefix(of("axxx", "ayyyy", "cxxxx")), 2); assertEquals(shortestUniquePrefix(of("aaaax", "aaaay")), 5); assertEquals(shortestUniquePrefix(of("aaaaxx", "aaaayx", "ccc")), 5); assertEquals(shortestUniquePrefix(of("a1", "b2", "b3", "b4", "b5")), 2); }
coordinator.updateAllAgentsAndWait(); int prefixSize = shortestUniquePrefix(asList(slot1.getId().toString(), slot2.getId().toString()), MIN_PREFIX_SIZE);