Parcourir la source

Reduce complexity of system time JNI implementation

Patrick-Christopher Mattulat il y a 1 an
Parent
commit
41a19653c8

+ 1 - 0
CMakeLists.txt

@@ -284,6 +284,7 @@ else (WIN32)
 endif ()
 
 set(SOURCE_FILES_TIME_JNI
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/common/jni/JniDateParameterMapper.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/system-time/jni/com_lynarstudios_ls_std_time_systemtime_SystemTimeJni.cpp)
 
 ####################################################################################################################

+ 15 - 0
include/ls-std/ls-std-time-jni.hpp

@@ -0,0 +1,15 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-04-09
+* Changed:         2023-04-08
+*
+* */
+
+#ifndef LS_STD_LS_STD_TIME_JNI_HPP
+#define LS_STD_LS_STD_TIME_JNI_HPP
+
+#include <ls-std/time/common/jni/JniDateParameterMapper.hpp>
+
+#endif

+ 30 - 0
include/ls-std/time/common/jni/JniDateParameterMapper.hpp

@@ -0,0 +1,30 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-04-09
+* Changed:         2023-04-09
+*
+* */
+
+#ifndef LS_STD_JNI_DATE_PARAMETER_MAPPER_HPP
+#define LS_STD_JNI_DATE_PARAMETER_MAPPER_HPP
+
+#include <ls-std/core/jni/JniClass.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <ls-std/time/common/DateParameter.hpp>
+
+namespace ls::std::time::experimental
+{
+  class LS_STD_DYNAMIC_GOAL JniDateParameterMapper
+  {
+    public:
+
+      JniDateParameterMapper();
+      ~JniDateParameterMapper();
+
+      [[nodiscard]] static ls::std::time::DateParameter toDateParameter(const ls::std::core::JniClass &_javaClass);
+  };
+}
+
+#endif

+ 35 - 0
source/ls-std/time/common/jni/JniDateParameterMapper.cpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-04-09
+* Changed:         2023-04-09
+*
+* */
+
+#include <ls-std/core/jni/JniClass.hpp>
+#include <ls-std/time/common/DateParameter.hpp>
+#include <ls-std/time/common/jni/JniDateParameterMapper.hpp>
+
+using ls::std::core::JniClass;
+using ls::std::time::DateParameter;
+using ls::std::time::experimental::JniDateParameterMapper;
+
+JniDateParameterMapper::JniDateParameterMapper() = default;
+
+JniDateParameterMapper::~JniDateParameterMapper() = default;
+
+DateParameter JniDateParameterMapper::toDateParameter(const JniClass &_javaClass)
+{
+  JniClass javaClass = _javaClass;
+  DateParameter dateParameter{};
+
+  dateParameter.setYear(javaClass.callMethod("getYear").getIntegerValue());
+  dateParameter.setMonth(javaClass.callMethod("getMonth").getByteValue());
+  dateParameter.setDay(javaClass.callMethod("getDay").getByteValue());
+  dateParameter.setHour(javaClass.callMethod("getHour").getByteValue());
+  dateParameter.setMinute(javaClass.callMethod("getMinute").getByteValue());
+  dateParameter.setSecond(javaClass.callMethod("getSecond").getByteValue());
+
+  return dateParameter;
+}

+ 21 - 23
source/ls-std/time/system-time/jni/com_lynarstudios_ls_std_time_systemtime_SystemTimeJni.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-03-16
-* Changed:         2023-04-08
+* Changed:         2023-04-09
 *
 * */
 
@@ -11,6 +11,7 @@
 #include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
 #include <ls-std/core/jni/JniClass.hpp>
 #include <ls-std/core/jni/JniClassParameter.hpp>
+#include <ls-std/time/common/jni/JniDateParameterMapper.hpp>
 #include <ls-std/time/system-time/SystemTime.hpp>
 #include <ls-std/time/system-time/jni/com_lynarstudios_ls_std_time_systemtime_SystemTimeJni.h>
 #include <memory>
@@ -20,6 +21,7 @@ using ls::std::core::JniClassParameter;
 using ls::std::core::NullPointerEvaluator;
 using ls::std::time::DateParameter;
 using ls::std::time::SystemTime;
+using ls::std::time::experimental::JniDateParameterMapper;
 using ls::std::time::type::Day;
 using ls::std::time::type::Hour;
 using ls::std::time::type::Minute;
@@ -40,32 +42,28 @@ JNIEXPORT jboolean JNICALL Java_com_lynarstudios_ls_std_time_systemtime_SystemTi
   parameter->setJavaObject(_dateParameter);
 
   JniClass javaClass{parameter, "com/lynarstudios/ls/std/time/systemtime/DateParameter"};
-  cout << (javaClass.load() ? "class loaded..." : "could not load class...") << endl;
+  javaClass.load();
 
-  cout << (javaClass.loadMethod("getYear", "()I") ? "method loaded..." : "could not load method...") << endl;
-  cout << (javaClass.loadMethod("getMonth", "()B") ? "method loaded..." : "could not load method...") << endl;
-  cout << (javaClass.loadMethod("getDay", "()B") ? "method loaded..." : "could not load method...") << endl;
-  cout << (javaClass.loadMethod("getHour", "()B") ? "method loaded..." : "could not load method...") << endl;
-  cout << (javaClass.loadMethod("getMinute", "()B") ? "method loaded..." : "could not load method...") << endl;
-  cout << (javaClass.loadMethod("getSecond", "()B") ? "method loaded..." : "could not load method...") << endl;
-
-  Year year = javaClass.callMethod("getYear").getIntegerValue();
-  Month month = javaClass.callMethod("getMonth").getByteValue();
-  Day day = javaClass.callMethod("getDay").getByteValue();
-  Hour hour = javaClass.callMethod("getHour").getByteValue();
-  Minute minute = javaClass.callMethod("getMinute").getByteValue();
-  Second second = javaClass.callMethod("getSecond").getByteValue();
+  javaClass.loadMethod("getYear", "()I");
+  javaClass.loadMethod("getMonth", "()B");
+  javaClass.loadMethod("getDay", "()B");
+  javaClass.loadMethod("getHour", "()B");
+  javaClass.loadMethod("getMinute", "()B");
+  javaClass.loadMethod("getSecond", "()B");
 
   // map
 
-  DateParameter dateParameter{};
+  DateParameter dateParameter = JniDateParameterMapper::toDateParameter(javaClass);
+  bool success = SystemTime{}.set(dateParameter);
 
-  dateParameter.setYear(year);
-  dateParameter.setMonth(month);
-  dateParameter.setDay(day);
-  dateParameter.setHour(hour);
-  dateParameter.setMinute(minute);
-  dateParameter.setSecond(second);
+  if (success)
+  {
+    cout << R"lit(system time successfully set to ")lit" << dateParameter.getYear() << "-" << (int) dateParameter.getMonth() << "-" << (int) dateParameter.getDay() << " " << (int) dateParameter.getHour() << ":" << (int) dateParameter.getMinute() << ":" << (int) dateParameter.getSecond() << R"lit(" ...)lit" << endl;
+  }
+  else
+  {
+    cout << "could not set system time ..." << endl;
+  }
 
-  return SystemTime{}.set(dateParameter);
+  return success;
 }