-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb1cff0
commit 2c4fe8d
Showing
11 changed files
with
340 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2022 Yusuf Arfan Ismail (RealYusufIsmail) | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.realyusufismail.jconfig.classes | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import io.github.realyusufismail.jconfig.JConfig | ||
import io.github.realyusufismail.jconfig.JConfigObject | ||
|
||
class JConfigImpl(entries: List<JsonEntry>) : JConfig { | ||
|
||
private var mapEntries: Map<String, Any> = HashMap() | ||
private var jsonEntries: Set<JsonEntry> = HashSet() | ||
|
||
init { | ||
this.mapEntries = JsonEntry.toMap(entries) | ||
jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet() | ||
} | ||
|
||
override val entries: Set<JsonEntry> | ||
get() = jsonEntries | ||
|
||
override fun get(key: String): JConfigObject { | ||
// mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") | ||
|
||
val value = | ||
mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") | ||
|
||
if (value is JsonNode) { | ||
return when { | ||
value.isInt -> JConfigObjectImpl(value.asInt()) | ||
value.isLong -> JConfigObjectImpl(value.asLong()) | ||
value.isDouble -> JConfigObjectImpl(value.asDouble()) | ||
value.isBoolean -> JConfigObjectImpl(value.asBoolean()) | ||
value.isTextual -> JConfigObjectImpl(value.asText()) | ||
value.isArray -> JConfigObjectImpl(value.map { it.asText() }) | ||
value.isBigDecimal -> JConfigObjectImpl(value.decimalValue()) | ||
value.isBigInteger -> JConfigObjectImpl(value.bigIntegerValue()) | ||
value.isFloat -> JConfigObjectImpl(value.floatValue()) | ||
value.isShort -> JConfigObjectImpl(value.shortValue()) | ||
value.isBinary -> JConfigObjectImpl(value.binaryValue()) | ||
value.isObject -> | ||
JConfigObjectImpl( | ||
value.fields().asSequence().map { it.key to it.value.asText() }.toMap()) | ||
else -> throw IllegalArgumentException("Unknown type: ${value.javaClass}") | ||
} | ||
} else { | ||
throw IllegalArgumentException("Unknown type: ${value.javaClass}") | ||
} | ||
} | ||
|
||
override fun get(key: String, defaultValue: Any): JConfigObject { | ||
return try { | ||
get(key) | ||
} catch (e: NoSuchElementException) { | ||
JConfigObjectImpl(defaultValue) | ||
} | ||
} | ||
|
||
override fun contains(key: String): Boolean { | ||
return mapEntries.containsKey(key) | ||
} | ||
} |
Oops, something went wrong.