8134373: use collections convenience factories in the JDK
Reviewed-by: scolebourne, prappo, dfuchs, redestad, smarks
Contributed-by: jbluettduncan@gmail.com
--- a/jdk/src/java.base/share/classes/java/lang/module/ModuleFinder.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/lang/module/ModuleFinder.java Wed Oct 12 11:54:38 2016 -0700
@@ -33,7 +33,6 @@
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 @@
*
* <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 @@
* @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<>();
--- a/jdk/src/java.base/share/classes/java/net/CookieManager.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/net/CookieManager.java Wed Oct 12 11:54:38 2016 -0700
@@ -81,7 +81,7 @@
* <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 @@
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 @@
// 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
--- a/jdk/src/java.base/share/classes/java/nio/file/FileTreeIterator.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/nio/file/FileTreeIterator.java Wed Oct 12 11:54:38 2016 -0700
@@ -31,11 +31,10 @@
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 @@
* @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)
--- a/jdk/src/java.base/share/classes/java/nio/file/FileTreeWalker.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/nio/file/FileTreeWalker.java Wed Oct 12 11:54:38 2016 -0700
@@ -171,7 +171,7 @@
* 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) {
--- a/jdk/src/java.base/share/classes/java/security/Signature.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/security/Signature.java Wed Oct 12 11:54:38 2016 -0700
@@ -37,7 +37,6 @@
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 @@
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
--- a/jdk/src/java.base/share/classes/java/time/Duration.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/Duration.java Wed Oct 12 11:54:38 2016 -0700
@@ -88,8 +88,6 @@
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 @@
* 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);
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/Period.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/Period.java Wed Oct 12 11:54:38 2016 -0700
@@ -83,8 +83,6 @@
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 @@
/**
* 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.
--- a/jdk/src/java.base/share/classes/java/time/ZoneId.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/ZoneId.java Wed Oct 12 11:54:38 2016 -0700
@@ -76,14 +76,14 @@
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 @@
* </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.
*/
--- a/jdk/src/java.base/share/classes/java/time/chrono/ChronoPeriodImpl.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/ChronoPeriodImpl.java Wed Oct 12 11:54:38 2016 -0700
@@ -77,8 +77,6 @@
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 @@
/**
* 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.
--- a/jdk/src/java.base/share/classes/java/time/chrono/HijrahChronology.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/HijrahChronology.java Wed Oct 12 11:54:38 2016 -0700
@@ -59,10 +59,7 @@
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.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 @@
@Override
public List<Era> eras() {
- return Arrays.<Era>asList(HijrahEra.values());
+ return List.of(HijrahEra.values());
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/chrono/IsoChronology.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/IsoChronology.java Wed Oct 12 11:54:38 2016 -0700
@@ -90,7 +90,6 @@
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 @@
@Override
public List<Era> eras() {
- return Arrays.<Era>asList(IsoEra.values());
+ return List.of(IsoEra.values());
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/chrono/JapaneseChronology.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/JapaneseChronology.java Wed Oct 12 11:54:38 2016 -0700
@@ -81,7 +81,6 @@
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 @@
@Override
public List<Era> eras() {
- return Arrays.<Era>asList(JapaneseEra.values());
+ return List.of(JapaneseEra.values());
}
JapaneseEra getCurrentEra() {
--- a/jdk/src/java.base/share/classes/java/time/chrono/MinguoChronology.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/MinguoChronology.java Wed Oct 12 11:54:38 2016 -0700
@@ -72,7 +72,6 @@
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 @@
@Override
public List<Era> eras() {
- return Arrays.<Era>asList(MinguoEra.values());
+ return List.of(MinguoEra.values());
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/chrono/ThaiBuddhistChronology.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/chrono/ThaiBuddhistChronology.java Wed Oct 12 11:54:38 2016 -0700
@@ -342,7 +342,7 @@
@Override
public List<Era> eras() {
- return Arrays.<Era>asList(ThaiBuddhistEra.values());
+ return List.of(ThaiBuddhistEra.values());
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/format/DateTimeFormatter.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/format/DateTimeFormatter.java Wed Oct 12 11:54:38 2016 -0700
@@ -1685,6 +1685,7 @@
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)) {
--- a/jdk/src/java.base/share/classes/java/time/zone/ZoneOffsetTransition.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/zone/ZoneOffsetTransition.java Wed Oct 12 11:54:38 2016 -0700
@@ -387,9 +387,9 @@
*/
List<ZoneOffset> getValidOffsets() {
if (isGap()) {
- return Collections.emptyList();
+ return List.of();
}
- return Arrays.asList(getOffsetBefore(), getOffsetAfter());
+ return List.of(getOffsetBefore(), getOffsetAfter());
}
//-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/zone/ZoneRules.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/time/zone/ZoneRules.java Wed Oct 12 11:54:38 2016 -0700
@@ -303,7 +303,6 @@
* 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 @@
* @return an immutable list of transition rules, not null
*/
public List<ZoneOffsetTransitionRule> getTransitionRules() {
- return Collections.unmodifiableList(Arrays.asList(lastRules));
+ return List.of(lastRules);
}
/**
--- a/jdk/src/java.base/share/classes/java/util/ResourceBundle.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/util/ResourceBundle.java Wed Oct 12 11:54:38 2016 -0700
@@ -2491,34 +2491,29 @@
/**
* 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
--- a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java Wed Oct 12 22:44:43 2016 +0530
+++ b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java Wed Oct 12 11:54:38 2016 -0700
@@ -27,7 +27,6 @@
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 @@
@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