View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2021 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.OBJECT_TYPE;
25  
26  import io.github.prolobjectlink.prolog.PrologProvider;
27  import io.github.prolobjectlink.prolog.PrologReference;
28  import io.github.prolobjectlink.prolog.PrologTerm;
29  import jTrolog.terms.Struct;
30  import jTrolog.terms.StructAtom;
31  import jTrolog.terms.Term;
32  
33  public final class JTrologReference extends JTrologTerm implements PrologReference {
34  
35  	protected final Object reference;
36  
37  	protected JTrologReference(PrologProvider provider, Object reference) {
38  		super(OBJECT_TYPE, provider, new Struct("'@'", new Term[] { new StructAtom("'" + reference + "'") }));
39  		this.reference = reference;
40  	}
41  
42  	@Override
43  	public int getArity() {
44  		return 1;
45  	}
46  
47  	@Override
48  	public String getFunctor() {
49  		return "@";
50  	}
51  
52  	@Override
53  	public PrologTerm[] getArguments() {
54  		String string = reference.toString();
55  		PrologTerm tag = provider.newAtom(string);
56  		return new PrologTerm[] { tag };
57  	}
58  
59  	@Override
60  	public PrologTerm getTerm() {
61  		String string = reference.toString();
62  		PrologTerm tag = provider.newAtom(string);
63  		return provider.newStructure(getFunctor(), tag);
64  	}
65  
66  	@Override
67  	public Class<?> getReferenceType() {
68  		return reference.getClass();
69  	}
70  
71  	public Object getObject() {
72  		return reference;
73  	}
74  
75  	@Override
76  	public String toString() {
77  		return "" + getTerm() + "";
78  	}
79  
80  	@Override
81  	public int hashCode() {
82  		final int prime = 31;
83  		int result = 1;
84  		result = prime * result + ((reference == null) ? 0 : reference.hashCode());
85  		return result;
86  	}
87  
88  	@Override
89  	public boolean equals(Object obj) {
90  		if (this == obj)
91  			return true;
92  		if (obj == null)
93  			return false;
94  		if (getClass() != obj.getClass())
95  			return false;
96  		JTrologReference other = (JTrologReference) obj;
97  		if (reference == null) {
98  			if (other.reference != null)
99  				return false;
100 		} else if (!reference.equals(other.reference)) {
101 			return false;
102 		}
103 		return true;
104 	}
105 }