aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/textserialize/item/ItemContext.java
blob: e0e80dd79f51cb7d08c0b6425e69dc0f56532d68 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.textserialize.item;

import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.TaggableItem;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

/**
 * @author Tony Vaagenes
 */
public class ItemContext {

    private class Connectivity {
        final String id;
        final double strength;

        public Connectivity(String id, double strength) {
            this.id = id;
            this.strength = strength;
        }
    }

    private final Map<String, Item> itemById = new HashMap<>();
    private final Map<TaggableItem, Connectivity> connectivityByItem = new IdentityHashMap<>();


    public void setItemId(String id, Item item) {
        itemById.put(id, item);
    }

    public void setConnectivity(TaggableItem item, String id, Double strength) {
        connectivityByItem.put(item, new Connectivity(id, strength));
    }

    public void connectItems() {
        for (Map.Entry<TaggableItem, Connectivity> entry : connectivityByItem.entrySet()) {
            entry.getKey().setConnectivity(getItem(entry.getValue().id), entry.getValue().strength);
        }
    }

    private Item getItem(String id) {
        Item item = itemById.get(id);
        if (item == null)
            throw new IllegalArgumentException("No item with id '" + id + "'");
        return item;
    }

}