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(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
066    private void generateHashCode() {
067        hashCodeComputed = true;
068        cachedHashCode = utf8.hashCode();
069    }
070
071    public String getName() {
072        return name;
073    }
074
075    @Override
076    protected ClassFileEntry[] getNestedClassFileEntries() {
077        return new ClassFileEntry[] { utf8, };
078    }
079
080    @Override
081    public int hashCode() {
082        if (!hashCodeComputed) {
083            generateHashCode();
084        }
085        return cachedHashCode;
086    }
087
088    @Override
089    protected void resolve(final ClassConstantPool pool) {
090        super.resolve(pool);
091        index = pool.indexOf(utf8);
092    }
093
094    @Override
095    public String toString() {
096        return "Class: " + getName();
097    }
098
099    @Override
100    protected void writeBody(final DataOutputStream dos) throws IOException {
101        dos.writeShort(index);
102    }
103}