Java Code Examples for com.google.common.collect.ImmutableRangeSet#Builder

The following examples show how to use com.google.common.collect.ImmutableRangeSet#Builder . 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: RangeSetDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@Override
public RangeSet<Comparable<?>> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (genericRangeListType == null) {
        throw new JsonParseException(p, "RangeSetJsonSerializer was not contextualized (no deserialize target type). " +
                "You need to specify the generic type down to the generic parameter of RangeSet.");
    } else {
        @SuppressWarnings("unchecked") final Iterable<Range<Comparable<?>>> ranges
                = (Iterable<Range<Comparable<?>>>) ctxt
                .findContextualValueDeserializer(genericRangeListType, null).deserialize(p, ctxt);
        ImmutableRangeSet.Builder<Comparable<?>> builder = ImmutableRangeSet.builder();
        for (Range<Comparable<?>> range : ranges) {
            builder.add(range);
        }
        return builder.build();
    }
}
 
Example 2
Source File: IdnTable.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Creates an IDN table given the lines from text file. */
static IdnTable createFrom(
    String language, Iterable<String> data, Optional<LanguageValidator> languageValidator) {
  ImmutableRangeSet.Builder<Integer> rangeSet = new ImmutableRangeSet.Builder<>();
  URI url = null;
  URI policy = null;
  for (String line : data) {
    // Remove leading and trailing whitespace.
    line = line.trim();

    // Handle special comment lines.
    if (line.startsWith(URL_LINE_PREFIX)) {
      url = URI.create(line.substring(URL_LINE_PREFIX.length()));
    } else if (line.startsWith(POLICY_LINE_PREFIX)) {
      policy = URI.create(line.substring(POLICY_LINE_PREFIX.length()));
    }

    // Skip empty and comment lines.
    if (line.isEmpty() || line.startsWith("#")) {
      continue;
    }

    int codepoint = readCodepoint(line);
    rangeSet.add(Range.singleton(codepoint));
  }
  return new IdnTable(language, url, policy, rangeSet.build(), languageValidator);
}
 
Example 3
Source File: QuoteFilter.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
public static QuoteFilter loadFrom(ByteSource source) throws IOException {
  final ImmutableList<String> input = source.asCharSource(Charsets.UTF_8).readLines();
  if (input.isEmpty()) {
    throw new IOException("Attempted to load QuoteFilter from empty file");
  }

  final int numEntries = Integer.parseInt(input.get(0));
  final int expectedLines = 2 * numEntries + 1;
  if (input.size() != expectedLines) {
    throw new IOException(String.format(
        "Invalid number of lines when loading QuoteFiler. Expected %d, got %d",
        expectedLines, input.size()));
  }

  final ImmutableMap.Builder<Symbol, ImmutableRangeSet<Integer>> ret = ImmutableMap.builder();
  int curLine = 1;
  for (int i = 0; i < numEntries; ++i) {
    final Symbol docid = Symbol.from(input.get(curLine++));
    final ImmutableRangeSet.Builder<Integer> ranges = ImmutableRangeSet.builder();
    for (final String part : StringUtils.onSpaces().split(input.get(curLine++))) {
      final List<String> endPointStrings = DASH_SPLITTER.splitToList(part);
      if (endPointStrings.size() != 2) {
        throw new IOException(String.format("Invalid range serialization %s", part));
      }
      ranges.add(Range.closed(Integer.parseInt(endPointStrings.get(0)),
          Integer.parseInt(endPointStrings.get(1))));
    }
    ret.put(docid, ranges.build());
  }
  return QuoteFilter.createFromBannedRegions(ret.build());
}
 
