|
1 | | -/* |
2 | | - * Copyright 2020-2021 ONIXLabs |
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 io.onixlabs.corda.bnms.contract |
18 | | - |
19 | | -/** |
20 | | - * Only declared so that I can fit method signatures on a single line. |
21 | | - */ |
22 | | -private typealias Settings = Iterable<Setting<*>> |
23 | | - |
24 | | -/** |
25 | | - * Casts a [Setting] of an unknown value type to a [Setting] of type [T]. |
26 | | - * |
27 | | - * @param T The underlying value type of the cast [Setting]. |
28 | | - * @param type The value type to cast to. |
29 | | - * @return Returns a [Setting] of type [T]. |
30 | | - * @throws ClassCastException if the value type cannot be cast to type [T]. |
31 | | - */ |
32 | | -fun <T : Any> Setting<*>.cast(type: Class<T>): Setting<T> { |
33 | | - return Setting(property, type.cast(value)) |
34 | | -} |
35 | | - |
36 | | -/** |
37 | | - * Casts a [Setting] of an unknown value type to a [Setting] of type [T]. |
38 | | - * |
39 | | - * @param T The underlying value type of the cast [Setting]. |
40 | | - * @return Returns a [Setting] of type [T]. |
41 | | - * @throws ClassCastException if the value type cannot be cast to type [T]. |
42 | | - */ |
43 | | -inline fun <reified T : Any> Setting<*>.cast(): Setting<T> { |
44 | | - return cast(T::class.java) |
45 | | -} |
46 | | - |
47 | | -/** |
48 | | - * Casts an [Iterable] of [Setting] of an unknown value type to an [Iterable] of [Setting] of type [T]. |
49 | | - * |
50 | | - * @param T The underlying value type of the cast [Setting]. |
51 | | - * @param type The value type to cast to. |
52 | | - * @return Returns an [Iterable] of [Setting] of type [T]. |
53 | | - * @throws ClassCastException if the value type cannot be cast to type [T]. |
54 | | - */ |
55 | | -fun <T : Any> Settings.cast(type: Class<T>): List<Setting<T>> { |
56 | | - return map { it.cast(type) } |
57 | | -} |
58 | | - |
59 | | -/** |
60 | | - * Casts an [Iterable] of [Setting] of an unknown value type to an [Iterable] of [Setting] of type [T]. |
61 | | - * |
62 | | - * @param T The underlying value type of the cast [Setting]. |
63 | | - * @return Returns an [Iterable] of [Setting] of type [T]. |
64 | | - * @throws ClassCastException if the value type cannot be cast to type [T]. |
65 | | - */ |
66 | | -inline fun <reified T : Any> Settings.cast(): List<Setting<T>> { |
67 | | - return cast(T::class.java) |
68 | | -} |
69 | | - |
70 | | -/** |
71 | | - * Filters an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
72 | | - * |
73 | | - * @param V The underlying value type. |
74 | | - * @param S The underlying setting type. |
75 | | - * @param settingType The setting type. |
76 | | - * @param valueType The value type, or null if the setting type is derived, or the value type is unknown or a wildcard. |
77 | | - * @return Returns an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
78 | | - */ |
79 | | -fun <V, S : Setting<in V>> Settings.filterByType(settingType: Class<S>, valueType: Class<in V>? = null): List<S> { |
80 | | - val settingsFilteredBySettingType = filter { it.javaClass == settingType }.filterIsInstance(settingType) |
81 | | - return if (valueType != null) settingsFilteredBySettingType.filter { it.value?.javaClass == valueType } |
82 | | - else settingsFilteredBySettingType |
83 | | -} |
84 | | - |
85 | | -/** |
86 | | - * Filters an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
87 | | - * |
88 | | - * @param T The underlying setting type. |
89 | | - * @return Returns an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
90 | | - */ |
91 | | -inline fun <reified T : Setting<*>> Iterable<Setting<*>>.filterByType(): List<T> { |
92 | | - val typeInfo = object : SettingTypeInfo<T>() {} |
93 | | - return filterByType(typeInfo.settingType, typeInfo.valueType) |
94 | | -} |
95 | | - |
96 | | -/** |
97 | | - * Filters an [Iterable] of [Setting] by the specified property. |
98 | | - * |
99 | | - * @param T The underlying setting type. |
100 | | - * @param property The property to filter by. |
101 | | - * @param ignoreCase Determines whether to ignore the property case when filtering; for example when filtering by a normalized property. |
102 | | - * @return Returns an [Iterable] of [Setting] by the specified property. |
103 | | - */ |
104 | | -fun <T : Setting<*>> Iterable<T>.filterByProperty(property: String, ignoreCase: Boolean = false): List<T> { |
105 | | - return filter { it.property.equals(property, ignoreCase) } |
106 | | -} |
| 1 | +///* |
| 2 | +// * Copyright 2020-2021 ONIXLabs |
| 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 io.onixlabs.corda.bnms.contract |
| 18 | +// |
| 19 | +///** |
| 20 | +// * Only declared so that I can fit method signatures on a single line. |
| 21 | +// */ |
| 22 | +//private typealias Settings = Iterable<Setting<*>> |
| 23 | +// |
| 24 | +///** |
| 25 | +// * Casts a [Setting] of an unknown value type to a [Setting] of type [T]. |
| 26 | +// * |
| 27 | +// * @param T The underlying value type of the cast [Setting]. |
| 28 | +// * @param type The value type to cast to. |
| 29 | +// * @return Returns a [Setting] of type [T]. |
| 30 | +// * @throws ClassCastException if the value type cannot be cast to type [T]. |
| 31 | +// */ |
| 32 | +//fun <T : Any> Setting<*>.cast(type: Class<T>): Setting<T> { |
| 33 | +// return Setting(property, type.cast(value)) |
| 34 | +//} |
| 35 | +// |
| 36 | +///** |
| 37 | +// * Casts a [Setting] of an unknown value type to a [Setting] of type [T]. |
| 38 | +// * |
| 39 | +// * @param T The underlying value type of the cast [Setting]. |
| 40 | +// * @return Returns a [Setting] of type [T]. |
| 41 | +// * @throws ClassCastException if the value type cannot be cast to type [T]. |
| 42 | +// */ |
| 43 | +//inline fun <reified T : Any> Setting<*>.cast(): Setting<T> { |
| 44 | +// return cast(T::class.java) |
| 45 | +//} |
| 46 | +// |
| 47 | +///** |
| 48 | +// * Casts an [Iterable] of [Setting] of an unknown value type to an [Iterable] of [Setting] of type [T]. |
| 49 | +// * |
| 50 | +// * @param T The underlying value type of the cast [Setting]. |
| 51 | +// * @param type The value type to cast to. |
| 52 | +// * @return Returns an [Iterable] of [Setting] of type [T]. |
| 53 | +// * @throws ClassCastException if the value type cannot be cast to type [T]. |
| 54 | +// */ |
| 55 | +//fun <T : Any> Settings.cast(type: Class<T>): List<Setting<T>> { |
| 56 | +// return map { it.cast(type) } |
| 57 | +//} |
| 58 | +// |
| 59 | +///** |
| 60 | +// * Casts an [Iterable] of [Setting] of an unknown value type to an [Iterable] of [Setting] of type [T]. |
| 61 | +// * |
| 62 | +// * @param T The underlying value type of the cast [Setting]. |
| 63 | +// * @return Returns an [Iterable] of [Setting] of type [T]. |
| 64 | +// * @throws ClassCastException if the value type cannot be cast to type [T]. |
| 65 | +// */ |
| 66 | +//inline fun <reified T : Any> Settings.cast(): List<Setting<T>> { |
| 67 | +// return cast(T::class.java) |
| 68 | +//} |
| 69 | +// |
| 70 | +///** |
| 71 | +// * Filters an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
| 72 | +// * |
| 73 | +// * @param V The underlying value type. |
| 74 | +// * @param S The underlying setting type. |
| 75 | +// * @param settingType The setting type. |
| 76 | +// * @param valueType The value type, or null if the setting type is derived, or the value type is unknown or a wildcard. |
| 77 | +// * @return Returns an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
| 78 | +// */ |
| 79 | +//fun <V, S : Setting<in V>> Settings.filterByType(settingType: Class<S>, valueType: Class<in V>? = null): List<S> { |
| 80 | +// val settingsFilteredBySettingType = filter { it.javaClass == settingType }.filterIsInstance(settingType) |
| 81 | +// return if (valueType != null) settingsFilteredBySettingType.filter { it.value?.javaClass == valueType } |
| 82 | +// else settingsFilteredBySettingType |
| 83 | +//} |
| 84 | +// |
| 85 | +///** |
| 86 | +// * Filters an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
| 87 | +// * |
| 88 | +// * @param T The underlying setting type. |
| 89 | +// * @return Returns an [Iterable] of [Setting] by the specified setting type, and optionally the setting value type. |
| 90 | +// */ |
| 91 | +//inline fun <reified T : Setting<*>> Iterable<Setting<*>>.filterByType(): List<T> { |
| 92 | +// val typeInfo = object : SettingTypeInfo<T>() {} |
| 93 | +// return filterByType(typeInfo.settingType, typeInfo.valueType) |
| 94 | +//} |
| 95 | +// |
| 96 | +///** |
| 97 | +// * Filters an [Iterable] of [Setting] by the specified property. |
| 98 | +// * |
| 99 | +// * @param T The underlying setting type. |
| 100 | +// * @param property The property to filter by. |
| 101 | +// * @param ignoreCase Determines whether to ignore the property case when filtering; for example when filtering by a normalized property. |
| 102 | +// * @return Returns an [Iterable] of [Setting] by the specified property. |
| 103 | +// */ |
| 104 | +//fun <T : Setting<*>> Iterable<T>.filterByProperty(property: String, ignoreCase: Boolean = false): List<T> { |
| 105 | +// return filter { it.property.equals(property, ignoreCase) } |
| 106 | +//} |
0 commit comments