    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;
    023
    024/**
    025 * String constant pool entry.
    026 */
    027public class CPString extends CPConstant {
    028
    029    private transient int nameIndex;
    030    private final CPUTF8 name;
    031
    032    private boolean hashCodeComputed;
    033
    034    private int cachedHashCode;
    035
    036    /**
    037     * Constructs a new instance.
    038     *
    039     * @param value The value.
    040     * @param globalIndex Global index.
    041     */
    042    public CPString(final CPUTF8 value, final int globalIndex) {
    043        super(CP_String, value, globalIndex);
    044        this.name = value;
    045    }
    046
    047    private void generateHashCode() {
    048        hashCodeComputed = true;
    049        final int PRIME = 31;
    050        int result = 1;
    051        result = PRIME * result + name.hashCode();
    052        cachedHashCode = result;
    053    }
    054
    055    @Override
    056    protected ClassFileEntry[] getNestedClassFileEntries() {
    057        return new ClassFileEntry[] { name };
    058    }
    059
    060    @Override
    061    public int hashCode() {
    062        if (!hashCodeComputed) {
    063            generateHashCode();
    064        }
    065        return cachedHashCode;
    066    }
    067
    068    /**
    069     * Allows the constant pool entries to resolve their nested entries
    070     *
    071     * @param pool TODO
    072     */
    073    @Override
    074    protected void resolve(final ClassConstantPool pool) {
    075        super.resolve(pool);
    076        nameIndex = pool.indexOf(name);
    077    }
    078
    079    @Override
    080    public String toString() {
    081        return "String: " + getValue();
    082    }
    083
    084    @Override
    085    protected void writeBody(final DataOutputStream dos) throws IOException {
    086        dos.writeShort(nameIndex);
    087    }
    088}