1 /*
2 * #%L
3 * prolobjectlink-jpi-jtrolog
4 * %%
5 * Copyright (C) 2012 - 2018 WorkLogic Project
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 2.1 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20 * #L%
21 */
22 package jTrolog.terms;
23
24 import jTrolog.errors.InvalidTermException;
25
26 /**
27 * This class represents a variable term. Variables are identified by a name
28 * (which must starts with an upper case letter) or the anonymous ('_') name.
29 *
30 * @see Term
31 */
32 @SuppressWarnings({ "serial" })
33 public class Var extends Term {
34
35 public final static String ANY = "_".intern();
36
37 private String name;
38 public final int nrInStruct;
39
40 /**
41 * needed to implement Wrapper Objects. Do not use to stringToStructList ANY
42 * Vars
43 */
44 Var() {
45 type = Term.VAR;
46 nrInStruct = 0;
47 }
48
49 /**
50 * Creates a variable identified by a name.
51 *
52 * @param n
53 * varaible name if n is "_" the variable is anonymous.
54 * @param nr
55 * its variable number of its root parent Struct.
56 */
57 public Var(String n, int nr) throws InvalidTermException {
58 type = Term.VAR;
59 name = n.intern();
60 nrInStruct = nr;
61 }
62
63 public boolean equals(Object t) {
64 return t instanceof Var && nrInStruct == ((Var) t).nrInStruct;
65 }
66
67 public int hashCode() {
68 return nrInStruct;
69 }
70
71 /**
72 * Tests if this variable is ANY
73 */
74 public boolean isAnonymous() {
75 return name == ANY;
76 }
77
78 /**
79 * Gets the string representation of this variable.
80 */
81 public String toString() {
82 return name;
83 }
84
85 public String toStringSmall() {
86 return name;
87 }
88
89 }