Re: Passing "<", ">", "=", etc as an argument to a method?

From: Raymond DeCampo (rdecampo_at_hold-the-spam.twcny.rr.com)
Date: 11/30/03


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



Relevant Pages

  • Re: Passing "<", ">", "=", etc as an argument to a method?
    ... > Borla sprinkled little black dots on a white screen, ... > Why not make relationalOp a number, and save the overhead ... > of doing string comparison? ...
    (comp.lang.java)
  • Re: new OO OS
    ... "Stewart Gordon" wrote ... > little black dots on a white screen, ... >> I'm looking for collaborators to develop new object oriented operating ... I've been trying to avoid this senseless thread, but this desperately needs to ...
    (comp.lang.cpp)
  • Re: is it possible to use a backslash as a StringTokenizer delimiter?
    ... Stewart Gordon wrote: ... > sprinkled little black dots on a white screen, ... You want platform independence? ... Use an URI, if OTOH, you want to access ...
    (comp.lang.java.help)
  • Re: Is it posible with JAVA to ...
    ... "Stewart Gordon" ... ... > black dots on a white screen, ... Andrew Thompson ... http://www.PhySci.org/ Open-source software suite ...
    (comp.lang.java.help)
  • Re: getopt
    ... While it was 14/10/03 3:23 pm throughout the UK, Stewart Gordon ... sprinkled little black dots on a white screen, ... Please keep replies on ...
    (comp.lang.cpp)