001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
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; either
010: * version 2.1 of the License, or (at your option) any later version.
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.util;
018:
019: // J2SE dependencies
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.Serializable;
024: import java.util.Locale;
025:
026: // Geotools dependencies
027: import org.geotools.resources.Utilities;
028:
029: /**
030: * A simple international string consisting of a single string for all locales.
031: * For such a particular case, this implementation is the more effective than
032: * other implementations provided in this package.
033: *
034: * @since 2.1
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/SimpleInternationalString.java $
036: * @version $Id: SimpleInternationalString.java 22443 2006-10-27 20:47:22Z desruisseaux $
037: * @author Martin Desruisseaux
038: */
039: public class SimpleInternationalString extends
040: AbstractInternationalString implements Serializable {
041: /**
042: * Serial number for interoperability with different versions.
043: */
044: private static final long serialVersionUID = 3543963804501667578L;
045:
046: /**
047: * Creates a new instance of international string.
048: *
049: * @param message The string for all locales.
050: */
051: public SimpleInternationalString(final String message) {
052: defaultValue = message;
053: ensureNonNull("message", message);
054: }
055:
056: /**
057: * If the specified string is null or an instance of
058: * {@link AbstractInternationalString}, returns it unchanged.
059: * Otherwise, wraps the string value in a {@code SimpleInternationalString}.
060: */
061: public static AbstractInternationalString wrap(
062: final CharSequence string) {
063: if (string == null
064: || string instanceof AbstractInternationalString) {
065: return (AbstractInternationalString) string;
066: }
067: return new SimpleInternationalString(string.toString());
068: }
069:
070: /**
071: * Returns the same string for all locales. This is the string given to the constructor.
072: */
073: public String toString(final Locale locale) {
074: return defaultValue;
075: }
076:
077: /**
078: * Compares this international string with the specified object for equality.
079: */
080: public boolean equals(final Object object) {
081: if (object != null && object.getClass().equals(getClass())) {
082: final SimpleInternationalString that = (SimpleInternationalString) object;
083: return Utilities.equals(this .defaultValue,
084: that.defaultValue);
085: }
086: return false;
087: }
088:
089: /**
090: * Returns a hash code value for this international text.
091: */
092: public int hashCode() {
093: return (int) serialVersionUID ^ defaultValue.hashCode();
094: }
095:
096: /**
097: * Write the string. This is required since {@link #defaultValue} is not serialized.
098: */
099: private void writeObject(final ObjectOutputStream out)
100: throws IOException {
101: out.defaultWriteObject();
102: out.writeUTF(defaultValue);
103: }
104:
105: /**
106: * Read the string. This is required since {@link #defaultValue} is not serialized.
107: */
108: private void readObject(final ObjectInputStream in)
109: throws IOException, ClassNotFoundException {
110: in.defaultReadObject();
111: defaultValue = in.readUTF();
112: }
113: }
|