• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 @file:Suppress("NOTHING_TO_INLINE") // Aliases to public API.
18 
19 package androidx.core.util
20 
21 import android.util.Range
22 import androidx.annotation.RequiresApi
23 
24 /**
25  * Creates a range from this [Comparable] value to [that].
26  *
27  * @throws IllegalArgumentException if this value is comparatively smaller than [that].
28  */
29 @RequiresApi(21)
rangeTonull30 inline infix fun <T : Comparable<T>> T.rangeTo(that: T): Range<T> = Range(this, that)
31 
32 /** Return the smallest range that includes this and [value]. */
33 @RequiresApi(21)
34 inline operator fun <T : Comparable<T>> Range<T>.plus(value: T): Range<T> = extend(value)
35 
36 /** Return the smallest range that includes this and [other]. */
37 @RequiresApi(21)
38 inline operator fun <T : Comparable<T>> Range<T>.plus(other: Range<T>): Range<T> = extend(other)
39 
40 /**
41  * Return the intersection of this range and [other].
42  *
43  * @throws IllegalArgumentException if this is disjoint from [other].
44  */
45 @RequiresApi(21)
46 inline infix fun <T : Comparable<T>> Range<T>.and(other: Range<T>): Range<T> = intersect(other)
47 
48 /** Returns this [Range] as a [ClosedRange]. */
49 @RequiresApi(21)
50 fun <T : Comparable<T>> Range<T>.toClosedRange(): ClosedRange<T> = object : ClosedRange<T> {
51     override val endInclusive get() = upper
52     override val start get() = lower
53 }
54 
55 /** Returns this [ClosedRange] as a [Range]. */
56 @RequiresApi(21)
toRangenull57 fun <T : Comparable<T>> ClosedRange<T>.toRange(): Range<T> = Range(start, endInclusive)
58