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