Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / jni / jni_vector.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "jni_vector.h"
22 #include "jni_string.h"
23 #include "simulator_utils_jni.h"
24
25 JniVector::JniVector(JNIEnv *env) : m_env(env) {}
26
27 jobject JniVector::toJava()
28 {
29     static jmethodID vectorCtor = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
30                                   "<init>", "()V");
31     return m_env->NewObject(gSimulatorClassRefs.vectorCls, vectorCtor);
32 }
33
34 jobject JniVector::toJava(std::vector<std::string> &cppVector)
35 {
36     static jmethodID addMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
37                                  "add", "(Ljava/lang/Object;)Z");
38
39     jobject vectorObject = toJava();
40     if (!vectorObject)
41         return nullptr;
42
43     for (size_t index = 0; index < cppVector.size(); index++)
44     {
45         jstring element = m_env->NewStringUTF(cppVector[index].c_str());
46         m_env->CallBooleanMethod(vectorObject, addMethod, element);
47         m_env->DeleteLocalRef(element);
48     }
49
50     return vectorObject;
51 }
52
53 int JniVector::getSize(jobject jVector)
54 {
55     static jmethodID sizeMethodID = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
56                                     "size", "()I");
57     return m_env->CallIntMethod(jVector, sizeMethodID);
58 }
59
60 void JniVector::addElementsCpp(jobject vector, int size, std::vector<int> &result)
61 {
62     static jmethodID getMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
63                                  "get", "(I)Ljava/lang/Object;");
64     static jmethodID intValueMethod = m_env->GetMethodID(gSimulatorClassRefs.integerCls,
65                                       "intValue", "()I");
66
67     for (int index = 0; index < size; index++)
68     {
69         jobject intObject = m_env->CallObjectMethod(vector, getMethod, index);
70         int value = m_env->CallIntMethod(intObject, intValueMethod);
71         result.push_back(value);
72     }
73 }
74
75 void JniVector::addElementsCpp(jobject vector, int size, std::vector<double> &result)
76 {
77     static jmethodID getMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
78                                  "get", "(I)Ljava/lang/Object;");
79     static jmethodID doubleValueMethod = m_env->GetMethodID(gSimulatorClassRefs.doubleCls,
80                                          "doubleValue", "()D");
81
82     for (int index = 0; index < size; index++)
83     {
84         jobject doubleObject = m_env->CallObjectMethod(vector, getMethod, index);
85         double value = m_env->CallDoubleMethod(doubleObject, doubleValueMethod);
86         result.push_back(value);
87     }
88 }
89
90 void JniVector::addElementsCpp(jobject vector, int size, std::vector<std::string> &result)
91 {
92     static jmethodID getMethodID = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
93                                    "get", "(I)Ljava/lang/Object;");
94
95     for (int index = 0; index < size; index++)
96     {
97         jstring stringObject = (jstring) m_env->CallObjectMethod(vector, getMethodID, index);
98         JniString value(m_env, stringObject);
99         result.push_back(value.get());
100     }
101 }