blob: cb4b3f6f532d713e0b839951007fae2d9651d9fb (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentmodel;
import com.yahoo.document.DataType;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author baldersheim
*/
public class DataTypeRepo implements DataTypeCollection {
Map<Integer, DataType> typeById = new LinkedHashMap<>();
Map<String, DataType> typeByName = new LinkedHashMap<>();
public DataType getDataType(String name) {
return typeByName.get(name);
}
public DataType getDataType(int id) {
return typeById.get(id);
}
public Collection<DataType> getTypes() { return typeById.values(); }
public DataTypeRepo add(DataType type) {
if (typeByName.containsKey(type.getName()) || typeById.containsKey(type.getId())) {
throw new IllegalStateException("Data type '" + type.getName() + "', id '" +
type.getId() + "' is already registered.");
}
typeByName.put(type.getName(), type);
typeById.put(type.getId(), type);
return this;
}
public DataTypeRepo addAll(DataTypeCollection repo) {
for (DataType dataType : repo.getTypes()) {
add(dataType);
}
return this;
}
public DataTypeRepo replace(DataType type) {
if (!typeByName.containsKey(type.getName()) ||
!typeById.containsKey(type.getId()))
{
throw new IllegalStateException("Data type '" + type.getName() + "' is not registered.");
}
typeByName.remove(type.getName());
typeByName.put(type.getName(), type);
typeById.remove(type.getId());
typeById.put(type.getId(), type);
return this;
}
}
|