/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2019 wcm.io
 * %%
 * 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.
 * #L%
 */
package io.wcm.qa.glnm.sampling.aem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import io.wcm.qa.glnm.sampling.Sampler;
import io.wcm.qa.glnm.sampling.transform.base.TransformationBasedSampler;

/**
 * Fetches all pages for
 *
 * @since 3.0.0
 */
public class AllPagesForTemplateSampler
    extends TransformationBasedSampler<
        Sampler<Map<String, String>>,
        Map<String, String>,
        Iterable<String>> {

  private static final String PATH_FRAGMENT_JCR_CONTENT = "/jcr:content";
  private static final String PROPERTY_NAME_CQ_TEMPLATE = "cq:template";

  /**
   * <p>Constructor for AllPagesForTemplateSampler.</p>
   *
   * @param namePattern used to constrain template names using jcr:like syntax
   * @param rootPath to constrain where to look for pages
   */
  public AllPagesForTemplateSampler(String namePattern, String rootPath) {
    super(buildInputSampler(namePattern, rootPath));
  }



  @Override
  protected Iterable transform(Map<String, String> inputSample) {
    List<String> result = new ArrayList<String>();
    Collection<String> contentPaths = inputSample.values();
    for (String path : contentPaths) {
      result.add(cleanPath(path));
    }
    return result;
  }

  protected String cleanPath(String path) {
    return StringUtils.removeEnd(path, PATH_FRAGMENT_JCR_CONTENT);
  }

  /**
   * <p>getQuerySampler.</p>
   *
   * @return a {@link io.wcm.qa.glnm.sampling.aem.JcrQuerySampler} object.
   */
  public JcrQuerySampler getQuerySampler() {
    return (JcrQuerySampler)getInput();
  }

  private static JcrQuerySampler buildInputSampler(String templateNamePattern, String rootPath) {
    JcrQuerySampler jcrQuerySampler = new JcrQuerySampler();
    jcrQuerySampler.addLikeProperty(PROPERTY_NAME_CQ_TEMPLATE, templateNamePattern);
    jcrQuerySampler.setPath(rootPath);
    return jcrQuerySampler;
  }

}

@@ -676,25 +676,19 @@
 va.util.
-ArrayList
+Map
 ;%0Aimport
@@ -702,63 +702,25 @@
 til.
-Collection;%0Aimport java.util.List;%0Aimport java.util.Map
+stream.Collectors
 ;%0A%0Ai
@@ -1686,171 +1686,86 @@
     
-List%3CString%3E result = new ArrayList%3CString%3E();%0A    Collection%3CString%3E contentPaths = inputSample.values();%0A    for (String path : contentPaths) %7B%0A      result.add(
+return inputSample.values().stream()%0A        .map(AllPagesForTemplateSampler::
 clea
@@ -1773,58 +1773,70 @@
 Path
-(path));
+)
 %0A    
-%7D%0A
     
-return result;%0A  %7D%0A%0A  protected
+.collect(Collectors.toList());%0A  %7D%0A%0A  private static
  Str

use streaming API