Skip to content

Commit

Permalink
Convert some uses of coll.toArray(coll.size()) to coll.toArray(Type[]…
Browse files Browse the repository at this point in the history
…::new) (#94816)
  • Loading branch information
thecoop committed Apr 17, 2023
1 parent 350274a commit 0c528cc
Show file tree
Hide file tree
Showing 18 changed files with 35 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public JtsGeometry buildS4J() {
if (parts.size() == 1) {
geometry = parts.get(0);
} else {
LineString[] lineStrings = parts.toArray(new LineString[parts.size()]);
geometry = FACTORY.createMultiLineString(lineStrings);
geometry = FACTORY.createMultiLineString(parts.toArray(LineString[]::new));
}
} else {
LineString[] lineStrings = new LineString[lines.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static ByteBuffer[] toByteBuffers(BytesReference reference) {
while ((r = byteRefIterator.next()) != null) {
buffers.add(ByteBuffer.wrap(r.bytes, r.offset, r.length));
}
return buffers.toArray(new ByteBuffer[buffers.size()]);
return buffers.toArray(ByteBuffer[]::new);

} catch (IOException e) {
// this is really an error since we don't do IO in our bytesreferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,7 @@ public Builder put(Settings settings, boolean copySecureSettings) {
}

private void processLegacyLists(Map<String, Object> map) {
String[] array = map.keySet().toArray(new String[map.size()]);
for (String key : array) {
for (String key : map.keySet().toArray(String[]::new)) {
if (key.endsWith(".0")) { // let's only look at the head of the list and convert in order starting there.
int counter = 0;
String prefix = key.substring(0, key.lastIndexOf('.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static Settings filterSettings(Iterable<String> patterns, Settings setti
}
}
if (simpleMatchPatternList.isEmpty() == false) {
String[] simpleMatchPatterns = simpleMatchPatternList.toArray(new String[simpleMatchPatternList.size()]);
String[] simpleMatchPatterns = simpleMatchPatternList.toArray(String[]::new);
builder.keys().removeIf(key -> Regex.simpleMatch(simpleMatchPatterns, key));
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ public Path[] resolveIndexFolder(String indexFolderName) {
paths.add(indexFolder);
}
}
return paths.toArray(new Path[paths.size()]);
return paths.toArray(Path[]::new);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ protected void testFilter(Builder expected, Builder actual, Collection<String> i
if (includes == null) {
sourceIncludes = randomBoolean() ? Strings.EMPTY_ARRAY : null;
} else {
sourceIncludes = includes.toArray(new String[includes.size()]);
sourceIncludes = includes.toArray(String[]::new);
}
String[] sourceExcludes;
if (excludes == null) {
sourceExcludes = randomBoolean() ? Strings.EMPTY_ARRAY : null;
} else {
sourceExcludes = excludes.toArray(new String[excludes.size()]);
sourceExcludes = excludes.toArray(String[]::new);
}

assertMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ protected void testFilter(Builder expected, Builder actual, Collection<String> i
if (includes == null) {
sourceIncludes = randomBoolean() ? Strings.EMPTY_ARRAY : null;
} else {
sourceIncludes = includes.toArray(new String[includes.size()]);
sourceIncludes = includes.toArray(String[]::new);
}
String[] sourceExcludes;
if (excludes == null) {
sourceExcludes = randomBoolean() ? Strings.EMPTY_ARRAY : null;
} else {
sourceExcludes = excludes.toArray(new String[excludes.size()]);
sourceExcludes = excludes.toArray(String[]::new);
}
SourceFilter filter = new SourceFilter(sourceIncludes, sourceExcludes);
Source filtered = Source.fromBytes(toBytesReference(actual, xContentType, humanReadable), xContentType).filter(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static List<String> getInsertPaths(XContentParser parser, Stack<String> currentP
currentPath.push(parser.currentName().replaceAll("\\.", "\\\\."));
}
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
validPaths.add(String.join(".", currentPath.toArray(new String[currentPath.size()])));
validPaths.add(String.join(".", currentPath.toArray(String[]::new)));
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
if (parser.currentToken() == XContentParser.Token.START_OBJECT
|| parser.currentToken() == XContentParser.Token.START_ARRAY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ public static void assertSearchHits(SearchResponse searchResponse, String... ids
);
}
assertThat(
"Some expected ids were not found in search results: "
+ Arrays.toString(idsSet.toArray(new String[idsSet.size()]))
+ "."
+ shardStatus,
"Some expected ids were not found in search results: " + new ArrayList<>(idsSet) + "." + shardStatus,
idsSet.size(),
equalTo(0)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static String[] readStringArray(XContentParser parser, boolean allowNull)
);
}
}
return list.toArray(new String[list.size()]);
return list.toArray(String[]::new);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CronSchedule parse(XContentParser parser) throws IOException {
throw new ElasticsearchParseException("could not parse [cron] schedule. no cron expression found in cron array");
}
try {
return new CronSchedule(crons.toArray(new String[crons.size()]));
return new CronSchedule(crons.toArray(String[]::new));
} catch (IllegalArgumentException iae) {
throw new ElasticsearchParseException("could not parse [cron] schedule", iae);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -65,11 +66,7 @@ public static Builder builder() {

static String[] crons(DayTimes[] times) {
assert times.length > 0 : "at least one time must be defined";
List<String> crons = new ArrayList<>(times.length);
for (DayTimes time : times) {
crons.add(time.cron());
}
return crons.toArray(new String[crons.size()]);
return Arrays.stream(times).map(DayTimes::cron).toArray(String[]::new);
}

public static class Parser implements Schedule.Parser<DailySchedule> {
Expand Down Expand Up @@ -122,7 +119,7 @@ public DailySchedule parse(XContentParser parser) throws IOException {
}
}

return times.isEmpty() ? new DailySchedule() : new DailySchedule(times.toArray(new DayTimes[times.size()]));
return times.isEmpty() ? new DailySchedule() : new DailySchedule(times.toArray(DayTimes[]::new));
}
}

Expand Down Expand Up @@ -153,7 +150,7 @@ public Builder atMidnight() {
}

public DailySchedule build() {
return times.isEmpty() ? new DailySchedule() : new DailySchedule(times.toArray(new DayTimes[times.size()]));
return times.isEmpty() ? new DailySchedule() : new DailySchedule(times.toArray(DayTimes[]::new));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
package org.elasticsearch.xpack.watcher.trigger.schedule;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.watcher.trigger.schedule.support.MonthTimes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -62,11 +62,7 @@ public static Builder builder() {

static String[] crons(MonthTimes[] times) {
assert times.length > 0 : "at least one time must be defined";
Set<String> crons = Sets.newHashSetWithExpectedSize(times.length);
for (MonthTimes time : times) {
crons.addAll(time.crons());
}
return crons.toArray(new String[crons.size()]);
return Arrays.stream(times).flatMap(mt -> mt.crons().stream()).distinct().toArray(String[]::new);
}

public static class Parser implements Schedule.Parser<MonthlySchedule> {
Expand Down Expand Up @@ -95,7 +91,7 @@ public MonthlySchedule parse(XContentParser parser) throws IOException {
throw new ElasticsearchParseException("could not parse [{}] schedule. invalid month times", pe, TYPE);
}
}
return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(new MonthTimes[times.size()]));
return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(MonthTimes[]::new));
}
throw new ElasticsearchParseException(
"could not parse [{}] schedule. expected either an object or an array "
Expand All @@ -122,7 +118,7 @@ public Builder time(MonthTimes.Builder builder) {
}

public MonthlySchedule build() {
return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(new MonthTimes[times.size()]));
return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(MonthTimes[]::new));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -61,11 +62,7 @@ public static Builder builder() {

static String[] crons(WeekTimes[] times) {
assert times.length > 0 : "at least one time must be defined";
List<String> crons = new ArrayList<>(times.length);
for (WeekTimes time : times) {
crons.addAll(time.crons());
}
return crons.toArray(new String[crons.size()]);
return Arrays.stream(times).flatMap(wt -> wt.crons().stream()).toArray(String[]::new);
}

public static class Parser implements Schedule.Parser<WeeklySchedule> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
package org.elasticsearch.xpack.watcher.trigger.schedule;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.watcher.trigger.schedule.support.YearTimes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -62,11 +62,7 @@ public static Builder builder() {

static String[] crons(YearTimes[] times) {
assert times.length > 0 : "at least one time must be defined";
Set<String> crons = Sets.newHashSetWithExpectedSize(times.length);
for (YearTimes time : times) {
crons.addAll(time.crons());
}
return crons.toArray(new String[crons.size()]);
return Arrays.stream(times).flatMap(yt -> yt.crons().stream()).distinct().toArray(String[]::new);
}

public static class Parser implements Schedule.Parser<YearlySchedule> {
Expand Down Expand Up @@ -95,7 +91,7 @@ public YearlySchedule parse(XContentParser parser) throws IOException {
throw new ElasticsearchParseException("could not parse [{}] schedule. invalid year times", pe, TYPE);
}
}
return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(new YearTimes[times.size()]));
return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(YearTimes[]::new));
}
throw new ElasticsearchParseException(
"could not parse [{}] schedule. expected either an object or an array "
Expand All @@ -122,7 +118,7 @@ public Builder time(YearTimes.Builder builder) {
}

public YearlySchedule build() {
return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(new YearTimes[times.size()]));
return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(YearTimes[]::new));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ public static MonthTimes parse(XContentParser parser, XContentParser.Token token
}
}
}
int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : CollectionUtils.toArray(daysSet);
DayTimes[] times = timesSet.isEmpty() ? new DayTimes[] { new DayTimes(0, 0) } : timesSet.toArray(new DayTimes[timesSet.size()]);
return new MonthTimes(days, times);

return new MonthTimes(CollectionUtils.toArray(daysSet), timesSet.toArray(DayTimes[]::new));
}

static int parseDayValue(XContentParser parser, XContentParser.Token token) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static WeekTimes parse(XContentParser parser, XContentParser.Token token)
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("could not parse week times. expected an object, but found [{}]", token);
}
Set<DayOfWeek> daysSet = new HashSet<>();
EnumSet<DayOfWeek> daysSet = EnumSet.noneOf(DayOfWeek.class);
Set<DayTimes> timesSet = new HashSet<>();
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -142,9 +142,8 @@ public static WeekTimes parse(XContentParser parser, XContentParser.Token token)
}
}
}
EnumSet<DayOfWeek> days = daysSet.isEmpty() ? EnumSet.of(DayOfWeek.MONDAY) : EnumSet.copyOf(daysSet);
DayTimes[] times = timesSet.isEmpty() ? new DayTimes[] { new DayTimes(0, 0) } : timesSet.toArray(new DayTimes[timesSet.size()]);
return new WeekTimes(days, times);

