aboutsummaryrefslogtreecommitdiffstats
blob: ab4ebd7e119c23c5afdce0a1e7bd268303f7e24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 1998, 2013 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 ******************************************************************************/  
package org.eclipse.persistence.jaxb.compiler;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import org.eclipse.persistence.jaxb.javamodel.Helper;
import org.eclipse.persistence.jaxb.javamodel.JavaClass;

/**
 * INTERNAL:
 * <p><b>Purpose:</b>A specialized TypeInfo that stores additional information for a 
 * Java 5 Enumeration type. 
 * <p><b>Responsibilities:</b><ul>
 * <li>Hold onto the restriction base type for schema generation</li>
 * <li>Hold onto a map of Object Enum values to String values for Mapping generation</li>
 * </ul>
 * 
 * @see org.eclipse.persistence.jaxb.compiler.TypeInfo
 * @see org.eclipse.persistence.jaxb.AnnotationsProcessor
 * @author mmacivor
 *
 */
public class EnumTypeInfo extends TypeInfo {
    private String m_className;
    private QName m_restrictionBase;
    private List<String> m_fieldNames;
    private List<Object> m_xmlEnumValues;
    
    public EnumTypeInfo(Helper helper, JavaClass javaClass) {
        super(helper, javaClass);
        m_fieldNames = new ArrayList<String>();
        m_xmlEnumValues = new ArrayList<Object>();
    }
    
    public boolean isEnumerationType() {
        return true;
    }

    public String getClassName() {
        return m_className;
    }
    
    public void setClassName(String className) {
        m_className = className;
    }
    
    public QName getRestrictionBase() {
        return m_restrictionBase;
    }
    
    public void setRestrictionBase(QName restrictionBase) {
        m_restrictionBase = restrictionBase;
    }

    /**
     * Add a Java field name to XmlEnumValue pair.
     * 
     * @param fieldName
     * @param xmlEnumValue
     */
    public void addJavaFieldToXmlEnumValuePair(String fieldName, Object xmlEnumValue) {
    	m_fieldNames.add(fieldName);
    	m_xmlEnumValues.add(xmlEnumValue);
    }

    /**
     * Add a Java field name to XmlEnumValue pair.  If an entry exists at the specified 
     * with the same fieldName, its value will be overridden.  A value of true for 
     * 'override' will typically be used when performing overrides via XML metadata 
     * in XmlProcessor.
     * 
     * @param override
     * @param fieldName
     * @param xmlEnumValue
     */
    public void addJavaFieldToXmlEnumValuePair(boolean override, String fieldName, Object xmlEnumValue) {
        if (!override) {
            addJavaFieldToXmlEnumValuePair(fieldName, xmlEnumValue);
        } else {
            int idx = getIndexForJavaField(fieldName);
            if (idx == -1) {
                // the entry doesn't exist, so add a new one
                addJavaFieldToXmlEnumValuePair(fieldName, xmlEnumValue);
            } else {
                // entry already exists, so replace the existing value
                m_xmlEnumValues.remove(idx);
                m_xmlEnumValues.add(idx, xmlEnumValue);
            }
        }
    }

    public List<String> getFieldNames() {
        return m_fieldNames;
    }

    public List<Object> getXmlEnumValues() {
		return m_xmlEnumValues;
	}
    
    /**
     * Return the index in the fieldNames List for a given Java field 
     * name, or -1 if it doesn't exist.
     * 
     * @param fieldName
     * @return
     */
    private int getIndexForJavaField(String fieldName) {
        for (int i=0; i<m_fieldNames.size(); i++) {
            if (m_fieldNames.get(i).equals(fieldName)) {
                return i;
            }
        }
        return -1;
    }
}