CPUTF8.java

  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. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.nio.charset.StandardCharsets;
  21. import java.util.Objects;

  22. /**
  23.  * UTF8 constant pool entry, used for storing long Strings.
  24.  */
  25. public class CPUTF8 extends ConstantPoolEntry {

  26.     private final String utf8;

  27.     private boolean hashCodeComputed;

  28.     private int cachedHashCode;

  29.     public CPUTF8(final String string) {
  30.         this(string, -1);
  31.     }

  32.     /**
  33.      * Creates a new CPUTF8 instance
  34.      *
  35.      * @param utf8        TODO
  36.      * @param globalIndex index in CpBands
  37.      * @throws NullPointerException if utf8 is null
  38.      */
  39.     public CPUTF8(final String utf8, final int globalIndex) {
  40.         super(CP_UTF8, globalIndex);
  41.         this.utf8 = Objects.requireNonNull(utf8, "utf8");
  42.     }

  43.     @Override
  44.     public boolean equals(final Object obj) {
  45.         if (this == obj) {
  46.             return true;
  47.         }
  48.         if (obj == null) {
  49.             return false;
  50.         }
  51.         if (this.getClass() != obj.getClass()) {
  52.             return false;
  53.         }
  54.         final CPUTF8 other = (CPUTF8) obj;
  55.         return utf8.equals(other.utf8);
  56.     }

  57.     private void generateHashCode() {
  58.         hashCodeComputed = true;
  59.         final int PRIME = 31;
  60.         cachedHashCode = PRIME + utf8.hashCode();
  61.     }

  62.     @Override
  63.     public int hashCode() {
  64.         if (!hashCodeComputed) {
  65.             generateHashCode();
  66.         }
  67.         return cachedHashCode;
  68.     }

  69.     public void setGlobalIndex(final int index) {
  70.         globalIndex = index;
  71.     }

  72.     @Override
  73.     public String toString() {
  74.         return StandardCharsets.UTF_8.name() + ":" + utf8;
  75.     }

  76.     public String underlyingString() {
  77.         return utf8;
  78.     }

  79.     @Override
  80.     protected void writeBody(final DataOutputStream dos) throws IOException {
  81.         dos.writeUTF(utf8);
  82.     }
  83. }