1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package io.github.prolobjectlink.prolog;
27
28
29
30
31
32
33
34
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 }