• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Regular expression to compare numbers

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method

compare(Object value, String searchTxt);

The value can either be a Number, Date, String.
The searchTxt can have values to compare Strings, Dates, Numbers. It also can have logical comparison operators like =, !=, >, <, >=, <= eg.

compare(numValue, "> 1000 <= 2000)

compare(dateValue, "> 2006/12/12 <= 2007/01/25)

compare(stringValue, "> abc <= xyz )

Can I have some regular expression to control this comparision and make my life little easier. Because spliting the string and checking for all possible options can be a tedious job.

Peace n Regards
 
chandrajeet padhy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The searchTxt can have values like

"> 1000 <= 2000"

"> 2006/12/12 <= 2007/01/25"

"> abc <= xyz"

But I can check for the instanceof for the "value" and know that the searchTxt contains logical opearators plus (only Number or only Date or only String).

The real requirement is to comare the "value" according to the search string.

Say for eg:
if the method compare is invoked as compare(200, ">=100<500") it should return true

if the method compare is invoked as compare("xyz", ">=abc<xxx") it should return false

and so on...>
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're stuck with splitting the string into tokens and processing each token. You're lucky in that your syntax is really simple

operator value [ operator value ... ]

And you're right, the code to test the object type, then test each token can be annoyingly boring and repetitive. I'd be tempted to build a swarm of little tiny classes - only a few lines each - to execute the operations.

An operation might look like:

Did that make sense? I bet you could build this with not one "if" test. Could be lost o fun.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps if you transform:
to
and invoke a scripting engine like beanshell, to evaluate the expression?
 
chandrajeet padhy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
Can you give an example in java please
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[chandrajeet padhy ]:

I have a method

compare(Object value, String searchTxt);


From what you say later, I think it might make a lot more sense to instead make three methods:

And if you must, you can have an Object version:

Within each of the other three methods, you will know whether you're dealing with a String, Number or Date, making the code in each individual method simpler.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do those searchTxt objects *need* to be Strings? Where are they coming from?
 
chandrajeet padhy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim Yingst,

I dont have difficulty in identifying Date, Number or String.

My problem is actually inside the compare method. How can I?
As I said The searchText is a tuff thing. And James got it right. But I need a java based solution.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if you can translate my babble into Java ... one line at a time should do it. We try to give really good hints here, but not complete solutions. If you take the effort to make some almost working code, we'll help you move it along. So jump in ... it's a lot more fun if you make it work yourself.
 
chandrajeet padhy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made it work this way. Any suggestions for improvement is welcomed.



ValueComparator looks as:-



And then different operations as per:



Thanks
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kool. I bet you can generalize a bit more yet. Just playing thought experiments here - you can decide if it's worth trying. What if the interface for Operation was still two objects?

Then the Operation would have to get the two arguments into the proper format itself. That seems like a good place to create a date or a number. An abstract DateOperation could do the date manipulation and call an abstract compare( Date, Date ) method.

Your map of operations might include the type:

That would multiply the number of operations to types*operators. Ick. Maybe we could cut back to one operator class per type:

where the arguments tell what to return if the standard compareTo() returns <0, 0 or >0.

Any of that sound fun?
 
chandrajeet padhy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yea. I thought of that to have only one method compare(obj, obj) in the Operation interface. But, that will lead upto creating types*operators number of classes like DateGT, DateLT, DateGEQ, DateLEQ, NumberGT, NumberLT...so on.
I just kept it simiplified to have only few Operation impls to handle all data types. Great going! Thanks again.
reply
    Bookmark Topic Watch Topic
  • New Topic