/*
 * Copyright 2016-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.rules.coercer;

import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.rules.CellPathResolver;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

import java.nio.file.Path;

public class OptionalTypeCoercer<T> extends LeafTypeCoercer<Optional<T>> {

  private final TypeCoercer<T> coercer;

  public OptionalTypeCoercer(TypeCoercer<T> coercer) {
    Preconditions.checkArgument(
        !coercer.getOutputClass().isAssignableFrom(Optional.class),
        "Nested optional fields are ambiguous.");
    this.coercer = coercer;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Class<Optional<T>> getOutputClass() {
    return (Class<Optional<T>>) (Class<?>) Optional.class;
  }

  @Override
  public Optional<T> coerce(
      CellPathResolver cellRoots,
      ProjectFilesystem filesystem,
      Path pathRelativeToProjectRoot,
      Object object)
      throws CoerceFailedException {
    if (object == null) {
      return Optional.absent();
    }
    return Optional.of(coercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, object));
  }

}

@@ -892,20 +892,19 @@
 %3CT%3E 
-extend
+implement
 s 
-Leaf
 Type
@@ -1359,24 +1359,306 @@
 class;%0A  %7D%0A%0A
+  @Override%0A  public boolean hasElementClass(Class%3C?%3E... types) %7B%0A    return coercer.hasElementClass(types);%0A  %7D%0A%0A  @Override%0A  public void traverse(Optional%3CT%3E object, Traversal traversal) %7B%0A    if (object.isPresent()) %7B%0A      coercer.traverse(object.get(), traversal);%0A    %7D%0A  %7D%0A%0A
   @Override%0A
@@ -1916,16 +1916,16 @@
 ;%0A    %7D%0A
-
     retu
@@ -2019,10 +2019,109 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public Optional%3COptional%3CT%3E%3E getOptionalValue() %7B%0A    return Optional.absent();%0A  %7D%0A%0A
 %7D%0A

Fix semantics for OptionalTypeCoercer