* Change null returns into exceptions.
Change-Id: I537cfed6aabc63f922a5a7100eaff26073b1bbf3
import static com.samsung.android.beyond.inference.Option.TAG;
public class InferenceHandler extends NativeInstance {
- public InferenceHandler(InferenceMode inferenceMode) {
+
+ InferenceHandler(InferenceMode inferenceMode) {
registerNativeInstance(nativeCreateInference(inferenceMode.toString()), (Long instance) -> destroy(instance));
}
public class InferenceModuleFactory {
public static InferenceHandler createHandler(InferenceMode inferenceMode) {
+ if (inferenceMode == null) {
+ throw new IllegalArgumentException("inferenceMode is null.");
+ }
+
return new InferenceHandler(inferenceMode);
}
public static Peer createPeerServer(Context context, String peerType) {
+ if (context == null || peerType == null) {
+ throw new IllegalArgumentException("Arguments are not correct.");
+ }
+
return new Peer(context, peerType, NodeType.SERVER);
}
public static Peer createPeerClient(Context context, String peerType) {
+ if (context == null || peerType == null) {
+ throw new IllegalArgumentException("Arguments are not correct.");
+ }
+
return new Peer(context, peerType, NodeType.CLIENT);
}
}
import com.samsung.android.beyond.NativeInstance;
import static com.samsung.android.beyond.inference.Option.TAG;
+import java.util.regex.Pattern;
+
public class Peer extends NativeInstance {
static {
initialize();
// TODO:
// List of runtimes
+
+ public Info() {}
+
+ public Info(String host, int port) {
+ this.host = host;
+ this.port = port;
+ }
}
- public Peer(Context context, String peerType, NodeType nodeType) {
+ Peer(Context context, String peerType, NodeType nodeType) {
switch (nodeType) {
case SERVER:
String[] serverConfiguration = new String[4];
registerNativeInstance(instance, (Long instance) -> destroy(instance));
}
+ public boolean setIPPort(String IP, int port) {
+ if (validateIPv4(IP) == false || port < 0) {
+ Log.e(TAG, "Arguments are invalid.");
+ return false;
+ }
+
+ return setInfo(instance, new Info(IP, port));
+ }
+
+ private boolean validateIPv4(String IP) {
+ if (IP == null) {
+ Log.e(TAG, "IP is null.");
+ }
+
+ String regexIPv4 = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$";
+ Pattern pattern = Pattern.compile(regexIPv4);
+ return pattern.matcher(IP).matches();
+ }
+
public boolean setInfo() {
Info info = new Info();
info.host = "0.0.0.0";
}
public boolean configure(char type, Object obj) {
+ if (Character.isDefined(type) || obj == null) {
+ Log.e(TAG, "Arguments are invalid.");
+ return false;
+ }
+
return configure(instance, type, obj);
}
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd 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.samsung.android.beyond;
+
+import android.content.Context;
+
+import com.samsung.android.beyond.inference.InferenceModuleFactory;
+import com.samsung.android.beyond.module.peer.NN.NNModule;
+
+import org.junit.Test;
+
+public class InferenceModuleFactoryUnitTest {
+
+ private static Context context;
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateHandlerWithNullArgument() {
+ InferenceModuleFactory.createHandler(null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreatePeerServerWithNullArguments() {
+ InferenceModuleFactory.createPeerServer(context, null);
+
+ InferenceModuleFactory.createPeerServer(null, NNModule.NAME);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreatePeerClientWithNullArgument() {
+ InferenceModuleFactory.createPeerClient(context, null);
+
+ InferenceModuleFactory.createPeerClient(null, NNModule.NAME);
+ }
+}
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd 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.samsung.android.beyond;
import static org.junit.Assert.assertArrayEquals;
ByteBuffer byteBuffer = (ByteBuffer) buffer;
}
- private class TestTensor extends Tensor {
+ private static class TestTensor extends Tensor {
TestTensor(Class classType, TensorInfo tensorInfo, ByteBuffer buffer) {
super(classType, tensorInfo);
super.setBuffer(buffer);