| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.resolution.types; |
| 23 | |
| 24 | import java.util.*; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | /** |
| 28 | * A union type is defined in java as list of types separates by pipes. |
| 29 | * |
| 30 | * @author Federico Tomassetti |
| 31 | */ |
| 32 | public class ResolvedUnionType implements ResolvedType { |
| 33 | private List<ResolvedType> elements; |
| 34 | |
| 35 | public ResolvedUnionType(List<ResolvedType> elements) { |
| 36 | if (elements.size() < 2) { |
| 37 | throw new IllegalArgumentException("An union type should have at least two elements. This has " + elements.size()); |
| 38 | } |
| 39 | this.elements = new LinkedList<>(elements); |
| 40 | } |
| 41 | |
| 42 | public Optional<ResolvedReferenceType> getCommonAncestor() { |
| 43 | Optional<List<ResolvedReferenceType>> reduce = elements.stream() |
| 44 | .map(ResolvedType::asReferenceType) |
| 45 | .map(ResolvedReferenceType::getAllAncestors) |
| 46 | .reduce((a, b) -> { |
| 47 | ArrayList<ResolvedReferenceType> common = new ArrayList<>(a); |
| 48 | common.retainAll(b); |
| 49 | return common; |
| 50 | }); |
| 51 | return reduce.orElse(new ArrayList<>()).stream().findFirst(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean equals(Object o) { |
| 56 | if (this == o) return true; |
| 57 | if (o == null || getClass() != o.getClass()) return false; |
| 58 | |
| 59 | ResolvedUnionType that = (ResolvedUnionType) o; |
| 60 | |
| 61 | return new HashSet<>(elements).equals(new HashSet<>(that.elements)); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int hashCode() { |
| 66 | return new HashSet<>(elements).hashCode(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String describe() { |
| 71 | return String.join(" | ", elements.stream().map(ResolvedType::describe).collect(Collectors.toList())); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isAssignableBy(ResolvedType other) { |
| 76 | return elements.stream().allMatch(e -> e.isAssignableBy(other)); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean isUnionType() { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ResolvedUnionType asUnionType() { |
| 86 | return this; |
| 87 | } |
| 88 | } |
| 89 |
Members