Re: Passing "<", ">", "=", etc as an argument to a method?
From: Raymond DeCampo (rdecampo_at_hold-the-spam.twcny.rr.com)
Date: 11/30/03
- Next message: Michael G: "Re: AWTEvents errors and deprecated methods"
- Previous message: Raymond DeCampo: "Re: Converting greek string to uppercase"
- In reply to: Stewart Gordon: "Re: Passing "<", ">", "=", etc as an argument to a method?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 Nov 2003 15:27:32 GMT
Stewart Gordon wrote:
> While it was 28/11/03 1:37 am throughout the UK, Anthony Borla sprinkled
> little black dots on a white screen, and they fell thus:
>
> <snip>
>
>> if (relationalOp.equals("<"))
>> doLessThan(value1, value2)
>> else if (relationalOp.equals("="))
>> doEquals(value1, value2)
>> ...
>
>
> Why not make relationalOp a number, and save the overhead of doing
> string comparison?
>
> Define
>
> final static int EQ = 1;
> final static int GT = 2;
> final static int GTEQ = 3;
> final static int LT = 4;
> final static int LTEQ = 5;
> final static int NOTEQ = 6;
>
> and pass these values in.
>
Even better: use polymorphism. E.g.
// WARNING: Uncompiled, untested code
public class Helper
{
public static final Finder EQUALS = new EqualsFinder();
public static final Finder GREATER_THAN = new GreaterThanFinder();
// etc.
public static List find(double[] array, double val, Finder finder)
{
List result = new ArrayList();
for (int i = 0; i < array.length; i++)
{
if (finder.satisfies(array[i], val))
{
result.add(new Double(array[i]));
}
}
}
public static interface Finder
{
public boolean satisfies(double candidate, double val);
}
public static class EqualsFinder
{
public boolean satisfies(double candidate, double val)
{
return (candidate == value);
}
}
public static class GreaterThanFinder
{
public boolean satisfies(double candidate, double val)
{
return (candidate > value);
}
}
}
Ray
- Next message: Michael G: "Re: AWTEvents errors and deprecated methods"
- Previous message: Raymond DeCampo: "Re: Converting greek string to uppercase"
- In reply to: Stewart Gordon: "Re: Passing "<", ">", "=", etc as an argument to a method?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|