
fj.test.Cogen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava-quickcheck_1.8 Show documentation
Show all versions of functionaljava-quickcheck_1.8 Show documentation
Functional Java is an open source library that supports closures for the Java programming language
The newest version!
package fj.test;
import fj.*;
import static fj.Function.curry;
import static fj.P.p;
import fj.data.*;
import static fj.data.Array.iterableArray;
import static fj.data.List.fromString;
import static fj.data.List.nil;
import static fj.test.Variant.variant;
import static java.lang.Double.doubleToRawLongBits;
import static java.lang.Float.floatToRawIntBits;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Properties;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
/**
* Transforms a type and a generator to produce a new generator. This function is used to generate
* {@link Gen gen} functions.
*
* @version %build.number%
*/
public abstract class Cogen {
/**
* Transforms the given value and generator to a new generator with a high probability of being
* independent.
*
* @param a The value to produce the generator from.
* @param g The generator to produce the new generator from.
* @return A new generator with a high probability of being independent.
*/
public abstract Gen cogen(A a, Gen g);
/**
* A curried version of {@link #cogen(Object, Gen)}.
*
* @param a The value to produce the generator from.
* @return A curried version of {@link #cogen(Object, Gen)}.
*/
public final F, Gen> cogen(final A a) {
return g -> cogen(a, g);
}
/**
* Composes the given function with this cogen to produce a new cogen.
*
* @param f The function to compose.
* @return A new cogen composed with the given function.
*/
public final Cogen compose(final F f) {
return new Cogen() {
public Gen cogen(final B b, final Gen g) {
return Cogen.this.cogen(f.f(b), g);
}
};
}
/**
* Contra-maps this cogen using the given function.
*
* @param f The function to co-map with.
* @return A contra-mapped cogen.
*/
public final Cogen contramap(final F f) {
return new Cogen() {
public Gen cogen(final B b, final Gen g) {
return Cogen.this.cogen(f.f(b), g);
}
};
}
/**
* A cogen for a function.
*
* @param a A gen for the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function.
*/
public static Cogen> cogenF(final Gen a, final Cogen c) {
return new Cogen>() {
public Gen cogen(final F f, final Gen g) {
return a.bind(a1 -> c.cogen(f.f(a1), g));
}
};
}
/**
* A cogen for a function-2.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-2.
*/
public static Cogen> cogenF2(final Gen aa, final Gen ab,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F2 f, final Gen g) {
return cogenF(aa, cogenF(ab, c)).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-3.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-3.
*/
public static Cogen> cogenF3(final Gen aa, final Gen ab,
final Gen ac, final Cogen c) {
return new Cogen>() {
public Gen cogen(final F3 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, c))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-4.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-4.
*/
public static Cogen> cogenF4(final Gen aa, final Gen ab,
final Gen ac, final Gen ad,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F4 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, c)))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-5.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-5.
*/
public static Cogen> cogenF5(final Gen aa,
final Gen ab,
final Gen ac,
final Gen ad,
final Gen ae,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F5 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, c))))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-6.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-6.
*/
public static Cogen> cogenF6(final Gen aa,
final Gen ab,
final Gen ac,
final Gen ad,
final Gen ae,
final Gen af,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F6 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, c)))))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-7.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param ag A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-7.
*/
public static Cogen> cogenF7(final Gen aa,
final Gen ab,
final Gen ac,
final Gen ad,
final Gen ae,
final Gen af,
final Gen ag,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F7 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, cogenF(ag, c)))))))
.cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-8.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param ag A gen for part of the domain of the function.
* @param ah A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-8.
*/
public static Cogen> cogenF8(final Gen aa,
final Gen ab,
final Gen ac,
final Gen ad,
final Gen ae,
final Gen af,
final Gen ag,
final Gen ah,
final Cogen c) {
return new Cogen>() {
public Gen cogen(final F8 f, final Gen g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, cogenF(ag, cogenF(ah, c))))))))
.cogen(curry(f), g);
}
};
}
/**
* A cogen for booleans.
*/
public static final Cogen cogenBoolean = new Cogen() {
public Gen cogen(final Boolean b, final Gen g) {
return variant(b ? 0 : 1, g);
}
};
/**
* A cogen for integers.
*/
public static final Cogen cogenInteger = new Cogen() {
public Gen cogen(final Integer i, final Gen g) {
return variant(i >= 0 ? 2 * i : -2 * i + 1, g);
}
};
/**
* A cogen for bytes.
*/
public static final Cogen cogenByte = new Cogen() {
public Gen cogen(final Byte b, final Gen g) {
return variant(b >= 0 ? 2 * b : -2 * b + 1, g);
}
};
/**
* A cogen for shorts.
*/
public static final Cogen cogenShort = new Cogen() {
public Gen cogen(final Short s, final Gen g) {
return variant(s >= 0 ? 2 * s : -2 * s + 1, g);
}
};
/**
* A cogen for longs.
*/
public static final Cogen cogenLong = new Cogen() {
public Gen cogen(final Long l, final Gen g) {
return variant(l >= 0L ? 2L * l : -2L * l + 1L, g);
}
};
/**
* A cogen for characters.
*/
public static final Cogen cogenCharacter = new Cogen() {
public Gen cogen(final Character c, final Gen g) {
return variant(c << 1, g);
}
};
/**
* A cogen for floats.
*/
public static final Cogen cogenFloat = new Cogen() {
public Gen cogen(final Float f, final Gen g) {
return cogenInteger.cogen(floatToRawIntBits(f), g);
}
};
/**
* A cogen for doubles.
*/
public static final Cogen cogenDouble = new Cogen() {
public Gen cogen(final Double d, final Gen g) {
return cogenLong.cogen(doubleToRawLongBits(d), g);
}
};
/**
* A cogen for the optional value.
*
* @param ca A cogen for the type of the optional value.
* @return A cogen for the optional value.
*/
public static Cogen