Home | History | Annotate | Download | only in preference
      1 /*
      2  * Copyright 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")
     18 
     19 package androidx.preference
     20 
     21 /**
     22  * Returns the preference with `key`.
     23  *
     24  * @throws NullPointerException if no preference is found with that key.
     25  */
     26 inline operator fun PreferenceGroup.get(key: CharSequence): Preference = findPreference(key)
     27 
     28 /**
     29  * Returns the preference at `index`.
     30  *
     31  * @throws IndexOutOfBoundsException if index is less than 0 or greater than or equal to the count.
     32  */
     33 operator fun PreferenceGroup.get(index: Int): Preference = getPreference(index)
     34         ?: throw IndexOutOfBoundsException("Index: $index, Size: $preferenceCount")
     35 
     36 /** Returns `true` if `preference` is found in this preference group. */
     37 operator fun PreferenceGroup.contains(preference: Preference): Boolean {
     38     for (index in 0 until preferenceCount) {
     39         if (getPreference(index) == preference) {
     40             return true
     41         }
     42     }
     43     return false
     44 }
     45 
     46 /** Adds `preference` to this preference group. */
     47 inline operator fun PreferenceGroup.plusAssign(preference: Preference) {
     48     addPreference(preference)
     49 }
     50 
     51 /** Removes `preference` from this preference group. */
     52 inline operator fun PreferenceGroup.minusAssign(preference: Preference) {
     53     removePreference(preference)
     54 }
     55 
     56 /** Returns the number of preferences in this preference group. */
     57 inline val PreferenceGroup.size: Int get() = preferenceCount
     58 
     59 /** Returns true if this preference group contains no preferences. */
     60 inline fun PreferenceGroup.isEmpty(): Boolean = size == 0
     61 
     62 /** Returns true if this preference group contains one or more preferences. */
     63 inline fun PreferenceGroup.isNotEmpty(): Boolean = size != 0
     64 
     65 /** Performs the given action on each preference in this preference group. */
     66 inline fun PreferenceGroup.forEach(action: (preference: Preference) -> Unit) {
     67     for (index in 0 until preferenceCount) {
     68         action(get(index))
     69     }
     70 }
     71 
     72 /** Performs the given action on each preference in this preference group, providing its sequential index. */
     73 inline fun PreferenceGroup.forEachIndexed(action: (index: Int, preference: Preference) -> Unit) {
     74     for (index in 0 until preferenceCount) {
     75         action(index, get(index))
     76     }
     77 }
     78 
     79 /** Returns a [MutableIterator] over the preferences in this preference group. */
     80 operator fun PreferenceGroup.iterator() = object : MutableIterator<Preference> {
     81     private var index = 0
     82     override fun hasNext() = index < size
     83     override fun next() = getPreference(index++) ?: throw IndexOutOfBoundsException()
     84     override fun remove() {
     85         removePreference(getPreference(--index))
     86     }
     87 }
     88 
     89 /** Returns a [Sequence] over the preferences in this preference group. */
     90 val PreferenceGroup.children: Sequence<Preference>
     91     get() = object : Sequence<Preference> {
     92         override fun iterator() = this@children.iterator()
     93     }
     94