8134373: use collections convenience factories in the JDK

Reviewed-by: scolebourne, prappo, dfuchs, redestad, smarks
This commit is contained in:
Jonathan Bluett-Duncan 2016-10-12 11:54:38 -07:00 committed by Stuart Marks
parent 8c7dc29389
commit a19fc7fbdb
19 changed files with 67 additions and 100 deletions

View File

@ -33,7 +33,6 @@ import java.nio.file.Paths;
import java.security.AccessController;
import java.security.Permission;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -333,7 +332,7 @@ public interface ModuleFinder {
*
* <p> When locating modules then any exceptions or errors thrown by the
* {@code find} or {@code findAll} methods of the underlying module finders
* will be propogated to the caller of the resulting module finder's
* will be propagated to the caller of the resulting module finder's
* {@code find} or {@code findAll} methods. </p>
*
* @param finders
@ -342,8 +341,8 @@ public interface ModuleFinder {
* @return A {@code ModuleFinder} that composes a sequence of module finders
*/
static ModuleFinder compose(ModuleFinder... finders) {
final List<ModuleFinder> finderList = Arrays.asList(finders);
finderList.forEach(Objects::requireNonNull);
// copy the list, also checking for nulls
final List<ModuleFinder> finderList = List.of(finders);
return new ModuleFinder() {
private final Map<String, ModuleReference> nameToModule = new HashMap<>();

View File

@ -81,7 +81,7 @@ import sun.util.logging.PlatformLogger;
* <li>
* Currently, only CookieStore.add(URI, HttpCookie) and CookieStore.get(URI)
* are used by CookieManager. Others are for completeness and might be needed
* by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieSotre.
* by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieStore.
* </li>
* </ul>
* </blockquote>
@ -201,10 +201,9 @@ public class CookieManager extends CookieHandler
throw new IllegalArgumentException("Argument is null");
}
Map<String, List<String>> cookieMap = new java.util.HashMap<>();
// if there's no default CookieStore, no way for us to get any cookie
if (cookieJar == null)
return Collections.unmodifiableMap(cookieMap);
return Map.of();
boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
List<HttpCookie> cookies = new java.util.ArrayList<>();
@ -244,8 +243,7 @@ public class CookieManager extends CookieHandler
// apply sort rule (RFC 2965 sec. 3.3.4)
List<String> cookieHeader = sortByPath(cookies);
cookieMap.put("Cookie", cookieHeader);
return Collections.unmodifiableMap(cookieMap);
return Map.of("Cookie", cookieHeader);
}
public void

View File

@ -31,11 +31,10 @@ import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.nio.file.FileTreeWalker.Event;
/**
* An {@code Iterator to iterate over the nodes of a file tree.
* An {@code Iterator} to iterate over the nodes of a file tree.
*
* <pre>{@code
* try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) {
@ -62,7 +61,7 @@ class FileTreeIterator implements Iterator<Event>, Closeable {
* @throws SecurityException
* if the security manager denies access to the starting file
* @throws NullPointerException
* if {@code start} or {@code options} is {@ocde null} or
* if {@code start} or {@code options} is {@code null} or
* the options array contains a {@code null} element
*/
FileTreeIterator(Path start, int maxDepth, FileVisitOption... options)

View File

@ -171,7 +171,7 @@ class FileTreeWalker implements Closeable {
* if {@code options} contains an element that is not a
* {@code FileVisitOption}
* @throws NullPointerException
* if {@code options} is {@ocde null} or the options
* if {@code options} is {@code null} or the options
* array contains a {@code null} element
*/
FileTreeWalker(Collection<FileVisitOption> options, int maxDepth) {

View File

@ -37,7 +37,6 @@ import java.nio.ByteBuffer;
import java.security.Provider.Service;
import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException;
@ -180,15 +179,12 @@ public abstract class Signature extends SignatureSpi {
private static final String RSA_CIPHER = "RSA/ECB/PKCS1Padding";
// all the services we need to lookup for compatibility with Cipher
private static final List<ServiceId> rsaIds = Arrays.asList(
new ServiceId[] {
new ServiceId("Signature", "NONEwithRSA"),
new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"),
new ServiceId("Cipher", "RSA/ECB"),
new ServiceId("Cipher", "RSA//PKCS1Padding"),
new ServiceId("Cipher", "RSA"),
}
);
private static final List<ServiceId> rsaIds = List.of(
new ServiceId("Signature", "NONEwithRSA"),
new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"),
new ServiceId("Cipher", "RSA/ECB"),
new ServiceId("Cipher", "RSA//PKCS1Padding"),
new ServiceId("Cipher", "RSA"));
/**
* Returns a Signature object that implements the specified signature

View File

@ -88,8 +88,6 @@ import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
@ -578,8 +576,7 @@ public final class Duration
* the simple initialization in Duration.
*/
private static class DurationUnits {
static final List<TemporalUnit> UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(SECONDS, NANOS));
static final List<TemporalUnit> UNITS = List.of(SECONDS, NANOS);
}
//-----------------------------------------------------------------------

View File

@ -83,8 +83,6 @@ import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
@ -152,8 +150,7 @@ public final class Period
/**
* The set of supported units.
*/
private static final List<TemporalUnit> SUPPORTED_UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
/**
* The number of years.

View File

@ -76,14 +76,14 @@ import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.zone.ZoneRules;
import java.time.zone.ZoneRulesException;
import java.time.zone.ZoneRulesProvider;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import static java.util.Map.entry;
/**
* A time-zone ID, such as {@code Europe/Paris}.
* <p>
@ -220,39 +220,36 @@ public abstract class ZoneId implements Serializable {
* </ul>
* The map is unmodifiable.
*/
public static final Map<String, String> SHORT_IDS;
static {
Map<String, String> map = new HashMap<>(64);
map.put("ACT", "Australia/Darwin");
map.put("AET", "Australia/Sydney");
map.put("AGT", "America/Argentina/Buenos_Aires");
map.put("ART", "Africa/Cairo");
map.put("AST", "America/Anchorage");
map.put("BET", "America/Sao_Paulo");
map.put("BST", "Asia/Dhaka");
map.put("CAT", "Africa/Harare");
map.put("CNT", "America/St_Johns");
map.put("CST", "America/Chicago");
map.put("CTT", "Asia/Shanghai");
map.put("EAT", "Africa/Addis_Ababa");
map.put("ECT", "Europe/Paris");
map.put("IET", "America/Indiana/Indianapolis");
map.put("IST", "Asia/Kolkata");
map.put("JST", "Asia/Tokyo");
map.put("MIT", "Pacific/Apia");
map.put("NET", "Asia/Yerevan");
map.put("NST", "Pacific/Auckland");
map.put("PLT", "Asia/Karachi");
map.put("PNT", "America/Phoenix");
map.put("PRT", "America/Puerto_Rico");
map.put("PST", "America/Los_Angeles");
map.put("SST", "Pacific/Guadalcanal");
map.put("VST", "Asia/Ho_Chi_Minh");
map.put("EST", "-05:00");
map.put("MST", "-07:00");
map.put("HST", "-10:00");
SHORT_IDS = Collections.unmodifiableMap(map);
}
public static final Map<String, String> SHORT_IDS = Map.ofEntries(
entry("ACT", "Australia/Darwin"),
entry("AET", "Australia/Sydney"),
entry("AGT", "America/Argentina/Buenos_Aires"),
entry("ART", "Africa/Cairo"),
entry("AST", "America/Anchorage"),
entry("BET", "America/Sao_Paulo"),
entry("BST", "Asia/Dhaka"),
entry("CAT", "Africa/Harare"),
entry("CNT", "America/St_Johns"),
entry("CST", "America/Chicago"),
entry("CTT", "Asia/Shanghai"),
entry("EAT", "Africa/Addis_Ababa"),
entry("ECT", "Europe/Paris"),
entry("IET", "America/Indiana/Indianapolis"),
entry("IST", "Asia/Kolkata"),
entry("JST", "Asia/Tokyo"),
entry("MIT", "Pacific/Apia"),
entry("NET", "Asia/Yerevan"),
entry("NST", "Pacific/Auckland"),
entry("PLT", "Asia/Karachi"),
entry("PNT", "America/Phoenix"),
entry("PRT", "America/Puerto_Rico"),
entry("PST", "America/Los_Angeles"),
entry("SST", "Pacific/Guadalcanal"),
entry("VST", "Asia/Ho_Chi_Minh"),
entry("EST", "-05:00"),
entry("MST", "-07:00"),
entry("HST", "-10:00")
);
/**
* Serialization version.
*/

View File

@ -77,8 +77,6 @@ import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@ -105,8 +103,7 @@ final class ChronoPeriodImpl
/**
* The set of supported units.
*/
private static final List<TemporalUnit> SUPPORTED_UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
/**
* The chronology.

View File

@ -59,10 +59,7 @@ package java.time.chrono;
import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
@ -83,7 +80,6 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import sun.util.logging.PlatformLogger;
@ -512,7 +508,7 @@ public final class HijrahChronology extends AbstractChronology implements Serial
@Override
public List<Era> eras() {
return Arrays.<Era>asList(HijrahEra.values());
return List.of(HijrahEra.values());
}
//-----------------------------------------------------------------------

View File

@ -90,7 +90,6 @@ import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -492,7 +491,7 @@ public final class IsoChronology extends AbstractChronology implements Serializa
@Override
public List<Era> eras() {
return Arrays.<Era>asList(IsoEra.values());
return List.of(IsoEra.values());
}
//-----------------------------------------------------------------------

View File

@ -81,7 +81,6 @@ import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
@ -379,7 +378,7 @@ public final class JapaneseChronology extends AbstractChronology implements Seri
@Override
public List<Era> eras() {
return Arrays.<Era>asList(JapaneseEra.values());
return List.of(JapaneseEra.values());
}
JapaneseEra getCurrentEra() {

View File

@ -72,7 +72,6 @@ import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -306,7 +305,7 @@ public final class MinguoChronology extends AbstractChronology implements Serial
@Override
public List<Era> eras() {
return Arrays.<Era>asList(MinguoEra.values());
return List.of(MinguoEra.values());
}
//-----------------------------------------------------------------------

View File

@ -342,7 +342,7 @@ public final class ThaiBuddhistChronology extends AbstractChronology implements
@Override
public List<Era> eras() {
return Arrays.<Era>asList(ThaiBuddhistEra.values());
return List.of(ThaiBuddhistEra.values());
}
//-----------------------------------------------------------------------

View File

@ -1685,6 +1685,7 @@ public final class DateTimeFormatter {
public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
Set<TemporalField> fields = null;
if (resolverFields != null) {
// Set.of cannot be used because it is hostile to nulls and duplicate elements
fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields)));
}
if (Objects.equals(this.resolverFields, fields)) {

View File

@ -387,9 +387,9 @@ public final class ZoneOffsetTransition
*/
List<ZoneOffset> getValidOffsets() {
if (isGap()) {
return Collections.emptyList();
return List.of();
}
return Arrays.asList(getOffsetBefore(), getOffsetAfter());
return List.of(getOffsetBefore(), getOffsetAfter());
}
//-----------------------------------------------------------------------

View File

@ -303,7 +303,6 @@ public final class ZoneRules implements Serializable {
* Creates an instance of ZoneRules that has fixed zone rules.
*
* @param offset the offset this fixed zone rules is based on, not null
* @return the zone rules, not null
* @see #isFixedOffset()
*/
private ZoneRules(ZoneOffset offset) {
@ -970,7 +969,7 @@ public final class ZoneRules implements Serializable {
* @return an immutable list of transition rules, not null
*/
public List<ZoneOffsetTransitionRule> getTransitionRules() {
return Collections.unmodifiableList(Arrays.asList(lastRules));
return List.of(lastRules);
}
/**

View File

@ -2491,34 +2491,29 @@ public abstract class ResourceBundle {
/**
* The default format <code>List</code>, which contains the strings
* <code>"java.class"</code> and <code>"java.properties"</code>, in
* this order. This <code>List</code> is {@linkplain
* Collections#unmodifiableList(List) unmodifiable}.
* this order. This <code>List</code> is unmodifiable.
*
* @see #getFormats(String)
*/
public static final List<String> FORMAT_DEFAULT
= Collections.unmodifiableList(Arrays.asList("java.class",
"java.properties"));
= List.of("java.class", "java.properties");
/**
* The class-only format <code>List</code> containing
* <code>"java.class"</code>. This <code>List</code> is {@linkplain
* Collections#unmodifiableList(List) unmodifiable}.
* <code>"java.class"</code>. This <code>List</code> is unmodifiable.
*
* @see #getFormats(String)
*/
public static final List<String> FORMAT_CLASS
= Collections.unmodifiableList(Arrays.asList("java.class"));
public static final List<String> FORMAT_CLASS = List.of("java.class");
/**
* The properties-only format <code>List</code> containing
* <code>"java.properties"</code>. This <code>List</code> is
* {@linkplain Collections#unmodifiableList(List) unmodifiable}.
* <code>"java.properties"</code>. This <code>List</code> is unmodifiable.
*
* @see #getFormats(String)
*/
public static final List<String> FORMAT_PROPERTIES
= Collections.unmodifiableList(Arrays.asList("java.properties"));
= List.of("java.properties");
/**
* The time-to-live constant for not caching loaded resource bundle

View File

@ -27,7 +27,6 @@ package java.util.stream;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@ -1720,12 +1719,12 @@ public final class Collectors {
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
return new AbstractSet<Map.Entry<Boolean, T>>() {
return new AbstractSet<>() {
@Override
public Iterator<Map.Entry<Boolean, T>> iterator() {
Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
return Arrays.asList(falseEntry, trueEntry).iterator();
return List.of(falseEntry, trueEntry).iterator();
}
@Override