Java Code Examples for com.google.common.collect.Range#open()

The following examples show how to use com.google.common.collect.Range#open() . 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: ConditionGroup.java    From Prism with MIT License 6 votes vote down vote up
/**
 * Convenience method to build conditions for a region of radius around a central location.
 *
 * @param location Location<?>
 * @param radius Integer
 * @return ConditionGroup
 */
public static ConditionGroup from(Location<?> location, int radius) {
    ConditionGroup conditions = new ConditionGroup(Operator.AND);

    // World
    conditions.add(FieldCondition.of(DataQueries.Location.then(DataQueries.WorldUuid), MatchRule.EQUALS, location.getExtent().getUniqueId().toString()));

    // X
    Range<Integer> xRange = Range.open(location.getBlockX() - radius, location.getBlockX() + radius);
    conditions.add(FieldCondition.of(DataQueries.Location.then(DataQueries.X), xRange));

    // Y
    Range<Integer> yRange = Range.open(location.getBlockY() - radius, location.getBlockY() + radius);
    conditions.add(FieldCondition.of(DataQueries.Location.then(DataQueries.Y), yRange));

    // Z
    Range<Integer> zRange = Range.open(location.getBlockZ() - radius, location.getBlockZ() + radius);
    conditions.add(FieldCondition.of(DataQueries.Location.then(DataQueries.Z), zRange));

    return conditions;
}
 
Example 2
Source File: RangeUtils.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public static Range<Double> fromArray(double array[]) {
  if ((array == null) || (array.length == 0))
    return Range.open(0.0, 0.0);
  double min = array[0], max = array[0];
  for (double d : array) {
    if (d > max)
      max = d;
    if (d < min)
      min = d;
  }
  return Range.closed(min, max);
}
 
Example 3
Source File: SafeRangeOperationUtilsTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertSafeIntersectionForBigDecimal() {
    Range<Comparable<?>> range = Range.upTo(new BigDecimal("2331.23211"), BoundType.CLOSED);
    Range<Comparable<?>> connectedRange = Range.open(135.13F, 45343.23F);
    Range<Comparable<?>> newRange = SafeRangeOperationUtils.safeIntersection(range, connectedRange);
    assertThat(newRange.lowerEndpoint(), is(new BigDecimal("135.13")));
    assertThat(newRange.lowerBoundType(), is(BoundType.OPEN));
    assertThat(newRange.upperEndpoint(), is(new BigDecimal("2331.23211")));
    assertThat(newRange.upperBoundType(), is(BoundType.CLOSED));
}
 
Example 4
Source File: RangeHeader.java    From notification with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param builder
 */
private RangeHeader(final Builder builder) {

  this.field = builder.field;

  if (builder.fromId != null && builder.toId != null) {
    if (builder.fromInclusive && builder.toInclusive) {
      range = Range.closed(builder.fromId, builder.toId);
    } else if (builder.fromInclusive && !builder.toInclusive) {
      range = Range.closedOpen(builder.fromId, builder.toId);
    } else if (!builder.fromInclusive && builder.toInclusive) {
      range = Range.openClosed(builder.fromId, builder.toId);
    } else {
      range = Range.open(builder.fromId, builder.toId);
    }
  } else if (builder.fromId != null && builder.toId == null) {
    if (builder.fromInclusive) {
      range = Range.atLeast(builder.fromId);
    } else {
      range = Range.greaterThan(builder.fromId);
    }
  } else if (builder.fromId == null && builder.toId != null) {
    if (builder.toInclusive) {
      range = Range.atMost(builder.toId);
    } else {
      range = Range.lessThan(builder.toId);
    }
  } else {
    range = Range.all();
  }

  this.max = builder.max;
}
 
Example 5
Source File: RangeUtils.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public static Range<Double> fromArray(double array[]) {
  if ((array == null) || (array.length == 0))
    return Range.open(0.0, 0.0);
  double min = array[0], max = array[0];
  for (double d : array) {
    if (d > max)
      max = d;
    if (d < min)
      min = d;
  }
  return Range.closed(min, max);
}
 
