    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;
    018
    019import java.util.HashMap;
    020import java.util.Map;
    021import java.util.stream.Stream;
    022
    023import org.apache.commons.lang3.arch.Processor;
    024
    025/**
    026 * An utility class for the os.arch System Property. The class defines methods for
    027 * identifying the architecture of the current JVM.
    028 * <p>
    029 * Important: The os.arch System Property returns the architecture used by the JVM
    030 * not of the operating system.
    031 * </p>
    032 * @since 3.6
    033 */
    034public class ArchUtils {
    035
    036    private static final Map<String, Processor> ARCH_TO_PROCESSOR;
    037
    038    static {
    039        ARCH_TO_PROCESSOR = new HashMap<>();
    040        init();
    041    }
    042
    043    private static void init() {
    044        init_X86_32Bit();
    045        init_X86_64Bit();
    046        init_IA64_32Bit();
    047        init_IA64_64Bit();
    048        init_PPC_32Bit();
    049        init_PPC_64Bit();
    050    }
    051
    052    private static void init_X86_32Bit() {
    053        final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.X86);
    054        addProcessors(processor, "x86", "i386", "i486", "i586", "i686", "pentium");
    055    }
    056
    057    private static void init_X86_64Bit() {
    058        final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.X86);
    059        addProcessors(processor, "x86_64", "amd64", "em64t", "universal");
    060    }
    061
    062    private static void init_IA64_32Bit() {
    063        final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64);
    064        addProcessors(processor, "ia64_32", "ia64n");
    065    }
    066
    067    private static void init_IA64_64Bit() {
    068        final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64);
    069        addProcessors(processor, "ia64", "ia64w");
    070    }
    071
    072    private static void init_PPC_32Bit() {
    073        final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.PPC);
    074        addProcessors(processor, "ppc", "power", "powerpc", "power_pc", "power_rs");
    075    }
    076
    077    private static void init_PPC_64Bit() {
    078        final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.PPC);
    079        addProcessors(processor, "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64");
    080    }
    081
    082    /**
    083     * Adds the given {@link Processor} with the given key {@link String} to the map.
    084     *
    085     * @param key The key as {@link String}.
    086     * @param processor The {@link Processor} to add.
    087     * @throws IllegalStateException If the key already exists.
    088     */
    089    private static void addProcessor(final String key, final Processor processor) {
    090        if (ARCH_TO_PROCESSOR.containsKey(key)) {
    091            throw new IllegalStateException("Key " + key + " already exists in processor map");
    092        }
    093        ARCH_TO_PROCESSOR.put(key, processor);
    094    }
    095
    096    /**
    097     * Adds the given {@link Processor} with the given keys to the map.
    098     *
    099     * @param keys The keys.
    100     * @param processor The {@link Processor} to add.
    101     * @throws IllegalStateException If the key already exists.
    102     */
    103    private static void addProcessors(final Processor processor, final String... keys) {
    104        Stream.of(keys).forEach(e -> addProcessor(e, processor));
    105    }
    106
    107    /**
    108     * Returns a {@link Processor} object of the current JVM.
    109     *
    110     * <p>
    111     * Important: The os.arch System Property returns the architecture used by the JVM
    112     * not of the operating system.
    113     * </p>
    114     *
    115     * @return A {@link Processor} when supported, else {@code null}.
    116     */
    117    public static Processor getProcessor() {
    118        return getProcessor(SystemUtils.OS_ARCH);
    119    }
    120
    121    /**
    122     * Returns a {@link Processor} object the given value {@link String}. The {@link String} must be
    123     * like a value returned by the os.arch System Property.
    124     *
    125     * @param value A {@link String} like a value returned by the os.arch System Property.
    126     * @return A {@link Processor} when it exists, else {@code null}.
    127     */
    128    public static Processor getProcessor(final String value) {
    129        return ARCH_TO_PROCESSOR.get(value);
    130    }
    131
    132}