Code Examples
A repository of 155 code examples for BeepBeep
BigIntegerFunctions.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 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 network.httppush.twinprimes;
19 
20 import java.math.BigInteger;
21 
22 import ca.uqac.lif.cep.functions.BinaryFunction;
23 import ca.uqac.lif.cep.functions.FunctionException;
24 import ca.uqac.lif.cep.functions.UnaryFunction;
25 
26 /**
27  * Contains a few utility functions for manipulating Java's {@link BigInteger}
28  * objects.
29  * @author Sylvain HallĂ©
30  */
31 public class BigIntegerFunctions
32 {
33  /**
34  * Converts a BigInteger into a string
35  */
36  public static class BigIntegerToString extends UnaryFunction<BigInteger,String>
37  {
38  public static final transient BigIntegerToString instance = new BigIntegerToString();
39 
40  BigIntegerToString()
41  {
42  super(BigInteger.class, String.class);
43  }
44 
45  @Override
46  public String getValue(BigInteger x) throws FunctionException
47  {
48  return x.toString();
49  }
50  }
51 
52  /**
53  * Adds two BigIntegers
54  */
55  public static class BigIntegerAdd extends BinaryFunction<BigInteger,BigInteger,BigInteger>
56  {
57  public static final transient BigIntegerAdd instance = new BigIntegerAdd();
58 
59  BigIntegerAdd()
60  {
61  super(BigInteger.class, BigInteger.class, BigInteger.class);
62  }
63 
64  @Override
65  public BigInteger getValue(BigInteger x, BigInteger y)
66  {
67  return x.add(y);
68  }
69 
70  @Override
71  public BigInteger getStartValue()
72  {
73  return BigInteger.ONE;
74  }
75  }
76 
77  /**
78  * Converts a string into a BigInteger
79  */
80  public static class StringToBigInteger extends UnaryFunction<String,BigInteger>
81  {
82  public static final transient StringToBigInteger instance = new StringToBigInteger();
83 
84  StringToBigInteger()
85  {
86  super(String.class, BigInteger.class);
87  }
88 
89  @Override
90  public BigInteger getValue(String x) throws FunctionException
91  {
92  return new BigInteger(x);
93  }
94  }
95 
96  /**
97  * Checks if a BigInteger is prime. This function uses a very slow method.
98  */
99  public static class IsPrime extends UnaryFunction<BigInteger,Boolean>
100  {
101  public static final transient IsPrime instance = new IsPrime();
102  private static final transient BigInteger TWO = new BigInteger("2");
103  private static final transient BigInteger THREE = new BigInteger("3");
104 
105  IsPrime()
106  {
107  super(BigInteger.class, Boolean.class);
108  }
109 
110  @Override
111  public Boolean getValue(BigInteger n) throws FunctionException
112  {
113  /* This algorithm was found on
114  * https://codereview.stackexchange.com/q/43490 */
115  if (n.compareTo(BigInteger.ONE) == 0 || n.compareTo(TWO) == 0)
116  {
117  return true;
118  }
119  BigInteger half=n.divide(TWO);
120  for (BigInteger i = THREE; i.compareTo(half) <= 0; i = i.add(TWO))
121  {
122 
123  if (n.mod(i).equals(BigInteger.ZERO))
124  {
125  return false;
126  }
127  }
128  return true;
129  }
130  }
131 
132  /**
133  * Increments a BigInteger by a specific value
134  */
135  public static class IncrementBigInteger extends UnaryFunction<BigInteger,BigInteger>
136  {
137  /**
138  * The increment
139  */
140  protected final BigInteger m_increment;
141 
142  /**
143  * Creates an instance of the BigInteger increment function
144  * @param increment The increment
145  */
146  public IncrementBigInteger(BigInteger increment)
147  {
148  super(BigInteger.class, BigInteger.class);
149  m_increment = increment;
150  }
151 
152  @Override
153  public BigInteger getValue(BigInteger x) throws FunctionException
154  {
155  return m_increment.add(x);
156  }
157  }
158 
159 
160 }
Contains a few utility functions for manipulating Java&#39;s BigInteger objects.