aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/MethodNode.java
blob: 86ae64906bda4eec44fb9255e0b8c9b40121af88 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package org.zwobble.couscous.ast;

import com.google.common.collect.ImmutableList;
import org.zwobble.couscous.ast.visitors.NodeMapper;

import java.util.List;
import java.util.function.Function;

import static org.zwobble.couscous.util.ExtraLists.eagerMap;

public class MethodNode implements CallableNode {
    public static Builder staticMethod(String name) {
        return builder(name).isStatic(true);
    }
    public static Builder builder(String name) {
        return new Builder(name);
    }

    public static class Builder {
        private final ImmutableList.Builder<AnnotationNode> annotations =
            ImmutableList.builder();
        private boolean isStatic = false;
        private final String name;
        private final ImmutableList.Builder<FormalArgumentNode> arguments =
            ImmutableList.builder();
        private final ImmutableList.Builder<StatementNode> body =
            ImmutableList.builder();
        
        public Builder(String name) {
            this.name = name;
        }
        
        public Builder annotations(List<AnnotationNode> annotations) {
            this.annotations.addAll(annotations);
            return this;
        }
        
        public Builder isStatic(boolean isStatic) {
            this.isStatic = isStatic;
            return this;
        }
        
        public Builder argument(FormalArgumentNode argument) {
            arguments.add(argument);
            return this;
        }
        
        public Builder arguments(List<FormalArgumentNode> arguments) {
            this.arguments.addAll(arguments);
            return this;
        }
        
        public Builder statement(StatementNode statement) {
            this.body.add(statement);
            return this;
        }
        
        public Builder body(List<StatementNode> statements) {
            this.body.addAll(statements);
            return this;
        }
        
        public MethodNode build() {
            return new MethodNode(
                annotations.build(),
                isStatic,
                name,
                arguments.build(),
                body.build());
        }
    }
    
    public static MethodNode method(
            List<AnnotationNode> annotations,
            boolean isStatic,
            String name,
            List<FormalArgumentNode> arguments, 
            List<StatementNode> body) {
        return new MethodNode(annotations, isStatic, name, arguments, body);
    }
    
    private final List<AnnotationNode> annotations;
    private final boolean isStatic;
    private final String name;
    private final List<FormalArgumentNode> arguments; 
    private final List<StatementNode> body;
    
    private MethodNode(
            List<AnnotationNode> annotations,
            boolean isStatic,
            String name,
            List<FormalArgumentNode> arguments, 
            List<StatementNode> body) {
        this.annotations = annotations;
        this.isStatic = isStatic;
        this.name = name;
        this.arguments = arguments;
        this.body = body;
    }
    
    public List<AnnotationNode> getAnnotations() {
        return annotations;
    }
    
    public boolean isStatic() {
        return isStatic;
    }
    
    public String getName() {
        return name;
    }
    
    public List<FormalArgumentNode> getArguments() {
        return arguments;
    }
    
    public List<StatementNode> getBody() {
        return body;
    }

    public MethodSignature signature() {
        return new MethodSignature(
            name,
            eagerMap(arguments, argument -> argument.getType()));
    }

    public MethodNode mapBody(Function<List<StatementNode>, List<StatementNode>> function) {
        return new MethodNode(annotations, isStatic, name, arguments, function.apply(body));
    }

    @Override
    public <T> T accept(NodeMapper<T> visitor) {
        return visitor.visit(this);
    }
    
    @Override
    public String toString() {
        return "MethodNode(annotations=" + annotations + ", isStatic="
               + isStatic + ", name=" + name + ", arguments=" + arguments
               + ", body=" + body + ")";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                 + ((annotations == null) ? 0 : annotations.hashCode());
        result = prime * result
                 + ((arguments == null) ? 0 : arguments.hashCode());
        result = prime * result + ((body == null) ? 0 : body.hashCode());
        result = prime * result + (isStatic ? 1231 : 1237);
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MethodNode other = (MethodNode) obj;
        if (annotations == null) {
            if (other.annotations != null)
                return false;
        } else if (!annotations.equals(other.annotations))
            return false;
        if (arguments == null) {
            if (other.arguments != null)
                return false;
        } else if (!arguments.equals(other.arguments))
            return false;
        if (body == null) {
            if (other.body != null)
                return false;
        } else if (!body.equals(other.body))
            return false;
        if (isStatic != other.isStatic)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}