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;
023
024/**
025 * The abstract superclass for all types of class file entries.
026 */
027public abstract class ClassFileEntry {
028
029    /**
030     * An empty ClassFileEntry array.
031     */
032    protected static final ClassFileEntry[] NONE = {};
033    private boolean resolved;
034
035    /**
036     * Writes this instance to the output stream.
037     *
038     * @param dos the output stream.
039     * @throws IOException if an I/O error occurs.
040     */
041    protected abstract void doWrite(DataOutputStream dos) throws IOException;
042
043    @Override
044    public abstract boolean equals(Object arg0);
045
046    /**
047     * Returns an empty array.
048     *
049     * @return an empty array.
050     */
051    protected ClassFileEntry[] getNestedClassFileEntries() {
052        return NONE;
053    }
054
055    @Override
056    public abstract int hashCode();
057
058    /**
059     * Delegates to super {@link #hashCode}.
060     *
061     * @return super {@link #hashCode}.
062     */
063    protected int objectHashCode() {
064        return super.hashCode();
065    }
066
067    /**
068     * Allows the constant pool entries to resolve their nested entries.
069     *
070     * @param pool The class constant pool.
071     */
072    protected void resolve(final ClassConstantPool pool) {
073        resolved = true;
074    }
075
076    @Override
077    public abstract String toString();
078
079    /**
080     * Writes this instance to the output stream.
081     *
082     * @param dos the output stream.
083     * @throws IOException if an I/O error occurs.
084     */
085    public final void write(final DataOutputStream dos) throws IOException {
086        if (!resolved) {
087            throw new IllegalStateException("Entry has not been resolved");
088        }
089        doWrite(dos);
090    }
091
092}