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

    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;
    018
    019import java.util.HashMap;
    020import java.util.Map;
    021
    022import org.apache.commons.lang3.arch.Processor;
    023import org.apache.commons.lang3.stream.Streams;
    024
    025/**
    026 * Provides methods for identifying the architecture of the current JVM based on the {@code "os.arch"} system property.
    027 * <p>
    028 * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
    029 * </p>
    030 *
    031 * @since 3.6
    032 */
    033public class ArchUtils {
    034
    035    private static final Map<String, Processor> ARCH_TO_PROCESSOR;
    036
    037    static {
    038        ARCH_TO_PROCESSOR = new HashMap<>();
    039        init();
    040    }
    041
    042    /**
    043     * Adds the given {@link Processor} with the given key {@link String} to the map.
    044     *
    045     * @param key       The key as {@link String}.
    046     * @param processor The {@link Processor} to add.
    047     * @throws IllegalStateException If the key already exists.
    048     */
    049    private static void addProcessor(final String key, final Processor processor) {
    050        if (ARCH_TO_PROCESSOR.containsKey(key)) {
    051            throw new IllegalStateException("Key " + key + " already exists in processor map");
    052        }
    053        ARCH_TO_PROCESSOR.put(key, processor);
    054    }
    055
    056    /**
    057     * Adds the given {@link Processor} with the given keys to the map.
    058     *
    059     * @param keys      The keys.
    060     * @param processor The {@link Processor} to add.
    061     * @throws IllegalStateException If the key already exists.
    062     */
    063    private static void addProcessors(final Processor processor, final String... keys) {
    064        Streams.of(keys).forEach(e -> addProcessor(e, processor));
    065    }
    066
    067    /**
    068     * Gets a {@link Processor} object of the current JVM.
    069     *
    070     * <p>
    071     * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
    072     * </p>
    073     *
    074     * @return A {@link Processor} when supported, else {@code null}.
    075     */
    076    public static Processor getProcessor() {
    077        return getProcessor(SystemProperties.getOsArch());
    078    }
    079
    080    /**
    081     * 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
    082     * property.
    083     *
    084     * @param value A {@link String} like a value returned by the {@code os.arch} System Property.
    085     * @return A {@link Processor} when it exists, else {@code null}.
    086     */
    087    public static Processor getProcessor(final String value) {
    088        return ARCH_TO_PROCESSOR.get(value);
    089    }
    090
    091    private static void init() {
    092        init_X86_32Bit();
    093        init_X86_64Bit();
    094        init_IA64_32Bit();
    095        init_IA64_64Bit();
    096        init_PPC_32Bit();
    097        init_PPC_64Bit();
    098        init_Aarch_64Bit();
    099        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}