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