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