storing data with variable numbers and types

3

3rdshiftcoder

i have some data i want to store. in the code sample below, the key pieces
of data
i wish to store are the name of the function: like, parameter1: str1,
paramater2: str2.

an easy part i think is i can use a 1st tree map to store an integer key and
Criterion value
as all methods return Criterion.

//method implementation for querybuilder criterion
public Criterion like(String str1, String str2){
restriction =
Restrictions.like(str1,str2);
counter = counter + 1;
return restriction;
}

can someone give me a hint how to store the function name and parameter list
(which can differ in number of parameters and type of parameters)?
i'd like to use a second tree map for that information. i think part of the
hint may be is to use a user-defined class for the value part of the 2nd
treemap collection.

thanks for any help,
jim
 
3

3rdshiftcoder

i think i figured it out.

Class MethodCall {
private String Str1;
private String Str2;
private Object obj1;
private Object obj2;
private Criterion crit1;
private Criterion crit2;

MethodCall( ....constructor 1)
MethodCall(....constructor 2)
etc.

i just use different constructors.
so this can be my user defined collection
that i can put in the 2nd treemap.

easier than i thought.

thanks,
jim
 
T

Tom Hawtin

3rdshiftcoder said:
i have some data i want to store. in the code sample below, the key pieces
of data
i wish to store are the name of the function: like, parameter1: str1,
paramater2: str2.

I'm not entirely sure what you are after.

The Java language has no support for passing functions around like
objects. If you want to pass things around like objects, use objects.

Possibly what you might want is an interface for your 'functions', that
just has one method defined. You can then implement your function
interface using anonymous inner classes, that can also take a snapshot
of (final) local variables.

So for instance.

interface TestString {
boolean test(String str);
}
....
public static TestString startsWith(final String start) {
return new TestString() {
public boolean test(String str) {
return str.startsWith(start);
// ^^^^^ a final local variable
// from the enclosing method
}
};
}
....
TestString fileURLTest = startsWith("file:");
...

...
System.err.println(
fileURLTest.test("file:///etc/passwd")
);

The anonymous inner class in startsWith above is the equivalent of doing:

class StartsWithTestString implements TestString {
private final String start;
public StartsWithTestString(String start) {
this.start = start;
}
public boolean test(String str) {
return str.startsWith(start);
}
}
....
public static TestString startsWith(final String start) {
return new StartsWithTestString(start);
}

[Disclaimer: I've not compiled or tested this code.]

Tom Hawtin
 
3

3rdshiftcoder

Hi -

thanks for the assistance.
a big thunderstorm is hitting.
i will store your post off-line for reviewing later.

have a nice week
jim
 
3

3rdshiftcoder

Hi Tom-
I'll post my code for this concept i am working on.
Maybe you can see better from a more complete posting.
I was trying to be brief but sometimes it may be better to see the code.
It is kind of like a prototype of my querybuilder not attached to a GUI yet.
I still plan on looking at your code later as i am trying to learn java and
need to read code examples.

I will fix the rest of the constructors to not include all of the null
initializations later.

the reason i use the treemaps is to have a record of what i am doing so
when i hook it up to the builder GUI screen i can delete , add,
and maybe edit the history based on the treemap values.

the methods are greater than, less than, equal to, between,
like etc.

hope someone likes this example.
i am going to try and add it to my checkbook program
when it is completed.
later,
jim

package demolib;
import org.hibernate.criterion.Criterion;
public class E {
public String str1 = null ;
public String str2 = null;
public String str3 =null;
private Criterion crit1 = null;
private Criterion crit2 = null ;
private Object obj1 = null;
private Object obj2 = null;
E(String s1, String s2, String s3)
{
str1 = s1;
str2 = s2;
str3 = s3;
}
E(String s1, String s2, Object object1, String s3, Object object2,
Criterion c1, Criterion c2)
{
str1 = s1;
str2 = s2;
obj1 = object1;
str3 = null;
obj2 = null;
crit1 = null;
crit2 = null;
}
E(String s1, Criterion c1, Criterion c2, String s2, String s3,
Object object1, Object object2){
str1 = s1;
crit1 = c1;
crit2 = c2;
str2 = null;
str3 = null;
obj1 = null;
obj2 = null;
}
E(String s1, Object object1, Object object2, String s2, String s3,
Criterion c1, Criterion c2)
{
str1 = s1;
obj1 = object1;
obj2 = object2;
str2 = null;
str3 = null;
crit1 = null;
crit2 = null;
}
public static void main(String[] args){
//E e = new E("like","payeepayor","chas%");
}
}

package demolib;
import java.util.Iterator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Criterion;
import org.hibernate.Criteria;
import java.util.List;
import org.hibernate.Query;
import demolib.Transact;
import java.util.TreeMap;
public class D {
static Criterion restriction;
static Criterion[] rArray = new Criterion[10];
Session session;
List lst;
Query qry;
Transact tTbl;
Transaction tx;
static TreeMap<Integer,E> tm = new TreeMap<Integer,E>();
static TreeMap<Integer,Criterion> tm2 = new TreeMap<Integer,Criterion>();
static int counter = -1;
public Criterion and(Criterion crit1, Criterion crit2){
restriction =
Restrictions.and(crit1, crit2);
counter = counter + 1;
return restriction;
}
public Criterion or(Criterion crit1, Criterion crit2){
restriction =
Restrictions.or(crit1, crit2);
counter = counter + 1;
return restriction;
}
public Criterion like(String str1, String str2){
restriction =
Restrictions.like(str1,str2);
counter = counter + 1;
return restriction;
}
public Criterion ilike(String str1, String str2){
restriction =
Restrictions.ilike(str1,str2);
counter = counter + 1;
return restriction;
}
public Criterion eq(String str1,Object obj1){
restriction =
Restrictions.eq(str1, obj1);
counter = counter + 1;
return restriction;
}
public Criterion gt(String str1,Object obj1){
restriction =
Restrictions.gt(str1, obj1);
counter = counter + 1;
return restriction;
}
public Criterion ge(String str1,Object obj1){
restriction =
Restrictions.ge(str1, obj1);
counter = counter + 1;
return restriction;
}
public Criterion lt(String str1,Object obj1){
restriction =
Restrictions.lt(str1, obj1);
counter = counter + 1;
return restriction;
}
public Criterion le(String str1,Object obj1){
restriction =
Restrictions.le(str1, obj1);
counter = counter + 1;
return restriction;
}
public Criterion between(String str1,Object obj1, Object obj2){
restriction =
Restrictions.between(str1, obj1, obj2);
counter = counter + 1;
return restriction;
}
public void runQuery(Criterion[] restriction){
try{
session =
HibernateUtil.getSessionFactory().openSession();
tx =
session.beginTransaction();
Criteria criteriaVal = session.createCriteria(Transact.class);
for(int i = 0; i < counter + 1; i++)
criteriaVal.add(restriction);
lst = criteriaVal.list();
for ( Iterator iter = lst.iterator();
iter.hasNext(); ) {
tTbl = (Transact)
iter.next();
System.out.println(tTbl.getTransactionid());
}
tx.commit();
HibernateUtil.shutdown();
} catch (HibernateException e) {
if (tx != null) tx.rollback();
Shell shellcreature = new Shell();
MessageDialog messageDialog = new
MessageDialog(shellcreature,"ERROR",null,e.getMessage(),MessageDialog.ERROR,
new String[]{"ok"},0);
messageDialog.open();
e.printStackTrace();
}catch(Exception e1){
e1.printStackTrace();
}finally {
if (session != null)
session.close();
}
}
public static void main(String[] args){
D d = new D();
//restriction = d.like("payeepayor", "chase%");
//rArray[counter]=restriction;
//restriction = d.like("paytype", "payor%");
//rArray[counter]=restriction;
//restriction = d.between("transactionamount",11.25,11.26);
//rArray[counter]=restriction;
//d.runQuery(rArray);
E e = new E("like","payeepayor","chas%");
if (e.str1.equals("like"))
restriction = d.like(e.str2, e.str3);
tm.put(Integer.valueOf(counter),e);
tm2.put(Integer.valueOf(counter),restriction );
rArray[counter]=restriction;
d.runQuery(rArray);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top