Java Code Examples for com.google.common.collect.RangeSet#contains()

The following examples show how to use com.google.common.collect.RangeSet#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QuotaManager.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private static Predicate<IAssignedTask> buildNonUpdatingTasksFilter(
    final Map<IJobKey, IJobUpdateInstructions> roleJobUpdates) {

  return task -> {
    Optional<IJobUpdateInstructions> update = Optional.ofNullable(
        roleJobUpdates.get(task.getTask().getJob()));

    if (update.isPresent()) {
      IJobUpdateInstructions instructions = update.get();
      RangeSet<Integer> initialInstances = getInstanceIds(instructions.getInitialState());
      RangeSet<Integer> desiredInstances = getInstanceIds(instructions.isSetDesiredState()
          ? ImmutableSet.of(instructions.getDesiredState())
          : ImmutableSet.of());

      int instanceId = task.getInstanceId();
      return !initialInstances.contains(instanceId) && !desiredInstances.contains(instanceId);
    }
    return true;
  };
}
 
Example 2
Source File: StringStringCodec.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
void validate(final String str) {
    if (lengthConstraint != null) {
        final RangeSet<Integer> ranges = lengthConstraint.getAllowedRanges();
        if (!ranges.contains(str.length())) {
            throw new YangInvalidValueException(ErrorType.PROTOCOL, lengthConstraint,
                "String " + str + " does not match allowed lengths " + ranges);
        }
    }
}
 
Example 3
Source File: BinaryStringCodec.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
void validate(final byte[] value) {
    final RangeSet<Integer> ranges = lengthConstraint.getAllowedRanges();
    if (!ranges.contains(value.length)) {
        throw new YangInvalidValueException(ErrorType.PROTOCOL, lengthConstraint,
                "Value length " + value.length + " is not in required ranges " + ranges);
    }
}
 
Example 4
Source File: AbstractIntegerStringCodec.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected final N deserializeImpl(final String product) {
    final int base = provideBase(product);
    final String stringRepresentation = base != 16 ? product : X_MATCHER.removeFrom(product);
    final N deserialized = verifyNotNull(deserialize(stringRepresentation, base));
    if (rangeConstraint != null) {
        final RangeSet<N> ranges = rangeConstraint.getAllowedRanges();
        if (!ranges.contains(deserialized)) {
            throw new YangInvalidValueException(ErrorType.PROTOCOL, rangeConstraint,
                "Value '" + deserialized + "'  is not in required ranges " + ranges);
        }
    }
    return deserialized;
}
 
Example 5
Source File: TopCategoriesExtractor.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException{
  Integer runningId = RunningTimer.recordStartTime("TopCategoriesExtractor");
  Language language = Language.getLanguageForString(aJCas.getDocumentLanguage());
  Collection<ConceptMentionCandidate> conceptMentionCandidatesJcas = JCasUtil.select(aJCas, ConceptMentionCandidate.class);
  Mentions addedMentionsNE = Mentions.getNeMentionsFromJCas(aJCas);
  RangeSet<Integer> added_before = TreeRangeSet.create();
  for (Map<Integer, Mention> innerMap : addedMentionsNE.getMentions().values()) {
    for (Mention m : innerMap.values()) {
      if (m.getCharLength() != 0) { // workaround for issue in knowner.
        added_before.add(Range.closed(m.getCharOffset(), m.getCharOffset() + m.getCharLength() - 1));
      }
    }
  }
  
  Set<String> mentions = new HashSet<>();
  for (ConceptMentionCandidate cc : conceptMentionCandidatesJcas) {
    if (added_before.contains(cc.getBegin()) || added_before.contains(cc.getEnd())) {
      continue;
    }
    mentions.add(cc.getConceptCandidate());
  }
  
  Map<String, int[]> mentionTypes;
  try {
    mentionTypes = DataAccess.getCategoryIdsForMentions(mentions, language, false);
  } catch (EntityLinkingDataAccessException e) {
    throw new AnalysisEngineProcessException(e);
  }
  
  Set<Integer> topTypes = getTopWikipediaTypes(mentionTypes, MAX_TOP_TYPE);
  
  for (Integer typeId : topTypes) {
    WikiType type = new WikiType(aJCas);
    type.setId(typeId);
    type.setName(ALL_WIKIPEDIA_CATEGORY_TYPES.get(typeId).getName());
    type.setKnowledgebase(ALL_WIKIPEDIA_CATEGORY_TYPES.get(typeId).getKnowledgeBase());
    type.addToIndexes();
  }
  RunningTimer.recordEndTime("TopCategoriesExtractor", runningId);
}
 
Example 6
Source File: ModifierOrderer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Reorders all modifiers in the given text and within the given character ranges to be in JLS
 * order.
 */
