View Javadoc

1   /*-
2    * #%L
3    * prolobjectlink-jpi-jpl
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 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.jpl;
23  
24  import java.util.Map.Entry;
25  
26  import io.github.prolobjectlink.prolog.AbstractCompounds;
27  import io.github.prolobjectlink.prolog.PrologEntry;
28  import io.github.prolobjectlink.prolog.PrologProvider;
29  import io.github.prolobjectlink.prolog.PrologTerm;
30  import io.github.prolobjectlink.prolog.PrologTermType;
31  
32  /**
33   * PrologEntry is key-value pair of PrologTerm. Is an implementation of
34   * {@link Entry} and {@link PrologTerm}.
35   * 
36   * @author Jose Zalacain
37   * @since 1.1
38   */
39  public final class JplEntry extends AbstractCompounds implements PrologEntry {
40  
41  	private final PrologTerm key;
42  	private PrologTerm value;
43  
44  	JplEntry(PrologProvider provider, PrologTerm key, PrologTerm value) {
45  		super(PrologTermType.MAP_ENTRY_TYPE, provider);
46  		this.value = value;
47  		this.key = key;
48  	}
49  
50  	public PrologTerm getKey() {
51  		return key;
52  	}
53  
54  	public PrologTerm getValue() {
55  		return value;
56  	}
57  
58  	public PrologTerm setValue(PrologTerm value) {
59  		this.value = value;
60  		return value;
61  	}
62  
63  	public boolean isList() {
64  		return false;
65  	}
66  
67  	public boolean isStructure() {
68  		return true;
69  	}
70  
71  	public boolean isEmptyList() {
72  		return false;
73  	}
74  
75  	public String getFunctor() {
76  		return "-";
77  	}
78  
79  	public int getArity() {
80  		return 2;
81  	}
82  
83  	public PrologTerm[] getArguments() {
84  		return new PrologTerm[] { key, value };
85  	}
86  
87  	@Override
88  	public int hashCode() {
89  		int result = 0;
90  		final int prime = 31;
91  		result = prime * result + ((key == null) ? 0 : key.hashCode());
92  		result = prime * result + ((value == null) ? 0 : value.hashCode());
93  		return result;
94  	}
95  
96  	@Override
97  	public boolean equals(Object obj) {
98  		if (this == obj)
99  			return true;
100 		if (obj == null)
101 			return false;
102 		if (getClass() != obj.getClass())
103 			return false;
104 		JplEntry other = (JplEntry) obj;
105 		if (key == null) {
106 			if (other.key != null)
107 				return false;
108 		} else if (!key.equals(other.key)) {
109 			return false;
110 		}
111 		if (value == null) {
112 			if (other.value != null)
113 				return false;
114 		} else if (!value.equals(other.value)) {
115 			return false;
116 		}
117 		return true;
118 	}
119 
120 	@Override
121 	public String toString() {
122 		return "" + key + "-" + value + "";
123 	}
124 
125 }