Example 4
Source File: CumulusNcluConfigurationBuilder.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static @Nonnull Set<String> toStrings(Glob_range_setContext ctx, long maxValue) {
  if (ctx.unnumbered != null) {
    return ImmutableSet.of(ctx.unnumbered.getText());
  }
  String baseWord = ctx.base_word.getText();
  if (ctx.first_interval_end == null && ctx.other_numeric_ranges == null) {
    return ImmutableSet.of(baseWord);
  }
  Matcher matcher = NUMBERED_WORD_PATTERN.matcher(baseWord);
  matcher.matches(); // parser+lexer guarantee match
  String prefix = matcher.group(1);
  long firstIntervalStart = Long.parseLong(matcher.group(2), 10);
  long firstIntervalEnd =
      ctx.first_interval_end != null
          ? Long.parseLong(ctx.first_interval_end.getText(), 10)
          : firstIntervalStart;
  checkArgument(firstIntervalStart <= maxValue && firstIntervalEnd <= maxValue);
  // attempt to add first interval
  ImmutableRangeSet.Builder<Long> builder = ImmutableRangeSet.builder();
  try {
    // TODO have better parsing for globs: https://github.com/batfish/batfish/issues/4386
    builder.add(Range.closed(firstIntervalStart, firstIntervalEnd));
  } catch (IllegalArgumentException e) {
    return ImmutableSet.of();
  }
  // All good, proceed to numeric ranges
  if (ctx.other_numeric_ranges != null) {
    // add other intervals
    RangeSet<Long> rangeSet = toRangeSet(ctx.other_numeric_ranges);
    checkUpperBound(rangeSet, maxValue);
    builder.addAll(rangeSet);
  }
  return builder.build().asRanges().stream()
      .flatMapToLong(r -> LongStream.rangeClosed(r.lowerEndpoint(), r.upperEndpoint()))
      .mapToObj(i -> String.format("%s%d", prefix, i))
      .collect(ImmutableSet.toImmutableSet());
}
 
Example 5
Source File: Numbers.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
/**
 * Performs {@link #toRange(IRange)} for a collection of ranges, and convert the result to a set
 * of integers.
 *
 * @param ranges Ranges to convert.
 * @return A set representing {@code ranges}.
 */
public static Set<Integer> rangesToInstanceIds(Iterable<IRange> ranges) {
  ImmutableRangeSet.Builder<Integer> instanceIds = ImmutableRangeSet.builder();
  for (IRange range : ranges) {
    instanceIds.add(toRange(range));
  }

  return instanceIds.build().asSet(DiscreteDomain.integers());
}
 
Example 6
Source File: Updates.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a range set representing all instance IDs represented by a set of instance
 * configurations included in a job update.
 *
 * @param configs Job update components.
 * @return A range set representing the instance IDs mentioned in instance groupings.
 */
public static ImmutableRangeSet<Integer> getInstanceIds(Set<IInstanceTaskConfig> configs) {
  ImmutableRangeSet.Builder<Integer> builder = ImmutableRangeSet.builder();
  for (IInstanceTaskConfig config : configs) {
    for (IRange range : config.getInstances()) {
      builder.add(Range.closed(range.getFirst(), range.getLast()));
    }
  }

  return builder.build();
}
 
Example 7
Source File: SampleRows.java    From hop with Apache License 2.0 4 votes vote down vote up
public boolean processRow() throws HopException {
  meta = (SampleRowsMeta) smi;
  data = (SampleRowsData) sdi;

  Object[] r = getRow(); // get row, set busy!
  if ( r == null ) { // no more input to be expected...

    setOutputDone();
    return false;
  }
  if ( first ) {
    first = false;

    String realRange = environmentSubstitute( meta.getLinesRange() );
    data.addlineField = ( !Utils.isEmpty( environmentSubstitute( meta.getLineNumberField() ) ) );

    // get the RowMeta
    data.previousRowMeta = getInputRowMeta().clone();
    data.NrPrevFields = data.previousRowMeta.size();
    data.outputRowMeta = data.previousRowMeta;
    if ( data.addlineField ) {
      meta.getFields( data.outputRowMeta, getTransformName(), null, null, this, metadataProvider );
    }

    String[] rangePart = realRange.split( "," );
    ImmutableRangeSet.Builder<Integer> setBuilder = ImmutableRangeSet.builder();

    for ( String part : rangePart ) {
      if ( part.matches( "\\d+" ) ) {
        if ( log.isDebug() ) {
          logDebug( BaseMessages.getString( PKG, "SampleRows.Log.RangeValue", part ) );
        }
        int vpart = Integer.valueOf( part );
        setBuilder.add( Range.singleton( vpart ) );

      } else if ( part.matches( "\\d+\\.\\.\\d+" ) ) {
        String[] rangeMultiPart = part.split( "\\.\\." );
        Integer start = Integer.valueOf( rangeMultiPart[ 0 ] );
        Integer end = Integer.valueOf( rangeMultiPart[ 1 ] );
        Range<Integer> range = Range.closed( start, end );
        if ( log.isDebug() ) {
          logDebug( BaseMessages.getString( PKG, "SampleRows.Log.RangeValue", range ) );
        }
        setBuilder.add( range );
      }
    }
    data.rangeSet = setBuilder.build();
  } // end if first

  if ( data.addlineField ) {
    data.outputRow = RowDataUtil.allocateRowData( data.outputRowMeta.size() );
    for ( int i = 0; i < data.NrPrevFields; i++ ) {
      data.outputRow[ i ] = r[ i ];
    }
  } else {
    data.outputRow = r;
  }

  int linesRead = (int) getLinesRead();
  if ( data.rangeSet.contains( linesRead ) ) {
    if ( data.addlineField ) {
      data.outputRow[ data.NrPrevFields ] = getLinesRead();
    }

    // copy row to possible alternate rowset(s).
    //
    putRow( data.outputRowMeta, data.outputRow );

    if ( log.isRowLevel() ) {
      logRowlevel( BaseMessages.getString( PKG, "SampleRows.Log.LineNumber", linesRead
        + " : " + getInputRowMeta().getString( r ) ) );
    }
  }

  // Check if maximum value has been exceeded
  if ( data.rangeSet.isEmpty() || linesRead >= data.rangeSet.span().upperEndpoint() ) {
    setOutputDone();
  }

  // Allowed to continue to read in data
  return true;
}
 
