List of usage examples for com.google.common.collect Range lowerEndpoint
public C lowerEndpoint()
From source file:com.google.googlejavaformat.java.RemoveUnusedImports.java
/** Applies the replacements to the given source, and re-format any edited javadoc. */ private static String applyReplacements(String source, RangeMap<Integer, String> replacements) { // save non-empty fixed ranges for reformatting after fixes are applied RangeSet<Integer> fixedRanges = TreeRangeSet.create(); // Apply the fixes in increasing order, adjusting ranges to account for // earlier fixes that change the length of the source. The output ranges are // needed so we can reformat fixed regions, otherwise the fixes could just // be applied in descending order without adjusting offsets. StringBuilder sb = new StringBuilder(source); int offset = 0; for (Map.Entry<Range<Integer>, String> replacement : replacements.asMapOfRanges().entrySet()) { Range<Integer> range = replacement.getKey(); String replaceWith = replacement.getValue(); int start = offset + range.lowerEndpoint(); int end = offset + range.upperEndpoint(); sb.replace(start, end, replaceWith); if (!replaceWith.isEmpty()) { fixedRanges.add(Range.closedOpen(start, end)); }/*from ww w. j a v a 2 s . co m*/ offset += replaceWith.length() - (range.upperEndpoint() - range.lowerEndpoint()); } String result = sb.toString(); // If there were any non-empty replaced ranges (e.g. javadoc), reformat the fixed regions. // We could avoid formatting twice in --fix-imports=also mode, but that is not the default // and removing imports won't usually affect javadoc. if (!fixedRanges.isEmpty()) { try { result = new Formatter().formatSource(result, fixedRanges.asRanges()); } catch (FormatterException e) { // javadoc reformatting is best-effort } } return result; }
From source file:net.sf.mzmine.modules.peaklistmethods.msms.msmsscore.MSMSScoreCalculator.java
/** * Returns a calculated similarity score of *//*from w w w . j a v a 2s. c o m*/ public static MSMSScore evaluateMSMS(IMolecularFormula parentFormula, Scan msmsScan, ParameterSet parameters) { MZTolerance msmsTolerance = parameters.getParameter(MSMSScoreParameters.msmsTolerance).getValue(); String massListName = parameters.getParameter(MSMSScoreParameters.massList).getValue(); MassList massList = msmsScan.getMassList(massListName); if (massList == null) { throw new IllegalArgumentException("Scan #" + msmsScan.getScanNumber() + " does not have a mass list called '" + massListName + "'"); } DataPoint msmsIons[] = massList.getDataPoints(); if (msmsIons == null) { throw new IllegalArgumentException( "Mass list " + massList + " does not contain data for scan #" + msmsScan.getScanNumber()); } MolecularFormulaRange msmsElementRange = new MolecularFormulaRange(); for (IIsotope isotope : parentFormula.isotopes()) { msmsElementRange.addIsotope(isotope, 0, parentFormula.getIsotopeCount(isotope)); } int totalMSMSpeaks = 0, interpretedMSMSpeaks = 0; Map<DataPoint, String> msmsAnnotations = new Hashtable<DataPoint, String>(); msmsCycle: for (DataPoint dp : msmsIons) { // Check if this is an isotope Range<Double> isotopeCheckRange = Range.closed(dp.getMZ() - 1.4, dp.getMZ() - 0.6); for (DataPoint dpCheck : msmsIons) { // If we have any MS/MS peak with 1 neutron mass smaller m/z // and higher intensity, it means the current peak is an // isotope and we should ignore it if (isotopeCheckRange.contains(dpCheck.getMZ()) && (dpCheck.getIntensity() > dp.getIntensity())) { continue msmsCycle; } } // If getPrecursorCharge() returns 0, it means charge is unknown. In // that case let's assume charge 1 int precursorCharge = msmsScan.getPrecursorCharge(); if (precursorCharge == 0) precursorCharge = 1; // We don't know the charge of the fragment, so we will simply // assume 1 double neutralLoss = msmsScan.getPrecursorMZ() * precursorCharge - dp.getMZ(); // Ignore negative neutral losses and parent ion, <5 may be a // good threshold if (neutralLoss < 5) { continue; } Range<Double> msmsTargetRange = msmsTolerance.getToleranceRange(neutralLoss); IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance(); MolecularFormulaGenerator msmsEngine; try { msmsEngine = new MolecularFormulaGenerator(builder, msmsTargetRange.lowerEndpoint(), msmsTargetRange.upperEndpoint(), msmsElementRange); } catch (CDKException e) { e.printStackTrace(); return null; } IMolecularFormula formula = msmsEngine.getNextFormula(); if (formula != null) { String formulaString = MolecularFormulaManipulator.getString(formula); msmsAnnotations.put(dp, formulaString); interpretedMSMSpeaks++; } totalMSMSpeaks++; } // If we did not evaluate any MS/MS peaks, we cannot calculate a score if (totalMSMSpeaks == 0) { return null; } double msmsScore = (double) interpretedMSMSpeaks / totalMSMSpeaks; MSMSScore result = new MSMSScore(msmsScore, msmsAnnotations); return result; }
From source file:net.bican.iplib.IPAddresses.java
static Range<IPAddress> canonical(final Range<IPAddress> range, final LongDiscreteDomain<IPAddress> domain) { if (range.isEmpty()) { return null; }/*w ww .jav a 2s. c o m*/ final boolean l = range.lowerBoundType() == BoundType.OPEN; final boolean u = range.upperBoundType() == BoundType.OPEN; final IPAddress s = range.lowerEndpoint(); final IPAddress e = range.upperEndpoint(); if (l && u) { Range.closed(domain.next(s), domain.previous(e)); } else if (l) { return Range.closed(domain.next(s), e); } else if (u) { return Range.closed(s, domain.previous(e)); } return range; }
From source file:org.apache.hadoop.hive.ql.optimizer.calcite.druid.DruidIntervalUtils.java
protected static boolean mergeable(Range range1, Range range2) { Comparable x1 = range1.upperEndpoint(); Comparable x2 = range2.lowerEndpoint(); int compare = x1.compareTo(x2); return compare > 0 || (compare == 0 && range1.upperBoundType() == BoundType.CLOSED && range2.lowerBoundType() == BoundType.CLOSED); }
From source file:org.apache.calcite.adapter.druid.DruidDateTimeUtils.java
protected static List<LocalInterval> toInterval(List<Range<Calendar>> ranges) { List<LocalInterval> intervals = Lists.transform(ranges, new Function<Range<Calendar>, LocalInterval>() { public LocalInterval apply(Range<Calendar> range) { if (!range.hasLowerBound() && !range.hasUpperBound()) { return DruidTable.DEFAULT_INTERVAL; }/*from w w w . j av a 2s . c o m*/ long start = range.hasLowerBound() ? range.lowerEndpoint().getTime().getTime() : DruidTable.DEFAULT_INTERVAL.getStartMillis(); long end = range.hasUpperBound() ? range.upperEndpoint().getTime().getTime() : DruidTable.DEFAULT_INTERVAL.getEndMillis(); if (range.hasLowerBound() && range.lowerBoundType() == BoundType.OPEN) { start++; } if (range.hasUpperBound() && range.upperBoundType() == BoundType.CLOSED) { end++; } return LocalInterval.create(start, end); } }); if (LOGGER.isInfoEnabled()) { LOGGER.info("Converted time ranges " + ranges + " to interval " + intervals); } return intervals; }
From source file:com.zulily.omicron.crontab.CrontabExpression.java
/** * Does the actual work of tearing apart the schedule expression and making them * into numerical sets of runtime whitelists * * @param expressionPart The current part we're working on * @param expression The text expression to evaluate * @return A set within the expression's possible execution range *///from w w w. j a va 2 s .c om private static ImmutableSortedSet<Integer> evaluateExpressionPart(final ExpressionPart expressionPart, final String expression) { // Order of operations -> // 1) Split value by commas (lists) and for each csv.n: // 2) Split value by slashes (range/rangeStep) // 3) Match all for '*' or split hyphenated range for rangeStart and rangeEnd // // Converts sun==7 -> sun==0 to make schedule interpretation logic easier in timeInSchedule() evaluation // NOTE: this breaks week spanning ranges such as fri-tue, which instead must // be handled as a list of ranges fri-sat,sun-tue final List<String> csvParts = Utils.COMMA_SPLITTER.splitToList(expression); final TreeSet<Integer> results = Sets.newTreeSet(); for (final String csvPart : csvParts) { final List<String> slashParts = Utils.FORWARD_SLASH_SPLITTER.splitToList(csvPart); // Range step of expression i.e. */2 (none is 1 obviously) int rangeStep = 1; checkArgument(!slashParts.isEmpty() && slashParts.size() <= 2, "Invalid cron expression for %s: %s", expressionPart.name(), expression); if (slashParts.size() == 2) { // Ordinal definition: 0 = rangeExpression, 1 = stepExpression final Integer rangeStepInteger = expressionPart.textUnitToInt(slashParts.get(1)); checkNotNull(rangeStepInteger, "Invalid cron expression for %s (rangeStep is not a positive int): %s", expressionPart.name(), expression); checkArgument(rangeStepInteger > 0, "Invalid cron expression for %s (rangeStep is not valid): %s", expressionPart.name(), expression); rangeStep = rangeStepInteger; } final String rangeExpression = slashParts.get(0); final Range<Integer> allowedRange = expressionPart.getAllowedRange(); int rangeStart = allowedRange.lowerEndpoint(); int rangeEnd = allowedRange.upperEndpoint(); // either * or 0 or 0-6, etc if (!"*".equals(rangeExpression)) { final List<String> hyphenParts = Utils.HYPHEN_SPLITTER.splitToList(rangeExpression); checkArgument(!hyphenParts.isEmpty() && hyphenParts.size() <= 2, "Invalid cron expression for %s: %s", expressionPart.name(), expression); Integer rangeStartInteger = expressionPart.textUnitToInt(hyphenParts.get(0)); checkNotNull(rangeStartInteger, "Invalid cron expression for %s (rangeStart is not an int): %s", expressionPart.name(), expression); //correct terrible "sunday can be either 0 or 7" bug/feature in crond if (expressionPart == ExpressionPart.DaysOfWeek && rangeStartInteger == 7) { rangeStartInteger = 0; } checkArgument(allowedRange.contains(rangeStartInteger), "Invalid cron expression for %s (valid range is %s): %s", expressionPart.name(), expressionPart.getAllowedRange(), expression); rangeStart = rangeStartInteger; if (hyphenParts.size() == 2) { Integer rangeEndInteger = expressionPart.textUnitToInt(hyphenParts.get(1)); checkNotNull(rangeEndInteger, "Invalid cron expression for %s (rangeEnd is not an int): %s", expressionPart.name(), expression); //correct terrible "sunday can be either 0 or 7" bug/feature in crond if (expressionPart == ExpressionPart.DaysOfWeek && rangeEndInteger == 7) { rangeEndInteger = 0; } checkArgument(allowedRange.contains(rangeEndInteger), "Invalid cron expression for %s (valid range is %s): %s", expressionPart.name(), expressionPart.getAllowedRange(), expression); rangeEnd = rangeEndInteger; } else { // Single value specified rangeEnd = rangeStart; } } checkArgument(rangeStart <= rangeEnd, "Invalid cron expression for %s (range start must not be greater than range end): %s", expressionPart.name(), expression); for (int runTime = rangeStart; runTime <= rangeEnd; runTime += rangeStep) { results.add(runTime); } } return ImmutableSortedSet.copyOf(results); }
From source file:org.nmdp.ngs.range.rtree.RangeGeometries.java
/** * Create and return a new rectangle geometry from the specified range. * * @param range range, must not be null, must not be empty, and must have lower and upper bounds * @return a new rectangle geometry from the specified range *///from w ww .j a v a 2 s . co m public static <N extends Number & Comparable<? super N>> Rectangle range(final Range<N> range) { checkNotNull(range); if (range.isEmpty()) { throw new IllegalArgumentException("range must not be empty"); } if (!range.hasLowerBound() || !range.hasUpperBound()) { throw new IllegalArgumentException("range must have lower and upper bounds"); } Number lowerEndpoint = range.lowerEndpoint(); BoundType lowerBoundType = range.lowerBoundType(); Number upperEndpoint = range.upperEndpoint(); BoundType upperBoundType = range.upperBoundType(); /* Since we are representing genomic coordinate systems, the expectation is that endpoints are instance of Integer, Long, or BigInteger; thus for open lower and upper bounds we can safely add or substract 1.0 respectively. Then by convention a rectangle with y1 0.0 and height of 1.0 is used. closed(10, 20) --> (10.0, 0.0, 20.0, 1.0) closedOpen(10, 20) --> (10.0, 0.0, 19.0, 1.0) openClosed(10, 20) --> (11.0, 0.0, 20.0, 1.0) open(10, 20) --> (11.0, 0.0, 19.0, 1.0); closed(10, 11) --> (10.0, 0.0, 11.0, 1.0) closedOpen(10, 11) --> (10.0, 0.0, 10.0, 1.0) openClosed(10, 11) --> (11.0, 0.0, 11.0, 1.0) open(10, 11) --> empty, throw exception closed(10, 10) --> (10.0, 0.0, 10.0, 1.0) closedOpen(10, 10) --> empty, throw exception openClosed(10, 10) --> empty, throw exception open(10, 10) --> empty, throw exception */ double x1 = lowerBoundType == BoundType.OPEN ? lowerEndpoint.doubleValue() + 1.0d : lowerEndpoint.doubleValue(); double y1 = 0.0d; double x2 = upperBoundType == BoundType.OPEN ? upperEndpoint.doubleValue() - 1.0d : upperEndpoint.doubleValue(); double y2 = 1.0d; return Geometries.rectangle(x1, y1, x2, y2); }
From source file:org.kitesdk.data.spi.ConstraintsSerialization.java
/** * Serializes an {@link Range} into the specified {@code out} stream. */// www. jav a 2s . co m private static void writeRangePredicate(Schema fieldSchema, Range range, ObjectOutputStream out) throws IOException { if (range.hasLowerBound()) { //write out that there is a lower endpoint and the value. out.writeBoolean(true); out.writeBoolean(range.isLowerBoundOpen()); writeValue(fieldSchema, range.lowerEndpoint(), out); } else { //Write that there is no lower bound out.writeBoolean(false); } if (range.hasUpperBound()) { out.writeBoolean(true); out.writeBoolean(range.isUpperBoundOpen()); //write out that there is a lower endpoint and the value. writeValue(fieldSchema, range.upperEndpoint(), out); } else { //write out that there is not an upper bound out.writeBoolean(false); } }
From source file:org.apache.druid.sql.calcite.filtration.MoveTimeFiltersToIntervals.java
private static Range<Long> toLongRange(final Range<BoundValue> range) { if (!range.hasUpperBound() && !range.hasLowerBound()) { return Range.all(); } else if (range.hasUpperBound() && !range.hasLowerBound()) { return Range.upTo(Long.parseLong(range.upperEndpoint().getValue()), range.upperBoundType()); } else if (!range.hasUpperBound() && range.hasLowerBound()) { return Range.downTo(Long.parseLong(range.lowerEndpoint().getValue()), range.lowerBoundType()); } else {//ww w . j a v a 2s . c om return Range.range(Long.parseLong(range.lowerEndpoint().getValue()), range.lowerBoundType(), Long.parseLong(range.upperEndpoint().getValue()), range.upperBoundType()); } }
From source file:org.dishevelled.bio.range.rtree.RangeGeometries.java
/** * Create and return a new rectangle geometry from the specified range. * * @param <N> value type// w ww . jav a 2 s . c o m * @param range range, must not be null, must not be empty, and must have lower and upper bounds * @return a new rectangle geometry from the specified range */ public static <N extends Number & Comparable<? super N>> Rectangle range(final Range<N> range) { checkNotNull(range); if (range.isEmpty()) { throw new IllegalArgumentException("range must not be empty"); } if (!range.hasLowerBound() || !range.hasUpperBound()) { throw new IllegalArgumentException("range must have lower and upper bounds"); } Number lowerEndpoint = range.lowerEndpoint(); BoundType lowerBoundType = range.lowerBoundType(); Number upperEndpoint = range.upperEndpoint(); BoundType upperBoundType = range.upperBoundType(); /* Since we are representing genomic coordinate systems, the expectation is that endpoints are instance of Integer, Long, or BigInteger; thus for open lower and upper bounds we can safely add or subtract 1.0 respectively. Then by convention a rectangle with y1 0.0 and height of 1.0 is used. closed(10, 20) --> (10.0, 0.0, 20.0, 1.0) closedOpen(10, 20) --> (10.0, 0.0, 19.0, 1.0) openClosed(10, 20) --> (11.0, 0.0, 20.0, 1.0) open(10, 20) --> (11.0, 0.0, 19.0, 1.0); closed(10, 11) --> (10.0, 0.0, 11.0, 1.0) closedOpen(10, 11) --> (10.0, 0.0, 10.0, 1.0) openClosed(10, 11) --> (11.0, 0.0, 11.0, 1.0) open(10, 11) --> empty, throw exception closed(10, 10) --> (10.0, 0.0, 10.0, 1.0) closedOpen(10, 10) --> empty, throw exception openClosed(10, 10) --> empty, throw exception open(10, 10) --> empty, throw exception */ double x1 = lowerBoundType == BoundType.OPEN ? lowerEndpoint.doubleValue() + 1.0d : lowerEndpoint.doubleValue(); double y1 = 0.0d; double x2 = upperBoundType == BoundType.OPEN ? upperEndpoint.doubleValue() - 1.0d : upperEndpoint.doubleValue(); double y2 = 1.0d; return Geometries.rectangle(x1, y1, x2, y2); }