/*
 * 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.sections;

import static android.support.v4.util.Pools.SynchronizedPool;

import android.support.annotation.VisibleForTesting;
import android.support.v4.util.Pools.Pool;
import com.facebook.litho.sections.annotations.GroupSectionSpec;
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class that represents the children of a {@link GroupSectionSpec}. This is used to mimic
 * litho's usage of the Container class in {@link com.facebook.litho.annotations.LayoutSpec}'s API
 */
public class Children {

  private static final Pool<Builder> sBuildersPool = new SynchronizedPool<>(2);

  private List<Section> mSections;

  private Children() {
    mSections = new ArrayList<>();
  }

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

    return builder;
  }

  @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
  public List<Section> getChildren() {
    return mSections;
  }

  public static class Builder {

    private Children mChildren;

    private Builder() {}

    private void init(Children children) {
      mChildren = children;
    }

    public Builder child(Section<?> section) {
      verifyValidState();

      if (section != null) {
        mChildren.mSections.add(section);
      }

      return this;
    }

    public Builder child(Section.Builder<?, ?> sectionBuilder) {
      verifyValidState();

      if (sectionBuilder != null) {
        mChildren.mSections.add(sectionBuilder.build());
      }

      return this;
    }

    public Children build() {
      verifyValidState();

      Children children = mChildren;
      mChildren = null;
      sBuildersPool.release(this);

      return children;
    }

    private void verifyValidState() {
      if (mChildren == null) {
        throw new IllegalStateException(".build() call has been already made on this Builder.");
      }
    }
  }
}

@@ -403,16 +403,60 @@
 dPool;%0A%0A
+import android.support.annotation.Nullable;%0A
 import a
@@ -1602,24 +1602,34 @@
 ilder child(
+@Nullable 
 Section%3C?%3E s
@@ -1800,167 +1800,1074 @@
 ild(
-Section.Builder%3C?, ?%3E sectionBuilder) %7B%0A      verifyValidState();%0A%0A      if (sectionBuilder != null) %7B%0A        mChildren.mSections.add(sectionBuilder.build());
+@Nullable List%3CSection%3C?%3E%3E sectionList) %7B%0A      verifyValidState();%0A%0A      if (sectionList == null %7C%7C sectionList.isEmpty()) %7B%0A        return this;%0A      %7D%0A%0A      for (int i = 0; i %3C sectionList.size(); i++) %7B%0A        Section%3C?%3E section = sectionList.get(i);%0A        if (section != null) %7B%0A          mChildren.mSections.add(section);%0A        %7D%0A      %7D%0A%0A      return this;%0A    %7D%0A%0A    public Builder child(@Nullable Section.Builder%3C?, ?%3E sectionBuilder) %7B%0A      verifyValidState();%0A%0A      if (sectionBuilder != null) %7B%0A        mChildren.mSections.add(sectionBuilder.build());%0A      %7D%0A%0A      return this;%0A    %7D%0A%0A    public Builder children(@Nullable List%3CSection.Builder%3C?, ?%3E%3E sectionBuilderList) %7B%0A      verifyValidState();%0A%0A      if (sectionBuilderList == null %7C%7C sectionBuilderList.isEmpty()) %7B%0A        return this;%0A      %7D%0A%0A      for (int i = 0; i %3C sectionBuilderList.size(); i++) %7B%0A        Section.Builder%3C?, ?%3E sectionBuilder = sectionBuilderList.get(i);%0A        if (sectionBuilder != null) %7B%0A          mChildren.mSections.add(sectionBuilder.build());%0A        %7D
 %0A   

Add API for list of sections and sectionBuilders in Children class