Example 8
Source File: CommandLineOptions.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
ImmutableRangeSet.Builder<Integer> linesBuilder() {
    return lines;
}
 
Example 9
Source File: CommandLineOptions.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
ImmutableRangeSet.Builder<Integer> linesBuilder() {
    return lines;
}
 
Example 10
Source File: CommandLineOptions.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
ImmutableRangeSet.Builder<Integer> linesBuilder() {
  return lines;
}
 
Example 11
Source File: QuoteFilter.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
/**
 * Given the string contents of a document, will return the offset ranges of those portions within
 * <quote> tags. This does not pay attention to the attributes of the quote tags.
 */
public static ImmutableRangeSet<Integer> computeQuotedRegions(String s) {
  checkNotNull(s);
  final ImmutableRangeSet.Builder<Integer> ret = ImmutableRangeSet.builder();

  // current search position
  int curPos = 0;
  // search for first opening <quote> tag
  int regionStart = StringUtils.earliestIndexOfAny(s, BANNED_REGION_STARTS, curPos);

  // if we found a <quote> tag
  while (regionStart != -1) {
    curPos = regionStart;
    int nestingCount = 1;

    // until we find the matching </quote> tag..
    while (nestingCount > 0) {
      final int nextStart = StringUtils.earliestIndexOfAny(s, BANNED_REGION_STARTS, curPos + 1);
      final int nextEnd = s.indexOf(BANNED_REGION_END, curPos + 1);

      if (nextEnd == -1) {
        // (a) uh-oh, we reached the end without ever finding a match
        throw new RuntimeException(
            String.format("<quote> tag opened at %d is never closed.", regionStart));
      } else if (nextStart == -1 || nextEnd < nextStart) {
        // (b) we find a </quote> before another <quote>, so
        // we reduce the nesting level and remember the location
        // of the closing tag
        --nestingCount;
        curPos = nextEnd;
      } else if (nextEnd > nextStart) {
        // (c) we found another <quote> before the end of the current
        // <quote>, so there must be nesting.
        ++nestingCount;
        curPos = nextStart;
      } else {
        throw new RuntimeException("It is impossible for nextEnd == nextStart");
      }
    }

    // the only way we successfully exited is case (b)
    // where curPos is the beginning of the </quote> tag
    ret.add(Range.closed(regionStart, curPos + BANNED_REGION_END.length() - 1));

    regionStart = StringUtils.earliestIndexOfAny(s, BANNED_REGION_STARTS, curPos + 1);
  }

  return ret.build();
}
 
