Commit c771d06e authored by Ryan Berkheimer's avatar Ryan Berkheimer

completed factoring condition utils methods into standalone class

parent 0caf80fe
......@@ -3,15 +3,14 @@
#include <jni.h>
/**
Constructor for the ConditionUtils object.
*/
ConditionUtils::ConditionUtils(JNIEnv *jvm, ListUtils *listUtils)
/* Default Constructor */
ConditionUtils::ConditionUtils(JNIEnv *jvm, TypeUtils *typeUtils, ListUtils *listUtils)
{
this->loadGlobalRefs(jvm, listUtils);
this->loadGlobalRefs(jvm, typeUtils, listUtils);
this->loadMethodIds();
}
/* Destructor */
ConditionUtils::~ConditionUtils()
{
try
......@@ -22,12 +21,226 @@ ConditionUtils::~ConditionUtils()
}
}
void ConditionUtils::loadGlobalRefs(JNIEnv *jvm, ListUtils *listUtils)
const char *ConditionUtils::getId(struct condition *condition)
{
jstring jConditionId = static_cast<jstring>(this->jvm->CallObjectMethod(condition->jcondition, this->getIdMethodId));
const char *conditionId = this->typeUtils->fromJavaString(jConditionId);
this->jvm->DeleteLocalRef(jConditionId);
return conditionId;
}
const char *ConditionUtils::getType(struct condition *condition)
{
jstring jConditionType = static_cast<jstring>(this->jvm->CallObjectMethod(condition->jcondition, this->getTypeMethodId));
const char *conditionType = this->typeUtils->fromJavaString(jConditionType);
this->jvm->DeleteLocalRef(jConditionType);
return conditionType;
}
const char *ConditionUtils::getOperator(struct condition *condition)
{
jstring jConditionOperator = static_cast<jstring>(this->jvm->CallObjectMethod(condition->jcondition, this->getOperatorMethodId));
const char *conditionOperator = this->typeUtils->fromJavaString(jConditionOperator);
this->jvm->DeleteLocalRef(jConditionOperator);
return conditionOperator;
}
bool ConditionUtils::isNull(struct condition *condition)
{
struct val *value = this->getVal(condition);
if (value->jvalue == NULL)
{
return true;
}
else
{
return false;
}
}
struct val *ConditionUtils::getVal(struct condition *condition)
{
jobject jConditionValue = this->jvm->CallObjectMethod(condition->jcondition, this->getValueMethodId);
struct val *value = (struct val *)malloc(sizeof(value) + sizeof(jConditionValue));
value->jvalue = jConditionValue;
return value;
}
int ConditionUtils::getIntVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
int intVal = (int)this->jvm->CallIntMethod(value->jvalue, this->typeUtils->getIntMethod());
return intVal;
}
long ConditionUtils::getLongVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
long longVal = (long)this->jvm->CallLongMethod(value->jvalue, this->typeUtils->getLongMethod());
return longVal;
}
float ConditionUtils::getFloatVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
float floatVal = (float)this->jvm->CallFloatMethod(value->jvalue, this->typeUtils->getFloatMethod());
return floatVal;
}
double ConditionUtils::getDoubleVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
double doubleVal = (double)this->jvm->CallDoubleMethod(value->jvalue, this->typeUtils->getDoubleMethod());
return doubleVal;
}
signed char ConditionUtils::getByteVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
jbyte jByte = this->jvm->CallByteMethod(value->jvalue, this->typeUtils->getByteMethod());
return (signed char)jByte;
}
const char *ConditionUtils::getStringVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
jstring jString = (jstring)value->jvalue;
const char *returnString = this->typeUtils->fromJavaString(jString);
jvm->DeleteLocalRef(jString);
return returnString;
}
bool ConditionUtils::getBoolVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
bool boolVal = (bool)jvm->CallBooleanMethod(value->jvalue, this->typeUtils->getBoolMethod());
return boolVal;
}
short ConditionUtils::getShortVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
short shortVal = (short)this->jvm->CallShortMethod(value->jvalue, this->typeUtils->getShortMethod());
return shortVal;
}
struct val_list *ConditionUtils::getListVal(struct condition *condition)
{
struct val *value = this->getVal(condition);
int itemCount = this->listUtils->getListLength(value->jvalue);
struct val_list *valueList = (struct val_list *)malloc(sizeof(struct val_list));
valueList->count = itemCount;
valueList->jlist = value->jvalue;
return valueList;
}
void ConditionUtils::setVal(struct condition *condition, struct val *value)
{
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, value->jvalue);
}
void ConditionUtils::setIntVal(struct condition *condition, int value)
{
jobject jIntVal = jvm->NewObject(this->typeUtils->getIntClass(), this->typeUtils->createIntMethod(), (jint)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jIntVal);
this->jvm->DeleteLocalRef(jIntVal);
}
void ConditionUtils::setLongVal(struct condition *condition, long value)
{
jobject jLongVal = jvm->NewObject(this->typeUtils->getLongClass(), this->typeUtils->createLongMethod(), (jlong)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jLongVal);
this->jvm->DeleteLocalRef(jLongVal);
}
void ConditionUtils::setFloatVal(struct condition *condition, float value)
{
jobject jFloatVal = jvm->NewObject(this->typeUtils->getFloatClass(), this->typeUtils->createFloatMethod(), (jfloat)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jFloatVal);
this->jvm->DeleteLocalRef(jFloatVal);
}
void ConditionUtils::setDoubleVal(struct condition *condition, double value)
{
jobject jDoubleVal = jvm->NewObject(this->typeUtils->getDoubleClass(), this->typeUtils->createDoubleMethod(), (jdouble)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jDoubleVal);
this->jvm->DeleteLocalRef(jDoubleVal);
}
void ConditionUtils::setByteVal(struct condition *condition, signed char value)
{
jobject jByteVal = jvm->NewObject(this->typeUtils->getByteClass(), this->typeUtils->createByteMethod(), (jbyte)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jByteVal);
this->jvm->DeleteLocalRef(jByteVal);
}
void ConditionUtils::setStringVal(struct condition *condition, const char *value)
{
jstring jStringVal = this->typeUtils->toJavaString(value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jStringVal);
this->jvm->DeleteLocalRef(jStringVal);
}
void ConditionUtils::setBoolVal(struct condition *condition, bool value)
{
jobject jBoolVal = jvm->NewObject(this->typeUtils->getBoolClass(), this->typeUtils->createBoolMethod(), (jboolean)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jBoolVal);
this->jvm->DeleteLocalRef(jBoolVal);
}
void ConditionUtils::setShortVal(struct condition *condition, short value)
{
jobject jShortVal = jvm->NewObject(this->typeUtils->getShortClass(), this->typeUtils->createShortMethod(), (jshort)value);
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, jShortVal);
this->jvm->DeleteLocalRef(jShortVal);
}
void ConditionUtils::setListVal(struct condition *condition, struct val_list *value)
{
this->jvm->CallVoidMethod(condition->jcondition, this->setValueMethodId, value->jlist);
}
/* Private Methods */
void ConditionUtils::loadGlobalRefs(JNIEnv *jvm, TypeUtils *typeUtils, ListUtils *listUtils)
{
this->jvm = jvm;
this->typeUtils = typeUtils;
this->listUtils = listUtils;
}
void ConditionUtils::loadMethodIds()
{
jclass conditionClass = JniUtils::getNamedClass(this->jvm, "gov/noaa/messageapi/interfaces/ICondition");
this->getIdMethodId = JniUtils::getMethod(this->jvm, conditionClass, "getId", this->getMethodSignature("getId"), false);
this->getTypeMethodId = JniUtils::getMethod(this->jvm, conditionClass, "getType", this->getMethodSignature("getType"), false);
this->getOperatorMethodId = JniUtils::getMethod(this->jvm, conditionClass, "getOperator", this->getMethodSignature("getOperator"), false);
this->getValueMethodId = JniUtils::getMethod(this->jvm, conditionClass, "getValue", this->getMethodSignature("getValue"), false);
this->setValueMethodId = JniUtils::getMethod(this->jvm, conditionClass, "setValue", this->getMethodSignature("setValue"), false);
this->jvm->DeleteLocalRef(conditionClass);
}
const char *ConditionUtils::getMethodSignature(const char *methodName)
{
if (strcmp(methodName, "getId") == 0)
{
return "()Ljava/lang/String;";
}
else if (strcmp(methodName, "getType") == 0)
{
return "()Ljava/lang/String;";
}
else if (strcmp(methodName, "getOperator") == 0)
{
return "()Ljava/lang/String;";
}
else if (strcmp(methodName, "getValue") == 0)
{
return "()Ljava/lang/Object;";
}
else if (strcmp(methodName, "setValue") == 0)
{
return "(Ljava/lang/Object;)V";
}
return NULL;
}
\ No newline at end of file
......@@ -22,17 +22,51 @@ class ConditionUtils
public:
/*Default constructor/destructors*/
ConditionUtils(JNIEnv *javaEnv, ListUtils *listUtils);
ConditionUtils(JNIEnv *javaEnv, TypeUtils *typeUtils, ListUtils *listUtils);
~ConditionUtils();
/*API Methods*/
const char *getId(struct condition *condition);
const char *getType(struct condition *condition);
const char *getOperator(struct condition *condition);
bool isNull(struct condition *condition);
struct val *getVal(struct condition *condition);
int getIntVal(struct condition *condition);
long getLongVal(struct condition *condition);
float getFloatVal(struct condition *condition);
double getDoubleVal(struct condition *condition);
signed char getByteVal(struct condition *condition);
const char *getStringVal(struct condition *condition);
bool getBoolVal(struct condition *condition);
short getShortVal(struct condition *condition);
struct val_list *getListVal(struct condition *condition);
void setVal(struct condition *condition, struct val *value);
void setIntVal(struct condition *condition, int value);
void setLongVal(struct condition *condition, long value);
void setFloatVal(struct condition *condition, float value);
void setDoubleVal(struct condition *condition, double value);
void setByteVal(struct condition *condition, signed char value);
void setStringVal(struct condition *condition, const char *value);
void setBoolVal(struct condition *condition, bool value);
void setShortVal(struct condition *condition, short value);
void setListVal(struct condition *condition, struct val_list *value);
private:
/*Vars*/
JNIEnv *jvm;
ListUtils *listUtils;
TypeUtils *typeUtils;
/*Condition Methods*/
jmethodID getIdMethodId;
jmethodID getTypeMethodId;
jmethodID getOperatorMethodId;
jmethodID getValueMethodId;
jmethodID setValueMethodId;
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadMethodIds();
void loadGlobalRefs(JNIEnv *env, ListUtils *listUtils);
void loadGlobalRefs(JNIEnv *env, TypeUtils *typeUtils, ListUtils *listUtils);
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getMethodSignature(const char *methodName);
......
This diff is collapsed.
......@@ -17,6 +17,7 @@
#include "RecordUtils.h"
#include "RejectionUtils.h"
#include "FieldUtils.h"
#include "ConditionUtils.h"
#include "PacketUtils.h"
/**
......@@ -43,36 +44,11 @@ public:
RecordUtils *getRecordUtils();
RejectionUtils *getRejectionUtils();
FieldUtils *getFieldUtils();
ConditionUtils *getConditionUtils();
PacketUtils *getPacketUtils();
ListUtils *getListUtils();
TypeUtils *getTypeUtils();
/*Condition Methods*/
const char *getConditionId(struct condition *condition);
const char *getConditionType(struct condition *condition);
const char *getConditionOperator(struct condition *condition);
bool getConditionIsNull(struct condition *condition);
struct val *getConditionVal(struct condition *condition);
int getConditionIntVal(struct condition *condition);
long getConditionLongVal(struct condition *condition);
float getConditionFloatVal(struct condition *condition);
double getConditionDoubleVal(struct condition *condition);
signed char getConditionByteVal(struct condition *condition);
const char *getConditionStringVal(struct condition *condition);
bool getConditionBoolVal(struct condition *condition);
short getConditionShortVal(struct condition *condition);
struct val_list *getConditionListVal(struct condition *condition);
void setConditionVal(struct condition *condition, struct val *value);
void setConditionIntVal(struct condition *condition, int value);
void setConditionLongVal(struct condition *condition, long value);
void setConditionFloatVal(struct condition *condition, float value);
void setConditionDoubleVal(struct condition *condition, double value);
void setConditionByteVal(struct condition *condition, signed char value);
void setConditionStringVal(struct condition *condition, const char *value);
void setConditionBoolVal(struct condition *condition, bool value);
void setConditionShortVal(struct condition *condition, short value);
void setConditionListVal(struct condition *condition, struct val_list *value);
private :
/*Global References*/
JNIEnv *jvm;
......@@ -84,23 +60,11 @@ private :
RecordUtils *recordUtils;
RejectionUtils *rejectionUtils;
FieldUtils *fieldUtils;
ConditionUtils *conditionUtils;
PacketUtils *packetUtils;
TypeUtils *typeUtils;
ListUtils *listUtils;
/*Condition Methods*/
jmethodID getConditionIdMethodId;
jmethodID getConditionTypeMethodId;
jmethodID getConditionOperatorMethodId;
jmethodID getConditionValueMethodId;
jmethodID setConditionValueMethodId;
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadConditionMethodIds();
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getConditionMethodSignature(const char *conditionMethodName);
};
extern "C"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment