下面列出了com.google.common.io.ByteSink#com.google.common.collect.ImmutableRangeSet 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
@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();
}
}
/**
* Test the PeriodicMarker constructor
*/
@Test
public void testConstructor() {
PeriodicMarker marker = new PeriodicMarker("name", "label", "id", "referenceid", "color", 1.0, "ms", Range.atLeast(0L), 0L, ImmutableRangeSet.of(Range.all()));
assertEquals("name", marker.getName());
assertEquals("label", marker.getLabel());
assertEquals("id", marker.getId());
assertEquals("referenceid", marker.getReferenceId());
assertEquals("color", marker.getColor());
assertEquals(1.0, marker.getPeriod(), 0);
assertEquals("ms", marker.getUnit());
assertEquals(Range.atLeast(0L), marker.getRange());
assertEquals(0L, marker.getOffset());
assertEquals(ImmutableRangeSet.of(Range.all()), marker.getIndexRange());
assertEquals(0, marker.getSubMarkers().size());
}
private ResponseMapping computeResponseMapping(final ArgumentOutput input) {
final ImmutableRangeSet<Integer> bannedRegions = docIdToBannedRegions.get(input.docId());
if (bannedRegions == null) {
throw new RuntimeException(String.format(
"QuoteFilter does not know about document ID %s", input.docId()));
}
final ImmutableSet.Builder<Response> toDeleteB = ImmutableSet.builder();;
for (final Response response : input.responses()) {
if (isInQuote(input.docId(), response.baseFiller())
|| isInQuote(input.docId(), response.canonicalArgument().charOffsetSpan()))
{
toDeleteB.add(response);
}
}
final ImmutableSet<Response> toDelete = toDeleteB.build();
log.info("For document {}, filtered out {} responses which were in quoted regions",
input.docId(), toDelete.size());
return ResponseMapping.create(ImmutableMap.<Response,Response>of(), toDelete);
}
private QuoteFilter(Map<Symbol, ImmutableRangeSet<Integer>> docIdToBannedRegions) {
this.docIdToBannedRegions = ImmutableMap.copyOf(docIdToBannedRegions);
for (RangeSet<Integer> rs : docIdToBannedRegions.values()) {
for (final Range<Integer> r : rs.asRanges()) {
checkArgument(r.hasLowerBound());
checkArgument(r.hasUpperBound());
checkArgument(r.lowerEndpoint() >= 0);
}
}
// these ensure we can serialize safely
for (Symbol sym : docIdToBannedRegions.keySet()) {
final String s = sym.toString();
checkArgument(!s.isEmpty(), "Document IDs may not be empty");
checkArgument(!CharMatcher.WHITESPACE.matchesAnyOf(s),
"Document IDs may not contain whitespace: %s", s);
}
}
public void saveTo(ByteSink sink) throws IOException {
final PrintWriter out = new PrintWriter(sink.asCharSink(Charsets.UTF_8).openBufferedStream());
out.println(docIdToBannedRegions.size());
for (final Map.Entry<Symbol, ImmutableRangeSet<Integer>> entry : docIdToBannedRegions
.entrySet()) {
out.println(entry.getKey());
final List<String> parts = Lists.newArrayList();
for (final Range<Integer> r : entry.getValue().asRanges()) {
// we know by construction these ranges are bounded above and below
parts.add(String.format("%d-%d", r.lowerEndpoint(), r.upperEndpoint()));
}
out.println(StringUtils.spaceJoiner().join(parts));
}
out.close();
}
@Test
public void testQuotedRegionComputation() throws IOException {
final Map<String, ImmutableRangeSet<Integer>> testCases = ImmutableMap.of(
"Foo <quote>bar <quote>baz</quote> <quote>meep</quote></quote> blah <quote>another</quote>",
ImmutableRangeSet.<Integer>builder().add(Range.closed(4, 60)).add(Range.closed(67, 88))
.build(),
"<quote>lalala</quote>", ImmutableRangeSet.of(Range.closed(0, 20)),
"No quotes!", ImmutableRangeSet.<Integer>of());
for (final Map.Entry<String, ImmutableRangeSet<Integer>> entry : testCases.entrySet()) {
final Symbol docid = Symbol.from("dummy");
final QuoteFilter reference =
QuoteFilter.createFromBannedRegions(ImmutableMap.of(docid, entry.getValue()));
final QuoteFilter computed = QuoteFilter.createFromOriginalText(ImmutableMap.of(docid,
CharSource.wrap(entry.getKey())));
assertEquals(reference, computed);
}
}
private @Nonnull Optional<TransformationStep> computeOutgoingSnatPoolTransformation(
SnatPool snatPool) {
RangeSet<Ip> pool =
ImmutableRangeSet.copyOf(
snatPool.getMembers().stream()
.map(_snatTranslations::get)
.filter(Objects::nonNull)
.map(SnatTranslation::getAddress)
.filter(Objects::nonNull)
.map(Range::singleton)
.collect(ImmutableList.toImmutableList()));
return pool.isEmpty()
? Optional.empty()
: Optional.of(
new ApplyAll(
ASSIGN_EPHEMERAL_SOURCE_PORT,
new AssignIpAddressFromPool(TransformationType.SOURCE_NAT, IpField.SOURCE, pool)));
}
@Test
public void exceptionTest() {
final UnresolvedNumber min = UnresolvedNumber.min();
final UnresolvedNumber max = UnresolvedNumber.max();
final EnumPair enumPair = EnumPairBuilder.create("enum1", 1).setDescription("description")
.setReference("reference").setUnknownSchemaNodes(mock(UnknownSchemaNode.class)).build();
final RangeSet<Integer> rangeset = ImmutableRangeSet.of(Range.closed(1, 2));
final InvalidRangeConstraintException invalidRangeConstraintException = new InvalidRangeConstraintException(
rangeset, "error msg", "other important messages");
assertSame(rangeset, invalidRangeConstraintException.getOffendingRanges());
final InvalidBitDefinitionException invalidBitDefinitionException = new InvalidBitDefinitionException(
BIT_A, "error msg", "other important messages");
assertEquals(invalidBitDefinitionException.getOffendingBit(), BIT_A);
final InvalidEnumDefinitionException invalidEnumDefinitionException = new InvalidEnumDefinitionException(
enumPair, "error msg", "other important messages");
assertEquals(invalidEnumDefinitionException.getOffendingEnum(), enumPair);
}
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);
}
private CrontabEntry(
RangeSet<Integer> minute,
RangeSet<Integer> hour,
RangeSet<Integer> dayOfMonth,
RangeSet<Integer> month,
RangeSet<Integer> dayOfWeek) {
checkEnclosed("minute", MINUTE, minute);
checkEnclosed("hour", HOUR, hour);
checkEnclosed("dayOfMonth", DAY_OF_MONTH, dayOfMonth);
checkEnclosed("month", MONTH, month);
checkEnclosed("dayOfWeek", DAY_OF_WEEK, dayOfWeek);
this.minute = ImmutableRangeSet.copyOf(minute);
this.hour = ImmutableRangeSet.copyOf(hour);
this.dayOfMonth = ImmutableRangeSet.copyOf(dayOfMonth);
this.month = ImmutableRangeSet.copyOf(month);
this.dayOfWeek = ImmutableRangeSet.copyOf(dayOfWeek);
checkArgument(hasWildcardDayOfMonth() || hasWildcardDayOfWeek(),
"Specifying both dayOfWeek and dayOfMonth is not supported.");
}
CommandLineOptions(
ImmutableList<String> files,
boolean inPlace,
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
boolean version,
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean removeJavadocOnlyImports,
boolean sortImports,
boolean removeUnusedImports) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
this.offsets = offsets;
this.lengths = lengths;
this.aosp = aosp;
this.version = version;
this.help = help;
this.stdin = stdin;
this.fixImportsOnly = fixImportsOnly;
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
this.sortImports = sortImports;
this.removeUnusedImports = removeUnusedImports;
}
CommandLineOptions(
ImmutableList<String> files,
boolean inPlace,
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
boolean version,
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean removeJavadocOnlyImports,
boolean sortImports,
boolean removeUnusedImports) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
this.offsets = offsets;
this.lengths = lengths;
this.aosp = aosp;
this.version = version;
this.help = help;
this.stdin = stdin;
this.fixImportsOnly = fixImportsOnly;
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
this.sortImports = sortImports;
this.removeUnusedImports = removeUnusedImports;
}
private IdnTable(
String name,
URI url,
URI policy,
ImmutableRangeSet<Integer> validCodepoints,
Optional<LanguageValidator> languageValidator) {
this.name = name;
this.url = checkNotNull(url, "%s missing '# URL: http://foo.example/page' line", name);
this.policy = checkNotNull(policy, "%s missing '# Policy: http://foo.example/page' line", name);
this.validCodepoints = checkNotNull(validCodepoints);
this.languageValidator = languageValidator;
}
/** 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);
}
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;
}
/**
* Test the SplitMarker and WeightedMarker constructors and method addMarker
*/
@Test
public void testAddSubMarker() {
PeriodicMarker marker = new PeriodicMarker("name", "label", "id", "referenceid", "color", 1.0, "ms", Range.atLeast(0L), 0L, ImmutableRangeSet.of(Range.all()));
SubMarker subMarkerA = new SplitMarker("A", "a", "a", "color", Range.atLeast(0L), ImmutableRangeSet.of(Range.all()));
marker.addSubMarker(subMarkerA);
SubMarker subMarkerB = new WeightedMarker("B");
marker.addSubMarker(subMarkerB);
assertEquals(Arrays.asList(subMarkerA, subMarkerB), marker.getSubMarkers());
}
/**
* Test the method addMarker
*/
@Test
public void testAddMarker() {
MarkerSet markerSet = new MarkerSet("name", "id");
Marker markerA = new PeriodicMarker("A", "A %d", "a", "ref.a", "color1", 1.0, "ms", Range.atLeast(1L), 1L, ImmutableRangeSet.of(Range.atLeast(1L)));
markerSet.addMarker(markerA);
Marker markerB = new PeriodicMarker("B", "B %d", "b", "ref.b", "color2", 2.0, "ns", Range.atLeast(2L), 2L, ImmutableRangeSet.of(Range.atLeast(2L)));
markerSet.addMarker(markerB);
assertEquals(Arrays.asList(markerA, markerB), markerSet.getMarkers());
}
CommandLineOptions(
ImmutableList<String> files,
boolean inPlace,
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
boolean version,
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean sortImports,
boolean removeUnusedImports,
boolean dryRun,
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings,
boolean formatJavadoc) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
this.offsets = offsets;
this.lengths = lengths;
this.aosp = aosp;
this.version = version;
this.help = help;
this.stdin = stdin;
this.fixImportsOnly = fixImportsOnly;
this.sortImports = sortImports;
this.removeUnusedImports = removeUnusedImports;
this.dryRun = dryRun;
this.setExitIfChanged = setExitIfChanged;
this.assumeFilename = assumeFilename;
this.reflowLongStrings = reflowLongStrings;
this.formatJavadoc = formatJavadoc;
}
@Test
public void testPortRulesToRanges() throws Exception {
RangeSet<Integer> actualRangeSet = Networking.portRulesToRanges(ImmutableList.of(
"22", "23", "5000-6000", "8081", "80-90", "90-100", "23", "8081"));
Asserts.assertEquals(actualRangeSet, ImmutableRangeSet.<Integer>builder()
.add(Range.closed(22, 22))
.add(Range.closed(23, 23))
.add(Range.closed(80, 100))
.add(Range.closed(5000, 6000))
.add(Range.closed(8081, 8081))
.build());
}
public boolean isInQuote(Symbol docId, CharOffsetSpan span) {
final ImmutableRangeSet<Integer> bannedRegions = docIdToBannedRegions.get(docId);
if (bannedRegions == null) {
throw new RuntimeException(String.format(
"QuoteFilter does not know about document ID %s", docId));
}
return inBannedSet(span, bannedRegions);
}
public static QuoteFilter createFromOriginalText(Map<Symbol, ? extends CharSource> originalTexts)
throws IOException {
checkNotNull(originalTexts);
final ImmutableMap.Builder<Symbol, ImmutableRangeSet<Integer>> ret = ImmutableMap.builder();
for (final Map.Entry<Symbol, ? extends CharSource> originalTextPair : originalTexts
.entrySet()) {
final Symbol docID = originalTextPair.getKey();
final CharSource originalTextSource = originalTextPair.getValue();
ret.put(docID, computeQuotedRegions(originalTextSource.read()));
}
return createFromBannedRegions(ret.build());
}
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());
}
@Test
public void testSerialization() throws IOException {
final QuoteFilter reference = QuoteFilter.createFromBannedRegions(
ImmutableMap.of(
Symbol.from("dummy"),
ImmutableRangeSet.<Integer>builder().add(Range.closed(4, 60)).add(Range.closed(67, 88))
.build(),
Symbol.from("dummy2"), ImmutableRangeSet.of(Range.closed(0, 20))));
final ByteArraySink sink = ByteArraySink.create();
reference.saveTo(sink);
final ByteSource source = ByteSource.wrap(sink.toByteArray());
final Object restored = QuoteFilter.loadFrom(source);
assertEquals(reference, restored);
}
/** Intersect two number spaces together. */
public final S intersection(S other) {
return newBuilder()
.including(
other._rangeset.asRanges().stream()
.map(_rangeset::subRangeSet) // intersect individual ranges with _rangeset
.map(RangeSet::asRanges) // flatten each intersection result to set of ranges
.flatMap(Set::stream) // stream for collection
.collect(ImmutableRangeSet.toImmutableRangeSet()))
.build();
}
@JsonCreator
private static AssignIpAddressFromPool jsonCreator(
@JsonProperty(PROP_TRANSFORMATION_TYPE) TransformationType type,
@JsonProperty(PROP_IP_FIELD) IpField ipField,
@JsonProperty(PROP_IP_RANGES) RangeSet<Ip> ipRanges) {
checkNotNull(type, PROP_TRANSFORMATION_TYPE + " cannot be null");
checkNotNull(ipField, PROP_IP_FIELD + " cannot be null");
checkNotNull(ipRanges, PROP_IP_RANGES + " cannot be null");
return new AssignIpAddressFromPool(type, ipField, ImmutableRangeSet.copyOf(ipRanges));
}
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());
}
private @Nonnull TransformationStep computeVirtualIncomingPoolMemberTransformation(
PoolMember member, boolean translateAddress, boolean translatePort) {
TransformationStep addressTranslation =
translateAddress
? new AssignIpAddressFromPool(
TransformationType.DEST_NAT,
IpField.DESTINATION,
ImmutableRangeSet.of(Range.singleton(member.getAddress())))
: null;
TransformationStep portTranslation =
translatePort
? new AssignPortFromPool(
TransformationType.DEST_NAT,
PortField.DESTINATION,
member.getPort(),
member.getPort())
: null;
if (translateAddress && translatePort) {
// pool
return new ApplyAll(addressTranslation, portTranslation);
} else if (translateAddress) {
// pool
return addressTranslation;
} else if (translatePort) {
// pool
return portTranslation;
} else {
// ip-forward or just pool with weird options
return Noop.NOOP_DEST_NAT;
}
}
@Nonnull
private RangeSet<Ip> ipRangeSetFromRuleEndpoints(
Collection<RuleEndpoint> endpoints, Vsys vsys, Warnings w) {
RangeSet<Ip> rangeSet = TreeRangeSet.create();
endpoints.stream()
.map(endpoint -> ruleEndpointToIpRangeSet(endpoint, vsys, w))
.forEach(rangeSet::addAll);
return ImmutableRangeSet.copyOf(rangeSet);
}