Example 12
Source File: SampleRows.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (SampleRowsMeta) smi;
  data = (SampleRowsData) sdi;

  Object[] r = getRow(); // get row, set busy!
  if ( r == null ) { // no more input to be expected...

    setOutputDone();
    return false;
  }
  if ( first ) {
    first = false;

    String realRange = environmentSubstitute( meta.getLinesRange() );
    data.addlineField = ( !Utils.isEmpty( environmentSubstitute( meta.getLineNumberField() ) ) );

    // get the RowMeta
    data.previousRowMeta = getInputRowMeta().clone();
    data.NrPrevFields = data.previousRowMeta.size();
    data.outputRowMeta = data.previousRowMeta;
    if ( data.addlineField ) {
      meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore );
    }

    String[] rangePart = realRange.split( "," );
    ImmutableRangeSet.Builder<Integer> setBuilder = ImmutableRangeSet.builder();

    for ( String part : rangePart ) {
      if ( part.matches( "\\d+" ) ) {
        if ( log.isDebug() ) {
          logDebug( BaseMessages.getString( PKG, "SampleRows.Log.RangeValue", part ) );
        }
        int vpart = Integer.valueOf( part );
        setBuilder.add( Range.singleton( vpart ) );

      } else if ( part.matches( "\\d+\\.\\.\\d+" ) ) {
        String[] rangeMultiPart = part.split( "\\.\\." );
        Integer start = Integer.valueOf( rangeMultiPart[0] );
        Integer end = Integer.valueOf( rangeMultiPart[1] );
        Range<Integer> range = Range.closed( start, end );
        if ( log.isDebug() ) {
          logDebug( BaseMessages.getString( PKG, "SampleRows.Log.RangeValue", range ) );
        }
        setBuilder.add( range );
      }
    }
    data.rangeSet = setBuilder.build();
  } // end if first

  if ( data.addlineField ) {
    data.outputRow = RowDataUtil.allocateRowData( data.outputRowMeta.size() );
    for ( int i = 0; i < data.NrPrevFields; i++ ) {
      data.outputRow[i] = r[i];
    }
  } else {
    data.outputRow = r;
  }

  int linesRead = (int) getLinesRead();
  if ( data.rangeSet.contains( linesRead ) ) {
    if ( data.addlineField ) {
      data.outputRow[data.NrPrevFields] = getLinesRead();
    }

    // copy row to possible alternate rowset(s).
    //
    putRow( data.outputRowMeta, data.outputRow );

    if ( log.isRowLevel() ) {
      logRowlevel( BaseMessages.getString( PKG, "SampleRows.Log.LineNumber", linesRead
        + " : " + getInputRowMeta().getString( r ) ) );
    }
  }

  // Check if maximum value has been exceeded
  if ( data.rangeSet.isEmpty() || linesRead >= data.rangeSet.span().upperEndpoint() ) {
    setOutputDone();
  }

  // Allowed to continue to read in data
  return true;
}
 
Example 13
Source File: CommandLineOptionsParser.java    From java-n-IDE-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Parse multiple --lines flags, like {"1:12,14,20:36", "40:45,50"}. Multiple ranges can be given
 * with multiple --lines flags or separated by commas. A single line can be set by a single
 * number. Line numbers are {@code 1}-based, but are converted to the {@code 0}-based numbering
 * used internally by google-java-format.
 */
private static void parseRangeSet(ImmutableRangeSet.Builder<Integer> result, String ranges) {
    for (String range : COMMA_SPLITTER.split(ranges)) {
        result.add(parseRange(range));
    }
}
 
Example 14
Source File: CommandLineOptionsParser.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse multiple --lines flags, like {"1:12,14,20:36", "40:45,50"}. Multiple ranges can be given
 * with multiple --lines flags or separated by commas. A single line can be set by a single
 * number. Line numbers are {@code 1}-based, but are converted to the {@code 0}-based numbering
 * used internally by google-java-format.
 */
private static void parseRangeSet(ImmutableRangeSet.Builder<Integer> result, String ranges) {
    for (String range : COMMA_SPLITTER.split(ranges)) {
        result.add(parseRange(range));
    }
}
 
Example 15
Source File: CommandLineOptionsParser.java    From google-java-format with Apache License 2.0 2 votes vote down vote up
/**
 * Parse multiple --lines flags, like {"1:12,14,20:36", "40:45,50"}. Multiple ranges can be given
 * with multiple --lines flags or separated by commas. A single line can be set by a single
 * number. Line numbers are {@code 1}-based, but are converted to the {@code 0}-based numbering
 * used internally by google-java-format.
 */
private static void parseRangeSet(ImmutableRangeSet.Builder<Integer> result, String ranges) {
  for (String range : COMMA_SPLITTER.split(ranges)) {
    result.add(parseRange(range));
  }
}