public List<String> getMatchingHosts(final List<HostSelector> selectors) { final List<String> matchingHosts = Lists.newArrayList(); for (final Map.Entry<String, Map<String, String>> entry : hostsAndLabels.entrySet()) { final String host = entry.getKey(); final Map<String, String> hostLabels = entry.getValue(); if (hostLabels == null) { continue; } // every hostSelector in the group has to have a match in this host. // a match meaning the host has a label for that key and the value matches final boolean match = selectors.stream() .allMatch(selector -> hostLabels.containsKey(selector.getLabel()) && selector.matches(hostLabels.get(selector.getLabel()))); if (match) { matchingHosts.add(host); } } Collections.sort(matchingHosts, new AlphaNumericComparator(Locale.ENGLISH)); return ImmutableList.copyOf(matchingHosts); } }
@Override public int compare(final CharSequence s1, final CharSequence s2) { final CharBuffer b1 = wrap(s1); final CharBuffer b2 = wrap(s2); while (b1.hasRemaining() && b2.hasRemaining()) { moveWindow(b1); moveWindow(b2); final int result = compare(b1, b2); if (result != 0) { return result; } prepareForNextIteration(b1); prepareForNextIteration(b2); } return s1.length() - s2.length(); }
private int compare(final CharBuffer b1, final CharBuffer b2) { if (isNumerical(b1) && isNumerical(b2)) { return compareNumerically(b1, b2); } return compareAsStrings(b1, b2); }
private void moveWindow(final CharBuffer buffer) { int start = buffer.position(); int end = buffer.position(); final boolean isNumerical = isDigit(buffer.get(start)); while (end < buffer.limit() && isNumerical == isDigit(buffer.get(end))) { ++end; if (isNumerical && (start + 1 < buffer.limit()) && isZero(buffer.get(start)) && isDigit(buffer.get(end))) { ++start; // trim leading zeros } } buffer.position(start) .limit(end); }
private boolean isNumerical(final CharBuffer buffer) { return isDigit(buffer.charAt(0)); }
private void moveWindow(final CharBuffer buffer) { int start = buffer.position(); int end = buffer.position(); final boolean isNumerical = isDigit(buffer.get(start)); while (end < buffer.limit() && isNumerical == isDigit(buffer.get(end))) { ++end; if (isNumerical && (start + 1 < buffer.limit()) && isZero(buffer.get(start)) && isDigit(buffer.get(end))) { ++start; // trim leading zeros } } buffer.position(start) .limit(end); }
private boolean isNumerical(final CharBuffer buffer) { return isDigit(buffer.charAt(0)); }
private int compare(final CharBuffer b1, final CharBuffer b2) { if (isNumerical(b1) && isNumerical(b2)) { return compareNumerically(b1, b2); } return compareAsStrings(b1, b2); }
@Override public int compare(final CharSequence s1, final CharSequence s2) { final CharBuffer b1 = wrap(s1); final CharBuffer b2 = wrap(s2); while (b1.hasRemaining() && b2.hasRemaining()) { moveWindow(b1); moveWindow(b2); final int result = compare(b1, b2); if (result != 0) { return result; } prepareForNextIteration(b1); prepareForNextIteration(b2); } return s1.length() - s2.length(); }
public List<String> getMatchingHosts(final DeploymentGroup deploymentGroup) { final List<String> matchingHosts = Lists.newArrayList(); if ((deploymentGroup.getHostSelectors() == null) || deploymentGroup.getHostSelectors().isEmpty()) { log.error("skipping deployment group with no host selectors: " + deploymentGroup.getName()); return emptyList(); } // determine the hosts that match the current deployment group hostLoop: for (final Map.Entry<String, Map<String, String>> entry : hostsAndLabels.entrySet()) { final String host = entry.getKey(); final Map<String, String> hostLabels = entry.getValue(); for (final HostSelector hostSelector : deploymentGroup.getHostSelectors()) { final String key = hostSelector.getLabel(); if (!hostLabels.containsKey(key)) { continue hostLoop; } final String hostValue = hostLabels.get(key); if (!hostSelector.matches(hostValue)) { continue hostLoop; } } matchingHosts.add(host); } Collections.sort(matchingHosts, new AlphaNumericComparator(Locale.ENGLISH)); return ImmutableList.copyOf(matchingHosts); } }