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

View File

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

View File

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

View File

@ -37,7 +37,6 @@ import java.nio.ByteBuffer;
import java.security.Provider.Service; import java.security.Provider.Service;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
@ -180,15 +179,12 @@ public abstract class Signature extends SignatureSpi {
private static final String RSA_CIPHER = "RSA/ECB/PKCS1Padding"; private static final String RSA_CIPHER = "RSA/ECB/PKCS1Padding";
// all the services we need to lookup for compatibility with Cipher // all the services we need to lookup for compatibility with Cipher
private static final List<ServiceId> rsaIds = Arrays.asList( private static final List<ServiceId> rsaIds = List.of(
new ServiceId[] { new ServiceId("Signature", "NONEwithRSA"),
new ServiceId("Signature", "NONEwithRSA"), new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"),
new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"), new ServiceId("Cipher", "RSA/ECB"),
new ServiceId("Cipher", "RSA/ECB"), new ServiceId("Cipher", "RSA//PKCS1Padding"),
new ServiceId("Cipher", "RSA//PKCS1Padding"), new ServiceId("Cipher", "RSA"));
new ServiceId("Cipher", "RSA"),
}
);
/** /**
* Returns a Signature object that implements the specified signature * 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.TemporalAmount;
import java.time.temporal.TemporalUnit; import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException; import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -578,8 +576,7 @@ public final class Duration
* the simple initialization in Duration. * the simple initialization in Duration.
*/ */
private static class DurationUnits { private static class DurationUnits {
static final List<TemporalUnit> UNITS = static final List<TemporalUnit> UNITS = List.of(SECONDS, NANOS);
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(SECONDS, NANOS));
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

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

View File

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

View File

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

View File

@ -59,10 +59,7 @@ package java.time.chrono;
import static java.time.temporal.ChronoField.EPOCH_DAY; import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilePermission; import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -83,7 +80,6 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
@ -512,7 +508,7 @@ public final class HijrahChronology extends AbstractChronology implements Serial
@Override @Override
public List<Era> eras() { 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.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -492,7 +491,7 @@ public final class IsoChronology extends AbstractChronology implements Serializa
@Override @Override
public List<Era> eras() { 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.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException; import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -379,7 +378,7 @@ public final class JapaneseChronology extends AbstractChronology implements Seri
@Override @Override
public List<Era> eras() { public List<Era> eras() {
return Arrays.<Era>asList(JapaneseEra.values()); return List.of(JapaneseEra.values());
} }
JapaneseEra getCurrentEra() { JapaneseEra getCurrentEra() {

View File

@ -72,7 +72,6 @@ import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -306,7 +305,7 @@ public final class MinguoChronology extends AbstractChronology implements Serial
@Override @Override
public List<Era> eras() { 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 @Override
public List<Era> eras() { 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) { public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
Set<TemporalField> fields = null; Set<TemporalField> fields = null;
if (resolverFields != 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))); fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields)));
} }
if (Objects.equals(this.resolverFields, fields)) { if (Objects.equals(this.resolverFields, fields)) {

View File

@ -387,9 +387,9 @@ public final class ZoneOffsetTransition
*/ */
List<ZoneOffset> getValidOffsets() { List<ZoneOffset> getValidOffsets() {
if (isGap()) { 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. * Creates an instance of ZoneRules that has fixed zone rules.
* *
* @param offset the offset this fixed zone rules is based on, not null * @param offset the offset this fixed zone rules is based on, not null
* @return the zone rules, not null
* @see #isFixedOffset() * @see #isFixedOffset()
*/ */
private ZoneRules(ZoneOffset offset) { private ZoneRules(ZoneOffset offset) {
@ -970,7 +969,7 @@ public final class ZoneRules implements Serializable {
* @return an immutable list of transition rules, not null * @return an immutable list of transition rules, not null
*/ */
public List<ZoneOffsetTransitionRule> getTransitionRules() { 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 * The default format <code>List</code>, which contains the strings
* <code>"java.class"</code> and <code>"java.properties"</code>, in * <code>"java.class"</code> and <code>"java.properties"</code>, in
* this order. This <code>List</code> is {@linkplain * this order. This <code>List</code> is unmodifiable.
* Collections#unmodifiableList(List) unmodifiable}.
* *
* @see #getFormats(String) * @see #getFormats(String)
*/ */
public static final List<String> FORMAT_DEFAULT public static final List<String> FORMAT_DEFAULT
= Collections.unmodifiableList(Arrays.asList("java.class", = List.of("java.class", "java.properties");
"java.properties"));
/** /**
* The class-only format <code>List</code> containing * The class-only format <code>List</code> containing
* <code>"java.class"</code>. This <code>List</code> is {@linkplain * <code>"java.class"</code>. This <code>List</code> is unmodifiable.
* Collections#unmodifiableList(List) unmodifiable}.
* *
* @see #getFormats(String) * @see #getFormats(String)
*/ */
public static final List<String> FORMAT_CLASS public static final List<String> FORMAT_CLASS = List.of("java.class");
= Collections.unmodifiableList(Arrays.asList("java.class"));
/** /**
* The properties-only format <code>List</code> containing * The properties-only format <code>List</code> containing
* <code>"java.properties"</code>. This <code>List</code> is * <code>"java.properties"</code>. This <code>List</code> is unmodifiable.
* {@linkplain Collections#unmodifiableList(List) unmodifiable}.
* *
* @see #getFormats(String) * @see #getFormats(String)
*/ */
public static final List<String> FORMAT_PROPERTIES 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 * 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.AbstractMap;
import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -1720,12 +1719,12 @@ public final class Collectors {
@Override @Override
public Set<Map.Entry<Boolean, T>> entrySet() { public Set<Map.Entry<Boolean, T>> entrySet() {
return new AbstractSet<Map.Entry<Boolean, T>>() { return new AbstractSet<>() {
@Override @Override
public Iterator<Map.Entry<Boolean, T>> iterator() { public Iterator<Map.Entry<Boolean, T>> iterator() {
Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse); Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue); Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
return Arrays.asList(falseEntry, trueEntry).iterator(); return List.of(falseEntry, trueEntry).iterator();
} }
@Override @Override