Debian Codegraph @

src/org/apache/commons/lang3/ArchUtils.java

raw symbols SCIP
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;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.lang3.arch.Processor;
23 import org.apache.commons.lang3.stream.Streams;
24
25 /**
26 * Provides methods for identifying the architecture of the current JVM based on the {@code "os.arch"} system property.
27 * <p>
28 * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
29 * </p>
30 *
31 * @since 3.6
32 */
33 public class ArchUtils {
34
35 private static final Map<String, Processor> ARCH_TO_PROCESSOR;
36
37 static {
38 ARCH_TO_PROCESSOR = new HashMap<>();
39 init();
40 }
41
42 /**
43 * Adds the given {@link Processor} with the given key {@link String} to the map.
44 *
45 * @param key The key as {@link String}.
46 * @param processor The {@link Processor} to add.
47 * @throws IllegalStateException If the key already exists.
48 */
49 private static void addProcessor(final String key, final Processor processor) {
50 if (ARCH_TO_PROCESSOR.containsKey(key)) {
51 throw new IllegalStateException("Key " + key + " already exists in processor map");
52 }
53 ARCH_TO_PROCESSOR.put(key, processor);
54 }
55
56 /**
57 * Adds the given {@link Processor} with the given keys to the map.
58 *
59 * @param keys The keys.
60 * @param processor The {@link Processor} to add.
61 * @throws IllegalStateException If the key already exists.
62 */
63 private static void addProcessors(final Processor processor, final String... keys) {
64 Streams.of(keys).forEach(e -> addProcessor(e, processor));
65 }
66
67 /**
68 * Gets a {@link Processor} object of the current JVM.
69 *
70 * <p>
71 * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
72 * </p>
73 *
74 * @return A {@link Processor} when supported, else {@code null}.
75 */
76 public static Processor getProcessor() {
77 return getProcessor(SystemProperties.getOsArch());
78 }
79
80 /**
81 * Gets a {@link Processor} object the given value {@link String}. The {@link String} must be like a value returned by the {@code "os.arch"} system
82 * property.
83 *
84 * @param value A {@link String} like a value returned by the {@code os.arch} System Property.
85 * @return A {@link Processor} when it exists, else {@code null}.
86 */
87 public static Processor getProcessor(final String value) {
88 return ARCH_TO_PROCESSOR.get(value);
89 }
90
91 private static void init() {
92 init_X86_32Bit();
93 init_X86_64Bit();
94 init_IA64_32Bit();
95 init_IA64_64Bit();
96 init_PPC_32Bit();
97 init_PPC_64Bit();
98 init_Aarch_64Bit();
99 init_RISCV_32Bit();
100 init_RISCV_64Bit();
101 }
102
103 private static void init_Aarch_64Bit() {
104 addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.AARCH_64), "aarch64");
105 }
106
107 private static void init_IA64_32Bit() {
108 addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64), "ia64_32", "ia64n");
109 }
110
111 private static void init_IA64_64Bit() {
112 addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64), "ia64", "ia64w");
113 }
114
115 private static void init_PPC_32Bit() {
116 addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.PPC), "ppc", "power", "powerpc", "power_pc", "power_rs");
117 }
118
119 private static void init_PPC_64Bit() {
120 addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.PPC), "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64");
121 }
122
123 private static void init_RISCV_32Bit() {
124 addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.RISC_V), "riscv32");
125 }
126
127 private static void init_RISCV_64Bit() {
128 addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.RISC_V), "riscv64");
129 }
130
131 private static void init_X86_32Bit() {
132 addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.X86), "x86", "i386", "i486", "i586", "i686", "pentium");
133 }
134
135 private static void init_X86_64Bit() {
136 addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.X86), "x86_64", "amd64", "em64t", "universal");
137 }
138
139 /**
140 * Make private in 4.0.
141 *
142 * @deprecated TODO Make private in 4.0.
143 */
144 @Deprecated
145 public ArchUtils() {
146 // empty
147 }
148
149 }