All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.sql.calcite.filtration.RangeSets Maven / Gradle / Ivy

There is a newer version: 0.12.3
Show newest version
/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.druid.sql.calcite.filtration;

import com.google.common.collect.BoundType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import org.joda.time.Interval;

import java.util.List;

public class RangeSets
{
  public static > RangeSet of(final Range range)
  {
    return unionRanges(ImmutableList.of(range));
  }

  /**
   * Unions a set of ranges, or returns null if the set is empty.
   */
  public static > RangeSet unionRanges(final Iterable> ranges)
  {
    RangeSet rangeSet = null;
    for (Range range : ranges) {
      if (rangeSet == null) {
        rangeSet = TreeRangeSet.create();
      }
      rangeSet.add(range);
    }
    return rangeSet;
  }

  /**
   * Unions a set of rangeSets, or returns null if the set is empty.
   */
  public static > RangeSet unionRangeSets(final Iterable> rangeSets)
  {
    final RangeSet rangeSet = TreeRangeSet.create();
    for (RangeSet set : rangeSets) {
      rangeSet.addAll(set);
    }
    return rangeSet;
  }

  /**
   * Intersects a set of ranges, or returns null if the set is empty.
   */
  public static > RangeSet intersectRanges(final Iterable> ranges)
  {
    RangeSet rangeSet = null;
    for (final Range range : ranges) {
      if (rangeSet == null) {
        rangeSet = TreeRangeSet.create();
        rangeSet.add(range);
      } else {
        rangeSet = TreeRangeSet.create(rangeSet.subRangeSet(range));
      }
    }
    return rangeSet;
  }

  /**
   * Intersects a set of rangeSets, or returns null if the set is empty.
   */
  public static > RangeSet intersectRangeSets(final Iterable> rangeSets)
  {
    RangeSet rangeSet = null;
    for (final RangeSet set : rangeSets) {
      if (rangeSet == null) {
        rangeSet = TreeRangeSet.create();
        rangeSet.addAll(set);
      } else {
        rangeSet.removeAll(set.complement());
      }
    }
    return rangeSet;
  }

  public static RangeSet fromIntervals(final Iterable intervals)
  {
    final RangeSet retVal = TreeRangeSet.create();
    for (Interval interval : intervals) {
      retVal.add(Range.closedOpen(interval.getStartMillis(), interval.getEndMillis()));
    }
    return retVal;
  }

  public static List toIntervals(final RangeSet rangeSet)
  {
    final List retVal = Lists.newArrayList();

    for (Range range : rangeSet.asRanges()) {
      final long start;
      final long end;

      if (range.hasLowerBound()) {
        final long millis = range.lowerEndpoint();
        start = millis + (range.lowerBoundType() == BoundType.OPEN ? 1 : 0);
      } else {
        start = Filtration.eternity().getStartMillis();
      }

      if (range.hasUpperBound()) {
        final long millis = range.upperEndpoint();
        end = millis + (range.upperBoundType() == BoundType.OPEN ? 0 : 1);
      } else {
        end = Filtration.eternity().getEndMillis();
      }

      retVal.add(new Interval(start, end));
    }

    return retVal;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy