View Javadoc

1   /*-
2    * #%L
3    * prolobjectlink-jpi-jlog
4    * %%
5    * Copyright (C) 2020 - 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 General Public License as
9    * published by the Free Software Foundation, either version 3 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 Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jlog;
23  
24  import java.util.Map.Entry;
25  
26  import io.github.prolobjectlink.prolog.PrologEntry;
27  import io.github.prolobjectlink.prolog.PrologProvider;
28  import io.github.prolobjectlink.prolog.PrologTerm;
29  import io.github.prolobjectlink.prolog.PrologTermType;
30  
31  /**
32   * PrologEntry is key-value pair of PrologTerm. Is an implementation of
33   * {@link Entry} and {@link PrologTerm}.
34   * 
35   * @author Jose Zalacain
36   * @since 1.1
37   */
38  public final class JLogEntry extends JLogTerm implements PrologEntry {
39  
40  	private final PrologTerm key;
41  	private PrologTerm value;
42  
43  	JLogEntry(PrologProvider provider, PrologTerm key, PrologTerm value) {
44  		super(PrologTermType.MAP_ENTRY_TYPE, provider);
45  		this.value = value;
46  		this.key = key;
47  	}
48  
49  	public PrologTerm getKey() {
50  		return key;
51  	}
52  
53  	public PrologTerm getValue() {
54  		return value;
55  	}
56  
57  	public PrologTerm setValue(PrologTerm value) {
58  		this.value = value;
59  		return value;
60  	}
61  
62  	public int getArity() {
63  		return 2;
64  	}
65  
66  	public String getFunctor() {
67  		return "-";
68  	}
69  
70  	public PrologTerm[] getArguments() {
71  		return new PrologTerm[] { key, value };
72  	}
73  
74  	@Override
75  	public int hashCode() {
76  		int result = 0;
77  		final int prime = 31;
78  		result = prime * result + ((key == null) ? 0 : key.hashCode());
79  		result = prime * result + ((value == null) ? 0 : value.hashCode());
80  		return result;
81  	}
82  
83  	@Override
84  	public boolean equals(Object obj) {
85  		if (this == obj)
86  			return true;
87  		if (obj == null)
88  			return false;
89  		if (getClass() != obj.getClass())
90  			return false;
91  		JLogEntry other = (JLogEntry) obj;
92  		if (key == null) {
93  			if (other.key != null)
94  				return false;
95  		} else if (!key.equals(other.key)) {
96  			return false;
97  		}
98  		if (value == null) {
99  			if (other.value != null)
100 				return false;
101 		} else if (!value.equals(other.value)) {
102 			return false;
103 		}
104 		return true;
105 	}
106 
107 	@Override
108 	public String toString() {
109 		return "" + key + "-" + value + "";
110 	}
111 
112 	@Override
113 	public PrologTerm getTerm() {
114 		return this;
115 	}
116 
117 	@Override
118 	public boolean isEvaluable() {
119 		return false;
120 	}
121 
122 	@Override
123 	public boolean isCompound() {
124 		return true;
125 	}
126 
127 }