| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.apache.commons.lang3.arch; |
| 18 | |
| 19 | /** |
| 20 | * The {@link Processor} represents a microprocessor and defines |
| 21 | * some properties like architecture and type of the microprocessor. |
| 22 | * |
| 23 | * @since 3.6 |
| 24 | */ |
| 25 | public class Processor { |
| 26 | |
| 27 | /** |
| 28 | * The {@link Arch} enum defines the architecture of |
| 29 | * a microprocessor. The architecture represents the bit value |
| 30 | * of the microprocessor. |
| 31 | * The following architectures are defined: |
| 32 | * <ul> |
| 33 | * <li>32-bit</li> |
| 34 | * <li>64-bit</li> |
| 35 | * <li>Unknown</li> |
| 36 | * </ul> |
| 37 | */ |
| 38 | public enum Arch { |
| 39 | |
| 40 | /** |
| 41 | * A 32-bit processor architecture. |
| 42 | */ |
| 43 | BIT_32("32-bit"), |
| 44 | |
| 45 | /** |
| 46 | * A 64-bit processor architecture. |
| 47 | */ |
| 48 | BIT_64("64-bit"), |
| 49 | |
| 50 | /** |
| 51 | * An unknown-bit processor architecture. |
| 52 | */ |
| 53 | UNKNOWN("Unknown"); |
| 54 | |
| 55 | /** |
| 56 | * A label suitable for display. |
| 57 | */ |
| 58 | private final String label; |
| 59 | |
| 60 | Arch(final String label) { |
| 61 | this.label = label; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the label suitable for display. |
| 66 | * |
| 67 | * @return the label. |
| 68 | */ |
| 69 | public String getLabel() { |
| 70 | return label; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The {@link Type} enum defines types of a microprocessor. |
| 76 | * The following types are defined: |
| 77 | * <ul> |
| 78 | * <li>AArch64</li> |
| 79 | * <li>x86</li> |
| 80 | * <li>ia64</li> |
| 81 | * <li>PPC</li> |
| 82 | * <li>RISCV</li> |
| 83 | * <li>Unknown</li> |
| 84 | * </ul> |
| 85 | */ |
| 86 | public enum Type { |
| 87 | |
| 88 | /** |
| 89 | * ARM 64-bit. |
| 90 | * |
| 91 | * @since 3.13.0 |
| 92 | */ |
| 93 | AARCH_64("AArch64"), |
| 94 | |
| 95 | /** |
| 96 | * Intel x86 series of instruction set architectures. |
| 97 | */ |
| 98 | X86("x86"), |
| 99 | |
| 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 | * |
| 203 | * @since 3.13.0 |
| 204 | */ |
| 205 | public boolean isAarch64() { |
| 206 | return Type.AARCH_64 == type; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests if {@link Processor} is type of Intel Itanium. |
| 211 | * |
| 212 | * @return {@code true}. if {@link Processor} is {@link Type#IA_64}, else {@code false}. |
| 213 | */ |
| 214 | public boolean isIA64() { |
| 215 | return Type.IA_64 == type; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Tests if {@link Processor} is type of Power PC. |
| 220 | * |
| 221 | * @return {@code true}. if {@link Processor} is {@link Type#PPC}, else {@code false}. |
| 222 | */ |
| 223 | public boolean isPPC() { |
| 224 | return Type.PPC == type; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests if {@link Processor} is type of RISC-V. |
| 229 | * |
| 230 | * @return {@code true}. if {@link Processor} is {@link Type#RISC_V}, else {@code false}. |
| 231 | * @since 3.14.0 |
| 232 | */ |
| 233 | public boolean isRISCV() { |
| 234 | return Type.RISC_V == type; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Tests if {@link Processor} is type of x86. |
| 239 | * |
| 240 | * @return {@code true}, if {@link Processor} is {@link Type#X86}, else {@code false}. |
| 241 | */ |
| 242 | public boolean isX86() { |
| 243 | return Type.X86 == type; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public String toString() { |
| 248 | final StringBuilder builder = new StringBuilder(); |
| 249 | builder.append(type.getLabel()).append(' ').append(arch.getLabel()); |
| 250 | return builder.toString(); |
| 251 | } |
| 252 | |
| 253 | } |