Coverage Report

##### Coverage Report - org.webmacro.servlet.MathTool

Classes in this File | Line Coverage | Branch Coverage | Complexity
MathTool |
9% | 3/31
0% | 0/20
| 1.434782608695652;1.435

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
23 |  |
     package org.webmacro.servlet;
24 |  |
25 |  |
     import org.webmacro.Context;
26 |  |
     import org.webmacro.ContextTool;
27 |  |
     import org.webmacro.PropertyException;
28 |  |
29 |  |
     /**
30 |  |
      * A ContextTool for performing the more useful methods from
31 |  |
      * <code>java.lang.Math</code>.
32 |  |
      *
33 |  |
      * @author Eric B. Ridge (mailto:ebr@tcdi.com)
34 |  |
      */
35 |  |
36 |  |
     public class MathTool extends ContextTool
37 |  |
     {
38 |  |
39 |  |
         /** our lonely singleton */
40 | 2 |
         private static final MathTool _instance = new MathTool();
41 |  |
42 |  |
43 |  |
         /**
44 |  |
          * @return the static instance of the MathTool
45 |  |
          */
46 |  |
         public static final MathTool getInstance ()
47 |  |
         {
48 | 0 |
             return _instance;
49 |  |
         }
50 |  |
51 |  |
52 |  |
         //
53 |  |
         // static MathTool methods and fields
54 |  |
         //
55 |  |
56 |  |
         /** the value of PI, as defined by <code>java.lang.Math.PI</code> */
57 |  |
         public static final double PI = java.lang.Math.PI;
58 |  |
59 |  |
         /**
60 |  |
          * @return the smaller of the specified number
61 |  |
          */
62 |  |
         public static final int min (int a, int b)
63 |  |
         {
64 | 0 |
             return (a < b) ? a : b;
65 |  |
         }
66 |  |
67 |  |
         public static final long min (long a, long b)
68 |  |
         {
69 | 0 |
             return (a < b) ? a : b;
70 |  |
         }
71 |  |
72 |  |
         public static final float min (float a, float b)
73 |  |
         {
74 | 0 |
             return (a < b) ? a : b;
75 |  |
         }
76 |  |
77 |  |
         public static final double min (double a, double b)
78 |  |
         {
79 | 0 |
             return (a < b) ? a : b;
80 |  |
         }
81 |  |
82 |  |
83 |  |
         /**
84 |  |
          * @return the larger of the specified number
85 |  |
          */
86 |  |
         public static final int max (int a, int b)
87 |  |
         {
88 | 0 |
             return (a > b) ? a : b;
89 |  |
         }
90 |  |
91 |  |
         public static final long max (long a, long b)
92 |  |
         {
93 | 0 |
             return (a > b) ? a : b;
94 |  |
         }
95 |  |
96 |  |
         public static final float max (float a, float b)
97 |  |
         {
98 | 0 |
             return (a > b) ? a : b;
99 |  |
         }
100 |  |
101 |  |
         public static final double max (double a, double b)
102 |  |
         {
103 | 0 |
             return (a > b) ? a : b;
104 |  |
         }
105 |  |
106 |  |
         /**
107 |  |
          * Creates a pseudo-random Integer between <code>start</code>
108 |  |
          * and <code>end</code>, inclusive
109 |  |
          */
110 |  |
         public static final int random (int start, int end)
111 |  |
         {
112 | 0 |
             return start + (int) ((end - start + 1) * java.lang.Math.random());
113 |  |
         }
114 |  |
115 |  |
         /**
116 |  |
          * @return <code>base</code> raised to the specified <code>power</code>
117 |  |
          */
118 |  |
         public static final int pow (int base, int power)
119 |  |
         {
120 | 0 |
             return base ^ power;
121 |  |
         }
122 |  |
123 |  |
         public static final long pow (long base, long power)
124 |  |
         {
125 | 0 |
             return base ^ power;
126 |  |
         }
127 |  |
128 |  |
129 |  |
         /**
130 |  |
          * @return the absolute value of the specified number
131 |  |
          */
132 |  |
         public static final int abs (int a)
133 |  |
         {
134 | 0 |
             return java.lang.Math.abs(a);
135 |  |
         }
136 |  |
137 |  |
         public static final long abs (long a)
138 |  |
         {
139 | 0 |
             return java.lang.Math.abs(a);
140 |  |
         }
141 |  |
142 |  |
         public static final float abs (float a)
143 |  |
         {
144 | 0 |
             return java.lang.Math.abs(a);
145 |  |
         }
146 |  |
147 |  |
         public static final double abs (double a)
148 |  |
         {
149 | 0 |
             return java.lang.Math.abs(a);
150 |  |
         }
151 |  |
152 |  |
153 |  |
         /**
154 |  |
          * @return <code>a</code> modulo <code>b</code>
155 |  |
          */
156 |  |
         public static final int mod (int a, int b)
157 |  |
         {
158 | 0 |
             return a % b;
159 |  |
         }
160 |  |
161 |  |
         /**
162 |  |
          * Converts args to doubles and multiplies them together.
163 |  |
          * @return <code>a * b</code>
164 |  |
          */
165 |  |
         public static final double multiply (Number a, Number b)
166 |  |
         {
167 | 0 |
             return a.doubleValue() * b.doubleValue();
168 |  |
         }
169 |  |
170 |  |
         /**
171 |  |
          * Converts args to doubles and divides the first by the second.
172 |  |
          * @return <code>a / b</code>
173 |  |
          */
174 |  |
         public static final double divide (Number a, Number b)
175 |  |
         {
176 | 0 |
             return a.doubleValue() / b.doubleValue();
177 |  |
         }
178 |  |
179 |  |
180 |  |
         //
181 |  |
         // ContextTool implementation methods
182 |  |
         //
183 |  |
184 |  |
         /** default contsructor.  Does nothing */
185 |  |
         public MathTool ()
186 | 8 |
         {
187 | 8 |
         }
188 |  |
189 |  |
         /**
190 |  |
          * public constructor.  Does nothing.  The MathTool doesn't
191 |  |
          * interact with the Context, so it is ignored
192 |  |
          */
193 |  |
         public MathTool (Context context)
194 | 0 |
         {
195 | 0 |
         }
196 |  |
197 |  |
         /**
198 |  |
          * Tool initialization method.  The MathTool doesn't
199 |  |
          * interact with the context, so the <code>context</code>
200 |  |
          * parameter is ignored.
201 |  |
          */
202 |  |
         public Object init (Context context) throws PropertyException
203 |  |
         {
204 | 0 |
             return MathTool.getInstance();
205 |  |
         }
206 |  |
207 |  |
         public static void main (String[] args)
208 |  |
         {
209 | 0 |
             System.out.println("Generating 200 random ints between 10 and 99 inclusive:");
210 | 0 |
             for (int i = 0; i < 200; i++)
211 |  |
             {
212 | 0 |
                 System.out.print(MathTool.random(10, 99) + " ");
213 | 0 |
                 if (((i + 1) % 20) == 0) System.out.println();
214 |  |
             }
215 | 0 |
             System.out.println("\nDone.");
216 | 0 |
         }
217 |  |
     }