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.xsom.impl;
27
28import com.sun.xml.internal.xsom.XSDeclaration;
29
30import java.util.Comparator;
31
32/**
33 * UName.
34 *
35 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
36 */
37public final class UName {
38    /**
39     * @param _nsUri
40     *      Use "" to indicate the no namespace.
41     */
42    public UName( String _nsUri, String _localName, String _qname ) {
43        if(_nsUri==null || _localName==null || _qname==null) {
44            throw new NullPointerException(_nsUri+" "+_localName+" "+_qname);
45        }
46        this.nsUri = _nsUri.intern();
47        this.localName = _localName.intern();
48        this.qname = _qname.intern();
49    }
50
51    public UName( String nsUri, String localName ) {
52        this(nsUri,localName,localName);
53    }
54
55    public UName(XSDeclaration decl) {
56        this(decl.getTargetNamespace(),decl.getName());
57    }
58
59    private final String nsUri;
60    private final String localName;
61    private final String qname;
62
63    public String getName() { return localName; }
64    public String getNamespaceURI() { return nsUri; }
65    public String getQualifiedName() { return qname; }
66
67
68    // Issue 540; XSComplexType.getAttributeUse(String,String) always return null
69    // UName was used in HashMap without overriden equals and hashCode methods.
70
71    @Override
72    public boolean equals(Object obj) {
73        if(obj instanceof UName) {
74            UName u = (UName)obj;
75
76            return ((this.getName().compareTo(u.getName()) == 0) &&
77                    (this.getNamespaceURI().compareTo(u.getNamespaceURI()) == 0) &&
78                    (this.getQualifiedName().compareTo(u.getQualifiedName()) == 0));
79        } else {
80            return false;
81        }
82    }
83
84    @Override
85    public int hashCode() {
86        int hash = 7;
87        hash = 13 * hash + (this.nsUri != null ? this.nsUri.hashCode() : 0);
88        hash = 13 * hash + (this.localName != null ? this.localName.hashCode() : 0);
89        hash = 13 * hash + (this.qname != null ? this.qname.hashCode() : 0);
90        return hash;
91    }
92
93    /**
94     * Compares {@link UName}s by their names.
95     */
96    public static final Comparator comparator = new Comparator() {
97        public int compare(Object o1, Object o2) {
98            UName lhs = (UName)o1;
99            UName rhs = (UName)o2;
100            int r = lhs.nsUri.compareTo(rhs.nsUri);
101            if(r!=0)    return r;
102            return lhs.localName.compareTo(rhs.localName);
103        }
104    };
105}
106