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 Prolog Builder interface. Is an String Builder
30   * wrapper to append the term in string form that compound the final clause
31   * (Fact, Rule or Query).
32   * 
33   * @author Jose Zalacain
34   * @since 1.0
35   */
36  abstract class AbstractBuilder implements PrologBuilder {
37  
38  	protected StringBuilder builder;
39  	protected final PrologEngine engine;
40  	private final PrologProvider provider;
41  
42  	AbstractBuilder(PrologEngine engine) {
43  		this.provider = engine.getProvider();
44  		this.builder = new StringBuilder();
45  		this.engine = engine;
46  	}
47  
48  	protected final void append(Object object) {
49  		builder.append(object);
50  	}
51  
52  	protected final void append(String functor, PrologTerm... arguments) {
53  		if (arguments != null && arguments.length > 0) {
54  			builder.append(provider.newStructure(functor, arguments));
55  		} else {
56  			builder.append(provider.newAtom(functor));
57  		}
58  	}
59  
60  	protected final void append(Object left, String operator, Object right) {
61  		builder.append(left);
62  		append(' ');
63  		builder.append(operator);
64  		append(' ');
65  		builder.append(right);
66  	}
67  
68  	public final PrologEngine getEngine() {
69  		return engine;
70  	}
71  
72  	@Override
73  	public final boolean equals(Object object) {
74  		if (this == object)
75  			return true;
76  		if (object == null)
77  			return false;
78  		if (getClass() != object.getClass())
79  			return false;
80  		DefaultQueryBuilder other = (DefaultQueryBuilder) object;
81  		if (engine == null) {
82  			if (other.engine != null)
83  				return false;
84  		} else if (!engine.equals(other.engine)) {
85  			return false;
86  		}
87  		if (builder == null) {
88  			if (other.builder != null)
89  				return false;
90  		} else if (!builder.toString().equals(other.builder.toString())) {
91  			return false;
92  		}
93  		return true;
94  	}
95  
96  	@Override
97  	public final int hashCode() {
98  		final int prime = 31;
99  		int result = 1;
100 		result = prime * result + ((engine == null) ? 0 : engine.hashCode());
101 		result = prime * result + ((builder == null) ? 0 : builder.hashCode());
102 		return result;
103 	}
104 
105 	@Override
106 	public final String toString() {
107 		return "" + builder + "";
108 	}
109 
110 }