View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
25   */
26  package io.github.prolobjectlink.prolog;
27  
28  /**
29   * Partial implementation of {@link PrologOperator}.
30   * 
31   * @author Jose Zalacain
32   * @since 1.0
33   */
34  public abstract class AbstractOperator implements PrologOperator {
35  
36  	private final int priority;
37  	private final String specifier;
38  	private final String operator;
39  
40  	public AbstractOperator(int priority, String specifier, String operator) {
41  		this.priority = priority;
42  		this.specifier = specifier;
43  		this.operator = operator;
44  	}
45  
46  	public final int getPriority() {
47  		return priority;
48  	}
49  
50  	public final String getSpecifier() {
51  		return specifier;
52  	}
53  
54  	public final String getOperator() {
55  		return operator;
56  	}
57  
58  	@Override
59  	public final String toString() {
60  		return "op(" + priority + "," + specifier + "," + operator + ")";
61  	}
62  
63  	@Override
64  	public final int hashCode() {
65  		final int prime = 31;
66  		int result = 1;
67  		result = prime * result + ((operator == null) ? 0 : operator.hashCode());
68  		result = prime * result + priority;
69  		result = prime * result + ((specifier == null) ? 0 : specifier.hashCode());
70  		return result;
71  	}
72  
73  	@Override
74  	public final boolean equals(Object object) {
75  		if (this == object)
76  			return true;
77  		if (object == null)
78  			return false;
79  		if (getClass() != object.getClass())
80  			return false;
81  		AbstractOperator other = (AbstractOperator) object;
82  		if (operator == null) {
83  			if (other.operator != null)
84  				return false;
85  		} else if (!operator.equals(other.operator)) {
86  			return false;
87  		}
88  		if (priority != other.priority)
89  			return false;
90  		if (specifier == null) {
91  			if (other.specifier != null)
92  				return false;
93  		} else if (!specifier.equals(other.specifier)) {
94  			return false;
95  		}
96  		return true;
97  	}
98  
99  	public int compareTo(PrologOperator o) {
100 		if (operator != null) {
101 			if (priority > o.getPriority()) {
102 				return 1;
103 			} else if (priority < o.getPriority()) {
104 				return -1;
105 			}
106 		}
107 		return 0;
108 	}
109 
110 }