Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Dynamic Expression in Java
- From: shawn.r.crane@xxxxxxxx (Crane, Shawn R (N-MAXIM Group))
- Subject: [Advanced-java] Dynamic Expression in Java
- Date: Mon, 18 Mar 2002 12:01:52 -0500
Mike,
Another solution might be to create a Comparison class that has a bunch of
inner classes to do what you want. Like this: (warning: I haven't tried to
compile this, but you get the idea :-) )
public class Comparison {
public static final EQUALS = new Comparison() {
public boolean compare(Object obj1, Object obj2) {
// Do equals comparison here.
}
};
public static final LESSTHAN = new Comparison() {
public boolean compare(Object obj1, Object obj2) {
// Do less than comparison here.
}
};
// And so on for other comparison operations...
protected Comparison() {}
public abstract boolean compare(Object obj1, Object obj2);
}
Then you can do something like:
Object obj1 = foo;
Object obj2 = bar;
Comparison comp = Comparison.EQUALS;
if ( comp.compare(obj1, obj2) ) {
// blah
}
If you want, you could tweak the Comparison class to implement a
getComparison(String comp) method that looks at the string passed in and
returns the appropriate Comparison subclass.
Good luck!
Shawn R. Crane
shawn.r.crane@xxxxxxxx
(618) 628-3195
-----Original Message-----
From: Paul Philion [mailto:philion@xxxxxxxxxxxxxx]
Sent: Monday, March 18, 2002 10:24 AM
To: advanced-java@xxxxxxxxxxxxxxxx
Cc: kmehta@xxxxxxxxxxxxxxxxxxx
Subject: Re: [Advanced-java] Dynamic Expression in Java
Mike -
If you're interested in comparing two objects in a flexible way, take a look
at the java.lang.Comparable and java.util.Comparator interfaces.
You might also consider the Command design pattern from the GOF book.
If you are looking for something that will compile Java code, in-line, at
runtime (like Perl) or even something like C++ operator overloading, those
solutions aren't really applicable in Java. I would suggest taking a more
abstract look at *what* you are trying to accomplish, as opposed to *how* it
might be done.
Good luck,
- Paul Philion
----- Original Message -----
From: "Michael Shoemaker" <shoemaker_m@xxxxxxxxx>
To: <advanced-java@xxxxxxxxxxxxxxxx>
Cc: <kmehta@xxxxxxxxxxxxxxxxxxx>
Sent: Monday, March 18, 2002 9:16 AM
Subject: [Advanced-java] Dynamic Expression in Java
> Anyone know of a way to dynamically create an
> expression within java and have it executed.
>
> For example
>
> Object obj1 = // some object
> Object obj2 = // some object
> String strComp = "==";
>
> Now I need to dynamically generate the conditional.
>
> if (str1 strComp str2) {
> // Do something useful
> }
>
> Thanks in advance.
>
>
> =====
> Mike Shoemaker
> Mike@xxxxxxxxxxxxxxxx
> Visit my website
> http://www.PavolDemitra.com
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Sports - live college hoops coverage
> http://sports.yahoo.com/
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
>
_______________________________________________
Advanced-java mailing list
Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
- Prev by Date: [Advanced-java] Dynamic Expression in Java
- Next by Date: [Advanced-java] JavaOne is next week
- Previous by thread: [Advanced-java] NYJavaSIG - Pink Noise - 3/20 Mtg
- Next by thread: [Advanced-java] JavaOne is next week
- Index(es):