blob: 4aa4c29db4841d4f98b2a94914f804c23d5e5801 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.datatypes;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.PrimitiveDataType;
import com.yahoo.document.serialization.FieldReader;
import com.yahoo.document.serialization.FieldWriter;
import com.yahoo.document.serialization.XmlSerializationHelper;
import com.yahoo.document.serialization.XmlStream;
import com.yahoo.vespa.objects.Ids;
/**
* FieldValue which encapsulates a byte.
*
* @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
*/
public class ByteFieldValue extends NumericFieldValue {
private static class Factory extends PrimitiveDataType.Factory {
public FieldValue create() {
return new ByteFieldValue();
}
}
public static PrimitiveDataType.Factory getFactory() { return new Factory(); }
public static final int classId = registerClass(Ids.document + 10, ByteFieldValue.class);
private byte value;
public ByteFieldValue() {
this((byte) 0);
}
public ByteFieldValue(byte value) {
this.value = value;
}
public ByteFieldValue(Byte value) {
this.value = value;
}
public ByteFieldValue(Integer value) {
this.value = (byte) value.intValue();
}
public ByteFieldValue(String s) { value = Byte.parseByte(s); }
@Override
public ByteFieldValue clone() {
ByteFieldValue val = (ByteFieldValue) super.clone();
val.value = value;
return val;
}
@Override
public Number getNumber() {
return value;
}
@Override
public void clear() {
value = (byte) 0;
}
@Override
public void assign(Object o) {
if (!checkAssign(o)) {
return;
}
if (o instanceof Number) {
value = ((Number) o).byteValue();
} else if (o instanceof NumericFieldValue) {
value = ((NumericFieldValue) o).getNumber().byteValue();
} else if (o instanceof String || o instanceof StringFieldValue) {
value = Byte.parseByte(o.toString());
} else {
throw new IllegalArgumentException("Class " + o.getClass() + " not applicable to an " + this.getClass() + " instance.");
}
}
public byte getByte() {
return value;
}
@Override
public Object getWrappedValue() {
return value;
}
@Override
public DataType getDataType() {
return DataType.BYTE;
}
@Override
public void printXml(XmlStream xml) {
XmlSerializationHelper.printByteXml(this, xml);
}
@Override
public String toString() {
return "" + value;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (int) value;
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ByteFieldValue)) return false;
if (!super.equals(o)) return false;
ByteFieldValue that = (ByteFieldValue) o;
if (value != that.value) return false;
return true;
}
@Override
public void serialize(Field field, FieldWriter writer) {
writer.write(field, this);
}
/* (non-Javadoc)
* @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader)
*/
@Override
public void deserialize(Field field, FieldReader reader) {
reader.read(field, this);
}
@Override
public int compareTo(FieldValue fieldValue) {
int comp = super.compareTo(fieldValue);
if (comp != 0) {
return comp;
}
//types are equal, this must be of this type
ByteFieldValue otherValue = (ByteFieldValue) fieldValue;
if (value < otherValue.value) {
return -1;
} else if (value > otherValue.value) {
return 1;
} else {
return 0;
}
}
}
|