001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.xbean.spring.generator;
018
019 import java.util.Collections;
020 import java.util.HashMap;
021 import java.util.Iterator;
022 import java.util.Map;
023 import java.util.Set;
024 import java.util.TreeSet;
025
026 /**
027 * @author Dain Sundstrom
028 * @version $Id$
029 * @since 1.0
030 */
031 public class NamespaceMapping implements Comparable {
032 private final String namespace;
033 private final Set elements;
034 private final Map elementsByName;
035 private final ElementMapping rootElement;
036
037 public NamespaceMapping(String namespace, Set elements, ElementMapping rootElement) {
038 this.namespace = namespace;
039 this.elements = Collections.unmodifiableSet(new TreeSet(elements));
040 this.rootElement = rootElement;
041
042 Map elementsByName = new HashMap();
043 for (Iterator iterator = elements.iterator(); iterator.hasNext();) {
044 ElementMapping elementMapping = (ElementMapping) iterator.next();
045 elementsByName.put(elementMapping.getElementName(), elementMapping);
046 }
047 this.elementsByName = Collections.unmodifiableMap(elementsByName);
048 }
049
050 public String getNamespace() {
051 return namespace;
052 }
053
054 public Set getElements() {
055 return elements;
056 }
057
058 public ElementMapping getElement(String elementName) {
059 return (ElementMapping) elementsByName.get(elementName);
060 }
061
062 public ElementMapping getRootElement() {
063 return rootElement;
064 }
065
066 public int hashCode() {
067 return namespace.hashCode();
068 }
069
070 public boolean equals(Object obj) {
071 if (obj instanceof NamespaceMapping) {
072 return namespace.equals(((NamespaceMapping) obj).namespace);
073 }
074 return false;
075 }
076
077 public int compareTo(Object obj) {
078 return namespace.compareTo(((NamespaceMapping) obj).namespace);
079 }
080
081 private final HashMap checkedTypes = new HashMap();
082
083 public boolean isSimpleType(Type type) {
084 Boolean b = (Boolean) checkedTypes.get(type);
085 if (b == null){
086 b = Utils.isSimpleType(type)? Boolean.TRUE: Boolean.FALSE;
087 checkedTypes.put(type, b);
088 }
089 return b.booleanValue();
090 }
091 }