View Javadoc

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