This is somewhat a 2 question in one. It's for a video game and I am using libgdx, so it must be relatively fast.
I have object that will be read and written to a derby database. Some of those objects data structure will be defined at runtime, so I cannot make a POJO with getters and setters and using the persistance API for example.
Instead a data object will look like this
import java.sql.Types;
import com.badlogic.gdx.utils.Array;
public class Creature extends PBDBsystem_table
{
protected static Array<PBDBsystem_field> s_field_list = new Array<PBDBsystem_field>();
protected static String s_table_name = new String ("creature");
Creature ()
{
create ( s_table_name, s_field_list );
}
public static boolean init()
{
s_field_list.add ( new PBDBsystem_field ("pk", Types.INTEGER, 0, "", true, true ));
s_field_list.add( new PBDBsystem_field ("name", Types.VARCHAR, 30 ));
s_field_list.add( new PBDBsystem_field ("cost", Types.INTEGER, 0, "1" ));
s_field_list.add( new PBDBsystem_field ("strength", Types.INTEGER ));
create_table ( s_table_name, s_field_list);
return true;
}
}
For your information, the full length constructor is defined as:
public PBDBsystem_field ( String name, int type, int size, String default_value, boolean not_null, boolean primary_key )
What this basically do is that init() will create a list of field in an Array that will remain statically in the class and a reference to the table name and field list is passed to the parent class though create(). So that each object has a reference on it's data structure (Which is allocated only once). Then the parent class define the data container for each object like this:
private ArrayMap<String, String> p_data_list; /** structure to contain database data */
Right now I am placing all fields as strings. The key is the field name, and the value is the field data. I am using generic getters and setters in the parent class that require a key like for example:
public void set ( String key, String value)
{
p_data_list.put( key, value);
}
public String get ( String key )
{
return p_data_list.get(key);
}
Question 1: Is it recommended to store everything as string as it will sometimes demand conversion to int which could slow things down. I was thinking of using for example the Interger class instead of primitives and make an array map of Object instead of String. But I should still get conversion and casting issues, but they should remain in the parent class if I do all the generic getters/setter correctly.
Ok, during the creation of the object, I had by reflex created a new string instance for each field in the table like this.
protected void create ( String table_name, Array<PBDBsystem_field> field_list )
{
p_table_name = table_name;
p_field_list = field_list;
p_data_list = new ArrayMap <String, String> ();
p_data_list.ordered = false;
for ( PBDBsystem_field field: p_field_list)
{
p_data_list.put ( field.name, new String() );
}
}
This method reference the table name and field list to the parent class, then for each field it create a new string to contain the data. But if I modify the content of a field using the set method above, it will use another string to insert it in the array map. Which leads me to
Question 2: Will it do a deep copy into the array map's string, or will it simply reference the string that was passed in the set method? Meaning that there is no reason to actually instantiate a string for each field. I should rather leave everything to null.
Considering it's for a video game, I want to minimize the use of the garbage collector by making reusable object or reduce pointless object destruction.