aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/ClassNode.java
blob: ce27694c1265f7515fc9a429d1c8aa3e6c466c7a (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package org.zwobble.couscous.ast;

import com.google.common.collect.Iterables;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.types.ScalarType;
import org.zwobble.couscous.types.Type;
import org.zwobble.couscous.util.ExtraIterables;

import java.util.List;
import java.util.Set;

import static org.zwobble.couscous.util.ExtraLists.concat;
import static org.zwobble.couscous.util.ExtraLists.eagerFlatMap;
import static org.zwobble.couscous.util.ExtraLists.list;

public class ClassNode implements TypeNode {
    public static ClassNodeBuilder builder(ScalarType name) {
        return new ClassNodeBuilder(name);
    }
    public static ClassNodeBuilder builder(String name) {
        return new ClassNodeBuilder(name);
    }

    public static ClassNode declareClass(
        ScalarType name,
        List<FormalTypeParameterNode> typeParameters,
        Set<Type> superTypes,
        List<FieldDeclarationNode> fields,
        List<StatementNode> staticConstructor,
        ConstructorNode constructor,
        List<MethodNode> methods,
        List<TypeNode> innerTypes
    )
    {
        return new ClassNode(name, typeParameters, superTypes, fields, staticConstructor, constructor, methods, innerTypes);
    }
    
    private final ScalarType name;
    private final List<FormalTypeParameterNode> typeParameters;
    private final Set<Type> superTypes;
    private final List<FieldDeclarationNode> fields;
    private final List<StatementNode> staticConstructor;
    private final ConstructorNode constructor;
    private final List<MethodNode> methods;
    private final List<TypeNode> innerTypes;

    public ClassNode(
        ScalarType name,
        List<FormalTypeParameterNode> typeParameters,
        Set<Type> superTypes,
        List<FieldDeclarationNode> fields,
        List<StatementNode> staticConstructor,
        ConstructorNode constructor,
        List<MethodNode> methodNodes,
        List<TypeNode> innerTypes
    )
    {
        this.name = name;
        this.typeParameters = typeParameters;
        this.superTypes = superTypes;
        this.fields = fields;
        this.staticConstructor = staticConstructor;
        this.constructor = constructor;
        methods = methodNodes;
        this.innerTypes = innerTypes;
    }

    @Override
    public ScalarType getName() {
        return name;
    }

    @Override
    public List<FormalTypeParameterNode> getTypeParameters() {
        return typeParameters;
    }

    @Override
    public Set<Type> getSuperTypes() {
        return superTypes;
    }

    public List<FieldDeclarationNode> getFields() {
        return fields;
    }

    public List<StatementNode> getStaticConstructor() {
        return staticConstructor;
    }

    public ConstructorNode getConstructor() {
        return constructor;
    }

    @Override
    public List<MethodNode> getMethods() {
        return methods;
    }

    @Override
    public List<TypeNode> getInnerTypes() {
        return innerTypes;
    }

    public String getSimpleName() {
        return name.getSimpleName();
    }

    @Override
    public int nodeType() {
        return NodeTypes.CLASS;
    }

    @Override
    public Iterable<? extends Node> childNodes() {
        return Iterables.concat(
            typeParameters,
            staticConstructor,
            ExtraIterables.of(constructor),
            fields,
            methods,
            innerTypes
        );
    }

    @Override
    public ClassNode transformSubtree(NodeTransformer transformer) {
        return new ClassNode(
            transformer.transform(name),
            transformer.transformFormalTypeParameters(typeParameters),
            transformer.transformTypes(superTypes),
            transformer.transformFields(fields),
            transformer.transformStatements(staticConstructor),
            transformer.transformConstructor(constructor),
            transformer.transformMethods(methods),
            eagerFlatMap(innerTypes, transformer::transformTypeDeclaration)
        );
    }

    @Override
    public TypeNode stripInnerTypes() {
        return withInnerTypes(list());
    }

    @Override
    public TypeNode addInnerTypes(List<TypeNode> types) {
        return withInnerTypes(concat(innerTypes, types));
    }

    private TypeNode withInnerTypes(List<TypeNode> types) {
        return new ClassNode(
            name,
            typeParameters,
            superTypes,
            fields,
            staticConstructor,
            constructor,
            methods,
            types
        );
    }

    @Override
    public String toString() {
        return "ClassNode(" +
            "name=" + name +
            ", typeParameters=" + typeParameters +
            ", superTypes=" + superTypes +
            ", fields=" + fields +
            ", staticConstructor=" + staticConstructor +
            ", constructor=" + constructor +
            ", methods=" + methods +
            ", innerTypes=" + innerTypes +
            ')';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ClassNode classNode = (ClassNode) o;

        if (!name.equals(classNode.name)) return false;
        if (!typeParameters.equals(classNode.typeParameters)) return false;
        if (!superTypes.equals(classNode.superTypes)) return false;
        if (!fields.equals(classNode.fields)) return false;
        if (!staticConstructor.equals(classNode.staticConstructor)) return false;
        if (!constructor.equals(classNode.constructor)) return false;
        if (!methods.equals(classNode.methods)) return false;
        return innerTypes.equals(classNode.innerTypes);

    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + typeParameters.hashCode();
        result = 31 * result + superTypes.hashCode();
        result = 31 * result + fields.hashCode();
        result = 31 * result + staticConstructor.hashCode();
        result = 31 * result + constructor.hashCode();
        result = 31 * result + methods.hashCode();
        result = 31 * result + innerTypes.hashCode();
        return result;
    }
}