View Javadoc

1   /*-
2    * #%L
3    * prolobjectlink-jpi-jpl7
4    * %%
5    * Copyright (C) 2012 - 2019 Prolobjectlink Project
6    * %%
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    * 
10   * 1. Redistributions of source code must retain the above copyright notice,
11   *    this list of conditions and the following disclaimer.
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * #L%
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  		// Term not implement hashCode()
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 }