KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > lang > NumberCompare


1 package gnu.jemacs.lang;
2 import gnu.math.*;
3 import gnu.mapping.*;
4
5 public class NumberCompare extends ProcedureN
6 {
7   // Return codes from Numeric.compare:
8
static final int RESULT_GRT = 1;
9   static final int RESULT_EQU = 0;
10   static final int RESULT_LSS = -1;
11   static final int RESULT_NAN = -2;
12   static final int RESULT_NEQ = -3;
13
14   static final int TRUE_IF_GRT = 16;
15   static final int TRUE_IF_EQU = 8;
16   static final int TRUE_IF_LSS = 4;
17   static final int TRUE_IF_NAN = 2;
18   static final int TRUE_IF_NEQ = 1;
19   int flags;
20
21   public static final NumberCompare $Eq = make("=",TRUE_IF_EQU);
22   public static final NumberCompare $Gr = make(">",TRUE_IF_GRT);
23   public static final NumberCompare $Gr$Eq= make(">=",TRUE_IF_GRT|TRUE_IF_EQU);
24   public static final NumberCompare $Ls = make("<",TRUE_IF_LSS);
25   public static final NumberCompare $Ls$Eq= make("<=",TRUE_IF_LSS|TRUE_IF_EQU);
26
27   public static NumberCompare make(String JavaDoc name, int flags)
28   {
29     NumberCompare proc = new NumberCompare();
30     proc.setName(name);
31     proc.flags = flags;
32     return proc;
33   }
34
35   public Object JavaDoc apply2 (Object JavaDoc arg1, Object JavaDoc arg2)
36   {
37     if (apply2(flags, arg1, arg2))
38       return ELisp.TRUE;
39     else
40       return ELisp.FALSE;
41   }
42
43   public static boolean apply2 (int flags, Object JavaDoc arg1, Object JavaDoc arg2)
44   {
45     Numeric num1 = ELisp.asNumber(arg1);
46     Numeric num2 = ELisp.asNumber(arg2);
47     return ((1 << (3 + num1.compare(num2))) & flags) != 0;
48   }
49
50   public Object JavaDoc applyN (Object JavaDoc[] args)
51   {
52     // if (args.length < 2)
53
// throw new WrongArguments(this.name(),2,"(< x1 x2 ...)");
54
for (int i = 0; i < args.length - 1; i++)
55       {
56     Object JavaDoc arg1 = args[i];
57     Object JavaDoc arg2 = args[i+1];
58     if (! apply2(flags, arg1, arg2))
59       return ELisp.FALSE;
60       }
61     return ELisp.TRUE;
62   }
63 }
64
Popular Tags