Debian Codegraph @

src/uk/me/parabola/imgfmt/app/labelenc/EncodedText.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007,2014 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 14-Jan-2007
16 */
17 package uk.me.parabola.imgfmt.app.labelenc;
18
19 /**
20 * Holds the bytes and length of an encoded character string used in a label.
21 * The length of the byte array may be longer than the part that is actually
22 * used, so the length property should always be used.
23 *
24 * Class is immutable.
25 *
26 * @author Steve Ratcliffe
27 */
28 public class EncodedText {
29 private final int hashCode;
30 private final byte[] ctext;
31 private final int length;
32 private final char[] chars;
33
34 public EncodedText(byte[] buf, int len, char[] chars) {
35 this.ctext = buf;
36 this.length = len;
37 this.chars = chars;
38
39 assert chars == null || chars.length == 0 || chars[chars.length - 1] != 0 : "found trailing 0 in chars";
40 int hc = 0;
41 for (int i = 0; i < length; i++)
42 hc = 31*hc + ctext[i];
43 hashCode = hc;
44 }
45
46 public byte[] getCtext() {
47 return ctext;
48 }
49
50 public int getLength() {
51 return length;
52 }
53
54 public char[] getChars() {
55 return chars;
56 }
57
58 public boolean equals(Object o) {
59 if (this == o) return true;
60 if (o == null || getClass() != o.getClass()) return false;
61
62 EncodedText that = (EncodedText) o;
63
64 if (length != that.length) return false;
65 for (int i = 0; i < length; i++)
66 if (ctext[i] != that.ctext[i])
67 return false;
68
69 return true;
70 }
71
72 public int hashCode() {
73 return hashCode;
74 }
75 }