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.parametrization; |
23 | |
24 | import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; |
25 | import com.github.javaparser.resolution.types.ResolvedType; |
26 | import com.github.javaparser.resolution.types.ResolvedTypeVariable; |
27 | |
28 | import java.util.*; |
29 | |
30 | /** |
31 | * A map of values associated to TypeParameters. |
32 | * |
33 | * @author Federico Tomassetti |
34 | */ |
35 | public class ResolvedTypeParametersMap { |
36 | |
37 | public static class Builder { |
38 | private Map<String, ResolvedType> nameToValue; |
39 | private Map<String, ResolvedTypeParameterDeclaration> nameToDeclaration; |
40 | |
41 | public Builder() { |
42 | nameToValue = new HashMap<>(); |
43 | nameToDeclaration = new HashMap<>(); |
44 | } |
45 | |
46 | private Builder(Map<String, ResolvedType> nameToValue, |
47 | Map<String, ResolvedTypeParameterDeclaration> nameToDeclaration) { |
48 | this.nameToValue = new HashMap<>(); |
49 | this.nameToValue.putAll(nameToValue); |
50 | this.nameToDeclaration = new HashMap<>(); |
51 | this.nameToDeclaration.putAll(nameToDeclaration); |
52 | } |
53 | |
54 | public ResolvedTypeParametersMap build() { |
55 | return new ResolvedTypeParametersMap(nameToValue, nameToDeclaration); |
56 | } |
57 | |
58 | public Builder setValue(ResolvedTypeParameterDeclaration typeParameter, |
59 | ResolvedType value) { |
60 | // TODO: we shouldn't just silently overwrite existing types! |
61 | String qualifiedName = typeParameter.getQualifiedName(); |
62 | nameToValue.put(qualifiedName, value); |
63 | nameToDeclaration.put(qualifiedName, typeParameter); |
64 | return this; |
65 | } |
66 | } |
67 | |
68 | @Override |
69 | public boolean equals(Object o) { |
70 | if (this == o) return true; |
71 | if (!(o instanceof ResolvedTypeParametersMap)) return false; |
72 | |
73 | ResolvedTypeParametersMap that = (ResolvedTypeParametersMap) o; |
74 | |
75 | return nameToValue.equals(that.nameToValue) && nameToDeclaration.equals(that.nameToDeclaration); |
76 | |
77 | } |
78 | |
79 | @Override |
80 | public int hashCode() { |
81 | return nameToValue.hashCode(); |
82 | } |
83 | |
84 | @Override |
85 | public String toString() { |
86 | return "TypeParametersMap{" + |
87 | "nameToValue=" + nameToValue + |
88 | '}'; |
89 | } |
90 | |
91 | private Map<String, ResolvedType> nameToValue; |
92 | private Map<String, ResolvedTypeParameterDeclaration> nameToDeclaration; |
93 | |
94 | public static ResolvedTypeParametersMap empty() { |
95 | return new Builder().build(); |
96 | } |
97 | |
98 | private ResolvedTypeParametersMap(Map<String, ResolvedType> nameToValue, |
99 | Map<String, ResolvedTypeParameterDeclaration> nameToDeclaration) { |
100 | this.nameToValue = new HashMap<>(); |
101 | this.nameToValue.putAll(nameToValue); |
102 | this.nameToDeclaration = new HashMap<>(); |
103 | this.nameToDeclaration.putAll(nameToDeclaration); |
104 | } |
105 | |
106 | public ResolvedType getValue(ResolvedTypeParameterDeclaration typeParameter) { |
107 | String qualifiedName = typeParameter.getQualifiedName(); |
108 | if (nameToValue.containsKey(qualifiedName)) { |
109 | return nameToValue.get(qualifiedName); |
110 | } else { |
111 | return new ResolvedTypeVariable(typeParameter); |
112 | } |
113 | } |
114 | |
115 | public Optional<ResolvedType> getValueBySignature(String signature) { |
116 | if (nameToValue.containsKey(signature)) { |
117 | return Optional.of(nameToValue.get(signature)); |
118 | } else { |
119 | return Optional.empty(); |
120 | } |
121 | } |
122 | |
123 | public List<String> getNames(){ |
124 | return new ArrayList<>(nameToValue.keySet()); |
125 | } |
126 | |
127 | public List<ResolvedType> getTypes(){ |
128 | return new ArrayList<>(nameToValue.values()); |
129 | } |
130 | |
131 | public Builder toBuilder() { |
132 | return new Builder(nameToValue, nameToDeclaration); |
133 | } |
134 | |
135 | public boolean isEmpty() { |
136 | return nameToValue.isEmpty(); |
137 | } |
138 | |
139 | public ResolvedType replaceAll(ResolvedType type) { |
140 | Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>(); |
141 | for (ResolvedTypeParameterDeclaration typeParameterDeclaration : this.nameToDeclaration.values()) { |
142 | type = type.replaceTypeVariables(typeParameterDeclaration, getValue(typeParameterDeclaration), inferredTypes); |
143 | } |
144 | return type; |
145 | } |
146 | } |
147 |
Members