1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.bind.v2.model.impl;
27
28import java.util.Arrays;
29import java.util.Collection;
30import java.util.Map;
31
32import javax.xml.bind.annotation.XmlElementWrapper;
33import javax.xml.namespace.QName;
34
35import com.sun.xml.internal.bind.v2.model.core.MapPropertyInfo;
36import com.sun.xml.internal.bind.v2.model.core.NonElement;
37import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
38import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
39
40/**
41 * @author Kohsuke Kawaguchi
42 */
43class MapPropertyInfoImpl<T,C,F,M> extends PropertyInfoImpl<T,C,F,M> implements MapPropertyInfo<T,C> {
44
45    private final QName xmlName;
46    private boolean nil;
47    private final T keyType;
48    private final T valueType;
49
50    // laziy computed to handle cyclic references
51    private NonElement<T,C> keyTypeInfo;
52    private NonElement<T,C> valueTypeInfo;
53
54
55    public MapPropertyInfoImpl(ClassInfoImpl<T,C,F,M> ci, PropertySeed<T,C,F,M> seed) {
56        super(ci, seed);
57
58        XmlElementWrapper xe = seed.readAnnotation(XmlElementWrapper.class);
59        xmlName = calcXmlName(xe);
60        nil = xe!=null && xe.nillable();
61
62        T raw = getRawType();
63        T bt = nav().getBaseClass(raw, nav().asDecl(Map.class) );
64        assert bt!=null;    // Map property is only for Maps
65
66        if(nav().isParameterizedType(bt)) {
67            keyType = nav().getTypeArgument(bt,0);
68            valueType = nav().getTypeArgument(bt,1);
69        } else {
70            keyType = valueType = nav().ref(Object.class);
71        }
72    }
73
74    public Collection<? extends TypeInfo<T,C>> ref() {
75        return Arrays.asList(getKeyType(),getValueType());
76    }
77
78    public final PropertyKind kind() {
79        return PropertyKind.MAP;
80    }
81
82    public QName getXmlName() {
83        return xmlName;
84    }
85
86    public boolean isCollectionNillable() {
87        return nil;
88    }
89
90    public NonElement<T,C> getKeyType() {
91        if(keyTypeInfo==null)
92            keyTypeInfo = getTarget(keyType);
93        return keyTypeInfo;
94    }
95
96    public NonElement<T,C> getValueType() {
97        if(valueTypeInfo==null)
98            valueTypeInfo = getTarget(valueType);
99        return valueTypeInfo;
100    }
101
102    public NonElement<T,C> getTarget(T type) {
103        assert parent.builder!=null : "this method must be called during the build stage";
104        return parent.builder.getTypeInfo(type,this);
105    }
106}
107