1 package com.fasterxml.jackson.databind.type;
2
3 import java.lang.reflect.TypeVariable;
4
5 import com.fasterxml.jackson.databind.JavaType;
6
7 /**
8  * Type that represents "true" Java Map types.
9  */

10 public final class MapType extends MapLikeType
11 {
12     private static final long serialVersionUID = 1L;
13
14     /*
15     /**********************************************************
16     /* Life-cycle
17     /**********************************************************
18      */

19
20     private MapType(Class<?> mapType, TypeBindings bindings,
21             JavaType superClass, JavaType[] superInts, JavaType keyT, JavaType valueT,
22             Object valueHandler, Object typeHandler, boolean asStatic) {
23         super(mapType, bindings, superClass, superInts,
24                 keyT, valueT, valueHandler, typeHandler, asStatic);
25     }
26
27     /**
28      * @since 2.7
29      */

30     protected MapType(TypeBase base, JavaType keyT, JavaType valueT) {
31         super(base, keyT, valueT);
32     }
33
34     /**
35      * @since 2.7
36      */

37     public static MapType construct(Class<?> rawType, TypeBindings bindings,
38             JavaType superClass, JavaType[] superInts,
39             JavaType keyT, JavaType valueT) {
40         return new MapType(rawType, bindings, superClass, superInts, keyT, valueT, nullnullfalse);
41     }
42     
43     @Deprecated // since 2.7
44     public static MapType construct(Class<?> rawType, JavaType keyT, JavaType valueT)
45     {
46         // First: may need to fabricate TypeBindings (needed for refining into
47         // concrete collection types, as per [databind#1102])
48         TypeVariable<?>[] vars = rawType.getTypeParameters();
49         TypeBindings bindings;
50         if ((vars == null) || (vars.length != 2)) {
51             bindings = TypeBindings.emptyBindings();
52         } else {
53             bindings = TypeBindings.create(rawType, keyT, valueT);
54         }
55         // !!! TODO: Wrong, does have supertypes
56         return new MapType(rawType, bindings, _bogusSuperClass(rawType), null,
57                 keyT, valueT, nullnullfalse);
58     }
59
60     @Deprecated // since 2.7
61     @Override
62     protected JavaType _narrow(Class<?> subclass) {
63         return new MapType(subclass, _bindings,
64                 _superClass, _superInterfaces, _keyType, _valueType,
65                 _valueHandler, _typeHandler, _asStatic);
66     }
67
68     @Override
69     public MapType withTypeHandler(Object h) {
70         return new MapType(_class, _bindings,
71                 _superClass, _superInterfaces, _keyType, _valueType, _valueHandler, h, _asStatic);
72     }
73
74     @Override
75     public MapType withContentTypeHandler(Object h)
76     {
77         return new MapType(_class, _bindings,
78                 _superClass, _superInterfaces, _keyType, _valueType.withTypeHandler(h),
79                 _valueHandler, _typeHandler, _asStatic);
80     }
81     
82     @Override
83     public MapType withValueHandler(Object h) {
84         return new MapType(_class, _bindings,
85                 _superClass, _superInterfaces, _keyType, _valueType, h, _typeHandler, _asStatic);
86     }
87
88     @Override
89     public MapType withContentValueHandler(Object h) {
90         return new MapType(_class, _bindings,
91                 _superClass, _superInterfaces, _keyType, _valueType.withValueHandler(h),
92                 _valueHandler, _typeHandler, _asStatic);
93     }
94
95     @Override
96     public MapType withStaticTyping() {
97         if (_asStatic) {
98             return this;
99         }
100         return new MapType(_class, _bindings,
101                 _superClass, _superInterfaces, _keyType.withStaticTyping(), _valueType.withStaticTyping(),
102                 _valueHandler, _typeHandler, true);
103     }
104
105     @Override
106     public JavaType withContentType(JavaType contentType) {
107         if (_valueType == contentType) {
108             return this;
109         }
110         return new MapType(_class, _bindings, _superClass, _superInterfaces,
111                 _keyType, contentType, _valueHandler, _typeHandler, _asStatic);
112     }
113     
114     @Override
115     public MapType withKeyType(JavaType keyType) {
116         if (keyType == _keyType) {
117             return this;
118         }
119         return new MapType(_class, _bindings, _superClass, _superInterfaces,
120                 keyType, _valueType, _valueHandler, _typeHandler, _asStatic);
121     }
122
123     @Override
124     public JavaType refine(Class<?> rawType, TypeBindings bindings,
125             JavaType superClass, JavaType[] superInterfaces) {
126         return new MapType(rawType, bindings,
127                 superClass, superInterfaces, _keyType, _valueType,
128                 _valueHandler, _typeHandler, _asStatic);
129     }
130
131     /*
132     /**********************************************************
133     /* Extended API
134     /**********************************************************
135      */

136     
137     @Override
138     public MapType withKeyTypeHandler(Object h)
139     {
140         return new MapType(_class, _bindings,
141                 _superClass, _superInterfaces, _keyType.withTypeHandler(h), _valueType,
142                 _valueHandler, _typeHandler, _asStatic);
143     }
144
145     @Override
146     public MapType withKeyValueHandler(Object h) {
147         return new MapType(_class, _bindings,
148                 _superClass, _superInterfaces, _keyType.withValueHandler(h), _valueType,
149                 _valueHandler, _typeHandler, _asStatic);
150     }
151
152     /*
153     /**********************************************************
154     /* Standard methods
155     /**********************************************************
156      */

157
158     @Override
159     public String toString()
160     {
161         return "[map type; class "+_class.getName()+", "+_keyType+" -> "+_valueType+"]";
162     }
163 }
164