/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.util.environment;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/** Represents the CPU architecture of a system. */
public enum Architecture {
  AARCH64("aarch64"),
  ARM("arm"),
  ARMEB("armeb"),
  I386("i386"),
  MIPS("mips"),
  MIPS64("mips64"),
  MIPSEL("mipsel"),
  MIPSEL64("mipsel64"),
  POWERPC("powerpc"),
  PPC64("ppc64"),
  UNKNOWN("unknown"),
  X86_64("x86_64");

  /** Maps names as used in the os.arch property to Architecture values. */
  private static Map<String, Architecture> nameToValueMap;

  static {
    nameToValueMap = new HashMap<>();
    for (Architecture arch : Architecture.values()) {
      nameToValueMap.put(arch.toString(), arch);
    }
    // Also add a few aliases
    nameToValueMap.put("amd64", X86_64);
    nameToValueMap.put("arm64", AARCH64);
  }

  Architecture(String name) {
    this.name = name;
  }

  /** Detect the host architecture from the given Java properties */
  public static Architecture detect(Properties properties) {
    String javaName = properties.getProperty("os.arch");
    Architecture result = nameToValueMap.get(javaName);
    if (result == null) {
      return UNKNOWN;
    } else {
      return result;
    }
  }

  public static Architecture detect() {
    return detect(System.getProperties());
  }

  public static Architecture fromName(String name) {
    return nameToValueMap.getOrDefault(name, UNKNOWN);
  }

  @Override
  public String toString() {
    return name;
  }

  private String name;
}

@@ -821,16 +821,25 @@
 aarch64%22
+, %22arm64%22
 ),%0A  ARM
@@ -1040,16 +1040,25 @@
 %22x86_64%22
+, %22amd64%22
 );%0A%0A  /*
@@ -1349,40 +1349,51 @@
     
-%7D%0A    // Also add a few aliases%0A
+  for (String archName : arch.names) %7B%0A    
     
@@ -1415,71 +1415,104 @@
 put(
-%22amd64%22, X86_64);%0A    nameToValueMap.put(%22arm64%22, AARCH64);%0A  %7D
+archName, arch);%0A      %7D%0A    %7D%0A  %7D%0A%0A  private final String name;%0A  private final String%5B%5D names;
 %0A%0A  
@@ -1527,32 +1527,49 @@
 ture(String name
+, String... names
 ) %7B%0A    this.nam
@@ -1576,25 +1576,138 @@
 e = name;%0A  
-%7D
+  this.names = names;%0A  %7D%0A%0A  private static final Architecture HOST_ARCHITECTURE = detect(System.getProperties());
 %0A%0A  /** Dete
@@ -1891,152 +1891,102 @@
     
-Architecture result = nameToValueMap.get(javaName);%0A    if (result == null) %7B%0A      return UNKNOWN;%0A    %7D else %7B%0A      return result;%0A    %7D%0A  %7D%0A
+return fromName(javaName);%0A  %7D%0A%0A  /** @return CPU architecture of the currently running OS. */
 %0A  p
@@ -2037,38 +2037,25 @@
 urn 
-detect(System.getProperties())
+HOST_ARCHITECTURE
 ;%0A  
@@ -2235,31 +2235,7 @@
 %0A  %7D
-%0A%0A  private String name;
 %0A%7D%0A

Refactor Architecture to be more explicit about aliases