-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathBaseSchema.java
297 lines (256 loc) · 7.19 KB
/
BaseSchema.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2023 Datastrato.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.graviton.meta.rel;
import com.datastrato.graviton.Entity;
import com.datastrato.graviton.Field;
import com.datastrato.graviton.HasIdentifier;
import com.datastrato.graviton.Namespace;
import com.datastrato.graviton.meta.AuditInfo;
import com.datastrato.graviton.rel.Schema;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
import lombok.ToString;
/** An abstract class representing a base schema in a relational database. */
@ToString
public class BaseSchema implements Schema, Entity, HasIdentifier {
public static final Field ID = Field.required("id", Long.class, "The schema's unique identifier");
public static final Field NAME = Field.required("name", String.class, "The schema's name");
public static final Field COMMENT =
Field.optional("comment", String.class, "The comment or description for the schema");
public static final Field PROPERTIES =
Field.optional("properties", Map.class, "The associated properties of the schema");
public static final Field AUDIT_INFO =
Field.required("audit_info", AuditInfo.class, "The audit details of the schema");
protected Long id;
protected String name;
@Nullable protected String comment;
@Nullable protected Map<String, String> properties;
protected AuditInfo auditInfo;
protected Namespace namespace;
/**
* Returns an unmodifiable map of the fields and their corresponding values for this schema.
*
* @return An unmodifiable map of the fields and values.
*/
@Override
public Map<Field, Object> fields() {
Map<Field, Object> fields = Maps.newHashMap();
fields.put(ID, id);
fields.put(NAME, name);
fields.put(COMMENT, comment);
fields.put(PROPERTIES, properties);
fields.put(AUDIT_INFO, auditInfo);
return Collections.unmodifiableMap(fields);
}
/**
* Returns the name of the schema.
*
* @return The name of the schema.
*/
@Override
public String name() {
return name;
}
/**
* Returns the unique id of the schema.
*
* @return The unique id of the schema.
*/
@Override
public Long id() {
return id;
}
/**
* Returns the namespace of the schema.
*
* @return The namespace of the schema.
*/
@Override
public Namespace namespace() {
return namespace;
}
/**
* Returns the comment or description for the schema.
*
* @return The comment or description for the schema.
*/
@Override
public String comment() {
return comment;
}
/**
* Returns the associated properties of the schema.
*
* @return The associated properties of the schema.
*/
@Override
public Map<String, String> properties() {
return properties;
}
/**
* Returns the audit details of the schema.
*
* @return The audit details of the schema.
*/
@Override
public AuditInfo auditInfo() {
return auditInfo;
}
/**
* Returns the type of the entity, which is {@link EntityType#SCHEMA}.
*
* @return The type of the entity.
*/
@Override
public EntityType type() {
return EntityType.SCHEMA;
}
// Ignore field namespace and comment
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BaseSchema schema = (BaseSchema) o;
return Objects.equal(id, schema.id)
&& Objects.equal(name, schema.name)
&& Objects.equal(properties, schema.properties)
&& Objects.equal(auditInfo, schema.auditInfo);
}
@Override
public int hashCode() {
return Objects.hashCode(id, name, properties, auditInfo);
}
/**
* Builder interface for creating instances of {@link BaseSchema}.
*
* @param <SELF> The type of the builder.
* @param <T> The type of the schema being built.
*/
interface Builder<SELF extends Builder<SELF, T>, T extends BaseSchema> {
SELF withId(Long id);
SELF withName(String name);
SELF withNamespace(Namespace namespace);
SELF withComment(String comment);
SELF withProperties(Map<String, String> properties);
SELF withAuditInfo(AuditInfo auditInfo);
T build();
}
/**
* An abstract class implementing the builder interface for {@link BaseSchema}.
*
* @param <SELF> The type of the builder.
* @param <T> The type of the schema being built.
*/
public abstract static class BaseSchemaBuilder<
SELF extends Builder<SELF, T>, T extends BaseSchema>
implements Builder<SELF, T> {
protected Long id;
protected String name;
protected Namespace namespace;
protected String comment;
protected Map<String, String> properties;
protected AuditInfo auditInfo;
/**
* Sets the unique identifier of the schema.
*
* @param id The unique identifier of the schema.
* @return The builder instance.
*/
@Override
public SELF withId(Long id) {
this.id = id;
return self();
}
/**
* Sets the name of the schema.
*
* @param name The name of the schema.
* @return The builder instance.
*/
@Override
public SELF withName(String name) {
this.name = name;
return self();
}
/**
* Sets the namespace of the schema.
*
* @param namespace The namespace of the schema.
* @return The builder instance.
*/
@Override
public SELF withNamespace(Namespace namespace) {
this.namespace = namespace;
return self();
}
/**
* Sets the comment of the schema.
*
* @param comment The comment or description for the schema.
* @return The builder instance.
*/
@Override
public SELF withComment(String comment) {
this.comment = comment;
return self();
}
/**
* Sets the associated properties of the schema.
*
* @param properties The associated properties of the schema.
* @return The builder instance.
*/
@Override
public SELF withProperties(Map<String, String> properties) {
this.properties = properties;
return self();
}
/**
* Sets the audit details of the schema.
*
* @param auditInfo The audit details of the schema.
* @return The builder instance.
*/
@Override
public SELF withAuditInfo(AuditInfo auditInfo) {
this.auditInfo = auditInfo;
return self();
}
/**
* Builds the instance of the schema with the provided attributes.
*
* @return The built schema instance.
*/
@Override
public T build() {
T t = internalBuild();
t.validate();
return t;
}
private SELF self() {
return (SELF) this;
}
protected abstract T internalBuild();
}
public static class SchemaBuilder extends BaseSchemaBuilder<SchemaBuilder, BaseSchema> {
@Override
protected BaseSchema internalBuild() {
BaseSchema baseSchema = new BaseSchema();
baseSchema.id = id;
baseSchema.name = name;
baseSchema.comment = comment;
baseSchema.properties = properties;
baseSchema.auditInfo = auditInfo;
return baseSchema;
}
}
}