Code Examples
A repository of 155 code examples for BeepBeep
IsPrime.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.FunctionException;
21 import ca.uqac.lif.cep.functions.UnaryFunction;
22 
23 /**
24  * Create a custom unary function that checks if a number is prime.
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 single argument,
29  * it is wise to extend {@link UnaryFunction}.
30  *
31  * @author Sylvain HallĂ©
32  */
33 public class IsPrime extends UnaryFunction<Number,Boolean>
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 IsPrime instance = new IsPrime();
45 
46  private IsPrime()
47  {
48  /* The constructor should call super(), and pass the type of
49  * the input and output. */
50  super(Number.class, Boolean.class);
51  }
52 
53  /*
54  * Method getValue() is where the output of the function is
55  * computed for the input. For the sake of our example, the
56  * actual way to check if x is prime does not matter;
57  * we'll simply enumerate all numbers up to sqrt(x) until we
58  * find one that divides x, and otherwise return true.
59  */
60  @Override
61  public Boolean getValue(Number x)
62  {
63  int k = x.intValue(); // Convert x to an int
64  int max = (int) Math.sqrt(k);
65  for (int n = 2; n <= max; n++)
66  {
67  if (k % n == 0)
68  return false; // Found a divisor: x is not prime
69  }
70  return true;
71  }
72 
73  /*
74  * A small main method to illustrate the function
75  */
76  public static void main(String[] args) throws FunctionException
77  {
79  Object[] value = new Object[1];
80  // A function is always called on an array of objects; this array
81  // corresponds to the arguments. Here the function is unary, hence
82  // the array is of size 1
83  ip.evaluate(new Integer[]{3}, value);
84  // Likewise, a function always returns an array of objects. Most
85  // functions (like this one) return a single object, so the output
86  // array is also of size 1
87  System.out.printf("Return value of the function: %b\n", value[0]);
88  ip.evaluate(new Integer[]{8}, value);
89  System.out.printf("Return value of the function: %b\n", value[0]);
90  }
91 }
Create a custom unary function that checks if a number is prime.
Definition: IsPrime.java:33
static final transient IsPrime instance
This is optional.
Definition: IsPrime.java:44