aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/readers/WeightedSetReader.java
blob: 2bdb5ee49971f7eefd5c55dc215dd1b0cd4b23ca (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json.readers;

import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.document.json.TokenBuffer;

import static com.yahoo.document.json.readers.JsonParserHelpers.expectObjectStart;


public class WeightedSetReader {

    public static void fillWeightedSet(TokenBuffer buffer, DataType valueType, @SuppressWarnings("rawtypes") WeightedSet weightedSet) {
        int initNesting = buffer.nesting();
        expectObjectStart(buffer.current());
        buffer.next();
        iterateThroughWeightedSet(buffer, initNesting, valueType, weightedSet);
    }

    public static void fillWeightedSetUpdate(TokenBuffer buffer, int initNesting, DataType valueType, @SuppressWarnings("rawtypes") WeightedSet weightedSet) {
        iterateThroughWeightedSet(buffer, initNesting, valueType, weightedSet);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static void iterateThroughWeightedSet(TokenBuffer buffer, int initNesting, DataType valueType, WeightedSet weightedSet) {
        while (buffer.nesting() >= initNesting) {
            // XXX the keys are defined in the spec to always be represented as strings
            FieldValue v = valueType.createFieldValue(buffer.currentName());
            weightedSet.put(v, Integer.valueOf(buffer.currentText()));
            buffer.next();
        }
    }

}