View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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   * UTF8 constant pool entry, used for storing long Strings.
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       * Creates a new CPUTF8 instance
41       *
42       * @param utf8        TODO
43       * @param globalIndex - index in CpBands
44       * @throws NullPointerException if utf8 is null
45       */
46      public CPUTF8(final String utf8, final int globalIndex) {
47          super(ConstantPoolEntry.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  }