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