/*
 * Copyright 2012 Greplin, Inc. All Rights Reserved.
 */

package com.greplin.lucene.index;

import com.google.common.collect.Lists;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ReaderUtil;

import java.util.List;

/**
 * Utilities for IndexReaders.
 */
public final class IndexReaders {

  /** Not instantiable. */
  private IndexReaders() { }


  /**
   * Constructs a list and gathers subreaders into it.  Subreaders will
   * be added to the list in doc id order.
   * @param readers the primary readers to gather from
   * @return a list of subreaders
   */
  public static List<IndexReader> gatherSubReaders(
      final IndexReader... readers) {
    if (readers.length == 1 && readers[0] instanceof GatheredSubReaders) {
      return ((GatheredSubReaders) readers[0]).gatherSubReaders();
    }
    List<IndexReader> result = Lists.newArrayList();
    for (IndexReader reader : readers) {
      if (reader != null) {
        if (reader instanceof GatheredSubReaders) {
          List<IndexReader> gathered =
              ((GatheredSubReaders) reader).gatherSubReaders();
          if (gathered == null) {
            result.add(reader);
          } else {
            result.addAll(gathered);
          }
        } else {
          ReaderUtil.gatherSubReaders(result, reader);
        }
      }
    }
    return result;
  }

}

@@ -89,16 +89,64 @@
 index;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -807,22 +807,54 @@
 %7B%0A      
-return
+List%3CIndexReader%3E gathered =%0A         
  ((Gathe
@@ -896,24 +896,97 @@
 bReaders();%0A
+      return gathered == null ? ImmutableList.of(readers%5B0%5D) : gathered;%0A
     %7D%0A    Li

Fix for terminal node case