001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.graph.properties.basic;
023
024 import net.jcip.annotations.Immutable;
025 import org.jboss.dna.common.text.TextEncoder;
026 import org.jboss.dna.common.util.CheckArg;
027 import org.jboss.dna.common.util.HashCode;
028 import org.jboss.dna.graph.properties.Name;
029 import org.jboss.dna.graph.properties.NamespaceRegistry;
030 import org.jboss.dna.graph.properties.Path;
031
032 /**
033 * A basic implementation of {@link Name}.
034 *
035 * @author Randall Hauch
036 * @author John Verhaeg
037 */
038 @Immutable
039 public class BasicName implements Name {
040
041 /**
042 */
043 private static final long serialVersionUID = -1737537720336990144L;
044 private final String namespaceUri;
045 private final String localName;
046 private final int hc;
047
048 public BasicName( String namespaceUri,
049 String localName ) {
050 CheckArg.isNotEmpty(localName, "localName");
051 this.namespaceUri = namespaceUri != null ? namespaceUri.trim() : "";
052 this.localName = localName != null ? localName.trim() : "";
053 this.hc = HashCode.compute(this.namespaceUri, this.localName);
054 }
055
056 /**
057 * {@inheritDoc}
058 */
059 public String getLocalName() {
060 return this.localName;
061 }
062
063 /**
064 * {@inheritDoc}
065 */
066 public String getNamespaceUri() {
067 return this.namespaceUri;
068 }
069
070 /**
071 * {@inheritDoc}
072 */
073 public String getString() {
074 return getString(Path.DEFAULT_ENCODER);
075 }
076
077 /**
078 * {@inheritDoc}
079 */
080 public String getString( TextEncoder encoder ) {
081 if (this.getNamespaceUri().length() == 0) {
082 if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
083 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
084 }
085 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
086 return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName);
087 }
088
089 /**
090 * {@inheritDoc}
091 */
092 public String getString( NamespaceRegistry namespaceRegistry ) {
093 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
094 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
095 if (prefix != null && prefix.length() != 0) {
096 return prefix + ":" + this.localName;
097 }
098 return this.localName;
099 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public String getString( NamespaceRegistry namespaceRegistry,
105 TextEncoder encoder ) {
106 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
107 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
108 if (prefix != null && prefix.length() != 0) {
109 return encoder.encode(prefix) + ":" + encoder.encode(this.localName);
110 }
111 return this.localName;
112 }
113
114 /**
115 * {@inheritDoc}
116 */
117 public int compareTo( Name that ) {
118 if (that == this) return 0;
119 int diff = this.getNamespaceUri().compareTo(that.getNamespaceUri());
120 if (diff != 0) return diff;
121 diff = this.getLocalName().compareTo(that.getLocalName());
122 return diff;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 @Override
129 public int hashCode() {
130 return this.hc;
131 }
132
133 /**
134 * {@inheritDoc}
135 */
136 @Override
137 public boolean equals( Object obj ) {
138 if (obj == this) return true;
139 if (obj instanceof Name) {
140 Name that = (Name)obj;
141 if (!this.getNamespaceUri().equals(that.getNamespaceUri())) return false;
142 return this.getLocalName().equals(that.getLocalName());
143 }
144 return false;
145 }
146
147 /**
148 * {@inheritDoc}
149 */
150 @Override
151 public String toString() {
152 return "{" + this.namespaceUri + "}" + this.localName;
153 }
154
155 }