    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 *
    017 */
    018package org.apache.bcel.generic;
    019
    020import org.apache.bcel.Const;
    021
    022/**
    023 * Denotes basic type such as int.
    024 *
    025 * @version $Id$
    026 */
    027public final class BasicType extends Type {
    028
    029    /**
    030     * Constructor for basic types such as int, long, `void'
    031     *
    032     * @param type one of T_INT, T_BOOLEAN, ..., T_VOID
    033     * @see Const
    034     */
    035    BasicType(final byte type) {
    036        super(type, Const.getShortTypeName(type));
    037        if ((type < Const.T_BOOLEAN) || (type > Const.T_VOID)) {
    038            throw new ClassGenException("Invalid type: " + type);
    039        }
    040    }
    041
    042
    043    // @since 6.0 no longer final
    044    public static BasicType getType( final byte type ) {
    045        switch (type) {
    046            case Const.T_VOID:
    047                return VOID;
    048            case Const.T_BOOLEAN:
    049                return BOOLEAN;
    050            case Const.T_BYTE:
    051                return BYTE;
    052            case Const.T_SHORT:
    053                return SHORT;
    054            case Const.T_CHAR:
    055                return CHAR;
    056            case Const.T_INT:
    057                return INT;
    058            case Const.T_LONG:
    059                return LONG;
    060            case Const.T_DOUBLE:
    061                return DOUBLE;
    062            case Const.T_FLOAT:
    063                return FLOAT;
    064            default:
    065                throw new ClassGenException("Invalid type: " + type);
    066        }
    067    }
    068
    069
    070    /** @return a hash code value for the object.
    071     */
    072    @Override
    073    public int hashCode() {
    074        return super.getType();
    075    }
    076
    077
    078    /** @return true if both type objects refer to the same type
    079     */
    080    @Override
    081    public boolean equals( final Object _type ) {
    082        return (_type instanceof BasicType) ? ((BasicType) _type).getType() == this.getType() : false;
    083    }
    084}