Code Examples
A repository of 155 code examples for BeepBeep
Maximum.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package functions;
19 
20 import ca.uqac.lif.cep.functions.BinaryFunction;
21 import ca.uqac.lif.cep.functions.FunctionException;
22 
23 /**
24  * Binary function that returns the maximum of two values.
25  * <p>
26  * Instead of inheriting from the top-level {@link Function} class,
27  * it is easier in most cases to descend from lower classes, which provide a
28  * few more functionalities. For a function that takes a two arguments,
29  * it is wise to extend {@link BinaryFunction}.
30  *
31  * @author Sylvain HallĂ©
32  */
33 public class Maximum extends BinaryFunction<Number,Number,Number>
34 {
35  /*
36  * This is optional. Since a Function object is stateless, the
37  * same instance can be used everywhere it is needed. Therefore,
38  * it is recommended to enforce the existence of a single instance
39  * by making the function's constructor private, and by making
40  * public a static field pointing to an instance of the function.
41  * By convention, you are encouraged to use the name "instance" for
42  * this field.
43  */
44  public static final transient Maximum instance = new Maximum();
45 
46  private Maximum()
47  {
48  // The constructor should call super(), and pass the type of
49  // the input arguments and the output
50  super(Number.class, Number.class, Number.class);
51  }
52 
53  @Override
54  public Number getValue(Number x, Number y)
55  {
56  /*
57  * Method getValue() is where the output of the function is
58  * computed from the inputs.
59  */
60  if (x.floatValue() > y.floatValue())
61  {
62  return x;
63  }
64  return y;
65  }
66 
67  /*
68  * A small main method to illustrate the function
69  */
70  public static void main(String[] args) throws FunctionException
71  {
72  Maximum max = Maximum.instance;
73  Object[] value = new Object[1];
74  // A function is always called on an array of objects; this array
75  // corresponds to the arguments. Here the function is unary, hence
76  // the array is of size 2
77  max.evaluate(new Float[]{3.5f, 10f}, value);
78  // Likewise, a function always returns an array of objects. Most
79  // functions (like this one) return a single object, so the output
80  // array is also of size 1
81  System.out.printf("Return value of the function: %f\n", value[0]);
82  max.evaluate(new Float[]{13.1f, 7.7f}, value);
83  System.out.printf("Return value of the function: %f\n", value[0]);
84  }
85 }
Binary function that returns the maximum of two values.
Definition: Maximum.java:33