1/*
2 * Copyright (c) 1997, 2013, 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.tools.internal.ws.processor.model.java;
27
28import com.sun.tools.internal.ws.processor.model.ModelException;
29
30import java.util.*;
31
32/**
33 *
34 * @author WS Development Team
35 */
36public class JavaStructureType extends JavaType {
37
38    public JavaStructureType() {}
39
40    public JavaStructureType(String name, boolean present, Object owner) {
41        super(name, present, "null");
42        this.owner = owner;
43    }
44
45    public void add(JavaStructureMember m) {
46        if (membersByName.containsKey(m.getName())) {
47            throw new ModelException("model.uniqueness.javastructuretype",
48                new Object[] {m.getName(), getRealName()});
49        }
50        members.add(m);
51        membersByName.put(m.getName(), m);
52    }
53
54
55    public JavaStructureMember getMemberByName(String name) {
56        if (membersByName.size() != members.size()) {
57            initializeMembersByName();
58        }
59        return membersByName.get(name);
60    }
61
62    public Iterator getMembers() {
63        return members.iterator();
64    }
65
66    public int getMembersCount() {
67        return members.size();
68    }
69
70    /* serialization */
71    public List<JavaStructureMember> getMembersList() {
72        return members;
73    }
74
75    /* serialization */
76    public void setMembersList(List<JavaStructureMember> l) {
77        members = l;
78    }
79
80    private void initializeMembersByName() {
81        membersByName = new HashMap<String, JavaStructureMember>();
82        if (members != null) {
83            for (JavaStructureMember m : members) {
84                if (m.getName() != null &&
85                    membersByName.containsKey(m.getName())) {
86
87                    throw new ModelException("model.uniqueness");
88                }
89                membersByName.put(m.getName(), m);
90            }
91        }
92    }
93
94    public boolean isAbstract() {
95        return isAbstract;
96    }
97
98    public void setAbstract(boolean isAbstract) {
99        this.isAbstract = isAbstract;
100    }
101
102    public JavaStructureType getSuperclass() {
103        return superclass;
104    }
105
106    public void setSuperclass(JavaStructureType superclassType) {
107        superclass = superclassType;
108    }
109
110    public void addSubclass(JavaStructureType subclassType) {
111        subclasses.add(subclassType);
112        subclassType.setSuperclass(this);
113    }
114
115    public Iterator getSubclasses() {
116        if (subclasses == null || subclasses.size() == 0) {
117            return null;
118        }
119        return subclasses.iterator();
120    }
121
122    public Set getSubclassesSet() {
123        return subclasses;
124    }
125
126    /* serialization */
127    public void setSubclassesSet(Set s) {
128        subclasses = s;
129        for (Iterator iter = s.iterator(); iter.hasNext();) {
130            ((JavaStructureType) iter.next()).setSuperclass(this);
131        }
132    }
133
134    public Iterator getAllSubclasses() {
135        Set subs = getAllSubclassesSet();
136        if (subs.size() == 0) {
137            return null;
138        }
139        return subs.iterator();
140    }
141
142    public Set getAllSubclassesSet() {
143        Set transitiveSet = new HashSet();
144        Iterator subs = subclasses.iterator();
145        while (subs.hasNext()) {
146            transitiveSet.addAll(
147                ((JavaStructureType)subs.next()).getAllSubclassesSet());
148        }
149        transitiveSet.addAll(subclasses);
150        return transitiveSet;
151    }
152
153    public Object getOwner() {
154
155        // usually a SOAPStructureType
156        return owner;
157    }
158
159    public void setOwner(Object owner) {
160
161        // usually a SOAPStructureType
162        this.owner = owner;
163    }
164
165    private List<JavaStructureMember> members = new ArrayList();
166    private Map<String, JavaStructureMember> membersByName = new HashMap();
167
168    // known subclasses of this type
169    private Set subclasses = new HashSet();
170    private JavaStructureType superclass;
171
172    // usually a SOAPStructureType
173    private Object owner;
174    private boolean isAbstract = false;
175}
176