-
Notifications
You must be signed in to change notification settings - Fork 7
/
Dictionary.kt
259 lines (217 loc) · 7.52 KB
/
Dictionary.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.fwdekker.randomness.word
import com.fwdekker.randomness.Cache
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
/**
* Thrown when a [Dictionary] is found to be invalid and cannot be used in the intended way.
*
* @param message the detail message
* @param cause the cause
*/
class InvalidDictionaryException(message: String? = null, cause: Throwable? = null) : Exception(message, cause)
/**
* A collection of words that may become inaccessible at any moment in time.
*/
interface Dictionary {
/**
* The words in the dictionary.
*/
@get:Throws(InvalidDictionaryException::class)
val words: Set<String>
/**
* Throws an [InvalidDictionaryException] iff this dictionary is currently invalid.
*
* @throws InvalidDictionaryException if this dictionary is currently invalid
*/
@Throws(InvalidDictionaryException::class)
fun validate()
/**
* Returns `true` iff [validate] does not throw an exception.
*
* @return `true` iff [validate] does not throw an exception
*/
@Suppress("SwallowedException") // That's exactly how this function should work
fun isValid(): Boolean =
try {
validate()
true
} catch (e: InvalidDictionaryException) {
false
}
}
/**
* A `Dictionary` of which the underlying file is a resource in the JAR.
*
* @property filename the path to the resource file
*/
class BundledDictionary private constructor(val filename: String) : Dictionary {
@get:Throws(InvalidDictionaryException::class)
override val words: Set<String> by lazy {
validate()
try {
getStream().bufferedReader()
.readLines()
.filterNot { it.isBlank() }
.filterNot { it.startsWith('#') }
.toSet()
} catch (e: IOException) {
throw InvalidDictionaryException(e.message, e)
}
}
@Throws(InvalidDictionaryException::class)
override fun validate() {
try {
getStream()
} catch (e: IOException) {
throw InvalidDictionaryException(e.message, e)
}
}
/**
* Returns `true` iff this dictionary's filename equals [other]'s filename.
*
* @param other an object
* @return `true` iff this dictionary's filename equals [other]'s filename
*/
override fun equals(other: Any?) = other is BundledDictionary && this.filename == other.filename
/**
* Returns the hash code of the filename.
*
* @return the hash code of the filename
*/
override fun hashCode() = filename.hashCode()
/**
* Returns a human-readable string of the dictionary's filename.
*
* @return a human-readable string of the dictionary's filename
*/
override fun toString() = filename
/**
* Returns a stream to the resource file.
*
* @return a stream to the resource file
* @throws IOException if the resource file could not be opened
*/
@Throws(IOException::class)
private fun getStream() =
BundledDictionary::class.java.classLoader.getResource(filename)?.openStream()
?: throw FileNotFoundException("File not found.")
/**
* Holds static elements.
*/
companion object {
/**
* The location of a simple English dictionary.
*/
const val SIMPLE_DICTIONARY = "english.dic"
/**
* The cache of bundled dictionaries, used to improve word generation times.
*/
val cache = Cache<String, BundledDictionary> { BundledDictionary(it) }
}
}
/**
* A `Dictionary` of which the underlying file is a regular file.
*
* @property filename the path to the file
*/
class UserDictionary private constructor(val filename: String) : Dictionary {
@get:Throws(InvalidDictionaryException::class)
override val words: Set<String> by lazy {
validate()
File(filename).readLines()
.filterNot { it.isBlank() }
.filterNot { it.startsWith('#') }
.toSet()
}
@Throws(InvalidDictionaryException::class)
override fun validate() {
val file = File(filename)
if (!file.exists()) throw InvalidDictionaryException("File not found.")
if (!file.canRead()) throw InvalidDictionaryException("File unreadable.")
try {
file.inputStream()
} catch (e: IOException) {
throw InvalidDictionaryException(e.message, e)
}
}
/**
* Returns a human-readable string of the dictionary's filename.
*
* @return a human-readable string of the dictionary's filename
*/
override fun toString() = filename
/**
* Returns `true` iff this dictionary's filename equals [other]'s filename.
*
* @param other an object
* @return `true` iff this dictionary's filename equals [other]'s filename
*/
override fun equals(other: Any?) = other is UserDictionary && this.filename == other.filename
/**
* Returns the hash code of the filename.
*
* @return the hash code of the filename
*/
override fun hashCode() = filename.hashCode()
/**
* Holds static elements.
*/
companion object {
/**
* The cache of bundled dictionaries, used to improve word generation times.
*/
val cache = Cache<String, UserDictionary> { UserDictionary(it) }
}
}
/**
* References a dictionary by its properties.
*
* Using a reference, each access goes through the cache first, so that outdated instances of a dictionary (i.e. those
* flushed when clearing the cache) are not used anymore. This ensures that only the latest instance of that dictionary
* is used, which is important when a dictionary has to be used both before and after clearing a cache.
*
* @property isBundled True if this dictionary refers to a [BundledDictionary].
* @property filename The filename of the referred-to dictionary.
*/
data class DictionaryReference(val isBundled: Boolean, var filename: String) : Dictionary {
/**
* The dictionary that is referred to by this reference, as fetched from the cache.
*/
val referent: Dictionary
get() =
if (isBundled) BundledDictionary.cache.get(filename)
else UserDictionary.cache.get(filename)
@get:Throws(InvalidDictionaryException::class)
override val words: Set<String>
get() = referent.words
@Throws(InvalidDictionaryException::class)
override fun validate() = referent.validate()
/**
* Returns the string representation of the [referent].
*
* @return the string representation of the [referent]
*/
override fun toString() = referent.toString()
/**
* Holds static elements.
*/
companion object {
/**
* The error message that is displayed if an unknown dictionary implementation is used.
*/
const val DICTIONARY_CAST_EXCEPTION = "Unexpected dictionary implementation."
/**
* Returns a reference to the given dictionary.
*
* @param dictionary the dictionary to return a reference to
*/
@Suppress("FunctionMinLength") // Function name is clear enough because of class name
fun to(dictionary: Dictionary) =
when (dictionary) {
is BundledDictionary -> DictionaryReference(true, dictionary.filename)
is UserDictionary -> DictionaryReference(false, dictionary.filename)
else -> error(DICTIONARY_CAST_EXCEPTION)
}
}
}