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

import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StructuredFieldValue;

/**
 * This adds an Extractor to the Field that can be used to get access the backed value
 * used in the concrete document types.
 * @author baldersheim
 */
public class ExtendedField extends Field {
    public static interface Extract {
        Object get(StructuredFieldValue doc);
        void set(StructuredFieldValue doc, Object value);
    }
    private final Extract extract;
    public ExtendedField(String name, DataType type, Extract extract) {
        super(name, type);
        this.extract = extract;
    }
    public FieldValue getFieldValue(StructuredFieldValue doc) {
        Object raw = extract.get(doc);
        return raw == null ? null : getDataType().createFieldValue(raw);
    }
    public FieldValue setFieldValue(StructuredFieldValue doc, FieldValue fv) {
        FieldValue old = getFieldValue(doc);
        extract.set(doc, (fv == null) ? null : fv.getWrappedValue());
        return old;
    }
}