11/*
2- * Copyright (c) 2005, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
4444import java .util .HashMap ;
4545import java .util .HashSet ;
4646import java .util .Map ;
47+ import java .util .Objects ;
4748import java .util .ResourceBundle ;
4849import java .util .Set ;
50+ import java .util .function .Supplier ;
51+
4952import sun .util .ResourceBundleEnumeration ;
5053
5154/**
@@ -69,12 +72,9 @@ protected OpenListResourceBundle() {
6972 // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
7073 @ Override
7174 protected Object handleGetObject (String key ) {
72- if (key == null ) {
73- throw new NullPointerException ();
74- }
75-
76- loadLookupTablesIfNecessary ();
77- return lookup .get (key ); // this class ignores locales
75+ Objects .requireNonNull (key );
76+ return lookup .get ()
77+ .get (key ); // this class ignores locales
7878 }
7979
8080 /**
@@ -93,65 +93,20 @@ public Enumeration<String> getKeys() {
9393 */
9494 @ Override
9595 protected Set <String > handleKeySet () {
96- loadLookupTablesIfNecessary ();
97- return lookup .keySet ();
96+ return lookup . get ()
97+ .keySet ();
9898 }
9999
100100 @ Override
101101 public Set <String > keySet () {
102- if (keyset != null ) {
103- return keyset ;
104- }
105- Set <String > ks = createSet ();
106- ks .addAll (handleKeySet ());
107- if (parent != null ) {
108- ks .addAll (parent .keySet ());
109- }
110- synchronized (this ) {
111- if (keyset == null ) {
112- keyset = ks ;
113- }
114- }
115- return keyset ;
102+ return keyset .get ();
116103 }
117104
118105 /**
119106 * See ListResourceBundle class description.
120107 */
121108 protected abstract Object [][] getContents ();
122109
123- /**
124- * Load lookup tables if they haven't been loaded already.
125- */
126- void loadLookupTablesIfNecessary () {
127- if (lookup == null ) {
128- loadLookup ();
129- }
130- }
131-
132- /**
133- * We lazily load the lookup hashtable. This function does the
134- * loading.
135- */
136- private void loadLookup () {
137- Object [][] contents = getContents ();
138- Map <String , Object > temp = createMap (contents .length );
139- for (int i = 0 ; i < contents .length ; ++i ) {
140- // key must be non-null String, value must be non-null
141- String key = (String ) contents [i ][0 ];
142- Object value = contents [i ][1 ];
143- if (key == null || value == null ) {
144- throw new NullPointerException ();
145- }
146- temp .put (key , value );
147- }
148- synchronized (this ) {
149- if (lookup == null ) {
150- lookup = temp ;
151- }
152- }
153- }
154-
155110 /**
156111 * Lets subclasses provide specialized Map implementations.
157112 * Default uses HashMap.
@@ -164,6 +119,31 @@ protected <E> Set<E> createSet() {
164119 return new HashSet <>();
165120 }
166121
167- private volatile Map <String , Object > lookup ;
168- private volatile Set <String > keyset ;
122+ private final Supplier <Map <String , Object >> lookup = StableValue .supplier (
123+ new Supplier <>() { public Map <String , Object > get () { return lookup0 (); }});
124+
125+ private Map <String , Object > lookup0 () {
126+ final Object [][] contents = getContents ();
127+ final Map <String , Object > temp = createMap (contents .length );
128+ for (Object [] content : contents ) {
129+ // key must be non-null String, value must be non-null
130+ final String key = Objects .requireNonNull ((String ) content [0 ]);
131+ final Object value = Objects .requireNonNull (content [1 ]);
132+ temp .put (key , value );
133+ }
134+ return temp ;
135+ }
136+
137+ private final Supplier <Set <String >> keyset = StableValue .supplier (
138+ new Supplier <>() { public Set <String > get () { return keyset0 (); }});
139+
140+ private Set <String > keyset0 () {
141+ final Set <String > ks = createSet ();
142+ ks .addAll (handleKeySet ());
143+ if (parent != null ) {
144+ ks .addAll (parent .keySet ());
145+ }
146+ return ks ;
147+ }
148+
169149}
0 commit comments