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

/**
 * Class used to represent up to 4 flags used in a DocumentUpdate.
 * These flags are stored as the 4 most significant bits in a 32 bit integer.
 *
 * Flags currently used:
 *   0) create-if-non-existent.
 *
 * @author geirst
 */
public class DocumentUpdateFlags {
    private byte flags;
    private DocumentUpdateFlags(byte flags) {
        this.flags = flags;
    }
    public DocumentUpdateFlags() {
        this.flags = 0;
    }
    public boolean getCreateIfNonExistent() {
        return (flags & 1) != 0;
    }
    public void setCreateIfNonExistent(boolean value) {
        flags &= ~1; // clear flag
        flags |= value ? (byte)1 : (byte)0; // set flag
    }
    public int injectInto(int value) {
        return extractValue(value) | (flags << 28);
    }
    public static DocumentUpdateFlags extractFlags(int combined) {
        return new DocumentUpdateFlags((byte)(combined >> 28));
    }
    public static int extractValue(int combined) {
        int mask = ~(~0 << 28);
        return combined & mask;
    }
}