/* * Copyright (C) 2014 Stichting Mapcode Foundation (http://www.mapcode.com) * * Licensed 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 com.mapcode; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; /** * ---------------------------------------------------------------------------------------------- * Package private implementation class. For internal use within the Mapcode implementation only. * ---------------------------------------------------------------------------------------------- * * This class contains a class for dealing with ranges of comparable items. */ class Range> { @Nonnull private final T min; @Nonnull private final T max; Range(@Nonnull final T min, @Nonnull final T max) { this.min = min; this.max = max; } Range(@Nonnull final Range range) { this.min = range.min; this.max = range.max; } @Nonnull T getMin() { return min; } @Nonnull T getMax() { return max; } boolean contains(@Nonnull final T value) { return (value.compareTo(min) >= 0) && (value.compareTo(max) <= 0); } boolean containsRange(@Nonnull final Range range) { return this.min.compareTo(range.getMin()) <= 0 && this.max.compareTo(range.getMax()) >= 0; } boolean intersects(@Nonnull final Range range) { return range.contains(min) || range.contains(max) || contains(range.max) || contains(range.min); } @Nullable Range constrain(@Nonnull final Range constrainingRange) { @Nonnull T newMin = this.min; @Nonnull T newMax = this.max; if (newMin.compareTo(constrainingRange.getMin()) < 0) { newMin = constrainingRange.getMin(); } if (newMax.compareTo(constrainingRange.getMax()) > 0) { newMax = constrainingRange.getMax(); } if (newMax.compareTo(newMin) <= 0) { return null; } return new Range(newMin, newMax); } @Nullable ArrayList> constrain(@Nonnull final ArrayList> constrainingRanges) { final ArrayList> resultRanges = new ArrayList>(); for (final Range range : constrainingRanges) { final Range constrainedRange = constrain(range); if (constrainedRange != null) { resultRanges.add(constrainedRange); } } if (!resultRanges.isEmpty()) { return resultRanges; } return null; } }