001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, GeoTools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library 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: package org.geotools.image.io.metadata;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021: import org.w3c.dom.Node;
022:
023: // Geotools dependencies
024: import org.geotools.resources.Utilities;
025:
026: /**
027: * An immutable ({@code "name"}, {@code "type"}) pair.
028: *
029: * @since 2.4
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/metadata/Identification.java $
031: * @version $Id: Identification.java 26137 2007-07-03 17:59:44Z desruisseaux $
032: * @author Martin Desruisseaux
033: */
034: public class Identification implements CharSequence, Serializable {
035: /**
036: * For cross-version compatibility.
037: */
038: private static final long serialVersionUID = 7439545624472885445L;
039:
040: /**
041: * The object name, or {@code null} if none.
042: */
043: public final String name;
044:
045: /**
046: * The object type, or {@code null} if none.
047: */
048: public final String type;
049:
050: /**
051: * Creates an identification from the specified object name and type.
052: */
053: public Identification(final String name, final String type) {
054: this .name = name;
055: this .type = type;
056: }
057:
058: /**
059: * Creates an identification from the {@code "name"} and {@code "type"} attributes
060: * in the specified accessor.
061: */
062: public Identification(final MetadataAccessor accessor) {
063: name = accessor.getString("name");
064: type = accessor.getString("type");
065: }
066:
067: /**
068: * Returns the {@linkplain #name} length.
069: */
070: public int length() {
071: return (name != null) ? name.length() : 0;
072: }
073:
074: /**
075: * Returns the {@linkplain #name} character at the specified index.
076: */
077: public char charAt(final int index) {
078: return name.charAt(index);
079: }
080:
081: /**
082: * Returns a subsequence of this identification. The new identification will contains a
083: * substring of the {@linkplain #name}, but the {@linkplain #type} will be unchanged.
084: */
085: public CharSequence subSequence(final int start, final int end) {
086: if (start == 0 && end == length()) {
087: return this ;
088: }
089: return new Identification(name.substring(start, end), type);
090: }
091:
092: /**
093: * Returns the {@linkplain #name}.
094: */
095: public String toString() {
096: return name;
097: }
098:
099: /**
100: * Returns a hash value for this identification.
101: */
102: public int hashCode() {
103: int code = (int) serialVersionUID;
104: if (name != null)
105: code ^= name.hashCode();
106: if (type != null)
107: code += type.hashCode() * 37;
108: return code;
109: }
110:
111: /**
112: * Compares the specified object with this identification for equality.
113: */
114: public boolean equals(final Object object) {
115: if (object != null && object.getClass().equals(getClass())) {
116: final Identification that = (Identification) object;
117: return Utilities.equals(this .name, that.name)
118: && Utilities.equals(this .type, that.type);
119: }
120: return false;
121: }
122: }
|