-
Notifications
You must be signed in to change notification settings - Fork 7
/
DecimalSettings.kt
172 lines (146 loc) · 5.72 KB
/
DecimalSettings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.fwdekker.randomness.decimal
import com.fwdekker.randomness.Scheme
import com.fwdekker.randomness.Scheme.Companion.DEFAULT_NAME
import com.fwdekker.randomness.Settings
import com.fwdekker.randomness.SettingsConfigurable
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.util.xmlb.XmlSerializerUtil
import com.intellij.util.xmlb.annotations.MapAnnotation
/**
* The user-configurable collection of schemes applicable to generating decimals.
*
* @property schemes the schemes that the user can choose from
* @property currentSchemeName the scheme that is currently active
*
* @see DecimalSettingsAction
* @see DecimalSettingsConfigurable
*/
@State(name = "DecimalSettings", storages = [Storage("\$APP_CONFIG\$/randomness.xml")])
data class DecimalSettings(
@MapAnnotation(sortBeforeSave = false)
override var schemes: MutableList<DecimalScheme> = DEFAULT_SCHEMES,
override var currentSchemeName: String = DEFAULT_CURRENT_SCHEME_NAME
) : Settings<DecimalSettings, DecimalScheme> {
override fun deepCopy() = copy(schemes = schemes.map { it.copy() }.toMutableList())
override fun getState() = this
override fun loadState(state: DecimalSettings) = XmlSerializerUtil.copyBean(state, this)
/**
* Holds constants.
*/
companion object {
/**
* The default value of the [schemes][schemes] field.
*/
val DEFAULT_SCHEMES: MutableList<DecimalScheme>
get() = mutableListOf(DecimalScheme())
/**
* The default value of the [currentSchemeName][currentSchemeName] field.
*/
const val DEFAULT_CURRENT_SCHEME_NAME = DEFAULT_NAME
/**
* The persistent `DecimalSettings` instance.
*/
val default: DecimalSettings
get() = service()
}
}
/**
* Contains settings for generating random decimals.
*
* @property myName The name of the scheme.
* @property minValue The minimum value to be generated, inclusive.
* @property maxValue The maximum value to be generated, inclusive.
* @property decimalCount The number of decimals to display.
* @property showTrailingZeroes Whether to include trailing zeroes in the decimals.
* @property groupingSeparator The character that should separate groups.
* @property decimalSeparator The character that should separate decimals.
* @property prefix The string to prepend to the generated value.
* @property suffix The string to append to the generated value.
*
* @see DecimalInsertAction
* @see DecimalSettings
*/
// TODO Turn separator properties into char properties once supported by the settings serializer
data class DecimalScheme(
override var myName: String = DEFAULT_NAME,
var minValue: Double = DEFAULT_MIN_VALUE,
var maxValue: Double = DEFAULT_MAX_VALUE,
var decimalCount: Int = DEFAULT_DECIMAL_COUNT,
var showTrailingZeroes: Boolean = DEFAULT_SHOW_TRAILING_ZEROES,
var groupingSeparator: String = DEFAULT_GROUPING_SEPARATOR,
var decimalSeparator: String = DEFAULT_DECIMAL_SEPARATOR,
var prefix: String = DEFAULT_PREFIX,
var suffix: String = DEFAULT_SUFFIX
) : Scheme<DecimalScheme> {
override fun copyFrom(other: DecimalScheme) = XmlSerializerUtil.copyBean(other, this)
override fun copyAs(name: String) = this.copy(myName = name)
/**
* Sets the grouping separator safely to ensure that exactly one character is set.
*
* @param groupingSeparator the possibly-unsafe grouping separator string
*/
fun safeSetGroupingSeparator(groupingSeparator: String?) =
if (groupingSeparator == null || groupingSeparator.isEmpty())
this.groupingSeparator = DEFAULT_GROUPING_SEPARATOR
else
this.groupingSeparator = groupingSeparator.substring(0, 1)
/**
* Sets the decimal separator safely to ensure that exactly one character is set.
*
* @param decimalSeparator the possibly-unsafe decimal separator string
*/
fun safeSetDecimalSeparator(decimalSeparator: String?) =
if (decimalSeparator == null || decimalSeparator.isEmpty())
this.decimalSeparator = DEFAULT_DECIMAL_SEPARATOR
else
this.decimalSeparator = decimalSeparator.substring(0, 1)
/**
* Holds constants.
*/
companion object {
/**
* The default value of the [minValue][minValue] field.
*/
const val DEFAULT_MIN_VALUE = 0.0
/**
* The default value of the [maxValue][maxValue] field.
*/
const val DEFAULT_MAX_VALUE = 1_000.0
/**
* The default value of the [decimalCount][decimalCount] field.
*/
const val DEFAULT_DECIMAL_COUNT = 2
/**
* The default value of the [showTrailingZeroes][showTrailingZeroes] field.
*/
const val DEFAULT_SHOW_TRAILING_ZEROES = true
/**
* The default value of the [groupingSeparator][groupingSeparator] field.
*/
const val DEFAULT_GROUPING_SEPARATOR = ""
/**
* The default value of the [decimalSeparator][decimalSeparator] field.
*/
const val DEFAULT_DECIMAL_SEPARATOR = "."
/**
* The default value of the [prefix][prefix] field.
*/
const val DEFAULT_PREFIX = ""
/**
* The default value of the [suffix][suffix] field.
*/
const val DEFAULT_SUFFIX = ""
}
}
/**
* The configurable for decimal settings.
*
* @see DecimalSettingsAction
*/
class DecimalSettingsConfigurable(
override val component: DecimalSettingsComponent = DecimalSettingsComponent()
) : SettingsConfigurable<DecimalSettings, DecimalScheme>() {
override fun getDisplayName() = "Decimals"
}