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