Java Code Examples for com.google.common.collect.RangeSet#add()
The following examples show how to use
com.google.common.collect.RangeSet#add() .
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: DateRangeRules.java From Quicksql with MIT License | 6 votes |
private RexNode compareFloorCeil(SqlKind comparison, RexNode operand, RexLiteral timeLiteral, TimeUnitRange timeUnit, boolean floor) { RangeSet<Calendar> rangeSet = operandRanges.get(operand); if (rangeSet == null) { rangeSet = ImmutableRangeSet.<Calendar>of().complement(); } final RangeSet<Calendar> s2 = TreeRangeSet.create(); final Calendar c = timestampValue(timeLiteral); final Range<Calendar> range = floor ? floorRange(timeUnit, comparison, c) : ceilRange(timeUnit, comparison, c); s2.add(range); // Intersect old range set with new. s2.removeAll(rangeSet.complement()); operandRanges.put(operand, ImmutableRangeSet.copyOf(s2)); if (range.isEmpty()) { return rexBuilder.makeLiteral(false); } return toRex(operand, range); }
Example 2
Source File: FieldUtil.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
static private <F extends Field<F> & HasContinuousDomain<F>> RangeSet<Double> parseValidRanges(F field){ RangeSet<Double> result = TreeRangeSet.create(); if(field.hasIntervals()){ List<Interval> intervals = field.getIntervals(); for(Interval interval : intervals){ Range<Double> range = DiscretizationUtil.toRange(interval); result.add(range); } } return result; }
Example 3
Source File: Formatter.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ public static RangeSet<Integer> lineRangesToCharRanges( String input, RangeSet<Integer> lineRanges) { List<Integer> lines = new ArrayList<>(); Iterators.addAll(lines, Newlines.lineOffsetIterator(input)); lines.add(input.length() + 1); final RangeSet<Integer> characterRanges = TreeRangeSet.create(); for (Range<Integer> lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { int lineStart = lines.get(lineRange.lowerEndpoint()); // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines // as empty ranges is convenient. int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; Range<Integer> range = Range.closedOpen(lineStart, lineEnd); characterRanges.add(range); } return characterRanges; }
Example 4
Source File: AbstractConsoleEventBusListener.java From buck with Apache License 2.0 | 6 votes |
/** * Get the summed elapsed time from all matched event pairs. Does not consider unmatched event * pairs. Pairs are determined by their {@link com.facebook.buck.event.EventKey}. * * @param eventIntervals a set of paired events (incomplete events are okay). * @return the sum of all times between matched event pairs. */ protected static long getTotalCompletedTimeFromEventIntervals( Collection<EventInterval> eventIntervals) { long totalTime = 0L; // Flatten the event groupings into a timeline, so that we don't over count parallel work. RangeSet<Long> timeline = TreeRangeSet.create(); for (EventInterval pair : eventIntervals) { if (pair.isComplete() && pair.getElapsedTimeMs() > 0) { timeline.add(Range.open(pair.getStartTime(), pair.getEndTime())); } } for (Range<Long> range : timeline.asRanges()) { totalTime += range.upperEndpoint() - range.lowerEndpoint(); } return totalTime; }
Example 5
Source File: Networking.java From brooklyn-server with Apache License 2.0 | 6 votes |
public static RangeSet<Integer> portRulesToRanges(Collection<String> portRules) { RangeSet<Integer> result = TreeRangeSet.create(); for (String portRule : portRules) { if (portRule.contains("-")) { String[] fromTo = portRule.split("-"); checkArgument(fromTo.length == 2, "Invalid port range '%s'", portRule); checkArgument(Strings.countOccurrences(portRule, '-') == 1, "Invalid port range '%s'", portRule); checkArgument(Strings.isNonEmpty(fromTo[0]), "Invalid port range '%s'", portRule); checkArgument(Strings.isNonEmpty(fromTo[1]), "Invalid port range '%s'", portRule); result.add(closedRange(fromTo[0], fromTo[1])); } else { result.add(closedRange(portRule, portRule)); } } return result; }
Example 6
Source File: Formatter.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ public static RangeSet<Integer> lineRangesToCharRanges( String input, RangeSet<Integer> lineRanges) { List<Integer> lines = new ArrayList<>(); Iterators.addAll(lines, Newlines.lineOffsetIterator(input)); lines.add(input.length() + 1); final RangeSet<Integer> characterRanges = TreeRangeSet.create(); for (Range<Integer> lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { int lineStart = lines.get(lineRange.lowerEndpoint()); // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines // as empty ranges is convenient. int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; Range<Integer> range = Range.closedOpen(lineStart, lineEnd); characterRanges.add(range); } return characterRanges; }
Example 7
Source File: DateRangeRules.java From calcite with Apache License 2.0 | 6 votes |
private RexNode compareFloorCeil(SqlKind comparison, RexNode operand, RexLiteral timeLiteral, TimeUnitRange timeUnit, boolean floor) { RangeSet<Calendar> rangeSet = operandRanges.get(operand); if (rangeSet == null) { rangeSet = ImmutableRangeSet.<Calendar>of().complement(); } final RangeSet<Calendar> s2 = TreeRangeSet.create(); final Calendar c = timestampValue(timeLiteral); final Range<Calendar> range = floor ? floorRange(timeUnit, comparison, c) : ceilRange(timeUnit, comparison, c); s2.add(range); // Intersect old range set with new. s2.removeAll(rangeSet.complement()); operandRanges.put(operand, ImmutableRangeSet.copyOf(s2)); if (range.isEmpty()) { return rexBuilder.makeLiteral(false); } return toRex(operand, range); }
Example 8
Source File: Formatter.java From google-java-format with Apache License 2.0 | 6 votes |
/** * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ public static RangeSet<Integer> lineRangesToCharRanges( String input, RangeSet<Integer> lineRanges) { List<Integer> lines = new ArrayList<>(); Iterators.addAll(lines, Newlines.lineOffsetIterator(input)); lines.add(input.length() + 1); final RangeSet<Integer> characterRanges = TreeRangeSet.create(); for (Range<Integer> lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { int lineStart = lines.get(lineRange.lowerEndpoint()); // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines // as empty ranges is convenient. int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; Range<Integer> range = Range.closedOpen(lineStart, lineEnd); characterRanges.add(range); } return characterRanges; }
Example 9
Source File: FancierDiffLogger.java From tac-kbp-eal with MIT License | 5 votes |
private String context(final String originalDocText, final Response response) { // [1,3], [2,5], [8,10] => [1,5], [8,10] final List<CharOffsetSpan> charSpans = justificationSpans(response); final List<CharOffsetSpan> unitedSpans = Lists.newArrayList(); // use RangeSet to do this final RangeSet<Integer> disconnected = TreeRangeSet.create(); for (CharOffsetSpan charSpan : charSpans) { int startInclusive = charSpan.startInclusive(); int endInclusive = charSpan.endInclusive(); startInclusive = (startInclusive - 100) >= 0 ? startInclusive - 100 : 0; endInclusive = (endInclusive + 100) < originalDocText.length() ? endInclusive + 100 : endInclusive; disconnected.add(Range.closed(startInclusive, endInclusive)); } for (Range<Integer> range : disconnected.asRanges()) { unitedSpans.add(CharOffsetSpan.fromOffsetsOnly(range.lowerEndpoint(), range.upperEndpoint())); } Collections.sort(unitedSpans); String justificationsString = ""; if (unitedSpans.get(0).startInclusive() != 0) { justificationsString += "[.....]"; } for (CharOffsetSpan span : unitedSpans) { justificationsString += originalDocText.substring(span.startInclusive(), span.endInclusive() + 1); justificationsString += "[.....]"; } return justificationsString; }
Example 10
Source File: GuavaRangeSetUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenRangeSet_whenSpanIsCalled_returnsSucessfully() { final RangeSet<Integer> numberRangeSet = TreeRangeSet.create(); numberRangeSet.add(Range.closed(0, 2)); numberRangeSet.add(Range.closed(3, 5)); numberRangeSet.add(Range.closed(6, 8)); final Range<Integer> experienceSpan = numberRangeSet.span(); assertEquals(0, experienceSpan.lowerEndpoint().intValue()); assertEquals(8, experienceSpan.upperEndpoint().intValue()); }
Example 11
Source File: LineRangesToCharRangesTest.java From google-java-format with Apache License 2.0 | 5 votes |
@SafeVarargs final Set<Range<Integer>> getCharRanges(String input, Range<Integer>... ranges) { RangeSet<Integer> rangeSet = TreeRangeSet.create(); for (Range<Integer> range : ranges) { rangeSet.add(range); } return Formatter.lineRangesToCharRanges(input, rangeSet).asRanges(); }
Example 12
Source File: JavaInput.java From google-java-format with Apache License 2.0 | 5 votes |
public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges) throws FormatterException { RangeSet<Integer> tokenRangeSet = TreeRangeSet.create(); for (Range<Integer> characterRange0 : characterRanges) { Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers()); tokenRangeSet.add( characterRangeToTokenRange( characterRange.lowerEndpoint(), characterRange.upperEndpoint() - characterRange.lowerEndpoint())); } return tokenRangeSet; }
Example 13
Source File: SnippetFormatter.java From google-java-format with Apache License 2.0 | 5 votes |
/** Runs the Google Java formatter on the given source, with only the given ranges specified. */ public ImmutableList<Replacement> format( SnippetKind kind, String source, List<Range<Integer>> ranges, int initialIndent, boolean includeComments) throws FormatterException { RangeSet<Integer> rangeSet = TreeRangeSet.create(); for (Range<Integer> range : ranges) { rangeSet.add(range); } if (includeComments) { if (kind != SnippetKind.COMPILATION_UNIT) { throw new IllegalArgumentException( "comment formatting is only supported for compilation units"); } return formatter.getFormatReplacements(source, ranges); } SnippetWrapper wrapper = snippetWrapper(kind, source, initialIndent); ranges = offsetRanges(ranges, wrapper.offset); String replacement = formatter.formatSource(wrapper.contents.toString(), ranges); replacement = replacement.substring( wrapper.offset, replacement.length() - (wrapper.contents.length() - wrapper.offset - source.length())); return toReplacements(source, replacement).stream() .filter(r -> rangeSet.encloses(r.getReplaceRange())) .collect(toImmutableList()); }
Example 14
Source File: GuavaRangeSetUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenRangeSet_whenRemoveRangeIsCalled_removesSucessfully() { final RangeSet<Integer> numberRangeSet = TreeRangeSet.create(); numberRangeSet.add(Range.closed(0, 2)); numberRangeSet.add(Range.closed(3, 5)); numberRangeSet.add(Range.closed(6, 8)); numberRangeSet.add(Range.closed(9, 15)); numberRangeSet.remove(Range.closed(3, 5)); numberRangeSet.remove(Range.closed(7, 10)); assertTrue(numberRangeSet.contains(1)); assertFalse(numberRangeSet.contains(9)); assertTrue(numberRangeSet.contains(12)); }
Example 15
Source File: MarkerConfigXmlParser.java From tracecompass with Eclipse Public License 2.0 | 5 votes |
private static RangeSet<Long> parseRangeSet(String rangeSetAttr) { if (rangeSetAttr.isEmpty()) { return ImmutableRangeSet.of(Range.all()); } RangeSet<Long> rangeSet = TreeRangeSet.create(); String[] ranges = rangeSetAttr.split(","); //$NON-NLS-1$ if (ranges.length == 0) { rangeSet.add(Range.all()); } else { for (String range : ranges) { rangeSet.add(parseRange(range)); } } return rangeSet; }
Example 16
Source File: GuavaRangeSetUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenRangeSet_whenSubRangeSetIsCalled_returnsSubRangeSucessfully() { final RangeSet<Integer> numberRangeSet = TreeRangeSet.create(); numberRangeSet.add(Range.closed(0, 2)); numberRangeSet.add(Range.closed(3, 5)); numberRangeSet.add(Range.closed(6, 8)); final RangeSet<Integer> numberSubRangeSet = numberRangeSet.subRangeSet(Range.closed(4, 14)); assertFalse(numberSubRangeSet.contains(3)); assertFalse(numberSubRangeSet.contains(14)); assertTrue(numberSubRangeSet.contains(7)); }
Example 17
Source File: SnippetFormatter.java From javaide with GNU General Public License v3.0 | 5 votes |
/** * Runs the Google Java formatter on the given source, with only the given ranges specified. */ public List<Replacement> format( SnippetKind kind, String source, List<Range<Integer>> ranges, int initialIndent, boolean includeComments) throws FormatterException { RangeSet<Integer> rangeSet = TreeRangeSet.create(); for (Range<Integer> range : ranges) { rangeSet.add(range); } if (includeComments) { if (kind != SnippetKind.COMPILATION_UNIT) { throw new IllegalArgumentException( "comment formatting is only supported for compilation units"); } return formatter.getFormatReplacements(source, ranges); } SnippetWrapper wrapper = snippetWrapper(kind, source, initialIndent); ranges = offsetRanges(ranges, wrapper.offset); String replacement = formatter.formatSource(wrapper.contents.toString(), ranges); replacement = replacement.substring( wrapper.offset, replacement.length() - (wrapper.contents.length() - wrapper.offset - source.length())); List<Replacement> replacements = toReplacements(source, replacement); List<Replacement> filtered = new ArrayList<>(); for (Replacement r : replacements) { if (rangeSet.encloses(r.getReplaceRange())) { filtered.add(r); } } return filtered; }
Example 18
Source File: JavaInput.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges) throws FormatterException { RangeSet<Integer> tokenRangeSet = TreeRangeSet.create(); for (Range<Integer> characterRange0 : characterRanges) { Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers()); tokenRangeSet.add( characterRangeToTokenRange( characterRange.lowerEndpoint(), characterRange.upperEndpoint() - characterRange.lowerEndpoint())); } return tokenRangeSet; }
Example 19
Source File: GuavaRangeSetUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenRangeSet_whenComplementIsCalled_returnsSucessfully() { final RangeSet<Integer> numberRangeSet = TreeRangeSet.create(); numberRangeSet.add(Range.closed(0, 2)); numberRangeSet.add(Range.closed(3, 5)); numberRangeSet.add(Range.closed(6, 8)); final RangeSet<Integer> numberRangeComplementSet = numberRangeSet.complement(); assertTrue(numberRangeComplementSet.contains(-1000)); assertFalse(numberRangeComplementSet.contains(2)); assertFalse(numberRangeComplementSet.contains(3)); assertTrue(numberRangeComplementSet.contains(1000)); }
Example 20
Source File: TestRangeSet.java From jackson-datatypes-collections with Apache License 2.0 | 4 votes |
public void testSerializeDeserialize() throws IOException { final RangeSet<Integer> rangeSet = TreeRangeSet.create(); rangeSet.add(Range.closedOpen(1, 2)); rangeSet.add(Range.openClosed(3, 4)); final String json = MAPPER.writeValueAsString(rangeSet); final TypeFactory tf = MAPPER.getTypeFactory(); final JavaType type = tf.constructParametricType(RangeSet.class, Integer.class); final ObjectReader reader = MAPPER.readerFor(type); final RangeSet<Integer> deserialized = reader.readValue(json); assertEquals(rangeSet, deserialized); }