/
GeoData.kt
435 lines (380 loc) · 11.5 KB
/
GeoData.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright 2012, Asamm Software, s. r. o.
*
* This file is part of LocusAPI.
*
* LocusAPI is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* LocusAPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public
* License along with LocusAPI. If not, see
* <http://www.gnu.org/licenses/lgpl.html/>.
*/
package locus.api.objects.geoData
import com.asamm.loggerV2.logE
import com.asamm.loggerV2.logW
import locus.api.objects.Storable
import locus.api.objects.extra.GeoDataExtra
import locus.api.objects.styles.GeoDataStyle
import locus.api.utils.DataReaderBigEndian
import locus.api.utils.DataWriterBigEndian
import java.io.IOException
import java.util.*
import kotlin.experimental.and
import kotlin.experimental.or
abstract class GeoData : Storable() {
/**
* Privacy settings for certain item.
*/
enum class Privacy {
/**
* Item is private only.
*/
PRIVATE,
/**
* Item is public thanks to public URL.
*/
SHARED_URL
}
// STORABLE PARAMETERS
/**
* Unique ID of this object.
*/
var id: Long = -1L
/**
* Name of object, have to be unique
*/
var name: String = ""
/**
* Time the data was created (ms).
*/
var timeCreated: Long = System.currentTimeMillis()
/**
* Time the data was updated for the last time (ms).
*/
var timeUpdated: Long = timeCreated
/**
* Extra data with parameters.
*/
var extraData: GeoDataExtra? = null
/**
* Style for normal state.
*/
var styleNormal: GeoDataStyle? = null
/**
* Style for highlight state.
*/
var styleHighlight: GeoDataStyle? = null
/**
* Define item privacy mode.
*/
var privacy = Privacy.PRIVATE
/**
* Define read-write mode of item. Protected item should not be exported from the Locus World
* or shared publicly to any place out of Locus scope.
*/
var protected = false
// TEMPORARY PARAMETERS
// not serialized!
/**
* Current item state.
*/
private var state: Byte = 0
/**
* Additional temporary storage object. Object is not serialized so transport over API does not work!
*/
private var tags: Hashtable<String, Any>? = null
//*************************************************
// STATE
//*************************************************
/**
* Item enabled/disabled state.
* "Enabled" state means item will be handled in lifecycle of drawing etc.
*/
var isEnabled: Boolean
get() = state and 1.toByte() == 1.toByte()
set(enabled) = setState(0, enabled)
/**
* Flag if item is currently visible (this means enabled and visible), so it should be drawn on the map.
*/
var isVisible: Boolean
get() = state and 1.toByte() == 1.toByte() // enabled
&& (state.toInt() shr 1).toByte() and 1.toByte() == 1.toByte() // visible
set(visible) = setState(1, visible)
/**
* Flag if item is currently selected (this means enabled, visible and selected).
*/
var isSelected: Boolean
get() = (state and 1.toByte() == 1.toByte() // enabled
&& state.toInt() shr 1 and 1 == 1 // visible
&& state.toInt() shr 2 and 1 == 1) // selected
set(selected) = setState(2, selected)
init {
isEnabled = true
isVisible = true
isSelected = false
}
/**
* Set certain state in byte.
*
* @param position position of state
* @param value value of state
*/
private fun setState(position: Int, value: Boolean) {
if (value) {
this.state = this.state or (1 shl position).toByte()
} else {
this.state = this.state and (1 shl position).inv().toByte()
}
}
//*************************************************
// EXTRA DATA - BASE
//*************************************************
@Throws(IOException::class)
protected fun readExtraData(dr: DataReaderBigEndian) {
if (dr.readBoolean()) {
extraData = GeoDataExtra().apply { read(dr) }
}
}
@Throws(IOException::class)
protected fun writeExtraData(dw: DataWriterBigEndian) {
extraData
?.takeIf { it.count > 0 }
?.let {
dw.writeBoolean(true)
dw.writeStorable(it)
} ?: run {
dw.writeBoolean(false)
}
}
/**
* Extra data serialized into byte array.
*
* @return serialized extra data or 'null' if data doesn't exists
*/
var extraDataRaw: ByteArray?
get() {
return try {
val dw = DataWriterBigEndian()
writeExtraData(dw)
dw.toByteArray()
} catch (e: IOException) {
logE(tag = TAG, ex = e) { "getExtraDataRaw()" }
null
}
}
set(data) = try {
readExtraData(DataReaderBigEndian(data))
} catch (e: Exception) {
logE(tag = TAG, ex = e) { "setExtraDataRaw($data)" }
extraData = null
}
//*************************************************
// EXTRA DATA - PARAMETERS
//*************************************************
/**
* Add parameter into attached [extraData] container.
* In case, container does not exists, it is created and also validated after "add" operation.
*
* @param addEvent event that fill [extraData] container
* @return `true` in case, parameter was correctly added
*/
internal fun addParameter(addEvent: GeoDataExtra.() -> Boolean): Boolean {
// check extra data and create if does not exists
val created = if (extraData == null) {
extraData = GeoDataExtra()
true
} else {
false
}
// add parameter
val added = addEvent(extraData!!)
// add parameter and return result
return if (added) {
true
} else {
if (created) {
extraData = null
}
false
}
}
/**
* Add single parameter defined by it's ID into `extraData` container.
*
* @param paramId ID of parameter
* @param param value of parameter
*/
fun addParameter(paramId: Int, param: Byte): Boolean {
return addParameter {
addParameter(paramId, param)
}
}
/**
* Add single parameter defined by it's ID into `extraData` container.
*
* @param paramId ID of parameter
* @param param value of parameter
*/
fun addParameter(paramId: Int, param: ByteArray): Boolean {
return addParameter {
addParameter(paramId, param)
}
}
/**
* Return parameter stored in extraData container.
*
* @param paramId ID of parameter
* @return loaded value (length() bigger then 0) or 'null' in case, parameter do not exists
*/
fun getParameter(paramId: Int): String? {
return extraData?.getParameter(paramId)
}
/**
* Add single parameter defined by it's ID into `extraData` container.
*
* @param paramId ID of parameter
* @param param text value of parameter
*/
fun addParameter(paramId: Int, param: String?): Boolean {
return addParameter {
addParameter(paramId, param)
}
}
/**
* Get parameter in raw original format.
*
* @param paramId ID of parameter
* @return parameter value in byte[] or `null` if no such parameter is stored
*/
fun getParameterRaw(paramId: Int): ByteArray? {
return extraData?.getParameterRaw(paramId)
}
/**
* Check if current object has parameter defined by it's ID.
*
* @param paramId ID of parameter
* @return `true` if non-empty parameter exists
*/
fun hasParameter(paramId: Int): Boolean {
return extraData?.hasParameter(paramId) ?: false
}
/**
* Remove parameter from storage.
*
* @param paramId ID of parameter
* @return parameter value
*/
fun removeParameter(paramId: Int): String? {
return extraData?.removeParameter(paramId)
}
//*************************************************
// STYLES
//*************************************************
@Throws(IOException::class)
protected fun readStyles(dr: DataReaderBigEndian) {
if (dr.readBoolean()) {
styleNormal = GeoDataStyle().apply { read(dr) }
}
if (dr.readBoolean()) {
styleHighlight = GeoDataStyle().apply { read(dr) }
}
}
@Throws(IOException::class)
protected fun writeStyles(dw: DataWriterBigEndian) {
styleNormal?.let {
dw.writeBoolean(true)
dw.writeStorable(it)
} ?: run {
dw.writeBoolean(false)
}
styleHighlight?.let {
dw.writeBoolean(true)
dw.writeStorable(it)
} ?: run {
dw.writeBoolean(false)
}
}
var styles: ByteArray?
get() = try {
DataWriterBigEndian().apply {
writeStyles(this)
}.toByteArray()
} catch (e: IOException) {
logE(tag = TAG, ex = e) { "getStylesRaw()" }
null
}
set(data) = try {
readStyles(DataReaderBigEndian(data))
} catch (e: Exception) {
logE(tag = TAG, ex = e) { "setExtraStyle($data)" }
extraData = null
}
//*************************************************
// TAGS
//*************************************************
/**
* Get tag attached to object, defined by "key".
*
* @param key key value that defined object
* @return required object otherwise 'null'
*/
@JvmOverloads
fun getTag(key: String = TAG_KEY_DEFAULT): Any? {
// check key
if (key.isEmpty()) {
logW(tag = TAG) { "getTag($key), invalid key" }
return null
}
// get tag
return tags?.get(key)
}
/**
* Set new tag/object under base key.
*/
fun setTag(value: Any?) {
setTag(key = TAG_KEY_DEFAULT, value = value)
}
/**
* Set new tag/object defined by key.
*
* @param key key that define object
* @param value object itself or 'null' if we wants to remove it
*/
fun setTag(key: String = TAG_KEY_DEFAULT, value: Any?) {
// check key
if (key.isEmpty()) {
logW(tag = TAG) { "setTag($key), invalid key" }
return
}
// set tag
if (value == null) {
tags?.remove(key)
} else {
if (tags == null) {
tags = Hashtable()
}
tags!![key] = value
}
}
/**
* Get all attached keys for tags.
*/
fun getTagsKeys(): Array<String> {
return tags?.keys?.toTypedArray()
?: emptyArray()
}
companion object {
// tag for logger
private const val TAG = "GeoData"
// default key for "empty" tag value
private const val TAG_KEY_DEFAULT = "default"
}
}