Skip to content

Commit

Permalink
[Java] Convert FutureResult and RealResult to kotlin (#27830)
Browse files Browse the repository at this point in the history
* Convert FutureResult and RealResult to kotlin

* Restyled by gn

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Dec 4, 2023
1 parent 6a29b96 commit 4423033
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 107 deletions.
20 changes: 4 additions & 16 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,12 @@ import("//build_overrides/chip.gni")
import("${chip_root}/build/chip/java/rules.gni")
import("${chip_root}/build/chip/tools.gni")

java_library("java") {
output_name = "JavaMatterController.jar"
deps = [
"${chip_root}/src/controller/java",
"${chip_root}/third_party/java_deps:annotation",
]

sources = [
"java/src/com/matter/controller/commands/common/FutureResult.java",
"java/src/com/matter/controller/commands/common/RealResult.java",
]

javac_flags = [ "-Xlint:deprecation" ]
}

kotlin_binary("java-matter-controller") {
output_name = "java-matter-controller"
deps = [
":java",
"${chip_root}/src/controller/java",
"${chip_root}/src/controller/java:onboarding_payload",
"${chip_root}/third_party/java_deps:annotation",
"${chip_root}/third_party/java_deps:kotlin-stdlib",
]

Expand All @@ -48,8 +34,10 @@ kotlin_binary("java-matter-controller") {
"java/src/com/matter/controller/commands/common/Command.kt",
"java/src/com/matter/controller/commands/common/CommandManager.kt",
"java/src/com/matter/controller/commands/common/CredentialsIssuer.kt",
"java/src/com/matter/controller/commands/common/FutureResult.kt",
"java/src/com/matter/controller/commands/common/IPAddress.kt",
"java/src/com/matter/controller/commands/common/MatterCommand.kt",
"java/src/com/matter/controller/commands/common/RealResult.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* 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
*
* 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 com.matter.controller.commands.common

import java.util.logging.Level
import java.util.logging.Logger

/**
* Implements the future result that encapculates the optional realResult, application would wait
* for realResult set by other thread wben receiving data from the other end. If the expected
* duration elapsed without receiving the expected realResult, the runtime exception would be
* raised.
*/
class RealResultException(message: String) : RuntimeException(message)

class FutureResult {
private var realResult: RealResult? = null
private var timeoutMs: Long = 0
private val logger = Logger.getLogger(FutureResult::class.java.name)
private val lock = Object()

fun setTimeoutMs(timeoutMs: Long) {
this.timeoutMs = timeoutMs
}

fun setRealResult(realResult: RealResult) {
synchronized(lock) {
if (this.realResult != null) {
throw RealResultException("Error, real result has been set!")
}
this.realResult = realResult
lock.notifyAll()
}
}

fun waitResult() {
val start = System.currentTimeMillis()
synchronized(lock) {
while (realResult == null) {
try {
if (System.currentTimeMillis() > start + timeoutMs) {
throw RealResultException("Timeout!")
}
lock.wait()
} catch (e: InterruptedException) {
logger.log(Level.INFO, "Wait Result failed with exception: " + e.message)
}
}
if (realResult?.getResult() == false) {
logger.log(Level.INFO, "Error: ${realResult?.getError()}")
throw RealResultException("Received failure test result")
}
}
}

fun clear() {
synchronized(lock) { realResult = null }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ abstract class MatterCommand(
protected abstract fun runCommand()

fun setSuccess() {
futureResult.setRealResult(RealResult.Success())
futureResult.setRealResult(RealResult.success())
}

fun setFailure(error: String?) {
futureResult.setRealResult(RealResult.Error(error))
futureResult.setRealResult(RealResult.error(error))
}

fun waitCompleteMs(timeoutMs: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
*/

package com.matter.controller.commands.common;
package com.matter.controller.commands.common

/**
* Implements a Result where an error string is associated with its failure state.
Expand All @@ -25,32 +25,26 @@
* contain either a `true` value for `Success` or a `false` value in which case the failure will
* also have an error string explaining the reason of the failure associated with it.
*/
public class RealResult {
private boolean result;
private String error;
class RealResult(private val result: Boolean, private val error: String?) {
constructor() : this(true, null)

public RealResult() {
this.result = true;
}

public RealResult(String error) {
this.result = false;
this.error = error;
}
constructor(error: String?) : this(false, error)

public static RealResult Success() {
return new RealResult();
}
companion object {
fun success(): RealResult {
return RealResult()
}

public static RealResult Error(String error) {
return new RealResult(error);
fun error(error: String?): RealResult {
return RealResult(error)
}
}

public boolean getResult() {
return result;
fun getResult(): Boolean {
return result
}

public String getError() {
return error;
fun getError(): String? {
return error
}
}

0 comments on commit 4423033

Please sign in to comment.