java.time: remove usages of concurrent classes

This commit is contained in:
Alexey Andreev 2020-04-27 14:48:04 +03:00 committed by Alexey Andreev
parent f12d33a032
commit 3513bacc2e
11 changed files with 50 additions and 80 deletions

View File

@ -39,8 +39,8 @@ import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.HashMap;
import java.util.Map;
import org.threeten.bp.jdk8.Jdk8Methods;
import org.threeten.bp.temporal.ChronoField;
@ -101,9 +101,9 @@ public final class ZoneOffset
};
/** Cache of time-zone offset by offset in seconds. */
private static final ConcurrentMap<Integer, ZoneOffset> SECONDS_CACHE = new ConcurrentHashMap<Integer, ZoneOffset>(16, 0.75f, 4);
private static final Map<Integer, ZoneOffset> SECONDS_CACHE = new HashMap<>();
/** Cache of time-zone offset by ID. */
private static final ConcurrentMap<String, ZoneOffset> ID_CACHE = new ConcurrentHashMap<String, ZoneOffset>(16, 0.75f, 4);
private static final Map<String, ZoneOffset> ID_CACHE = new HashMap<>();
/**
* The number of seconds per hour.

View File

@ -36,15 +36,13 @@ import java.io.DataOutput;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.threeten.bp.Clock;
import org.threeten.bp.DateTimeException;
@ -153,24 +151,11 @@ public abstract class Chronology implements Comparable<Chronology> {
/**
* Map of available calendars by ID.
*/
private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<String, Chronology>();
private static final Map<String, Chronology> CHRONOS_BY_ID = new HashMap<>();
/**
* Map of available calendars by calendar type.
*/
private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<String, Chronology>();
/**
* Access JDK 7 method if on JDK 7.
*/
private static final Method LOCALE_METHOD;
static {
Method method = null;
try {
method = Locale.class.getMethod("getUnicodeLocaleType", String.class);
} catch (Throwable ex) {
// ignore
}
LOCALE_METHOD = method;
}
private static final Map<String, Chronology> CHRONOS_BY_TYPE = new HashMap<>();
//-----------------------------------------------------------------------
/**
@ -240,18 +225,7 @@ public abstract class Chronology implements Comparable<Chronology> {
init();
Jdk8Methods.requireNonNull(locale, "locale");
String type = "iso";
if (LOCALE_METHOD != null) {
// JDK 7: locale.getUnicodeLocaleType("ca");
try {
type = (String) LOCALE_METHOD.invoke(locale, "ca");
} catch (IllegalArgumentException ex) {
// ignore
} catch (IllegalAccessException ex) {
// ignore
} catch (InvocationTargetException ex) {
// ignore
}
} else if (locale.equals(JapaneseChronology.LOCALE)) {
if (locale.equals(JapaneseChronology.LOCALE)) {
type = "japanese";
}
if (type == null || "iso".equals(type) || "iso8601".equals(type)) {

View File

@ -38,7 +38,6 @@ import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import org.threeten.bp.DateTimeException;
import org.threeten.bp.LocalDate;
@ -106,7 +105,7 @@ public final class JapaneseEra
private static final long serialVersionUID = 1466499369062886794L;
// array for the singleton JapaneseEra instances
private static final AtomicReference<JapaneseEra[]> KNOWN_ERAS;
private static JapaneseEra[] KNOWN_ERAS;
static {
JapaneseEra[] array = new JapaneseEra[5];
@ -115,7 +114,7 @@ public final class JapaneseEra
array[2] = SHOWA;
array[3] = HEISEI;
array[4] = REIWA;
KNOWN_ERAS = new AtomicReference<JapaneseEra[]>(array);
KNOWN_ERAS = array;
}
/**
@ -177,7 +176,7 @@ public final class JapaneseEra
* @throws DateTimeException if an additional era has already been registered
*/
public static JapaneseEra registerEra(LocalDate since, String name) {
JapaneseEra[] known = KNOWN_ERAS.get();
JapaneseEra[] known = KNOWN_ERAS;
if (known.length > 5) {
throw new DateTimeException("Only one additional Japanese era can be added");
}
@ -189,9 +188,7 @@ public final class JapaneseEra
JapaneseEra era = new JapaneseEra(ADDITIONAL_VALUE, since, name);
JapaneseEra[] newArray = Arrays.copyOf(known, 6);
newArray[5] = era;
if (!KNOWN_ERAS.compareAndSet(known, newArray)) {
throw new DateTimeException("Only one additional Japanese era can be added");
}
KNOWN_ERAS = newArray;
return era;
}
@ -207,7 +204,7 @@ public final class JapaneseEra
* @throws DateTimeException if the value is invalid
*/
public static JapaneseEra of(int japaneseEra) {
JapaneseEra[] known = KNOWN_ERAS.get();
JapaneseEra[] known = KNOWN_ERAS;
if (japaneseEra < MEIJI.eraValue || japaneseEra > known[known.length - 1].eraValue) {
throw new DateTimeException("japaneseEra is invalid");
}
@ -226,7 +223,7 @@ public final class JapaneseEra
*/
public static JapaneseEra valueOf(String japaneseEra) {
Jdk8Methods.requireNonNull(japaneseEra, "japaneseEra");
JapaneseEra[] known = KNOWN_ERAS.get();
JapaneseEra[] known = KNOWN_ERAS;
for (JapaneseEra era : known) {
if (japaneseEra.equals(era.name)) {
return era;
@ -247,7 +244,7 @@ public final class JapaneseEra
* @return an array of JapaneseEras
*/
public static JapaneseEra[] values() {
JapaneseEra[] known = KNOWN_ERAS.get();
JapaneseEra[] known = KNOWN_ERAS;
return Arrays.copyOf(known, known.length);
}
@ -262,7 +259,7 @@ public final class JapaneseEra
if (date.isBefore(MEIJI.since)) {
throw new DateTimeException("Date too early: " + date);
}
JapaneseEra[] known = KNOWN_ERAS.get();
JapaneseEra[] known = KNOWN_ERAS;
for (int i = known.length - 1; i >= 0; i--) {
JapaneseEra era = known[i];
if (date.compareTo(era.since) >= 0) {

View File

@ -34,7 +34,6 @@ package org.threeten.bp.format;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReference;
import org.threeten.bp.temporal.TemporalField;
@ -51,7 +50,7 @@ import org.threeten.bp.temporal.TemporalField;
*/
public abstract class DateTimeTextProvider {
private static final AtomicReference<DateTimeTextProvider> MUTABLE_PROVIDER = new AtomicReference<DateTimeTextProvider>();
private static DateTimeTextProvider MUTABLE_PROVIDER;
/**
* Gets the provider.
@ -72,9 +71,10 @@ public abstract class DateTimeTextProvider {
* @throws IllegalStateException if initialization has already occurred or another provider has been set
*/
public static void setInitializer(DateTimeTextProvider provider) {
if (!MUTABLE_PROVIDER.compareAndSet(null, provider)) {
if (MUTABLE_PROVIDER != null) {
throw new IllegalStateException("Provider was already set, possibly with a default during initialization");
}
MUTABLE_PROVIDER = provider;
}
//-----------------------------------------------------------------------
@ -120,8 +120,8 @@ public abstract class DateTimeTextProvider {
// initialize the provider
static DateTimeTextProvider initialize() {
// Set the default initializer if none has been provided yet
MUTABLE_PROVIDER.compareAndSet(null, new SimpleDateTimeTextProvider());
return MUTABLE_PROVIDER.get();
MUTABLE_PROVIDER = new SimpleDateTimeTextProvider();
return MUTABLE_PROVIDER;
}
}

View File

@ -33,11 +33,11 @@ package org.threeten.bp.format;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.threeten.bp.jdk8.Jdk8Methods;
@ -61,7 +61,7 @@ public final class DecimalStyle {
/**
* The cache of symbols instances.
*/
private static final ConcurrentMap<Locale, DecimalStyle> CACHE = new ConcurrentHashMap<Locale, DecimalStyle>(16, 0.75f, 2);
private static final Map<Locale, DecimalStyle> CACHE = new HashMap<>();
/**
* The zero digit.

View File

@ -33,9 +33,9 @@ package org.threeten.bp.format;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.Map;
import org.threeten.bp.chrono.Chronology;
@ -51,8 +51,7 @@ final class SimpleDateTimeFormatStyleProvider extends DateTimeFormatStyleProvide
// TODO: Better implementation based on CLDR
/** Cache of formatters. */
private static final ConcurrentMap<String, Object> FORMATTER_CACHE =
new ConcurrentHashMap<String, Object>(16, 0.75f, 2);
private static final Map<String, Object> FORMATTER_CACHE = new HashMap<>();
@Override
public Locale[] getAvailableLocales() {

View File

@ -49,8 +49,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.threeten.bp.temporal.IsoFields;
import org.threeten.bp.temporal.TemporalField;
@ -75,8 +73,7 @@ final class SimpleDateTimeTextProvider extends DateTimeTextProvider {
};
/** Cache. */
private final ConcurrentMap<Entry<TemporalField, Locale>, Object> cache =
new ConcurrentHashMap<Entry<TemporalField, Locale>, Object>(16, 0.75f, 2);
private final Map<Entry<TemporalField, Locale>, Object> cache = new HashMap<>();
//-----------------------------------------------------------------------
@Override

View File

@ -44,6 +44,7 @@ import static org.threeten.bp.temporal.ChronoUnit.YEARS;
import java.io.InvalidObjectException;
import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -127,7 +128,7 @@ public final class WeekFields implements Serializable {
* The cache of rules by firstDayOfWeek plus minimalDays.
* Initialized first to be available for definition of ISO, etc.
*/
private static final ConcurrentMap<String, WeekFields> CACHE = new ConcurrentHashMap<String, WeekFields>(4, 0.75f, 2);
private static final Map<String, WeekFields> CACHE = new HashMap<>();
/**
* The ISO-8601 definition, where a week starts on Monday and the first week

View File

@ -38,9 +38,9 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.Map;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;
@ -98,8 +98,7 @@ final class StandardZoneRules extends ZoneRules implements Serializable {
/**
* The map of recent transitions.
*/
private final ConcurrentMap<Integer, ZoneOffsetTransition[]> lastRulesCache =
new ConcurrentHashMap<Integer, ZoneOffsetTransition[]>();
private final Map<Integer, ZoneOffsetTransition[]> lastRulesCache = new HashMap<>();
/**
* Creates an instance.

View File

@ -33,8 +33,6 @@ package org.threeten.bp.zone;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/**
* Controls how the time-zone rules are initialized.
@ -57,8 +55,8 @@ public abstract class ZoneRulesInitializer {
*/
public static final ZoneRulesInitializer DO_NOTHING = new DoNothingZoneRulesInitializer();
private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false);
private static final AtomicReference<ZoneRulesInitializer> INITIALIZER = new AtomicReference<ZoneRulesInitializer>();
private static boolean INITIALIZED;
private static ZoneRulesInitializer INITIALIZER;
/**
* Sets the initializer to use.
@ -70,23 +68,27 @@ public abstract class ZoneRulesInitializer {
* @throws IllegalStateException if initialization has already occurred or another initializer has been set
*/
public static void setInitializer(ZoneRulesInitializer initializer) {
if (INITIALIZED.get()) {
if (INITIALIZED) {
throw new IllegalStateException("Already initialized");
}
if (!INITIALIZER.compareAndSet(null, initializer)) {
if (INITIALIZER != null) {
throw new IllegalStateException("Initializer was already set, possibly with a default during initialization");
}
INITIALIZER = initializer;
}
//-----------------------------------------------------------------------
// initialize the providers
static void initialize() {
if (INITIALIZED.getAndSet(true)) {
if (INITIALIZED) {
throw new IllegalStateException("Already initialized");
}
INITIALIZED = true;
// Set the default initializer if none has been provided yet.
INITIALIZER.compareAndSet(null, new ServiceLoaderZoneRulesInitializer());
INITIALIZER.get().initializeProviders();
if (INITIALIZER == null) {
INITIALIZER = new ServiceLoaderZoneRulesInitializer();
}
INITIALIZER.initializeProviders();
}
/**

View File

@ -31,12 +31,13 @@
*/
package org.threeten.bp.zone;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.threeten.bp.DateTimeException;
import org.threeten.bp.ZoneId;
@ -73,11 +74,11 @@ public abstract class ZoneRulesProvider {
/**
* The set of loaded providers.
*/
private static final CopyOnWriteArrayList<ZoneRulesProvider> PROVIDERS = new CopyOnWriteArrayList<ZoneRulesProvider>();
private static final List<ZoneRulesProvider> PROVIDERS = new ArrayList<>();
/**
* The lookup from zone region ID to provider.
*/
private static final ConcurrentMap<String, ZoneRulesProvider> ZONES = new ConcurrentHashMap<String, ZoneRulesProvider>(512, 0.75f, 2);
private static final Map<String, ZoneRulesProvider> ZONES = new HashMap<>();
static {
ZoneRulesInitializer.initialize();
}