change Connection API Option to Nullable, to match Kotlin's style

This commit is contained in:
CodeSteak 2019-06-03 18:40:37 +02:00
parent fd749069a1
commit 3e5d2da366

View File

@ -5,9 +5,8 @@ import javafx.scene.image.Image
import org.hso.texturesyncclient.model.Sha256 import org.hso.texturesyncclient.model.Sha256
import org.hso.texturesyncclient.model.Texture import org.hso.texturesyncclient.model.Texture
import java.io.* import java.io.*
import java.lang.IllegalArgumentException import java.net.InetAddress
import java.lang.RuntimeException import java.net.Socket
import java.net.*
import java.util.* import java.util.*
@Suppress("MemberVisibilityCanBePrivate") @Suppress("MemberVisibilityCanBePrivate")
@ -96,7 +95,7 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
@Throws(IOException::class, ConnectionException::class) @Throws(IOException::class, ConnectionException::class)
@Synchronized @Synchronized
fun getTextureById(id: UUID): Optional<Texture> { fun getTextureById(id: UUID): Texture? {
val io = getStreams() val io = getStreams()
val obj = JsonObject() val obj = JsonObject()
@ -111,14 +110,12 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
when (val pkg = Package.read(io.first)) { when (val pkg = Package.read(io.first)) {
is JsonPackage -> { is JsonPackage -> {
return if (pkg.content.isJsonNull) { return if (pkg.content.isJsonNull) {
Optional.empty() null
} else { } else {
try { try {
Optional.of( Gson()
Gson() .fromJson<InternalTexture>(pkg.content, InternalTexture::class.java)
.fromJson<InternalTexture>(pkg.content, InternalTexture::class.java) .toTexture()
.toTexture()
)
} catch (e: JsonSyntaxException) { } catch (e: JsonSyntaxException) {
throw ConnectionInvalidJsonException() throw ConnectionInvalidJsonException()
} }
@ -133,7 +130,7 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
@Throws(IOException::class, ConnectionException::class) @Throws(IOException::class, ConnectionException::class)
@Synchronized @Synchronized
fun getTextureByName(name: String): Optional<Texture> { fun getTextureByName(name: String): Texture? {
val io = getStreams() val io = getStreams()
val obj = JsonObject() val obj = JsonObject()
@ -148,14 +145,12 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
when (val pkg = Package.read(io.first)) { when (val pkg = Package.read(io.first)) {
is JsonPackage -> { is JsonPackage -> {
return if (pkg.content.isJsonNull) { return if (pkg.content.isJsonNull) {
Optional.empty() null
} else { } else {
try { try {
Optional.of( Gson()
Gson() .fromJson<InternalTexture>(pkg.content, InternalTexture::class.java)
.fromJson<InternalTexture>(pkg.content, InternalTexture::class.java) .toTexture()
.toTexture()
)
} catch (e: JsonSyntaxException) { } catch (e: JsonSyntaxException) {
throw ConnectionInvalidJsonException() throw ConnectionInvalidJsonException()
} }