1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package io.github.prolobjectlink.prolog.jpl7;
30
31 import static io.github.prolobjectlink.prolog.PrologTermType.OBJECT_TYPE;
32
33 import org.jpl7.JPL;
34 import org.jpl7.Term;
35
36 import io.github.prolobjectlink.prolog.PrologProvider;
37 import io.github.prolobjectlink.prolog.PrologReference;
38 import io.github.prolobjectlink.prolog.PrologTerm;
39
40 public final class JplReference extends JplTerm implements PrologReference {
41
42 JplReference(PrologProvider provider, Term reference) {
43 super(OBJECT_TYPE, provider, reference);
44 }
45
46 JplReference(PrologProvider provider, Object reference) {
47 super(OBJECT_TYPE, provider, JPL.newJRef(reference));
48 }
49
50 @Override
51 public Class<?> getReferenceType() {
52 return getObject().getClass();
53 }
54
55 @Override
56 public int getArity() {
57 return value.arity();
58 }
59
60 @Override
61 public String getFunctor() {
62 return "<jref>";
63 }
64
65 @Override
66 public PrologTerm[] getArguments() {
67 return new PrologTerm[0];
68 }
69
70 public Object getObject() {
71 if (value.isJFalse()) {
72 return Boolean.FALSE;
73 } else if (value.isJTrue()) {
74 return Boolean.TRUE;
75 } else if (value.isJVoid()) {
76 return void.class;
77 } else if (value.isJRef()) {
78 return value.object();
79 }
80 return null;
81 }
82
83 @Override
84 public int hashCode() {
85 final int prime = 31;
86 int result = 1;
87 result = prime * result + type;
88
89 result = prime * result + ((value == null) ? 0 : value.toString().hashCode());
90 return result;
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj)
96 return true;
97 if (obj == null)
98 return false;
99 if (!(obj instanceof JplTerm))
100 return false;
101 JplTerm other = (JplTerm) obj;
102 if (value == null) {
103 if (other.value != null)
104 return false;
105 } else if (!getObject().equals(other.getObject())) {
106 return false;
107 }
108 return true;
109 }
110
111 public boolean unify(PrologTerm term) {
112 PrologTerm thisTerm = this;
113 PrologTerm otherTerm = term;
114 if (thisTerm == otherTerm) {
115 return true;
116 } else if (thisTerm.isVariable()) {
117 if (thisTerm == thisTerm.getTerm()) {
118 return true;
119 }
120 return thisTerm.getTerm().unify(otherTerm);
121 } else if (otherTerm.isVariable()) {
122 if (otherTerm == otherTerm.getTerm()) {
123 return true;
124 }
125 return otherTerm.getTerm().unify(thisTerm);
126 }
127 return equals(term);
128 }
129
130 public int compareTo(PrologTerm term) {
131 PrologTerm thisTerm = this;
132 PrologTerm otherTerm = term;
133
134 Integer thisHashCode = thisTerm.hashCode();
135 Integer otherHashCode = otherTerm.hashCode();
136
137 if (thisHashCode < otherHashCode) {
138 return -1;
139 } else if (thisHashCode > otherHashCode) {
140 return 1;
141 }
142
143 return 0;
144 }
145
146 }