/*
 * Copyright 2014-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.java;

import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.rules.AbstractBuildable;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.FakeBuildRuleParams;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSortedSet;

import java.nio.file.Path;

public class PrebuiltJarBuilder {

  private PrebuiltJarBuilder() {
    // Utility class
  }

  public static Builder createBuilder(BuildTarget target) {
    return new Builder(target);
  }

  public static class Builder {

    private Path binaryJar;
    private Optional<String> javadocUrl = Optional.absent();
    private Optional<Path> sourceJar = Optional.absent();
    private final BuildTarget target;
    private final ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();

    public Builder(BuildTarget target) {
      this.target = target;
    }

    public Builder setBinaryJar(Path binaryJar) {
      this.binaryJar = binaryJar;
      return this;
    }

    public Builder addDep(BuildRule dep) {
      deps.add(dep);
      return this;
    }

    public PrebuiltJar build() {
      FakeBuildRuleParams params = new FakeBuildRuleParams(target, deps.build());
      return new PrebuiltJar(params, binaryJar, sourceJar, javadocUrl);
    }

    public BuildRule build(BuildRuleResolver resolver) {
      BuildRule rule = new AbstractBuildable.AnonymousBuildRule(
          PrebuiltJarDescription.TYPE,
          build(),
          new FakeBuildRuleParams(target, deps.build()));
      resolver.addToIndex(target, rule);
      return rule;
    }
  }
}

@@ -721,20 +721,18 @@
 actBuild
-abl
 e
+r
 ;%0Aimport
@@ -771,621 +771,293 @@
 le;%0A
-import com.facebook.buck.rules.BuildRuleResolver;%0Aimport com.facebook.buck.rules.FakeBuildRuleParams;%0Aimport com.google.common.base.Optional;%0Aimport com.google.common.collect.ImmutableSortedSet;%0A%0Aimport java.nio.file.Path;%0A%0Apublic class PrebuiltJarBuilder %7B%0A%0A  private PrebuiltJarBuilder() %7B%0A    // Utility class%0A  %7D%0A%0A  public static Builder createBuilder(BuildTarget target) %7B%0A    return new Builder(target);%0A  %7D%0A%0A  public static class Builder %7B%0A%0A    private Path binaryJar;%0A    private Optional%3CString%3E javadocUrl = Optional.absent();%0A    private Optional%3CPath%3E sourceJar = Optional.absent();%0A    private final 
+%0Aimport java.nio.file.Path;%0A%0Apublic class PrebuiltJarBuilder extends AbstractBuilder%3CPrebuiltJar, PrebuiltJarDescription.Arg%3E %7B%0A%0A  protected PrebuiltJarBuilder(BuildTarget target) %7B%0A    super(new PrebuiltJarDescription(), target);%0A  %7D%0A%0A  public static PrebuiltJarBuilder createBuilder(
 Buil
@@ -1074,196 +1074,80 @@
 rget
-;
+) %7B
 %0A    
-private final ImmutableSortedSet.Builder%3CBuildRule%3E deps = ImmutableSortedSet.naturalOrder();%0A%0A    public Builder(BuildTarget target) %7B%0A      this.target = target
+return new PrebuiltJarBuilder(target)
 ;%0A  
- 
- 
 %7D%0A%0A  
- 
- 
 public 
+PrebuiltJar
 Buil
@@ -1189,14 +1189,11 @@
     
-  
-this
+arg
 .bin
@@ -1212,26 +1212,24 @@
 aryJar;%0A    
-  
 return this;
@@ -1235,578 +1235,100 @@
 ;%0A  
- 
- 
 %7D%0A%0A  
-  public Builder addDep(BuildRule dep) %7B%0A      deps.add(dep);%0A      return this;%0A    %7D%0A%0A    public PrebuiltJar build() %7B%0A      FakeBuildRuleParams params = new FakeBuildRuleParams(target, deps.build());%0A      return new PrebuiltJar(params, binaryJar, sourceJar, javadocUrl);%0A    %7D%0A%0A    public BuildRule build(BuildRuleResolver resolver) %7B%0A      BuildRule rule = new AbstractBuildable.AnonymousBuildRule(%0A          PrebuiltJarDescription.TYPE,%0A          build(),%0A          new FakeBuildRuleParams(target, deps.build()));%0A      resolver.addToIndex(target, rule
+public PrebuiltJarBuilder addDep(BuildRule dep) %7B%0A    arg.deps = amend(arg.deps, dep
 );%0A
-  
     
@@ -1338,22 +1338,17 @@
 urn 
-rule
+this
 ;%0A  
- 
- 
 %7D%0A
-  %7D
 %0A%7D%0A

Convert PrebuiltJarBuilder to use AbstractBuilder.