1 /*
2  * Copyright 2019 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 package androidx.compose.ui.text.intl
18 
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.ui.text.SpanStyle
21 import androidx.compose.ui.text.TextStyle
22 import androidx.compose.ui.util.fastMap
23 
24 /**
25  * Defines a list of [Locale] objects.
26  *
27  * @see TextStyle
28  * @see SpanStyle
29  */
30 @Immutable
31 class LocaleList(val localeList: List<Locale>) : Collection<Locale> {
32     companion object {
33 
34         /**
35          * An empty instance of [LocaleList]. Usually used to reference a lack of explicit [Locale]
36          * configuration.
37          */
38         val Empty = LocaleList(listOf())
39 
40         /** Returns Locale object which represents current locale */
41         val current: LocaleList
42             get() = platformLocaleDelegate.current
43     }
44 
45     /**
46      * Create a [LocaleList] object from comma separated language tags.
47      *
48      * @param languageTags A comma separated [IETF BCP47](https://tools.ietf.org/html/bcp47)
49      *   compliant language tag.
50      */
51     constructor(
52         languageTags: String
<lambda>null53     ) : this(languageTags.split(",").fastMap { it.trim() }.fastMap { Locale(it) })
54 
55     /** Creates a [LocaleList] object from a list of [Locale]s. */
56     constructor(vararg locales: Locale) : this(locales.toList())
57 
getnull58     operator fun get(i: Int) = localeList[i]
59 
60     // Collection overrides for easy iterations.
61     override val size: Int = localeList.size
62 
63     override operator fun contains(element: Locale): Boolean = localeList.contains(element)
64 
65     override fun containsAll(elements: Collection<Locale>): Boolean =
66         localeList.containsAll(elements)
67 
68     override fun isEmpty(): Boolean = localeList.isEmpty()
69 
70     override fun iterator(): Iterator<Locale> = localeList.iterator()
71 
72     override fun equals(other: Any?): Boolean {
73         if (this === other) return true
74         if (other !is LocaleList) return false
75         if (localeList != other.localeList) return false
76         return true
77     }
78 
hashCodenull79     override fun hashCode(): Int {
80         return localeList.hashCode()
81     }
82 
toStringnull83     override fun toString(): String {
84         return "LocaleList(localeList=$localeList)"
85     }
86 }
87