summaryrefslogtreecommitdiff
path: root/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java
blob: 824fd8de46d1e9fd31452c4529ae40b7912e6154 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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<Integer> subObject;

    public MyObject(String string, int value, SubObject<Integer> subObject) {
        this.string = string;
        this.value = value;
        this.subObject = subObject;
    }

    @UseForGeneration
    public MyObject(
            @UseGenerator(StringGenerator.class) String string,
            SubObject<Integer> subObject) {
        this(string, string.length(), subObject);
    }

    public static class StringGenerator implements Generator<String> {
        public final Generator<String> gen = stringOf(asciiCharacter());

        @Override
        public ShrinkTree<String> 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;
    }
}