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


import java.util.ArrayList;
import java.util.List;
import java.nio.ByteBuffer;


/**
 * A sequence of values used to represent parameters and return values
 * associated with a {@link Request}. The individual values are
 * represented by {@link Value} objects.
 **/
public class Values
{
    private List<Value> values = new ArrayList<Value>(16);

    /**
     * Check whether the values stored in this object satisfies the
     * given type string.
     *
     * @return true if this value sequence satisfies 'types'
     * @param types type string
     **/
    public boolean satisfies(String types) {
        int off = 0;
        int len = Math.min(types.length(), size());
        while (off < len && types.charAt(off) == get(off).type()) {
            off++;
        }
        return ((off == types.length() && off == size()) ||
                (off + 1 == types.length() && types.charAt(off) == '*'));
    }

    /**
     * Create an empty sequence of values
     **/
    public Values() {
    }

    /**
     * Create a sequence of values by decoding them from the given
     * buffer
     *
     * @param src buffer containing a contained value sequence
     **/
    Values(ByteBuffer src) {
        decode(src);
    }

    /**
     * Add a value to the end of the sequence
     *
     * @return this, to enable chaining
     * @param value the value to add
     **/
    public Values add(Value value) {
        values.add(value);
        return this;
    }

    /**
     * Obtain a specific value from this sequence
     *
     * @return a value from this sequence
     * @param idx the index of the value to obtain
     **/
    public Value get(int idx) {
        return values.get(idx);
    }

    /**
     * Obtain the number of values in this sequence
     *
     * @return the number of values in this sequence
     **/
    public int size() {
        return values.size();
    }

    /**
     * Determine the number of bytes needed to store this value
     * sequence when encoded into a buffer
     *
     * @return number of bytes needed for encoding this value sequence
     **/
    int bytes() {
        int bytes = 4 + values.size();
        for (int i = 0; i < values.size(); i++) {
            bytes += get(i).bytes();
        }
        return bytes;
    }

    /**
     * Encode this value sequence into the given buffer
     *
     * @param dst where to encode this value sequence
     **/
    void encode(ByteBuffer dst) {
        byte[] types = new byte[values.size()];
        for (int i = 0; i < types.length; i++) {
            types[i] = get(i).type();
        }
        dst.putInt(types.length);
        dst.put(types);
        for (int i = 0; i < types.length; i++) {
            get(i).encode(dst);
        }
    }

    /**
     * Decode a value sequence from the given buffer into this object
     *
     * @param src where the value sequence is stored
     **/
    void decode(ByteBuffer src) {
        values.clear();
        int cnt = src.getInt();
        byte[] types = new byte[cnt];
        src.get(types);
        for (int i = 0; i < cnt; i++) {
            values.add(Value.decode(types[i], src));
        }
    }

    @Override
    public String toString() {
        if (values.size()==0) return "";
        if (values.size()==1) return values.get(0).toString();
        StringBuffer buffer=new StringBuffer();
        for (int i=0; i<values.size(); i++) {
            buffer.append(values.get(i).toString());
            if (i<values.size()-1)
                buffer.append(",");
        }
        return buffer.toString();
    }

}