return new WeekTimes(daysSet, timesSet.toArray(DayTimes[]::new));
}

static DayOfWeek parseDayValue(XContentParser parser, XContentParser.Token token) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static YearTimes parse(XContentParser parser, XContentParser.Token token)
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("could not parse year times. expected an object, but found [{}]", token);
}
Set<Month> monthsSet = new HashSet<>();
EnumSet<Month> monthsSet = EnumSet.noneOf(Month.class);
Set<Integer> daysSet = new HashSet<>();
Set<DayTimes> timesSet = new HashSet<>();
String currentFieldName = null;
Expand Down Expand Up @@ -190,10 +190,8 @@ public static YearTimes parse(XContentParser parser, XContentParser.Token token)
}
}
}
EnumSet<Month> months = monthsSet.isEmpty() ? DEFAULT_MONTHS : EnumSet.copyOf(monthsSet);
int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : CollectionUtils.toArray(daysSet);
DayTimes[] times = timesSet.isEmpty() ? new DayTimes[] { new DayTimes(0, 0) } : timesSet.toArray(new DayTimes[timesSet.size()]);
return new YearTimes(months, days, times);

return new YearTimes(monthsSet, CollectionUtils.toArray(daysSet), timesSet.toArray(DayTimes[]::new));
}

static Month parseMonthValue(XContentParser parser, XContentParser.Token token) throws IOException {
Expand Down

0 comments on commit 0c528cc

Please sign in to comment.