package au.id.zancanaro.javacheck.object; import au.id.zancanaro.javacheck.Generator; import au.id.zancanaro.javacheck.ShrinkTree; import java.util.Random; import static au.id.zancanaro.javacheck.Generators.asciiCharacter; import static au.id.zancanaro.javacheck.Generators.stringOf; public class MyObject { public final String string; public final int value; public final SubObject subObject; public MyObject(String string, int value, SubObject subObject) { this.string = string; this.value = value; this.subObject = subObject; } @UseForGeneration public MyObject( @UseGenerator(StringGenerator.class) String string, SubObject subObject) { this(string, string.length(), subObject); } public static class StringGenerator implements Generator { public final Generator gen = stringOf(asciiCharacter()); @Override public ShrinkTree generate(Random random, int size) { return gen.generate(random, size); } } public MyObject add(MyObject other) { return new MyObject( this.string + other.string, this.value + other.value, this.subObject.add(other.subObject)); } @Override public String toString() { return "{" + string + ", " + value + ", " + subObject + "}"; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MyObject myObject = (MyObject) o; return value == myObject.value && string.equals(myObject.string) && subObject.equals(myObject.subObject); } @Override public int hashCode() { int result = string.hashCode(); result = 31 * result + value; result = 31 * result + subObject.hashCode(); return result; } }