forked from apache/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordBuilderBase.java
192 lines (176 loc) · 6.54 KB
/
RecordBuilderBase.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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avro.data;
import java.io.IOException;
import java.util.Arrays;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Conversion;
import org.apache.avro.Conversions;
import org.apache.avro.LogicalType;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.IndexedRecord;
/** Abstract base class for RecordBuilder implementations. Not thread-safe. */
public abstract class RecordBuilderBase<T extends IndexedRecord>
implements RecordBuilder<T> {
private static final Field[] EMPTY_FIELDS = new Field[0];
private final Schema schema;
private final Field[] fields;
private final boolean[] fieldSetFlags;
private final GenericData data;
protected final Schema schema() { return schema; }
protected final Field[] fields() { return fields; }
protected final boolean[] fieldSetFlags() { return fieldSetFlags; }
protected final GenericData data() { return data; }
/**
* Creates a RecordBuilderBase for building records of the given type.
* @param schema the schema associated with the record class.
*/
protected RecordBuilderBase(Schema schema, GenericData data) {
this.schema = schema;
this.data = data;
fields = (Field[]) schema.getFields().toArray(EMPTY_FIELDS);
fieldSetFlags = new boolean[fields.length];
}
/**
* RecordBuilderBase copy constructor.
* Makes a deep copy of the values in the other builder.
* @param other RecordBuilderBase instance to copy.
*/
protected RecordBuilderBase(RecordBuilderBase<T> other, GenericData data) {
this.schema = other.schema;
this.data = data;
fields = (Field[]) schema.getFields().toArray(EMPTY_FIELDS);
fieldSetFlags = new boolean[other.fieldSetFlags.length];
System.arraycopy(
other.fieldSetFlags, 0, fieldSetFlags, 0, fieldSetFlags.length);
}
/**
* Validates that a particular value for a given field is valid according to
* the following algorithm:
* 1. If the value is not null, or the field type is null, or the field type
* is a union which accepts nulls, returns.
* 2. Else, if the field has a default value, returns.
* 3. Otherwise throws AvroRuntimeException.
* @param field the field to validate.
* @param value the value to validate.
* @throws NullPointerException if value is null and the given field does
* not accept null values.
*/
protected void validate(Field field, Object value) {
if (isValidValue(field, value)) {
return;
}
else if (field.defaultVal() != null) {
return;
}
else {
throw new AvroRuntimeException(
"Field " + field + " does not accept null values");
}
}
/**
* Tests whether a value is valid for a specified field.
* @param f the field for which to test the value.
* @param value the value to test.
* @return true if the value is valid for the given field; false otherwise.
*/
protected static boolean isValidValue(Field f, Object value) {
if (value != null) {
return true;
}
Schema schema = f.schema();
Type type = schema.getType();
// If the type is null, any value is valid
if (type == Type.NULL) {
return true;
}
// If the type is a union that allows nulls, any value is valid
if (type == Type.UNION) {
for (Schema s : schema.getTypes()) {
if (s.getType() == Type.NULL) {
return true;
}
}
}
// The value is null but the type does not allow nulls
return false;
}
/**
* Gets the default value of the given field, if any.
* @param field the field whose default value should be retrieved.
* @return the default value associated with the given field,
* or null if none is specified in the schema.
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object defaultValue(Field field) throws IOException {
return data.deepCopy(field.schema(), data.getDefaultValue(field));
}
/**
* Gets the default value of the given field, if any. Pass in a conversion
* to convert data to logical type class. Please make sure the schema does
* have a logical type, otherwise an exception would be thrown out.
* @param field the field whose default value should be retrieved.
* @param conversion the tool to convert data to logical type class
* @return the default value associated with the given field,
* or null if none is specified in the schema.
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object defaultValue(Field field, Conversion<?> conversion) throws IOException {
Schema schema = field.schema();
LogicalType logicalType = schema.getLogicalType();
Object rawDefaultValue = data.deepCopy(schema, data.getDefaultValue(field));
if (conversion == null || logicalType == null) {
return rawDefaultValue;
} else {
return Conversions.convertToLogicalType(rawDefaultValue, schema,
logicalType, conversion);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(fieldSetFlags);
result = prime * result + ((schema == null) ? 0 : schema.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("rawtypes")
RecordBuilderBase other = (RecordBuilderBase) obj;
if (!Arrays.equals(fieldSetFlags, other.fieldSetFlags))
return false;
if (schema == null) {
if (other.schema != null)
return false;
} else if (!schema.equals(other.schema))
return false;
return true;
}
}