-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #813 from SpineEventEngine/promote-closeable
Promote `Closeable` inteface from `io.spine.server`
- Loading branch information
Showing
6 changed files
with
182 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2024, TeamDev. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package io.spine.io | ||
|
||
/** | ||
* Base interface for objects that may hold resources that need to be released | ||
* at the end of the object lifecycle. | ||
* | ||
* A class will benefit from implementing *this* interface instead of | ||
* [AutoCloseable] if it needs to see if the instance is [open][isOpen] | ||
* prior to making other calls. | ||
* | ||
* @see isOpen | ||
* @see checkOpen | ||
*/ | ||
public interface Closeable : AutoCloseable { | ||
|
||
/** | ||
* Tells if the object is still open. | ||
* | ||
* Implementations must return `false` after [close] is invoked. | ||
*/ | ||
public val isOpen: Boolean | ||
|
||
/** | ||
* Performs the release of the resources held by this object. | ||
* | ||
* Overrides to remove the checked exception from the signature. | ||
* | ||
* Implementations <em>may</em> require that the object implementing this interface | ||
* invokes this method only once, and throw [IllegalStateException] for repeated invocation. | ||
* If this is the case, and you would like to avoid [pre-checking][isOpen] consider | ||
* using [closeIfOpen], which does the check itself. | ||
* | ||
* @see closeIfOpen | ||
*/ | ||
public override fun close() | ||
|
||
/** | ||
* Ensures that the object [isOpen]. | ||
* | ||
* @throws IllegalStateException otherwise | ||
*/ | ||
@Throws(IllegalStateException::class) | ||
public fun checkOpen() { | ||
check(isOpen) { "`$this` is already closed." } | ||
} | ||
|
||
/** | ||
* Performs the release of the resources held by this object only if it is still open. | ||
* | ||
* Otherwise, does nothing. | ||
* | ||
* @see close | ||
*/ | ||
public fun closeIfOpen() { | ||
if (isOpen) { | ||
close() | ||
} | ||
} | ||
} |
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,82 @@ | ||
/* | ||
* Copyright 2024, TeamDev. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.io | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
@DisplayName("`Closeable` interface should") | ||
internal class CloseableSpec { | ||
|
||
@Test | ||
fun `tell if the object is open`() { | ||
Window(true).isOpen shouldBe true | ||
Window(false).isOpen shouldBe false | ||
} | ||
|
||
@Test | ||
fun `close the object`() { | ||
with (Window(true)) { | ||
close() | ||
isOpen shouldBe false | ||
} | ||
} | ||
|
||
@Test | ||
fun `ensure that the object is open`() { | ||
assertThrows<IllegalStateException> { | ||
Window(false).checkOpen() | ||
} | ||
} | ||
|
||
@Test | ||
fun `close the object, if open`() { | ||
with(Window(true)) { | ||
isOpen shouldBe true | ||
closeIfOpen() | ||
isOpen shouldBe false | ||
} | ||
with(Window(false)) { | ||
isOpen shouldBe false | ||
assertDoesNotThrow { closeIfOpen() } | ||
isOpen shouldBe false | ||
} | ||
} | ||
} | ||
|
||
private class Window(private var state: Boolean) : Closeable { | ||
|
||
override val isOpen: Boolean | ||
get() = state | ||
|
||
override fun close() { | ||
state = false | ||
} | ||
} |
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