View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2019 Prolobjectlink 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 io.github.prolobjectlink.prolog.jtrolog;
23  
24  import static io.github.prolobjectlink.prolog.PrologTermType.VARIABLE_TYPE;
25  
26  import io.github.prolobjectlink.prolog.ArityError;
27  import io.github.prolobjectlink.prolog.FunctorError;
28  import io.github.prolobjectlink.prolog.PrologProvider;
29  import io.github.prolobjectlink.prolog.PrologTerm;
30  import io.github.prolobjectlink.prolog.PrologVariable;
31  import jTrolog.terms.Term;
32  import jTrolog.terms.Var;
33  
34  /**
35   * 
36   * @author Jose Zalacain
37   * @since 1.0
38   */
39  class JTrologVariable extends JTrologTerm implements PrologVariable {
40  
41  	private String name;
42  
43  	JTrologVariable(PrologProvider provider, int n) {
44  		this(provider, "_", n);
45  		this.name = "_";
46  	}
47  
48  	JTrologVariable(PrologProvider provider, String name, int n) {
49  		super(VARIABLE_TYPE, provider, name, n);
50  		this.name = name;
51  	}
52  
53  	JTrologVariable(int type, PrologProvider provider) {
54  		super(type, provider);
55  	}
56  
57  	JTrologVariable(int type, PrologProvider provider, String name, int n) {
58  		super(type, provider, name, n);
59  		this.name = name;
60  	}
61  
62  	JTrologVariable(int type, PrologProvider provider, Term var) {
63  		super(type, provider, var);
64  	}
65  
66  	public boolean isAnonymous() {
67  		return ((Var) value).isAnonymous();
68  	}
69  
70  	public String getName() {
71  		return name;
72  	}
73  
74  	public void setName(String name) {
75  		this.name = name;
76  	}
77  
78  	public PrologTerm[] getArguments() {
79  		return new JTrologVariable[0];
80  	}
81  
82  	public int getArity() {
83  		throw new ArityError(this);
84  	}
85  
86  	public String getFunctor() {
87  		throw new FunctorError(this);
88  	}
89  
90  	public int getPosition() {
91  		return vIndex;
92  	}
93  
94  	@Override
95  	public int hashCode() {
96  		final int prime = 31;
97  		int result = super.hashCode();
98  		result = prime * result + ((name == null) ? 0 : name.hashCode());
99  		return result;
100 	}
101 
102 	@Override
103 	public boolean equals(Object obj) {
104 		if (this == obj)
105 			return true;
106 		if (obj == null)
107 			return false;
108 		if (getClass() != obj.getClass())
109 			return false;
110 		JTrologVariable other = (JTrologVariable) obj;
111 		if (name == null) {
112 			if (other.name != null)
113 				return false;
114 		} else if (!name.equals(other.name)) {
115 			return false;
116 		}
117 		return true;
118 	}
119 
120 }