// Copyright (C) 2017 The Android Open Source Project
//
// 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.google.gerrit.elasticsearch;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

@Singleton
class ElasticIndexVersionDiscovery {
  private final RestClient client;

  @Inject
  ElasticIndexVersionDiscovery(ElasticRestClientBuilder clientBuilder) {
    this.client = clientBuilder.build();
  }

  List<String> discover(String prefix, String indexName) throws IOException {
    String name = prefix + indexName + "_";
    Response response = client.performRequest(HttpGet.METHOD_NAME, name + "*/_aliases");

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
      String content = AbstractElasticIndex.getContent(response);
      JsonObject object = new JsonParser().parse(content).getAsJsonObject();

      List<String> versions = new ArrayList<>(object.size());
      for (Entry<String, JsonElement> entry : object.entrySet()) {
        versions.add(entry.getKey().replace(name, ""));
      }
      return versions;
    }
    return Collections.emptyList();
  }
}

@@ -655,72 +655,52 @@
 ort 
-com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObjec
+static java.util.stream.Collectors.toLis
 t;%0A
+%0A
 impo
@@ -831,36 +831,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -884,36 +884,8 @@
 st;%0A
-import java.util.Map.Entry;%0A
 impo
@@ -1483,24 +1483,11 @@
    i
-nt statusCode = 
+f (
 resp
@@ -1526,28 +1526,8 @@
 de()
-;%0A    if (statusCode
  == 
@@ -1556,25 +1556,49 @@
     
-String content = 
+return new JsonParser()%0A          .parse(
 Abst
@@ -1638,67 +1638,20 @@
 nse)
-;
+)
 %0A      
-JsonObject object = new JsonParser().parse(content)
+    
 .get
@@ -1668,166 +1668,72 @@
 ct()
-;%0A
 %0A      
-List%3CString%3E versions = new ArrayList%3C%3E(object.size());%0A      for (Entry%3CString, JsonElement%3E entry : object.entrySet()) %7B%0A        versions.add(entry
+    .entrySet()%0A          .stream()%0A          .map(e -%3E e
 .get
@@ -1760,39 +1760,37 @@
 %22%22))
-;
 %0A      
-%7D%0A
     
-  return versions
+.collect(toList())
 ;%0A  

Convert to Java stream API