Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8284013

Code example provided by RandomGeneratorFactory does not compile

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 19
    • core-libs
    • None
    • minimal
    • Document change only. No change to spec.
    • Java API
    • SE

      Summary

      A code example in RandomGeneratorFactory was not updated to reflect a change in the API.

      Problem

      The original example fetched the stateBits from an instance of RandomGenerator. The API was updated to fetch the stateBits from the RandomGeneratorFactory. It makes more sense to query before instances are created.

      Solution

      Update the example to work correctly.

      Specification

      diff --git a/src/java.base/share/classes/java/util/random/RandomGeneratorFactory.java b/src/java.base/share/classes/java/util/random/RandomGeneratorFactory.java
      index 552d7b03d53..a2ff3407a15 100644
      --- a/src/java.base/share/classes/java/util/random/RandomGeneratorFactory.java
      +++ b/src/java.base/share/classes/java/util/random/RandomGeneratorFactory.java
      @@ -1,5 +1,5 @@
       /*
      - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
      + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
        * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
        *
        * This code is free software; you can redistribute it and/or modify it
      @@ -67,7 +67,7 @@ import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
        * {@link RandomGeneratorFactory#create()} is used for random seed
        * construction. Example;
        *
      - * <pre>{@code
      + * {@snippet :
        *    RandomGeneratorFactory<RandomGenerator> factory = RandomGeneratorFactory.of("Random");
        *
        *     for (int i = 0; i < 10; i++) {
      @@ -76,7 +76,7 @@ import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
        *             System.out.println(random.nextDouble());
        *         }).start();
        *     }
      - * }</pre>
      + * }
        *
        * RandomGeneratorFactory also provides methods describing the attributes (or properties)
        * of a generator and can be used to select random number generator
      @@ -87,16 +87,17 @@ import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
        * {@link RandomGenerator RandomGenerators}
        * with the highest number of state bits.
        *
      - * <pre>{@code
      + * {@snippet :
        *     RandomGeneratorFactory<RandomGenerator> best = RandomGeneratorFactory.all()
      - *         .sorted(Comparator.comparingInt(RandomGenerator::stateBits).reversed())
      + *         .filter(rgf -> !rgf.name().equals("SecureRandom")) // SecureRandom has MAX_VALUE stateBits.
      + *         .sorted(Comparator.comparingInt(RandomGeneratorFactory<RandomGenerator>::stateBits).reversed())
        *         .findFirst()
        *         .orElse(RandomGeneratorFactory.of("Random"));
        *     System.out.println(best.name() + " in " + best.group() + " was selected");
        *
        *     RandomGenerator rng = best.create();
        *     System.out.println(rng.nextLong());
      - * }</pre>
      + * }
        *
        * @since 17
        *

            jlaskey Jim Laskey
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: