| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.se.dto.list; |
| 17 | |
| 18 | import org.qedeq.kernel.se.base.list.Atom; |
| 19 | import org.qedeq.kernel.se.base.list.Element; |
| 20 | import org.qedeq.kernel.se.base.list.ElementList; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * An object of this class represents a text string. |
| 25 | * |
| 26 | * @author Michael Meyling |
| 27 | */ |
| 28 | public final class DefaultAtom implements Atom { |
| 29 | |
| 30 | /** The plain text. */ |
| 31 | private final String text; |
| 32 | |
| 33 | /** |
| 34 | * Constructs an <code>Atom</code>. |
| 35 | * |
| 36 | * @param text Atom string. |
| 37 | * @throws IllegalArgumentException <code>text</code> is a NullPointer. |
| 38 | */ |
| 39 | public DefaultAtom(final String text) { |
| 40 | if (text == null) { |
| 41 | throw new IllegalArgumentException("a NullPointer is no valid text"); |
| 42 | } |
| 43 | this.text = text; |
| 44 | } |
| 45 | |
| 46 | public final String getString() { |
| 47 | return text; |
| 48 | } |
| 49 | |
| 50 | public final boolean isAtom() { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | public final Atom getAtom() { |
| 55 | return this; |
| 56 | } |
| 57 | |
| 58 | public final boolean isList() { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | public final ElementList getList() { |
| 63 | throw new ClassCastException("this is no " + ElementList.class.getName() |
| 64 | + ", but a " + this.getClass().getName()); |
| 65 | } |
| 66 | |
| 67 | public final Element copy() { |
| 68 | return new DefaultAtom(text); |
| 69 | } |
| 70 | |
| 71 | public final Element replace(final Element search, final Element replacement) { |
| 72 | if (this.equals(search)) { |
| 73 | return replacement.copy(); |
| 74 | } |
| 75 | return this.copy(); |
| 76 | } |
| 77 | |
| 78 | public final String toString() { |
| 79 | StringBuffer result = new StringBuffer(); |
| 80 | result.append("\""); |
| 81 | |
| 82 | for (int i = 0; i < text.length(); i++) { |
| 83 | if (text.charAt(i) == '\"') { |
| 84 | result.append("\"\""); |
| 85 | } else { |
| 86 | result.append(text.charAt(i)); |
| 87 | } |
| 88 | } |
| 89 | result.append('\"'); |
| 90 | return result.toString(); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | public final boolean equals(final Object object) { |
| 95 | if (object instanceof DefaultAtom) { |
| 96 | return ((DefaultAtom) object).text.equals(text); |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | public final int hashCode() { |
| 102 | return toString().hashCode(); |
| 103 | } |
| 104 | |
| 105 | } |