    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.util.Objects;
    022
    023/**
    024 * Constant pool entry for a class
    025 */
    026public class CPClass extends ConstantPoolEntry {
    027
    028    private int index;
    029
    030    public String name;
    031
    032    private final CPUTF8 utf8;
    033
    034    private boolean hashCodeComputed;
    035
    036    private int cachedHashCode;
    037
    038    /**
    039     * Creates a new CPClass
    040     *
    041     * @param name TODO
    042     * @param globalIndex index in CpBands
    043     * @throws NullPointerException if name is null
    044     */
    045    public CPClass(final CPUTF8 name, final int globalIndex) {
    046        super(ConstantPoolEntry.CP_Class, globalIndex);
    047        this.name = Objects.requireNonNull(name, "name").underlyingString();
    048        this.utf8 = name;
    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 CPClass other = (CPClass) obj;
    063        return utf8.equals(other.utf8);
    064    }
    065    private void generateHashCode() {
    066        hashCodeComputed = true;
    067        cachedHashCode = utf8.hashCode();
    068    }
    069
    070    public String getName() {
    071        return name;
    072    }
    073
    074    @Override
    075    protected ClassFileEntry[] getNestedClassFileEntries() {
    076        return new ClassFileEntry[] {utf8,};
    077    }
    078
    079    @Override
    080    public int hashCode() {
    081        if (!hashCodeComputed) {
    082            generateHashCode();
    083        }
    084        return cachedHashCode;
    085    }
    086
    087    @Override
    088    protected void resolve(final ClassConstantPool pool) {
    089        super.resolve(pool);
    090        index = pool.indexOf(utf8);
    091    }
    092
    093    @Override
    094    public String toString() {
    095        return "Class: " + getName();
    096    }
    097
    098    @Override
    099    protected void writeBody(final DataOutputStream dos) throws IOException {
    100        dos.writeShort(index);
    101    }
    102}