/*
 * Copyright (c) 2017-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.litho.widget;

import android.support.v4.util.Pools;
import com.facebook.litho.Component;

/** {@link RenderInfo} that can render components. */
public class ComponentRenderInfo extends RenderInfo {

  private static final Pools.Pool<Builder> sBuilderPool = new Pools.SynchronizedPool<>(2);

  private final Component mComponent;

  public static Builder create() {
    Builder builder = sBuilderPool.acquire();
    if (builder == null) {
      builder = new Builder();
    }

    return builder;
  }

  private ComponentRenderInfo(Builder builder) {
    super(builder);
    mComponent = builder.component;
  }

  /** Create empty {@link ComponentRenderInfo} for testing purposes. */
  public static RenderInfo createEmpty() {
    return new ComponentRenderInfo(new Builder());
  }

  @Override
  public Component getComponent() {
    return mComponent;
  }

  @Override
  public boolean rendersComponent() {
    return true;
  }

  @Override
  public String getName() {
    return mComponent.getSimpleName();
  }

  public static class Builder extends RenderInfo.Builder<Builder> {
    private Component component;

    /** Specify {@link Component} that will be rendered as an item of the list. */
    public Builder component(Component component) {
      this.component = component;
      return this;
    }

    public ComponentRenderInfo build() {
      if (component == null) {
        throw new IllegalStateException("Component must be provided.");
      }
      final ComponentRenderInfo renderInfo = new ComponentRenderInfo(this);
      release();

      return renderInfo;
    }

    @Override
    void release() {
      super.release();
      component = null;
      sBuilderPool.release(this);
    }
  }
}

@@ -404,22 +404,100 @@
 litho.Co
-mponen
+lumn;%0Aimport com.facebook.litho.Component;%0Aimport com.facebook.litho.ComponentContex
 t;%0A%0A/** 
@@ -972,16 +972,131 @@
 ilder);%0A
+%0A    if (builder.component == null) %7B%0A      throw new IllegalStateException(%22Component must be provided.%22);%0A    %7D%0A%0A
     mCom
@@ -1178,29 +1178,8 @@
 nfo%7D
- for testing purposes
 . */
@@ -1237,45 +1237,55 @@
 urn 
-new ComponentRenderInfo(new Builder()
+create().component(new EmptyComponent()).build(
 );%0A 
@@ -1862,119 +1862,8 @@
 ) %7B%0A
-      if (component == null) %7B%0A        throw new IllegalStateException(%22Component must be provided.%22);%0A      %7D%0A
     
@@ -2109,11 +2109,483 @@
    %7D%0A  %7D
+%0A%0A  private static class EmptyComponent extends Component %7B%0A%0A    @Override%0A    public String getSimpleName() %7B%0A      return %22EmptyComponent%22;%0A    %7D%0A%0A    @Override%0A    protected Component onCreateLayout(ComponentContext c) %7B%0A      return Column.create(c).build();%0A    %7D%0A%0A    @Override%0A    public boolean isEquivalentTo(Component other) %7B%0A      return EmptyComponent.this == other%0A          %7C%7C (other != null && EmptyComponent.this.getClass() == other.getClass());%0A    %7D%0A  %7D
 %0A%7D%0A

Fix crash due to ComponentRenderInfo having no component