001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.se.dto.list;
017
018 import org.qedeq.kernel.se.base.list.Atom;
019 import org.qedeq.kernel.se.base.list.Element;
020 import org.qedeq.kernel.se.base.list.ElementList;
021
022
023 /**
024 * An object of this class represents a text string.
025 *
026 * @author Michael Meyling
027 */
028 public final class DefaultAtom implements Atom {
029
030 /** The plain text. */
031 private final String text;
032
033 /**
034 * Constructs an <code>Atom</code>.
035 *
036 * @param text Atom string.
037 * @throws IllegalArgumentException <code>text</code> is a NullPointer.
038 */
039 public DefaultAtom(final String text) {
040 if (text == null) {
041 throw new IllegalArgumentException("a NullPointer is no valid text");
042 }
043 this.text = text;
044 }
045
046 public final String getString() {
047 return text;
048 }
049
050 public final boolean isAtom() {
051 return true;
052 }
053
054 public final Atom getAtom() {
055 return this;
056 }
057
058 public final boolean isList() {
059 return false;
060 }
061
062 public final ElementList getList() {
063 throw new ClassCastException("this is no " + ElementList.class.getName()
064 + ", but a " + this.getClass().getName());
065 }
066
067 public final Element copy() {
068 return new DefaultAtom(text);
069 }
070
071 public final Element replace(final Element search, final Element replacement) {
072 if (this.equals(search)) {
073 return replacement.copy();
074 }
075 return this.copy();
076 }
077
078 public final String toString() {
079 StringBuffer result = new StringBuffer();
080 result.append("\"");
081
082 for (int i = 0; i < text.length(); i++) {
083 if (text.charAt(i) == '\"') {
084 result.append("\"\"");
085 } else {
086 result.append(text.charAt(i));
087 }
088 }
089 result.append('\"');
090 return result.toString();
091
092 }
093
094 public final boolean equals(final Object object) {
095 if (object instanceof DefaultAtom) {
096 return ((DefaultAtom) object).text.equals(text);
097 }
098 return false;
099 }
100
101 public final int hashCode() {
102 return toString().hashCode();
103 }
104
105 }
|