1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 }