List of usage examples for com.google.common.collect Range contains
public boolean contains(C value)
From source file:net.bican.iplib.sample.Sample.java
public static void main(String[] args) { // creating a network range by CIDR address CIDR cidr1 = new CIDR("10.10.10.0/30"); Range<IPAddress> range1 = IPAddresses.fromCIDR(cidr1); // iterating a network range Iterables.all(new AddressIterable(range1), new Predicate<IPAddress>() { @Override/*from w w w . j a v a2 s . c o m*/ public boolean apply(IPAddress input) { System.out.println(input); return true; } }); // creating a network range by netmask Netmask netmask1 = new Netmask("10.10.20.0/255.255.255.0"); Range<IPAddress> range2 = IPAddresses.fromNetmask(netmask1); System.out.println("range contains ip: " + range2.contains(IPAddress.getInstance(InetAddresses.forString("10.10.20.20")))); System.out.println("range contains ip: " + range2.contains(IPAddress.getInstance(InetAddresses.forString("10.10.21.20")))); // creating a network range from an arbitrary interval Range<IPAddress> interval = Range.closed(IPAddress.getInstance(InetAddresses.forString("1.0.0.1")), IPAddress.getInstance(InetAddresses.forString("1.0.2.22"))); Set<Range<IPAddress>> ips = IPAddresses.fromInterval(interval); for (Range<IPAddress> i : ips) { System.out.println(i); } }
From source file:com.stackframe.collect.RangeUtilities.java
/** * Determine if a value is contained in any supplied Ranges. * * @param <C> the class that Range is constrained to * @param ranges the ranges to check/*from w ww .j ava2s .com*/ * @param value the value to check for * @return true if value is contained in any of the ranges */ public static <C extends Comparable> boolean contains(Iterable<Range<C>> ranges, C value) { for (Range<C> r : ranges) { if (r.contains(value)) { return true; } } return false; }
From source file:me.lucko.luckperms.common.utils.Predicates.java
public static Predicate<Integer> notInRange(int start, int end) { Range<Integer> range = Range.closed(start, end); return value -> !range.contains(value); }
From source file:com.android.build.gradle.integration.common.utils.AndroidVersionMatcher.java
public static Matcher<AndroidVersion> forRange(Range<Integer> range) { return new BaseMatcher<AndroidVersion>() { @Override/*from w w w . j a v a 2s .c om*/ public boolean matches(Object item) { return item instanceof AndroidVersion && range.contains(((AndroidVersion) item).getApiLevel()); } @Override public void describeTo(Description description) { description.appendText("Android versions in the ").appendText(range.toString()) .appendText(" range."); } }; }
From source file:org.asoem.greyfish.utils.collect.LinearSequences.java
/** * Create a crossover product between two {@code Iterables} with crossovers at given {@code indices}. This means * that both {@code Iterable}s in the returned product are a combination of the input iterables in such a way, that * the constructed iterable switches the input iterable at each given position. Both input {@code Iterable}s will be * zipped with {@link Products#zip(Iterable, Iterable)}. Therefore the returned {@code Iterable}s will have the same * size equal to the size of the input iterable with the fewest elements. * * @param x The first Iterable/*w w w . ja v a 2 s. c om*/ * @param y The second Iterable * @param indices the indices at which to do the crossovers * @param <E> the type of the elements in {@code Iterable}s * @return a product of Iterables with crossovers at the given indices */ public static <E> Product2<Iterable<E>, Iterable<E>> crossover(final Iterable<E> x, final Iterable<E> y, final Set<Integer> indices) { checkNotNull(x); checkNotNull(y); checkNotNull(indices); final Iterable<Product2<E, E>> zipped = Products.zip(x, y); if (indices.isEmpty()) { return Products.unzip(zipped); } else { final FunctionalList<Range<Integer>> ranges = ImmutableFunctionalList.copyOf( Iterables.transform(Iterables.partition(Ordering.natural().immutableSortedCopy(indices), 2), new Function<List<Integer>, Range<Integer>>() { @Nullable @Override public Range<Integer> apply(@Nullable final List<Integer> input) { assert input != null; return input.size() == 2 ? Range.closed(input.get(0), input.get(1)) : Range.atLeast(input.get(0)); } })); return Products.unzip(Iterables.transform(Products.zipWithIndex(zipped), new Function<Product2<Product2<E, E>, Integer>, Product2<E, E>>() { @Nullable @Override public Product2<E, E> apply(@Nullable final Product2<Product2<E, E>, Integer> input) { assert input != null; return ranges.any(new Predicate<Range<Integer>>() { @Override public boolean apply(@Nullable final Range<Integer> range) { assert range != null; return range.contains(input.second()); } }) ? Products.swap(input.first()) : input.first(); } })); } }
From source file:jetbrains.jetpad.cell.util.Cells.java
public static Cell findClosestFocusableToSide(Cell current, Vector loc) { if (!current.visible().get()) return null; Rectangle bounds = current.getBounds(); Range<Integer> range = Range.closed(bounds.origin.y, bounds.origin.y + bounds.dimension.y); if (!range.contains(loc.y)) { return null; }/*from w w w . ja v a 2 s. c o m*/ Cell result = null; int distance = Integer.MAX_VALUE; for (Cell child : current.children()) { if (!child.visible().get()) continue; Cell closest = findClosestFocusableToSide(child, loc); if (closest == null) continue; int newDistance = (int) closest.getBounds().distance(loc); if (newDistance < distance) { result = closest; distance = newDistance; } } if (result == null && current.focusable().get()) { return current; } return result; }
From source file:net.sf.mzmine.modules.peaklistmethods.isotopes.isotopepatternscore.IsotopePatternScoreCalculator.java
/** * Returns a calculated similarity score of two isotope patterns in the * range of 0 (not similar at all) to 1 (100% same). *//*from w w w . ja v a 2s . com*/ public static double getSimilarityScore(IsotopePattern ip1, IsotopePattern ip2, ParameterSet parameters) { assert ip1 != null; assert ip2 != null; MZTolerance mzTolerance = parameters.getParameter(IsotopePatternScoreParameters.mzTolerance).getValue(); assert mzTolerance != null; final double patternIntensity = Math.max(ip1.getHighestDataPoint().getIntensity(), ip2.getHighestDataPoint().getIntensity()); final double noiseIntensity = parameters.getParameter(IsotopePatternScoreParameters.isotopeNoiseLevel) .getValue(); // Normalize the isotopes to intensity 0..1 IsotopePattern nip1 = IsotopePatternCalculator.normalizeIsotopePattern(ip1); IsotopePattern nip2 = IsotopePatternCalculator.normalizeIsotopePattern(ip2); // Merge the data points from both isotope patterns into a single array. // Data points from first pattern will have positive intensities, data // points from second pattern will have negative intensities. ArrayList<DataPoint> mergedDataPoints = new ArrayList<DataPoint>(); for (DataPoint dp : nip1.getDataPoints()) { if (dp.getIntensity() * patternIntensity < noiseIntensity) continue; mergedDataPoints.add(dp); } for (DataPoint dp : nip2.getDataPoints()) { if (dp.getIntensity() * patternIntensity < noiseIntensity) continue; DataPoint negativeDP = new SimpleDataPoint(dp.getMZ(), dp.getIntensity() * -1); mergedDataPoints.add(negativeDP); } DataPoint mergedDPArray[] = mergedDataPoints.toArray(new DataPoint[0]); // Sort the merged data points by m/z Arrays.sort(mergedDPArray, new DataPointSorter(SortingProperty.MZ, SortingDirection.Ascending)); // Iterate the merged data points and sum all isotopes within m/z // tolerance for (int i = 0; i < mergedDPArray.length - 1; i++) { Range<Double> toleranceRange = mzTolerance.getToleranceRange(mergedDPArray[i].getMZ()); if (!toleranceRange.contains(mergedDPArray[i + 1].getMZ())) continue; double summedIntensity = mergedDPArray[i].getIntensity() + mergedDPArray[i + 1].getIntensity(); double newMZ = mergedDPArray[i + 1].getMZ(); // Update the next data point and remove the current one mergedDPArray[i + 1] = new SimpleDataPoint(newMZ, summedIntensity); mergedDPArray[i] = null; } // Calculate the resulting score. Ideal score is 1, in case the final // data point array is empty. double result = 1; for (DataPoint dp : mergedDPArray) { if (dp == null) continue; double remainingIntensity = Math.abs(dp.getIntensity()); // In case some large isotopes were grouped together, the summed // intensity may be over 1 if (remainingIntensity > 1) remainingIntensity = 1; // Decrease the score with each remaining peak result *= 1 - remainingIntensity; } return result; }
From source file:net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.rdbe.RDBERestrictionChecker.java
public static boolean checkRDBE(double rdbeValue, ParameterSet parameters) { boolean mustBeInteger = parameters.getParameter(RDBERestrictionParameters.rdbeWholeNum).getValue(); Range<Double> rdbeRange = parameters.getParameter(RDBERestrictionParameters.rdbeRange).getValue(); if ((mustBeInteger) && (Math.floor(rdbeValue) != rdbeValue)) return false; return rdbeRange.contains(rdbeValue); }
From source file:jetbrains.jetpad.projectional.view.ScrollUtil.java
static int moveDelta(Range<Integer> container, Range<Integer> range) { if (container.encloses(range)) return 0; if (container.upperEndpoint() - container.lowerEndpoint() < range.upperEndpoint() - range.lowerEndpoint()) { return container.lowerEndpoint() - range.lowerEndpoint(); }/*from ww w .j a va2s . co m*/ if (container.contains(range.upperEndpoint())) { return container.lowerEndpoint() - range.lowerEndpoint(); } if (container.contains(range.lowerEndpoint())) { return container.upperEndpoint() - range.upperEndpoint(); } if (container.upperEndpoint() < range.lowerEndpoint()) { return container.upperEndpoint() - range.upperEndpoint(); } else if (container.lowerEndpoint() > range.upperEndpoint()) { return container.lowerEndpoint() - range.lowerEndpoint(); } else { throw new IllegalStateException("This can't happen"); } }
From source file:dk.dma.commons.web.rest.query.QueryParameterValidators.java
/** * Parses the specified parameter value to an integer * //from w w w .j a v a 2s . c om * @param info * the URI info * @param parameterName * the name of the parameter we are parsing * @param value * the string value of the parameter * @param range * the range the value bust be within. Use Range#all to include all possible integers * @return the value as an integer * @see Range#all() * @throws QueryParameterException * if the specified value is not an integer. Or if the specified value is not within the specified range */ static Integer parseInt(UriInfo info, String parameterName, String value, Range<Integer> range) { Integer result; try { result = Integer.parseInt(value); } catch (NumberFormatException e) { throw new QueryParameterException(info, parameterName, value, Integer.class); } if (!range.contains(result)) { throw new QueryParameterException(info, parameterName, value, range); } return result; }