aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java
blob: cd324b93cee90dc3222ba866bf042831bde54524 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.zwobble.couscous.ast;

import java.util.Optional;
import java.util.function.Function;

import com.google.common.collect.ImmutableList;

public class ClassNodeBuilder {
    private final TypeName name;
    private final ImmutableList.Builder<FieldDeclarationNode> fields;
    private Optional<ConstructorNode> constructor;
    private final ImmutableList.Builder<MethodNode> methods;
    
    public ClassNodeBuilder(String name) {
        this.name = TypeName.of(name);
        this.fields = ImmutableList.builder();
        this.constructor = Optional.empty();
        this.methods = ImmutableList.builder();
    }

    public ClassNodeBuilder field(String name, TypeName type) {
        this.fields.add(FieldDeclarationNode.field(name, type));
        return this;
    }
    
    public ClassNodeBuilder constructor(Function<ConstructorBuilder, ConstructorBuilder> build) {
        this.constructor = Optional.of(build.apply(new ConstructorBuilder()).build());
        return this;
    }
    
    public ClassNodeBuilder method(MethodNode method) {
        this.methods.add(method);
        return this;
    }
    
    public ClassNodeBuilder method(String name, Function<MethodBuilder, MethodBuilder> build) {
        this.methods.add(build.apply(new MethodBuilder(name)).build());
        return this;
    }
    
    public ClassNode build() {
        return new ClassNode(
            name,
            constructor.orElse(new ConstructorBuilder().build()),
            methods.build());
    }
    
    public class ConstructorBuilder {
        private final ImmutableList.Builder<FormalArgumentNode> arguments;
        private final ImmutableList.Builder<StatementNode> statements;
        
        public ConstructorBuilder() {
            this.arguments = ImmutableList.builder();
            this.statements = ImmutableList.builder();
        }
        
        public ThisReferenceNode thisReference() {
            return ThisReferenceNode.thisReference(name);
        }
        
        public ConstructorBuilder argument(FormalArgumentNode argument) {
            arguments.add(argument);
            return this;
        }
        
        public ConstructorBuilder statement(StatementNode statement) {
            statements.add(statement);
            return this;
        }
        
        public ConstructorNode build() {
            return ConstructorNode.constructor(
                arguments.build(),
                statements.build());
        }
    }
    
    public class MethodBuilder {
        private final String methodName;
        private final ImmutableList.Builder<StatementNode> statements;

        public MethodBuilder(String name) {
            this.methodName = name;
            this.statements = ImmutableList.builder();
        }
        
        public ThisReferenceNode thisReference() {
            return ThisReferenceNode.thisReference(name);
        }
        
        public MethodBuilder statement(StatementNode statement) {
            statements.add(statement);
            return this;
        }

        public MethodNode build() {
            return MethodNode.builder()
                .name(methodName)
                .body(statements.build())
                .build();
        }
    }
}