MCPcopy Create free account
hub / github.com/1345414527/MIT6.830 / StringField

Class StringField

src/java/simpledb/storage/StringField.java:11–118  ·  view source on GitHub ↗

Instance of Field that stores a single String of a fixed length.

Source from the content-addressed store, hash-verified

9 * Instance of Field that stores a single String of a fixed length.
10 */
11public class StringField implements Field {
12
13 private static final long serialVersionUID = 1L;
14
15 private final String value;
16 private final int maxSize;
17
18 public String getValue() {
19 return value;
20 }
21
22 /**
23 * Constructor.
24 *
25 * @param s
26 * The value of this field.
27 * @param maxSize
28 * The maximum size of this string
29 */
30 public StringField(String s, int maxSize) {
31 this.maxSize = maxSize;
32
33 if (s.length() > maxSize)
34 value = s.substring(0, maxSize);
35 else
36 value = s;
37 }
38
39 public String toString() {
40 return value;
41 }
42
43 public int hashCode() {
44 return value.hashCode();
45 }
46
47 public boolean equals(Object field) {
48 if (!(field instanceof StringField)) return false;
49 return ((StringField) field).value.equals(value);
50 }
51
52 /**
53 * Write this string to dos. Always writes maxSize + 4 bytes to the passed
54 * in dos. First four bytes are string length, next bytes are string, with
55 * remainder padded with 0 to maxSize.
56 *
57 * @param dos
58 * Where the string is written
59 */
60 public void serialize(DataOutputStream dos) throws IOException {
61 String s = value;
62 int overflow = maxSize - s.length();
63 if (overflow < 0) {
64 s = s.substring(0, maxSize);
65 }
66 dos.writeInt(s.length());
67 dos.writeBytes(s);
68 while (overflow-- > 0)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected