18 package network.httppush.twinprimes;
20 import java.math.BigInteger;
22 import ca.uqac.lif.cep.functions.BinaryFunction;
23 import ca.uqac.lif.cep.functions.FunctionException;
24 import ca.uqac.lif.cep.functions.UnaryFunction;
36 public static class BigIntegerToString
extends UnaryFunction<BigInteger,String>
38 public static final transient BigIntegerToString instance =
new BigIntegerToString();
42 super(BigInteger.class, String.class);
46 public String getValue(BigInteger x)
throws FunctionException
55 public static class BigIntegerAdd
extends BinaryFunction<BigInteger,BigInteger,BigInteger>
57 public static final transient BigIntegerAdd instance =
new BigIntegerAdd();
61 super(BigInteger.class, BigInteger.class, BigInteger.class);
65 public BigInteger getValue(BigInteger x, BigInteger y)
71 public BigInteger getStartValue()
73 return BigInteger.ONE;
80 public static class StringToBigInteger
extends UnaryFunction<String,BigInteger>
82 public static final transient StringToBigInteger instance =
new StringToBigInteger();
86 super(String.class, BigInteger.class);
90 public BigInteger getValue(String x)
throws FunctionException
92 return new BigInteger(x);
99 public static class IsPrime
extends UnaryFunction<BigInteger,Boolean>
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");
107 super(BigInteger.class, Boolean.class);
111 public Boolean getValue(BigInteger n)
throws FunctionException
115 if (n.compareTo(BigInteger.ONE) == 0 || n.compareTo(TWO) == 0)
119 BigInteger half=n.divide(TWO);
120 for (BigInteger i = THREE; i.compareTo(half) <= 0; i = i.add(TWO))
123 if (n.mod(i).equals(BigInteger.ZERO))
135 public static class IncrementBigInteger
extends UnaryFunction<BigInteger,BigInteger>
140 protected final BigInteger m_increment;
146 public IncrementBigInteger(BigInteger increment)
148 super(BigInteger.class, BigInteger.class);
149 m_increment = increment;
153 public BigInteger getValue(BigInteger x)
throws FunctionException
155 return m_increment.add(x);
Contains a few utility functions for manipulating Java's BigInteger objects.