001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.harmony.unpack200.bytecode;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.nio.charset.StandardCharsets;
024import java.util.Objects;
025
026/**
027 * UTF8 constant pool entry, used for storing long Strings.
028 */
029public class CPUTF8 extends ConstantPoolEntry {
030
031    private final String utf8;
032
033    private boolean hashCodeComputed;
034
035    private int cachedHashCode;
036
037    /**
038     * Constructs a new instance.
039     *
040     * @param string a constant pool string.
041     */
042    public CPUTF8(final String string) {
043        this(string, -1);
044    }
045
046    /**
047     * Creates a new instance
048     *
049     * @param string      a constant pool string.
050     * @param globalIndex index in CpBands
051     * @throws NullPointerException if utf8 is null.
052     */
053    public CPUTF8(final String string, final int globalIndex) {
054        super(CP_UTF8, globalIndex);
055        this.utf8 = Objects.requireNonNull(string, "utf8");
056    }
057
058    @Override
059    public boolean equals(final Object obj) {
060        if (this == obj) {
061            return true;
062        }
063        if (obj == null || this.getClass() != obj.getClass()) {
064            return false;
065        }
066        final CPUTF8 other = (CPUTF8) obj;
067        return utf8.equals(other.utf8);
068    }
069
070    private void generateHashCode() {
071        hashCodeComputed = true;
072        final int PRIME = 31;
073        cachedHashCode = PRIME + utf8.hashCode();
074    }
075
076    @Override
077    public int hashCode() {
078        if (!hashCodeComputed) {
079            generateHashCode();
080        }
081        return cachedHashCode;
082    }
083
084    /**
085     * Sets the global index.
086     *
087     * @param index the global index.
088     */
089    public void setGlobalIndex(final int index) {
090        globalIndex = index;
091    }
092
093    @Override
094    public String toString() {
095        return StandardCharsets.UTF_8.name() + ":" + utf8;
096    }
097
098    /**
099     * 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}