Example 6
Source File: OccurrenceResource.java    From occurrence with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected static Range<Integer> parseYearRange(String year) {
  final int now = 1901 + new Date().getYear();
  if (Strings.isNullOrEmpty(year)) {
    // return all years between 1500 and now
    return Range.open(1500, now);
  }
  try {
    Range<Integer> result = null;
    String[] years = year.split(",");
    if (years.length == 1) {
      result = Range.open(Integer.parseInt(years[0].trim()), now);

    } else if (years.length == 2) {
      result = Range.open(Integer.parseInt(years[0].trim()), Integer.parseInt(years[1].trim()));
    }

    // verify upper and lower bounds are sensible
    if (result == null || result.lowerEndpoint().intValue() < 1000 || result.upperEndpoint().intValue() > now) {
      throw new IllegalArgumentException("Valid year range between 1000 and now expected, separated by a comma");
    }
    return result;

  } catch (IllegalArgumentException e) {
    throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
      .entity("Parameter "+ year +" is not a valid year range")
      .build());
  }
}
 
Example 7
Source File: RangeTest.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testClosed() {

    Range anull = Range.closedOpen(0, 0);

    Range r1 = Range.closed(2, 5);

    Range a1 = Range.open(1, 2);
    Range a2 = Range.open(1, 3);
    Range a3 = Range.open(1, 5);
    Range a4 = Range.open(1, 6);
    Range a5 = Range.open(6, 7);
    Range a6 = Range.open(5, 7);
    Range a7 = Range.open(4, 7);
    Range a8 = Range.open(2, 7);
    Range a9 = Range.open(1, 7);

    Range b1 = Range.closed(1, 2);
    Range b2 = Range.closed(1, 3);
    Range b3 = Range.closed(1, 5);
    Range b4 = Range.closed(1, 6);
    Range b5 = Range.closed(6, 7);
    Range b6 = Range.closed(5, 7);
    Range b7 = Range.closed(4, 7);
    Range b8 = Range.closed(2, 7);
    Range b9 = Range.closed(1, 7);

    Range c1 = Range.open(2, 3);
    Range c2 = Range.open(3, 4);
    Range c3 = Range.open(4, 5);

    Range d1 = Range.closed(2, 3);
    Range d2 = Range.closed(3, 4);
    Range d3 = Range.closed(4, 5);

    Assert.assertTrue(RangeUtil.remove(r1, anull).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a1).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a2).equals(Lists.newArrayList(Range.closed(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, a3).equals(Lists.newArrayList(Range.closed(5, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, a4).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, a5).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a6).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a7).equals(Lists.newArrayList(Range.closed(2, 4))));
    Assert.assertTrue(RangeUtil.remove(r1, a8).equals(Lists.newArrayList(Range.closed(2, 2))));
    Assert.assertTrue(RangeUtil.remove(r1, a9).equals(Lists.newArrayList()));

    Assert.assertTrue(RangeUtil.remove(r1, b1).equals(Lists.newArrayList(Range.openClosed(2, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b2).equals(Lists.newArrayList(Range.openClosed(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b3).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b4).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b5).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, b6).equals(Lists.newArrayList(Range.closedOpen(2, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b7).equals(Lists.newArrayList(Range.closedOpen(2, 4))));
    Assert.assertTrue(RangeUtil.remove(r1, b8).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b9).equals(Lists.newArrayList()));

    Assert.assertTrue(RangeUtil.remove(r1, c1).equals(Lists.newArrayList(Range.closed(2, 2), Range.closed(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, c2).equals(Lists.newArrayList(Range.closed(2, 3), Range.closed(4, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, c3).equals(Lists.newArrayList(Range.closed(2, 4), Range.closed(5, 5))));

    Assert.assertTrue(RangeUtil.remove(r1, d1).equals(Lists.newArrayList(Range.openClosed(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, d2).equals(Lists.newArrayList(Range.closedOpen(2, 3), Range.openClosed(4, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, d3).equals(Lists.newArrayList(Range.closedOpen(2, 4))));

}
 
Example 8
Source File: RangeTest.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpen() {

    Range anull = Range.closedOpen(0, 0);

    Range r1 = Range.open(2, 5);

    Range a1 = Range.open(1, 2);
    Range a2 = Range.open(1, 3);
    Range a3 = Range.open(1, 5);
    Range a4 = Range.open(1, 6);
    Range a5 = Range.open(6, 7);
    Range a6 = Range.open(5, 7);
    Range a7 = Range.open(4, 7);
    Range a8 = Range.open(2, 7);
    Range a9 = Range.open(1, 7);

    Range b1 = Range.closed(1, 2);
    Range b2 = Range.closed(1, 3);
    Range b3 = Range.closed(1, 5);
    Range b4 = Range.closed(1, 6);
    Range b5 = Range.closed(6, 7);
    Range b6 = Range.closed(5, 7);
    Range b7 = Range.closed(4, 7);
    Range b8 = Range.closed(2, 7);
    Range b9 = Range.closed(1, 7);

    Range c1 = Range.open(2, 3);
    Range c2 = Range.open(3, 4);
    Range c3 = Range.open(4, 5);

    Range d1 = Range.closed(2, 3);
    Range d2 = Range.closed(3, 4);
    Range d3 = Range.closed(4, 5);

    Assert.assertTrue(RangeUtil.remove(r1, anull).equals(Lists.newArrayList(r1)));

    Assert.assertTrue(RangeUtil.remove(r1, a1).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a2).equals(Lists.newArrayList(Range.closedOpen(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, a3).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, a4).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, a5).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a6).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, a7).equals(Lists.newArrayList(Range.openClosed(2, 4))));
    Assert.assertTrue(RangeUtil.remove(r1, a8).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, a9).equals(Lists.newArrayList()));

    Assert.assertTrue(RangeUtil.remove(r1, b1).equals(Lists.newArrayList(Range.open(2, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b2).equals(Lists.newArrayList(Range.open(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b3).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b4).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b5).equals(Lists.newArrayList(r1)));
    Assert.assertTrue(RangeUtil.remove(r1, b6).equals(Lists.newArrayList(Range.open(2, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, b7).equals(Lists.newArrayList(Range.open(2, 4))));
    Assert.assertTrue(RangeUtil.remove(r1, b8).equals(Lists.newArrayList()));
    Assert.assertTrue(RangeUtil.remove(r1, b9).equals(Lists.newArrayList()));

    Assert.assertTrue(RangeUtil.remove(r1, c1).equals(Lists.newArrayList(Range.closedOpen(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, c2).equals(Lists.newArrayList(Range.openClosed(2, 3), Range.closedOpen(4, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, c3).equals(Lists.newArrayList(Range.openClosed(2, 4))));

    Assert.assertTrue(RangeUtil.remove(r1, d1).equals(Lists.newArrayList(Range.open(3, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, d2).equals(Lists.newArrayList(Range.open(2, 3), Range.open(4, 5))));
    Assert.assertTrue(RangeUtil.remove(r1, d3).equals(Lists.newArrayList(Range.open(2, 4))));

}
 
Example 9
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public Range<Double> toRange(Interval interval){
	Double leftMargin = NumberUtil.asDouble(interval.getLeftMargin());
	Double rightMargin = NumberUtil.asDouble(interval.getRightMargin());

	// "The leftMargin and rightMargin attributes are optional, but at least one value must be defined"
	if(leftMargin == null && rightMargin == null){
		throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_LEFTMARGIN);
	} // End if

	if(leftMargin != null && rightMargin != null && NumberUtil.compare(leftMargin, rightMargin) > 0){
		throw new InvalidElementException(interval);
	}

	Interval.Closure closure = interval.getClosure();
	if(closure == null){
		throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_CLOSURE);
	}

	switch(closure){
		case OPEN_OPEN:
			{
				if(leftMargin == null){
					return Range.lessThan(rightMargin);
				} else

				if(rightMargin == null){
					return Range.greaterThan(leftMargin);
				}

				return Range.open(leftMargin, rightMargin);
			}
		case OPEN_CLOSED:
			{
				if(leftMargin == null){
					return Range.atMost(rightMargin);
				} else

				if(rightMargin == null){
					return Range.greaterThan(leftMargin);
				}

				return Range.openClosed(leftMargin, rightMargin);
			}
		case CLOSED_OPEN:
			{
				if(leftMargin == null){
					return Range.lessThan(rightMargin);
				} else

				if(rightMargin == null){
					return Range.atLeast(leftMargin);
				}

				return Range.closedOpen(leftMargin, rightMargin);
			}
		case CLOSED_CLOSED:
			{
				if(leftMargin == null){
					return Range.atMost(rightMargin);
				} else

				if(rightMargin == null){
					return Range.atLeast(leftMargin);
				}

				return Range.closed(leftMargin, rightMargin);
			}
		default:
			throw new UnsupportedAttributeException(interval, closure);
	}
}