    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.lang3.arch;
    018
    019/**
    020 * The {@link Processor} represents a microprocessor and defines
    021 * some properties like architecture and type of the microprocessor.
    022 *
    023 * @since 3.6
    024 */
    025public class Processor {
    026
    027    /**
    028     * The {@link Arch} enum defines the architecture of
    029     * a microprocessor. The architecture represents the bit value
    030     * of the microprocessor.
    031     * The following architectures are defined:
    032     * <ul>
    033     *     <li>32-bit</li>
    034     *     <li>64-bit</li>
    035     *     <li>Unknown</li>
    036     * </ul>
    037     */
    038    public enum Arch {
    039
    040        /**
    041         * A 32-bit processor architecture.
    042         */
    043        BIT_32("32-bit"),
    044
    045        /**
    046         * A 64-bit processor architecture.
    047         */
    048        BIT_64("64-bit"),
    049
    050        /**
    051         * An unknown-bit processor architecture.
    052         */
    053        UNKNOWN("Unknown");
    054
    055        /**
    056         * A label suitable for display.
    057         *
    058         * @since 3.10
    059         */
    060        private final String label;
    061
    062        Arch(final String label) {
    063            this.label = label;
    064        }
    065
    066        /**
    067         * Gets the label suitable for display.
    068         *
    069         * @return the label.
    070         */
    071        public String getLabel() {
    072            return label;
    073        }
    074    }
    075
    076    /**
    077     * The {@link Type} enum defines types of a microprocessor.
    078     * The following types are defined:
    079     * <ul>
    080     *     <li>x86</li>
    081     *     <li>ia64</li>
    082     *     <li>PPC</li>
    083     *     <li>Unknown</li>
    084     * </ul>
    085     */
    086    public enum Type {
    087
    088        /**
    089         * Intel x86 series of instruction set architectures.
    090         */
    091        X86,
    092
    093        /**
    094         * Intel Itanium  64-bit architecture.
    095         */
    096        IA_64,
    097
    098        /**
    099         * Apple–IBM–Motorola PowerPC architecture.
    100         */
    101        PPC,
    102
    103        /**
    104         * Unknown architecture.
    105         */
    106        UNKNOWN
    107    }
    108
    109    private final Arch arch;
    110    private final Type type;
    111
    112    /**
    113     * Constructs a {@link Processor} object with the given
    114     * parameters.
    115     *
    116     * @param arch The processor architecture.
    117     * @param type The processor type.
    118     */
    119    public Processor(final Arch arch, final Type type) {
    120        this.arch = arch;
    121        this.type = type;
    122    }
    123
    124    /**
    125     * Returns the processor architecture as an {@link Arch} enum.
    126     * The processor architecture defines, if the processor has
    127     * a 32 or 64 bit architecture.
    128     *
    129     * @return A {@link Arch} enum.
    130     */
    131    public Arch getArch() {
    132        return arch;
    133    }
    134
    135    /**
    136     * Returns the processor type as {@link Type} enum.
    137     * The processor type defines, if the processor is for example
    138     * a x86 or PPA.
    139     *
    140     * @return A {@link Type} enum.
    141     */
    142    public Type getType() {
    143        return type;
    144    }
    145
    146    /**
    147     * Checks if {@link Processor} is 32 bit.
    148     *
    149     * @return {@code true}, if {@link Processor} is {@link Arch#BIT_32}, else {@code false}.
    150     */
    151    public boolean is32Bit() {
    152        return Arch.BIT_32.equals(arch);
    153    }
    154
    155    /**
    156     * Checks if {@link Processor} is 64 bit.
    157     *
    158     * @return {@code true}, if {@link Processor} is {@link Arch#BIT_64}, else {@code false}.
    159     */
    160    public boolean is64Bit() {
    161        return Arch.BIT_64.equals(arch);
    162    }
    163
    164    /**
    165     * Checks if {@link Processor} is type of x86.
    166     *
    167     * @return {@code true}, if {@link Processor} is {@link Type#X86}, else {@code false}.
    168     */
    169    public boolean isX86() {
    170        return Type.X86.equals(type);
    171    }
    172
    173    /**
    174     * Checks if {@link Processor} is type of Intel Itanium.
    175     *
    176     * @return {@code true}. if {@link Processor} is {@link Type#IA_64}, else {@code false}.
    177     */
    178    public boolean isIA64() {
    179        return Type.IA_64.equals(type);
    180    }
    181
    182    /**
    183     * Checks if {@link Processor} is type of Power PC.
    184     *
    185     * @return {@code true}. if {@link Processor} is {@link Type#PPC}, else {@code false}.
    186     */
    187    public boolean isPPC() {
    188        return Type.PPC.equals(type);
    189    }
    190
    191}