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