001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature;
017:
018: import org.geotools.resources.Utilities;
019:
020: /**
021: * Simple implementation of Name, hope to bring in line with GenericName.
022: * <p>
023: * This class emulates QName, and is used as the implementation of both AttributeName and
024: * TypeName (so when the API settles down we should have a quick fix.
025: * <p>
026: * Its is advantagous to us to be able to:
027: * <ul>
028: * <li>Have a API in agreement with QName - considering our target audience
029: * <li>Strongly type AttributeName and TypeName seperatly
030: * </ul>
031: * The ISO interface move towards combining the AttributeName and Attribute classes,
032: * and TypeName and Type classes, while we understand the atractiveness of this on a
033: * UML diagram it is very helpful to keep these concepts seperate when playing with
034: * a strongly typed langauge like java.
035: * </p>
036: * <p>
037: * It case it is not obvious this is a value object and equality is based on
038: * namespace and name.
039: * </p>
040: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
041: *
042: */
043: public class Name implements org.opengis.feature.type.Name {
044: /** namespace / scope */
045: protected String namespace;
046:
047: /** local part */
048: protected String local;
049:
050: /**
051: * Constructs an instance with the local part set. Namespace / scope is
052: * set to null.
053: *
054: * @param local The local part of the name.
055: */
056: public Name(String local) {
057: this (null, local);
058: }
059:
060: /**
061: * Constructs an instance with the local part and namespace set.
062: *
063: * @param namespace The namespace or scope of the name.
064: * @param local The local part of the name.
065: *
066: */
067: public Name(String namespace, String local) {
068: this .namespace = namespace;
069: this .local = local;
070: }
071:
072: public boolean isGlobal() {
073: return getNamespaceURI() == null;
074: }
075:
076: public String getNamespaceURI() {
077: return namespace;
078: }
079:
080: public String getLocalPart() {
081: return local;
082: }
083:
084: public String getURI() {
085: if ((namespace == null) && (local == null)) {
086: return null;
087: }
088:
089: if (namespace == null) {
090: return local;
091: }
092:
093: if (local == null) {
094: return namespace;
095: }
096:
097: //return new StringBuffer(namespace).append(':').append(local).toString();
098: return new StringBuffer(namespace).append(local).toString();
099: }
100:
101: /**
102: * value object with equality based on name and namespace.
103: */
104: public int hashCode() {
105: String uri = getURI();
106:
107: return (uri != null) ? uri.hashCode() : 0;
108:
109: //
110: // final int PRIME = 1000003;
111: // int result = 0;
112: //
113: // if (local != null) {
114: // result = (PRIME * result) + local.hashCode();
115: // }
116: //
117: // if (namespace != null) {
118: // result = (PRIME * result) + namespace.hashCode();
119: // }
120: //
121: // return result;
122: }
123:
124: /**
125: * value object with equality based on name and namespace.
126: */
127: public boolean equals(Object obj) {
128: if (obj instanceof Name) {
129: Name other = (Name) obj;
130:
131: return Utilities.equals(getURI(), other.getURI());
132: }
133:
134: return false;
135: }
136:
137: /** name or namespace:name */
138: public String toString() {
139: return getURI();
140: }
141: }
|