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;
023import java.util.Objects;
024
025/**
026 * Constant pool entry for a class
027 */
028public class CPClass extends ConstantPoolEntry {
029
030    private int index;
031
032    public String name;
033
034    private final CPUTF8 utf8;
035
036    private boolean hashCodeComputed;
037
038    private int cachedHashCode;
039
040    /**
041     * Creates a new CPClass
042     *
043     * @param name        TODO
044     * @param globalIndex index in CpBands
045     * @throws NullPointerException if name is null
046     */
047    public CPClass(final CPUTF8 name, final int globalIndex) {
048        super(CP_Class, globalIndex);
049        this.name = Objects.requireNonNull(name, "name").underlyingString();
050        this.utf8 = name;
051    }
052
053    @Override
054    public boolean equals(final Object obj) {
055        if (this == obj) {
056            return true;
057        }
058        if (obj == null || this.getClass() != obj.getClass()) {
059            return false;
060        }
061        final CPClass other = (CPClass) obj;
062        return utf8.equals(other.utf8);
063    }
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}