aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/documentmodel/DataTypeRepo.java
blob: 9f4eeeb44c9b7863eb318668fc6e85242430c066 (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
// Copyright Yahoo. 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 {

    private Map<Integer, DataType> typeById = new LinkedHashMap<>();
    private 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 IllegalArgumentException("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.");
        }
        var oldByName = typeByName.remove(type.getName());
        var oldById = typeById.remove(type.getId());
        if (oldByName != oldById) {
            throw new IllegalStateException("Data type '" + type.getName() +
                                            "' inconsistent replace, by name: " + oldByName
                                            + " but by id: " + oldById);
        }
        typeByName.put(type.getName(), type);
        typeById.put(type.getId(), type);
        return this;
    }

}