RenameVo.java
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.module;
017 
018 import org.qedeq.base.utility.EqualsUtility;
019 import org.qedeq.kernel.se.base.list.Element;
020 import org.qedeq.kernel.se.base.module.Rename;
021 
022 
023 /**
024  * Usage of rule for rename bound subject variable.
025  *
026  @author  Michael Meyling
027  */
028 public class RenameVo implements Rename {
029 
030     /** Reference to previously proven formula. */
031     private String reference;
032 
033     /** Bound subject variable that will be replaced. */
034     private Element originalSubjectVariable;
035 
036     /** Replacement subject variable. */
037     private Element replacementSubjectVariable;
038 
039     /** This bound occurrence should be replaced. If this value is 0, the replacement position
040      * is not specified. */
041     private int occurrence;
042 
043     /**
044      * Constructs an addition argument.
045      *
046      @param   reference                   Reference to a valid formula.
047      @param   originalSubjectVariable     Bound subject variable that will be replaced.
048      @param   replacementSubjectVariable  Replacement subject variable.
049      @param   occurrence                  This bound occurrence should be replaced. If this
050      *                                      value is 0, the replacement position
051      *                                      is not specified. */
052 
053     public RenameVo(final String reference, final Element originalSubjectVariable,
054             final Element replacementSubjectVariable, final int occurrence) {
055         this.reference = reference;
056         this.originalSubjectVariable = originalSubjectVariable;
057         this.replacementSubjectVariable = replacementSubjectVariable;
058         this.occurrence = occurrence;
059     }
060 
061     /**
062      * Default constructor.
063      */
064     public RenameVo() {
065         // nothing to do
066     }
067 
068     public Rename getRename() {
069         return this;
070     }
071 
072     public String getReference() {
073         return reference;
074     }
075 
076     /**
077      * Set formula reference.
078      *
079      @param   reference   Reference to formula.
080      */
081     public void setReference(final String reference) {
082         this.reference = reference;
083     }
084 
085     public String[] getReferences() {
086         if (reference == null || reference.length() == 0) {
087             return new String[] {};
088         else {
089             return new String[] {reference };
090         }
091     }
092 
093     public int getOccurrence() {
094         return occurrence;
095     }
096 
097     /**
098      * Set occurrence of bound subject variable that should be renamed.
099      *
100      @param   occurrence  This occurrence should be renamed.
101      */
102     public void setOccurrence(final int occurrence) {
103         this.occurrence = occurrence;
104     }
105 
106     public Element getOriginalSubjectVariable() {
107         return originalSubjectVariable;
108     }
109 
110     /**
111      * Set original subject variable, which will be replaced.
112      *
113      @param   originalSubjectVariable     Subject variable.
114      */
115     public void setOriginalSubjectVariable(final Element originalSubjectVariable) {
116         this.originalSubjectVariable = originalSubjectVariable;
117     }
118 
119     public Element getReplacementSubjectVariable() {
120         return replacementSubjectVariable;
121     }
122 
123     /**
124      * Set new subject variable subject variable.
125      *
126      @param   replacementSubjectVariable  New subject variable.
127      */
128     public void setReplacementSubjectVariable(final Element replacementSubjectVariable) {
129         this.replacementSubjectVariable = replacementSubjectVariable;
130     }
131 
132     public String getName() {
133         return "Rename";
134     }
135 
136     public boolean equals(final Object obj) {
137         if (!(obj instanceof RenameVo)) {
138             return false;
139         }
140         final RenameVo other = (RenameVoobj;
141         return EqualsUtility.equals(reference, other.reference)
142             && EqualsUtility.equals(originalSubjectVariable, other.originalSubjectVariable)
143             && EqualsUtility.equals(replacementSubjectVariable, other.replacementSubjectVariable)
144             && (occurrence == other.occurrence);
145     }
146 
147     public int hashCode() {
148         return (reference != null ? reference.hashCode() 0)
149             (originalSubjectVariable != null ^ originalSubjectVariable.hashCode() 0)
150             (replacementSubjectVariable != null ^ replacementSubjectVariable.hashCode() 0)
151             (^ occurrence);
152     }
153 
154     public String toString() {
155         StringBuffer result = new StringBuffer();
156         result.append("Rename");
157         if (reference != null || originalSubjectVariable != null
158                 || replacementSubjectVariable != null
159                 || occurrence != 0) {
160             result.append(" (");
161             boolean w = false;
162             if (reference != null) {
163                 result.append(reference);
164                 w = true;
165             }
166             if (originalSubjectVariable != null) {
167                 if (w) {
168                     result.append(", ");
169                 }
170                 result.append(originalSubjectVariable);
171                 w = true;
172             }
173             if (replacementSubjectVariable != null) {
174                 if (w) {
175                     result.append(", ");
176                 }
177                 result.append("by ");
178                 result.append(replacementSubjectVariable);
179                 w = true;
180             }
181             if (occurrence != 0) {
182                 if (w) {
183                     result.append(", ");
184                 }
185                 result.append("occurrence: ");
186                 result.append(occurrence);
187                 w = true;
188             }
189             result.append(")");
190         }
191         return result.toString();
192     }
193 
194 }