aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime/Slime.java
blob: 7d29131cbdb00dd1b483862e478ee5daf4c826da (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

/**
 * Top-level value class that contains one Value data object and a
 * symbol table (shared between all directly or indirectly contained
 * ObjectValue data objects).
 * 
 * @author havardpe
 */
public final class Slime {

    private final SymbolTable names = new SymbolTable();
    private Value root = NixValue.instance();

    SymbolTable symbolTable() { return names; }

    /**
     * Construct an empty Slime with an empty top-level value.
     */
    public Slime() {}

    /** Returns a count of names in the symbol table. */
    public int symbols() {
        return names.symbols();
    }

    /**
     * Return the symbol name associated with an id.
     * 
     * @param symbol the id, must be in range [0, symbols()-1]
     */
    public String inspect(int symbol) {
        return names.inspect(symbol);
    }

    /**
     * Add a name to the symbol table; if the name is already
     * in the symbol table just returns the id it already had.
     * 
     * @param name the name to insert
     * @return the id now associated with the name
     */
    public int insert(String name) {
        return names.insert(name);
    }

    /**
     * Find the id associated with a symbol name; if the
     * name was not in the symbol table returns the
     * constant Integer.MAX_VALUE instead.
     */
    public int lookup(String name) {
        return names.lookup(name);
    }

    /** Get a Cursor connected to the top-level data object. */
    public Cursor get() { return root; }

    /**
     * Create a new empty value and make it the new top-level data object.
     */
    public Cursor setNix() {
        root = NixValue.instance();
        return root;
    }

    /**
     * Create a new boolean value and make it the new top-level data object.
     * 
     * @param bit the actual boolean value for the new value
     */
    public Cursor setBool(boolean bit) {
        root = BoolValue.instance(bit);
        return root;
    }

    /**
     * Create a new double value and make it the new top-level data object.
     * 
     * @param l the actual long value for the new value
     */
    public Cursor setLong(long l) {
        root = new LongValue(l);
        return root;
    }

    /**
     * Create a new double value and make it the new top-level data object.
     * 
     * @param d the actual double value for the new value
     */
    public Cursor setDouble(double d) {
        root = new DoubleValue(d);
        return root;
    }

    /**
     * Create a new string value and make it the new top-level data object.
     * 
     * @param str the actual string for the new value
     */
    public Cursor setString(String str) {
        root = StringValue.create(str);
        return root;
    }

    /**
     * Create a new string value and make it the new top-level data object.
     * 
     * @param utf8 the actual string (encoded as UTF-8 data) for the new value
     */
    public Cursor setString(byte[] utf8) {
        root = Utf8Value.create(utf8);
        return root;
    }

    /**
     * Create a new data value and make it the new top-level data object.
     * 
     * @param data the actual data to be put into the new value.
     */
    public Cursor setData(byte[] data) {
        root = DataValue.create(data);
        return root;
    }

    /**
     * Create a new array value and make it the new top-level data object.
     */
    public Cursor setArray() {
        root = new ArrayValue(names);
        return root;
    }

    /**
     * Create a new object value and make it the new top-level data object.
     */
    public Cursor setObject() {
        root = new ObjectValue(names);
        return root;
    }

    /**
     * Take the current top-level data object and make it a field in a
     * new ObjectValue with the given symbol id as field id; the new
     * ObjectValue will also become the new top-level data object.
     */
    public Cursor wrap(int sym) {
        root = new ObjectValue(names, sym, root);
        return root;
    }

    /**
     * Take the current top-level data object and make it a field in a
     * new ObjectValue with the given symbol name as field name; the new
     * ObjectValue will also become the new top-level data object.
     */
    public Cursor wrap(String name) {
        return wrap(names.insert(name));
    }

    /**
     * Tests whether the two Inspectors are equal.
     *
     * <p>Since equality of two Inspectors is subtle, {@link Object#equals(Object)} is not used.</p>
     *
     * @return true if they are equal.
     */
    public boolean equalTo(Slime that) {
        return get().equalTo(that.get());
    }

    @Override
    public String toString() {
        return get().toString();
    }
}