View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.property.basic;
25  
26  import net.jcip.annotations.Immutable;
27  import org.modeshape.graph.property.NamespaceRegistry;
28  import org.modeshape.graph.property.NamespaceRegistry.Namespace;
29  
30  /**
31   * Basic implementation of a {@link NamespaceRegistry} namespace.
32   */
33  @Immutable
34  public class BasicNamespace implements NamespaceRegistry.Namespace {
35      private final String prefix;
36      private final String namespaceUri;
37  
38      /**
39       * Create a namespace instance.
40       * 
41       * @param prefix the namespace prefix; may not be null (this is not checked)
42       * @param namespaceUri the namespace URI; may not be null (this is not checked)
43       */
44      public BasicNamespace( String prefix,
45                             String namespaceUri ) {
46          assert prefix != null;
47          assert namespaceUri != null;
48          this.prefix = prefix;
49          this.namespaceUri = namespaceUri;
50      }
51  
52      /**
53       * {@inheritDoc}
54       * 
55       * @see org.modeshape.graph.property.NamespaceRegistry.Namespace#getNamespaceUri()
56       */
57      public String getNamespaceUri() {
58          return namespaceUri;
59      }
60  
61      /**
62       * {@inheritDoc}
63       * 
64       * @see org.modeshape.graph.property.NamespaceRegistry.Namespace#getPrefix()
65       */
66      public String getPrefix() {
67          return prefix;
68      }
69  
70      /**
71       * {@inheritDoc}
72       * 
73       * @see java.lang.Object#hashCode()
74       */
75      @Override
76      public int hashCode() {
77          return namespaceUri.hashCode();
78      }
79  
80      /**
81       * {@inheritDoc}
82       * 
83       * @see java.lang.Comparable#compareTo(java.lang.Object)
84       */
85      public int compareTo( Namespace that ) {
86          if (that == null) return 1;
87          if (this == that) return 0;
88          return this.getNamespaceUri().compareTo(that.getNamespaceUri());
89      }
90  
91      /**
92       * {@inheritDoc}
93       * 
94       * @see java.lang.Object#equals(java.lang.Object)
95       */
96      @Override
97      public boolean equals( Object obj ) {
98          if (obj == this) return true;
99          if (obj instanceof Namespace) {
100             Namespace that = (Namespace)obj;
101             if (!this.namespaceUri.equals(that.getNamespaceUri())) return false;
102             // if (!this.prefix.equals(that.getPrefix())) return false;
103             return true;
104         }
105         return false;
106     }
107 
108     /**
109      * {@inheritDoc}
110      * 
111      * @see java.lang.Object#toString()
112      */
113     @Override
114     public String toString() {
115         return prefix + "=" + namespaceUri;
116     }
117 }