View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">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.bo.logic.model;
17  
18  import org.qedeq.base.utility.EqualsUtility;
19  
20  /**
21   * Operators with arguments.
22   *
23   * @author  Michael Meyling
24   */
25  public abstract class Operator {
26  
27      /** Text to identify the variable. */
28      private final String name;
29  
30      /** Argument number for variable. */
31      private final int number;
32  
33      /**
34       * Constructor.
35       *
36       * @param   name    Text to identify the operator.
37       * @param   number  Argument number for operator.
38       */
39      public Operator(final String name, final int number) {
40          this.name = name;
41          this.number = number;
42      }
43  
44      /**
45       * Get operator name.
46       *
47       * @return  Operator name.
48       */
49      public String getName() {
50          return name;
51      }
52  
53      /**
54       * Get number of arguments this operator has.
55       *
56       * @return  Number of arguments for this operator.
57       */
58      public int getArgumentNumber() {
59          return number;
60      }
61  
62      public int hashCode() {
63          return name.hashCode() ^ number ^ getClass().hashCode();
64      }
65  
66      public boolean equals(final Object other) {
67          if (!(other instanceof Operator)) {
68              return false;
69          }
70          if (!other.getClass().equals(getClass())) {
71              return false;
72          }
73          final Operator var = (Operator) other;
74          return number == var.number && EqualsUtility.equals(name, var.name);
75      }
76  
77      public String toString() {
78          final StringBuffer buffer = new StringBuffer();
79          buffer.append(name);
80          if (number > 0) {
81              buffer.append("(");
82              for (int i = 0; i < number; i++) {
83                  if (i > 0) {
84                      buffer.append(", ");
85                  }
86                  buffer.append("*");
87              }
88              buffer.append(")");
89          }
90          return buffer.toString();
91      }
92  
93  }