Skip to content

Commit a0c978f

Browse files
committedJan 8, 2024
Add storage type for some factories
1 parent 4fef161 commit a0c978f

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed
 

‎src/java.base/share/classes/java/lang/ComputedConstant.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* </li>
5959
* <li>Collections
6060
* <ul>
61-
* <li>{@linkplain ComputedConstant#of(int, IntFunction) ComputedConstant.of(int length, IntFunction&lt;? super V&gt; mappingProvider)}
61+
* <li>{@linkplain ComputedConstant#of(Class, int, IntFunction) ComputedConstant.of(Class&lt;? super V&gt, int length, IntFunction&lt;? super V&gt; mappingProvider)}
6262
* providing a new List of ComputedConstant elements</li>
6363
* </ul>
6464
* </li>
@@ -428,7 +428,8 @@ static <V, R extends V> ComputedConstant<V> of(Class<R> returnType, MethodHandle
428428

429429
/**
430430
* {@return a new unmodifiable List of {@link ComputedConstant } elements with the provided
431-
* {@code size} and given pre-set {@code mappingProvider} to be used to compute element values}
431+
* {@code size} and given pre-set {@code mappingProvider} to be used to compute
432+
* element values}
432433
* <p>
433434
* The List and its elements are eligible for constant folding optimizations by the JVM.
434435
* <p>
@@ -448,11 +449,14 @@ static <V, R extends V> ComputedConstant<V> of(Class<R> returnType, MethodHandle
448449
* ComputedConstant elements that has no valid identity.
449450
*
450451
* @param <V> the type of the values
452+
* @param storageType a class literal representing an optional storage type of the bound values
451453
* @param size the size of the List
452454
* @param mappingProvider to invoke when computing and binding element values
453455
*/
454-
static <V> List<ComputedConstant<V>> of(int size,
456+
static <V> List<ComputedConstant<V>> of(Class<? super V> storageType,
457+
int size,
455458
IntFunction<? extends V> mappingProvider) {
459+
Objects.requireNonNull(storageType);
456460
if (size < 0) {
457461
throw new IllegalArgumentException();
458462
}

‎src/java.base/share/classes/java/lang/SettableConstant.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static <V> SettableConstant<V> of(Class<? super V> storageType, V value) {
226226
* class DemoList {
227227
*
228228
* private static final List<SettableConstant<Long>> CONSTANTS =
229-
* SettableConstant.of(32);
229+
* SettableConstant.of(long.class, 32);
230230
* static {
231231
* // Compute values in the CONSTANTS list
232232
* }
@@ -239,10 +239,12 @@ static <V> SettableConstant<V> of(Class<? super V> storageType, V value) {
239239
* @apiNote The list is free to return <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
240240
* ComputedConstant elements that has no valid identity.
241241
*
242-
* @param <V> the type of the values
243-
* @param size the size of the List
244-
*/
245-
static <V> List<SettableConstant<V>> ofList(int size) {
242+
* @param <V> the type of the values
243+
* @param storageType a class literal representing an optional storage type of the bound value
244+
* @param size the size of the List
245+
*/
246+
static <V> List<SettableConstant<V>> of(Class<? super V> storageType, int size) {
247+
Objects.requireNonNull(storageType);
246248
if (size < 0) {
247249
throw new IllegalArgumentException();
248250
}

‎src/java.base/share/classes/java/lang/snippet-files/ComputedConstantSnippets.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class Fibonacci2 {
227227
private final List<ComputedConstant<Integer>> list;
228228

229229
public Fibonacci2(int upperBound) {
230-
list = ComputedConstant.of(upperBound, this::number);
230+
list = ComputedConstant.of(int.class, upperBound, this::number);
231231
}
232232

233233
public int number(int n) {
@@ -251,7 +251,7 @@ public static void main(String[] args) {
251251
class Fibonacci3 {
252252

253253
private static final List<ComputedConstant<Integer>> LIST
254-
= ComputedConstant.of(40, Fibonacci3::number);
254+
= ComputedConstant.of(int.class, 40, Fibonacci3::number);
255255

256256
public static int number(int n) {
257257
return (n < 2)

‎test/jdk/java/lang/ComputedConstant/SettableConstantTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ void predicates(Class<? super Integer> storageType) {
116116
assertTrue(constant.isBound());
117117
}
118118

119+
@ParameterizedTest
120+
@MethodSource("storageTypes")
121+
void computeIfUnbound(Class<? super Integer> storageType) {
122+
SettableConstant<Integer> constant = SettableConstant.of(storageType);
123+
Integer actual = constant.computeIfUnbound(() -> 2);
124+
assertEquals(2, actual);
125+
assertEquals(2, constant.get());
126+
}
127+
128+
@ParameterizedTest
129+
@MethodSource("storageTypes")
130+
void computeIfUnboundBound(Class<? super Integer> storageType) {
131+
SettableConstant<Integer> constant = SettableConstant.of(storageType, 1);
132+
Integer actual = constant.computeIfUnbound(() -> 2);
133+
assertEquals(1, actual);
134+
assertEquals(1, constant.get());
135+
}
136+
119137
@ParameterizedTest
120138
@MethodSource("storageTypes")
121139
void setIfUnbound(Class<? super Integer> storageType) {

0 commit comments

Comments
 (0)