/*
 * Copyright 2018-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.core.model;

import com.google.common.base.Joiner;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.util.Optional;

public abstract class AbstractBuildTarget implements BuildTarget {

  @Override
  public abstract UnconfiguredBuildTargetView getUnconfiguredBuildTargetView();

  @Override
  public UnflavoredBuildTargetView getUnflavoredBuildTarget() {
    return getUnconfiguredBuildTargetView().getUnflavoredBuildTargetView();
  }

  @Override
  public ImmutableSortedSet<Flavor> getFlavors() {
    return getUnconfiguredBuildTargetView().getFlavors();
  }

  @Override
  public Optional<String> getCell() {
    return getUnconfiguredBuildTargetView().getCell();
  }

  @Override
  public Path getCellPath() {
    return getUnconfiguredBuildTargetView().getCellPath();
  }

  @Override
  public String getBaseName() {
    return getUnconfiguredBuildTargetView().getBaseName();
  }

  @Override
  public Path getBasePath() {
    return getUnconfiguredBuildTargetView().getBasePath();
  }

  @Override
  public String getShortName() {
    return getUnconfiguredBuildTargetView().getShortName();
  }

  @Override
  public String getShortNameAndFlavorPostfix() {
    return getUnconfiguredBuildTargetView().getShortNameAndFlavorPostfix();
  }

  @Override
  public String getFlavorPostfix() {
    if (getFlavors().isEmpty()) {
      return "";
    }
    return "#" + getFlavorsAsString();
  }

  protected String getFlavorsAsString() {
    return Joiner.on(",").join(getFlavors());
  }

  @Override
  public String getFullyQualifiedName() {
    return getUnconfiguredBuildTargetView().getFullyQualifiedName();
  }

  @Override
  public boolean isFlavored() {
    return getUnconfiguredBuildTargetView().isFlavored();
  }

  @Override
  public BuildTarget assertUnflavored() {
    getUnconfiguredBuildTargetView().assertUnflavored();
    return this;
  }

  @Override
  public int compareTo(BuildTarget o) {
    if (this == o) {
      return 0;
    }

    return ComparisonChain.start()
        .compare(getUnconfiguredBuildTargetView(), o.getUnconfiguredBuildTargetView())
        .result();
  }
}

@@ -681,58 +681,8 @@
 er;%0A
-import com.google.common.collect.ComparisonChain;%0A
 impo
@@ -2626,55 +2626,96 @@
     
-return ComparisonChain.start()%0A        
+int unconfiguredBuildTargetComparison =%0A        getUnconfiguredBuildTargetView()
 .compare
 (get
@@ -2710,17 +2710,21 @@
 .compare
-(
+To(o.
 getUncon
@@ -2751,64 +2751,613 @@
 ew()
-, o.getUnconfiguredBuildTargetView())%0A        .result();
+);%0A    if (unconfiguredBuildTargetComparison != 0) %7B%0A      return unconfiguredBuildTargetComparison;%0A    %7D%0A    if (getTargetConfiguration().equals(o.getTargetConfiguration())) %7B%0A      return 0;%0A    %7D%0A    if (getTargetConfiguration().hashCode() == o.getTargetConfiguration().hashCode()) %7B%0A      // configurations are not equal, but hash codes are equal (hash collision)%0A      return System.identityHashCode(getTargetConfiguration())%0A          - System.identityHashCode(o.getTargetConfiguration());%0A    %7D else %7B%0A      return getTargetConfiguration().hashCode() - o.getTargetConfiguration().hashCode();%0A    %7D
 %0A  %7D

Extend BuildTarget.compareTo to use TargetConfiguration