static JavaInput reorderModifiers(JavaInput javaInput, Collection<Range<Integer>> characterRanges)
        throws FormatterException {
    if (javaInput.getTokens().isEmpty()) {
        // There weren't any tokens, possible because of a lexing error.
        // Errors about invalid input will be reported later after parsing.
        return javaInput;
    }
    RangeSet<Integer> tokenRanges = javaInput.characterRangesToTokenRanges(characterRanges);
    Iterator<? extends Token> it = javaInput.getTokens().iterator();
    TreeRangeMap<Integer, String> replacements = TreeRangeMap.create();
    while (it.hasNext()) {
        Token token = it.next();
        if (!tokenRanges.contains(token.getTok().getIndex())) {
            continue;
        }
        Modifier mod = asModifier(token);
        if (mod == null) {
            continue;
        }

        List<Token> modifierTokens = new ArrayList<>();
        List<Modifier> mods = new ArrayList<>();

        int begin = token.getTok().getPosition();
        mods.add(mod);
        modifierTokens.add(token);

        int end = -1;
        while (it.hasNext()) {
            token = it.next();
            mod = asModifier(token);
            if (mod == null) {
                break;
            }
            mods.add(mod);
            modifierTokens.add(token);
            end = token.getTok().getPosition() + token.getTok().length();
        }

        if (!Ordering.natural().isOrdered(mods)) {
            Collections.sort(mods);
            StringBuilder replacement = new StringBuilder();
            for (int i = 0; i < mods.size(); i++) {
                if (i > 0) {
                    addTrivia(replacement, modifierTokens.get(i).getToksBefore());
                }
                replacement.append(mods.get(i).toString());
                if (i < (modifierTokens.size() - 1)) {
                    addTrivia(replacement, modifierTokens.get(i).getToksAfter());
                }
            }
            replacements.put(Range.closedOpen(begin, end), replacement.toString());
        }
    }
    return applyReplacements(javaInput, replacements);
}
 
Example 7
Source File: ModifierOrderer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Reorders all modifiers in the given text and within the given character ranges to be in JLS
 * order.
 */
static JavaInput reorderModifiers(JavaInput javaInput, Collection<Range<Integer>> characterRanges)
        throws FormatterException {
    if (javaInput.getTokens().isEmpty()) {
        // There weren't any tokens, possible because of a lexing error.
        // Errors about invalid input will be reported later after parsing.
        return javaInput;
    }
    RangeSet<Integer> tokenRanges = javaInput.characterRangesToTokenRanges(characterRanges);
    Iterator<? extends Token> it = javaInput.getTokens().iterator();
    TreeRangeMap<Integer, String> replacements = TreeRangeMap.create();
    while (it.hasNext()) {
        Token token = it.next();
        if (!tokenRanges.contains(token.getTok().getIndex())) {
            continue;
        }
        Modifier mod = asModifier(token);
        if (mod == null) {
            continue;
        }

        List<Token> modifierTokens = new ArrayList<>();
        List<Modifier> mods = new ArrayList<>();

        int begin = token.getTok().getPosition();
        mods.add(mod);
        modifierTokens.add(token);

        int end = -1;
        while (it.hasNext()) {
            token = it.next();
            mod = asModifier(token);
            if (mod == null) {
                break;
            }
            mods.add(mod);
            modifierTokens.add(token);
            end = token.getTok().getPosition() + token.getTok().length();
        }

        if (!Ordering.natural().isOrdered(mods)) {
            Collections.sort(mods);
            StringBuilder replacement = new StringBuilder();
            for (int i = 0; i < mods.size(); i++) {
                if (i > 0) {
                    addTrivia(replacement, modifierTokens.get(i).getToksBefore());
                }
                replacement.append(mods.get(i).toString());
                if (i < (modifierTokens.size() - 1)) {
                    addTrivia(replacement, modifierTokens.get(i).getToksAfter());
                }
            }
            replacements.put(Range.closedOpen(begin, end), replacement.toString());
        }
    }
    return applyReplacements(javaInput, replacements);
}
 
Example 8
Source File: ModifierOrderer.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/**
 * Reorders all modifiers in the given text and within the given character ranges to be in JLS
 * order.
 */
static JavaInput reorderModifiers(JavaInput javaInput, Collection<Range<Integer>> characterRanges)
    throws FormatterException {
  if (javaInput.getTokens().isEmpty()) {
    // There weren't any tokens, possible because of a lexing error.
    // Errors about invalid input will be reported later after parsing.
    return javaInput;
  }
  RangeSet<Integer> tokenRanges = javaInput.characterRangesToTokenRanges(characterRanges);
  Iterator<? extends Token> it = javaInput.getTokens().iterator();
  TreeRangeMap<Integer, String> replacements = TreeRangeMap.create();
  while (it.hasNext()) {
    Token token = it.next();
    if (!tokenRanges.contains(token.getTok().getIndex())) {
      continue;
    }
    Modifier mod = asModifier(token);
    if (mod == null) {
      continue;
    }

    List<Token> modifierTokens = new ArrayList<>();
    List<Modifier> mods = new ArrayList<>();

    int begin = token.getTok().getPosition();
    mods.add(mod);
    modifierTokens.add(token);

    int end = -1;
    while (it.hasNext()) {
      token = it.next();
      mod = asModifier(token);
      if (mod == null) {
        break;
      }
      mods.add(mod);
      modifierTokens.add(token);
      end = token.getTok().getPosition() + token.getTok().length();
    }

    if (!Ordering.natural().isOrdered(mods)) {
      Collections.sort(mods);
      StringBuilder replacement = new StringBuilder();
      for (int i = 0; i < mods.size(); i++) {
        if (i > 0) {
          addTrivia(replacement, modifierTokens.get(i).getToksBefore());
        }
        replacement.append(mods.get(i).toString());
        if (i < (modifierTokens.size() - 1)) {
          addTrivia(replacement, modifierTokens.get(i).getToksAfter());
        }
      }
      replacements.put(Range.closedOpen(begin, end), replacement.toString());
    }
  }
  return applyReplacements(javaInput, replacements);
}
 
Example 9
Source File: QuoteFilter.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
private static boolean inBannedSet(CharOffsetSpan span, RangeSet<Integer> bannedRegions) {
  return bannedRegions.contains(span.startInclusive())
      || bannedRegions.contains(span.endInclusive());
}