Source code
Description: source: package: org.apache.commons.lang3.arch, class: Processor

    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 *      https://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        private final String label;
    059
    060        Arch(final String label) {
    061            this.label = label;
    062        }
    063
    064        /**
    065         * Gets the label suitable for display.
    066         *
    067         * @return the label.
    068         */
    069        public String getLabel() {
    070            return label;
    071        }
    072    }
    073
    074    /**
    075     * The {@link Type} enum defines types of a microprocessor.
    076     * The following types are defined:
    077     * <ul>
    078     *     <li>AArch64</li>
    079     *     <li>x86</li>
    080     *     <li>ia64</li>
    081     *     <li>PPC</li>
    082     *     <li>RISCV</li>
    083     *     <li>Unknown</li>
    084     * </ul>
    085     */
    086    public enum Type {
    087
    088        /**
    089         * ARM 64-bit.
    090         *
    091         * @since 3.13.0
    092         */
    093        AARCH_64("AArch64"),
    094
    095        /**
    096         * Intel x86 series of instruction set architectures.
    097         */
    098        X86("x86"),
    099
    100        /**
    101         * Intel Itanium 64-bit architecture.
    102         */
    103        IA_64("IA-64"),
    104
    105        /**
    106         * Apple–IBM–Motorola PowerPC architecture.
    107         */
    108        PPC("PPC"),
    109
    110        /**
    111         * RISC-V architecture.
    112         *
    113         * @since 3.14.0
    114         */
    115        RISC_V("RISC-V"),
    116
    117        /**
    118         * Unknown architecture.
    119         */
    120        UNKNOWN("Unknown");
    121
    122        /**
    123         * A label suitable for display.
    124         */
    125        private final String label;
    126
    127        Type(final String label) {
    128            this.label = label;
    129        }
    130
    131        /**
    132         * Gets the label suitable for display.
    133         *
    134         * @return the label.
    135         * @since 3.13.0
    136         */
    137        public String getLabel() {
    138            return label;
    139        }
    140
    141    }
    142
    143    private final Arch arch;
    144    private final Type type;
    145
    146    /**
    147     * Constructs a {@link Processor} object with the given
    148     * parameters.
    149     *
    150     * @param arch The processor architecture.
    151     * @param type The processor type.
    152     */
    153    public Processor(final Arch arch, final Type type) {
    154        this.arch = arch;
    155        this.type = type;
    156    }
    157
    158    /**
    159     * Gets the processor architecture as an {@link Arch} enum.
    160     * The processor architecture defines, if the processor has
    161     * a 32 or 64 bit architecture.
    162     *
    163     * @return A {@link Arch} enum.
    164     */
    165    public Arch getArch() {
    166        return arch;
    167    }
    168
    169    /**
    170     * Gets the processor type as {@link Type} enum.
    171     * The processor type defines, if the processor is for example
    172     * an x86 or PPA.
    173     *
    174     * @return A {@link Type} enum.
    175     */
    176    public Type getType() {
    177        return type;
    178    }
    179
    180    /**
    181     * Tests if {@link Processor} is 32 bit.
    182     *
    183     * @return {@code true}, if {@link Processor} is {@link Arch#BIT_32}, else {@code false}.
    184     */
    185    public boolean is32Bit() {
    186        return Arch.BIT_32 == arch;
    187    }
    188
    189    /**
    190     * Tests if {@link Processor} is 64 bit.
    191     *
    192     * @return {@code true}, if {@link Processor} is {@link Arch#BIT_64}, else {@code false}.
    193     */
    194    public boolean is64Bit() {
    195        return Arch.BIT_64 == arch;
    196    }
    197
    198    /**
    199     * Tests if {@link Processor} is type of Aarch64.
    200     *
    201     * @return {@code true}, if {@link Processor} is {@link Type#AARCH_64}, else {@code false}.
    202     * @since 3.13.0
    203     */
    204    public boolean isAarch64() {
    205        return Type.AARCH_64 == type;
    206    }
    207
    208    /**
    209     * Tests if {@link Processor} is type of Intel Itanium.
    210     *
    211     * @return {@code true}. if {@link Processor} is {@link Type#IA_64}, else {@code false}.
    212     */
    213    public boolean isIA64() {
    214        return Type.IA_64 == type;
    215    }
    216
    217    /**
    218     * Tests if {@link Processor} is type of Power PC.
    219     *
    220     * @return {@code true}. if {@link Processor} is {@link Type#PPC}, else {@code false}.
    221     */
    222    public boolean isPPC() {
    223        return Type.PPC == type;
    224    }
    225
    226    /**
    227     * Tests if {@link Processor} is type of RISC-V.
    228     *
    229     * @return {@code true}. if {@link Processor} is {@link Type#RISC_V}, else {@code false}.
    230     * @since 3.14.0
    231     */
    232    public boolean isRISCV() {
    233        return Type.RISC_V == type;
    234    }
    235
    236    /**
    237     * Tests if {@link Processor} is type of x86.
    238     *
    239     * @return {@code true}, if {@link Processor} is {@link Type#X86}, else {@code false}.
    240     */
    241    public boolean isX86() {
    242        return Type.X86 == type;
    243    }
    244
    245    @Override
    246    public String toString() {
    247        final StringBuilder builder = new StringBuilder();
    248        builder.append(type.getLabel()).append(' ').append(arch.getLabel());
    249        return builder.toString();
    250    }
    251
    252}