blob: adecae9d7eb132ac29599b59c342e4deb5868322 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Date: Apr 15, 2008
*
* @author humbe
*/
public abstract class BaseStructDataType extends StructuredDataType {
protected Map<Integer, Field> fieldIds = new LinkedHashMap<>();
protected Map<String, Field> fields = new LinkedHashMap<>();
BaseStructDataType(String name) {
super(name);
}
BaseStructDataType(int id, String name) {
super(id, name);
}
protected void assign(BaseStructDataType type) {
BaseStructDataType stype = type.clone();
fieldIds = stype.fieldIds;
fields = stype.fields;
}
@Override
public BaseStructDataType clone() {
BaseStructDataType type = (BaseStructDataType) super.clone();
type.fieldIds = new LinkedHashMap<>();
type.fields = new LinkedHashMap<>();
for (Field field : fieldIds.values()) {
type.fields.put(field.getName(), field);
type.fieldIds.put(field.getId(), field);
}
return type;
}
public void addField(Field field) {
if (fields.containsKey(field.getName())) {
throw new IllegalArgumentException("Struct " + getName() + " already contains field with name " + field.getName());
}
if (fieldIds.containsKey(field.getId())) {
throw new IllegalArgumentException("Struct " + getName() + " already contains field with id " + field.getId());
}
fields.put(field.getName(), field);
fieldIds.put(field.getId(), field);
}
public Field removeField(String fieldName) {
Field old = fields.remove(fieldName);
if (old != null) {
fieldIds.remove(old.getId());
}
return old;
}
public void clearFields() {
fieldIds.clear();
fields.clear();
}
@Override
public Field getField(String fieldName) {
return fields.get(fieldName);
}
@Override
public Field getField(int id) {
return fieldIds.get(id);
}
public boolean hasField(Field field) {
Field f = getField(field.getId());
return f != null && f.equals(field);
}
public boolean hasField(String name) {
return fields.containsKey(name);
}
@Override
public Collection<Field> getFields() {
return fields.values();
}
public int getFieldCount() {
return fields.size();
}
}
|