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