1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.harmony.unpack200.bytecode;
18
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Objects;
23
24
25
26
27 public class CPUTF8 extends ConstantPoolEntry {
28
29 private final String utf8;
30
31 private boolean hashCodeComputed;
32
33 private int cachedHashCode;
34
35 public CPUTF8(final String string) {
36 this(string, -1);
37 }
38
39
40
41
42
43
44
45
46 public CPUTF8(final String utf8, final int globalIndex) {
47 super(CP_UTF8, globalIndex);
48 this.utf8 = Objects.requireNonNull(utf8, "utf8");
49 }
50
51 @Override
52 public boolean equals(final Object obj) {
53 if (this == obj) {
54 return true;
55 }
56 if (obj == null) {
57 return false;
58 }
59 if (this.getClass() != obj.getClass()) {
60 return false;
61 }
62 final CPUTF8 other = (CPUTF8) obj;
63 return utf8.equals(other.utf8);
64 }
65
66 private void generateHashCode() {
67 hashCodeComputed = true;
68 final int PRIME = 31;
69 cachedHashCode = PRIME + utf8.hashCode();
70 }
71
72 @Override
73 public int hashCode() {
74 if (!hashCodeComputed) {
75 generateHashCode();
76 }
77 return cachedHashCode;
78 }
79
80 public void setGlobalIndex(final int index) {
81 globalIndex = index;
82 }
83
84 @Override
85 public String toString() {
86 return StandardCharsets.UTF_8.name() + ":" + utf8;
87 }
88
89 public String underlyingString() {
90 return utf8;
91 }
92
93 @Override
94 protected void writeBody(final DataOutputStream dos) throws IOException {
95 dos.writeUTF(utf8);
96 }
97 }