1 package com.fasterxml.jackson.databind.type;
2
3 import java.lang.reflect.TypeVariable;
4 import java.util.Collection;
5
6 import com.fasterxml.jackson.databind.JavaType;
7
8
15 public class CollectionLikeType extends TypeBase
16 {
17 private static final long serialVersionUID = 1L;
18
19
22 protected final JavaType _elementType;
23
24
29
30 protected CollectionLikeType(Class<?> collT, TypeBindings bindings,
31 JavaType superClass, JavaType[] superInts, JavaType elemT,
32 Object valueHandler, Object typeHandler, boolean asStatic)
33 {
34 super(collT, bindings, superClass, superInts,
35 elemT.hashCode(), valueHandler, typeHandler, asStatic);
36 _elementType = elemT;
37 }
38
39
42 protected CollectionLikeType(TypeBase base, JavaType elemT)
43 {
44 super(base);
45 _elementType = elemT;
46 }
47
48
51 public static CollectionLikeType construct(Class<?> rawType, TypeBindings bindings,
52 JavaType superClass, JavaType[] superInts, JavaType elemT) {
53 return new CollectionLikeType(rawType, bindings, superClass, superInts, elemT,
54 null, null, false);
55 }
56
57
61 @Deprecated
62 public static CollectionLikeType construct(Class<?> rawType, JavaType elemT) {
63
64
65 TypeVariable<?>[] vars = rawType.getTypeParameters();
66 TypeBindings bindings;
67 if ((vars == null) || (vars.length != 1)) {
68 bindings = TypeBindings.emptyBindings();
69 } else {
70 bindings = TypeBindings.create(rawType, elemT);
71 }
72 return new CollectionLikeType(rawType, bindings,
73 _bogusSuperClass(rawType), null,
74 elemT, null, null, false);
75 }
76
77
83 public static CollectionLikeType upgradeFrom(JavaType baseType, JavaType elementType) {
84
85
86 if (baseType instanceof TypeBase) {
87 return new CollectionLikeType((TypeBase) baseType, elementType);
88 }
89 throw new IllegalArgumentException("Cannot upgrade from an instance of "+baseType.getClass());
90 }
91
92 @Override
93 @Deprecated
94 protected JavaType _narrow(Class<?> subclass) {
95 return new CollectionLikeType(subclass, _bindings,
96 _superClass, _superInterfaces, _elementType,
97 _valueHandler, _typeHandler, _asStatic);
98 }
99
100 @Override
101 public JavaType withContentType(JavaType contentType) {
102 if (_elementType == contentType) {
103 return this;
104 }
105 return new CollectionLikeType(_class, _bindings, _superClass, _superInterfaces,
106 contentType, _valueHandler, _typeHandler, _asStatic);
107 }
108
109 @Override
110 public CollectionLikeType withTypeHandler(Object h) {
111 return new CollectionLikeType(_class, _bindings,
112 _superClass, _superInterfaces, _elementType, _valueHandler, h, _asStatic);
113 }
114
115 @Override
116 public CollectionLikeType withContentTypeHandler(Object h)
117 {
118 return new CollectionLikeType(_class, _bindings,
119 _superClass, _superInterfaces, _elementType.withTypeHandler(h),
120 _valueHandler, _typeHandler, _asStatic);
121 }
122
123 @Override
124 public CollectionLikeType withValueHandler(Object h) {
125 return new CollectionLikeType(_class, _bindings,
126 _superClass, _superInterfaces, _elementType, h, _typeHandler, _asStatic);
127 }
128
129 @Override
130 public CollectionLikeType withContentValueHandler(Object h) {
131 return new CollectionLikeType(_class, _bindings,
132 _superClass, _superInterfaces, _elementType.withValueHandler(h),
133 _valueHandler, _typeHandler, _asStatic);
134 }
135
136 @Override
137 public JavaType withHandlersFrom(JavaType src) {
138 JavaType type = super.withHandlersFrom(src);
139 JavaType srcCt = src.getContentType();
140 if (srcCt != null) {
141 JavaType ct = _elementType.withHandlersFrom(srcCt);
142 if (ct != _elementType) {
143 type = type.withContentType(ct);
144 }
145 }
146 return type;
147 }
148
149 @Override
150 public CollectionLikeType withStaticTyping() {
151 if (_asStatic) {
152 return this;
153 }
154 return new CollectionLikeType(_class, _bindings,
155 _superClass, _superInterfaces, _elementType.withStaticTyping(),
156 _valueHandler, _typeHandler, true);
157 }
158
159 @Override
160 public JavaType refine(Class<?> rawType, TypeBindings bindings,
161 JavaType superClass, JavaType[] superInterfaces) {
162 return new CollectionLikeType(rawType, bindings,
163 superClass, superInterfaces, _elementType,
164 _valueHandler, _typeHandler, _asStatic);
165 }
166
167
172
173 @Override
174 public boolean isContainerType() { return true; }
175
176 @Override
177 public boolean isCollectionLikeType() { return true; }
178
179 @Override
180 public JavaType getContentType() { return _elementType; }
181
182 @Override
183 public Object getContentValueHandler() {
184 return _elementType.getValueHandler();
185 }
186
187 @Override
188 public Object getContentTypeHandler() {
189 return _elementType.getTypeHandler();
190 }
191
192 @Override
193 public boolean hasHandlers() {
194 return super.hasHandlers() || _elementType.hasHandlers();
195 }
196
197 @Override
198 public StringBuilder getErasedSignature(StringBuilder sb) {
199 return _classSignature(_class, sb, true);
200 }
201
202 @Override
203 public StringBuilder getGenericSignature(StringBuilder sb) {
204 _classSignature(_class, sb, false);
205 sb.append('<');
206 _elementType.getGenericSignature(sb);
207 sb.append(">;");
208 return sb;
209 }
210
211 @Override
212 protected String buildCanonicalName() {
213 StringBuilder sb = new StringBuilder();
214 sb.append(_class.getName());
215 if (_elementType != null) {
216 sb.append('<');
217 sb.append(_elementType.toCanonical());
218 sb.append('>');
219 }
220 return sb.toString();
221 }
222
223
228
229
235 public boolean isTrueCollectionType() {
236 return Collection.class.isAssignableFrom(_class);
237 }
238
239
244
245 @Override
246 public boolean equals(Object o)
247 {
248 if (o == this) return true;
249 if (o == null) return false;
250 if (o.getClass() != getClass()) return false;
251
252 CollectionLikeType other = (CollectionLikeType) o;
253 return (_class == other._class) && _elementType.equals(other._elementType);
254 }
255
256 @Override
257 public String toString()
258 {
259 return "[collection-like type; class "+_class.getName()+", contains "+_elementType+"]";
260 }
261
262 }
263