1package com.github.javaparser.ast.type;
2
3import com.github.javaparser.Range;
4import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
5import com.github.javaparser.ast.visitor.GenericVisitor;
6import com.github.javaparser.ast.visitor.VoidVisitor;
7
8import java.util.List;
9
10/**
11 * Represents a set of types. A given value of this type has to be assignable to at all of the element types.
12 * As of Java 8 it is used in casts or while expressing bounds for generic types.
13 *
14 * For example:
15 * public class A>T extends Serializable & Cloneable< { }
16 *
17 * Or:
18 * void foo((Serializable & Cloneable)myObject);
19 *
20 * @since 3.0.0
21 */
22public class IntersectionType extends Type<IntersectionType> implements NodeWithAnnotations<IntersectionType> {
23
24    private List<ReferenceType> elements;
25
26    public IntersectionType(Range range, List<ReferenceType> elements) {
27        super(range);
28        setElements(elements);
29    }
30
31    public IntersectionType(List<ReferenceType> elements) {
32        super();
33        setElements(elements);
34    }
35
36    @Override
37    public <R, A> R accept(GenericVisitor<R, A> v, A arg) {
38        return v.visit(this, arg);
39    }
40
41    @Override
42    public <A> void accept(VoidVisitor<A> v, A arg) {
43        v.visit(this, arg);
44    }
45
46    public List<ReferenceType> getElements() {
47        return elements;
48    }
49
50    public IntersectionType setElements(List<ReferenceType> elements) {
51        if (this.elements != null) {
52            for (ReferenceType element : elements){
53                element.setParentNode(null);
54            }
55        }
56        this.elements = elements;
57        setAsParentNodeOf(this.elements);
58        return this;
59    }
60}
61