blob: 23f6760f5e7d630f834aa0f6681c90d3024deba4 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;
import java.util.ArrayList;
/**
* This class represents a buffer of byte values to be used as a backing buffer
* for raw buckets.
*
* @author Ulf Lilleengen
*/
public class RawBuffer implements Comparable<RawBuffer>, Cloneable {
private final ArrayList<Byte> buffer;
/**
* Create an empty buffer.
*/
public RawBuffer() {
this.buffer = new ArrayList<>();
}
/**
* Create a buffer with initial content.
*
* @param buffer A buffer of values to be assigned this buffer.
*/
public RawBuffer(ArrayList<Byte> buffer) {
this.buffer = buffer;
}
/**
* Create a buffer with initial content.
*
* @param bytes A buffer of bytes to be assigned this buffer.
*/
public RawBuffer(byte[] bytes) {
buffer = new ArrayList<>();
put(bytes);
}
/**
* Insert a byte value into this buffer.
*
* @param value The value to add to the buffer.
* @return Reference to this.
*/
public RawBuffer put(byte value) {
buffer.add(value);
return this;
}
/**
* Insert an array of byte values into this buffer.
*
* @param values The array to add to the buffer.
* @return Reference to this.
*/
public RawBuffer put(byte[] values) {
for (int i = 0; i < values.length; i++) {
buffer.add(values[i]);
}
return this;
}
/**
* Create a copy of data in the internal buffer.
*
* @return A copy of the data.
*/
public byte[] getBytes() {
byte[] ret = new byte[buffer.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = buffer.get(i);
}
return ret;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("{");
for (int i = 0; i < buffer.size(); i++) {
s.append(buffer.get(i));
if (i < buffer.size() - 1) {
s.append(",");
}
}
s.append("}");
return s.toString();
}
@Override
public RawBuffer clone() {
return new RawBuffer(new ArrayList<>(buffer));
}
@Override
public int compareTo(RawBuffer rhs) {
Byte[] my = buffer.toArray(new Byte[0]);
Byte[] their = rhs.buffer.toArray(new Byte[0]);
for (int i = 0; i < my.length && i < their.length; i++) {
if (my[i] < their[i]) {
return -1;
} else if (my[i] > their[i]) {
return 1;
}
}
return Integer.compare(my.length, their.length);
}
@Override
public int hashCode() {
return buffer.hashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) return true;
if ( ! (other instanceof RawBuffer)) return false;
return (compareTo((RawBuffer)other) == 0);
}
}
|