You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RandomContext.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.Random;
  17. /**
  18. * This class effectively encapsulates the stateful logic of the "Rnd"
  19. * function.
  20. *
  21. * @author James Ahlborn
  22. */
  23. public class RandomContext
  24. {
  25. private Source _defRnd;
  26. private Map<Integer,Source> _rnds;
  27. // default to the value access uses for "last val" when none has been
  28. // returned yet
  29. private float _lastVal = 1.953125E-02f;
  30. public RandomContext()
  31. {
  32. }
  33. public float getRandom(Integer seed) {
  34. if(seed == null) {
  35. if(_defRnd == null) {
  36. _defRnd = new SimpleSource(createRandom(System.currentTimeMillis()));
  37. }
  38. return _defRnd.get();
  39. }
  40. if(_rnds == null) {
  41. // note, we don't use a SimpleCache here because if we discard a Random
  42. // instance, that will cause the values to be reset
  43. _rnds = new HashMap<Integer,Source>();
  44. }
  45. Source rnd = _rnds.get(seed);
  46. if(rnd == null) {
  47. int seedInt = seed;
  48. if(seedInt > 0) {
  49. // normal random with a user specified seed
  50. rnd = new SimpleSource(createRandom(seedInt));
  51. } else if(seedInt < 0) {
  52. // returns the same value every time and resets all randoms
  53. rnd = new ResetSource(createRandom(seedInt));
  54. } else {
  55. // returns the last random value returned
  56. rnd = new LastValSource();
  57. }
  58. _rnds.put(seed, rnd);
  59. }
  60. return rnd.get();
  61. }
  62. private float setLast(float lastVal) {
  63. _lastVal = lastVal;
  64. return lastVal;
  65. }
  66. private void reset() {
  67. if(_rnds != null) {
  68. _rnds.clear();
  69. }
  70. }
  71. private static Random createRandom(long seed) {
  72. // TODO, support SecureRandom?
  73. return new Random(seed);
  74. }
  75. private abstract class Source
  76. {
  77. public float get() {
  78. return setLast(getImpl());
  79. }
  80. protected abstract float getImpl();
  81. }
  82. private class SimpleSource extends Source
  83. {
  84. private final Random _rnd;
  85. private SimpleSource(Random rnd) {
  86. _rnd = rnd;
  87. }
  88. @Override
  89. protected float getImpl() {
  90. return _rnd.nextFloat();
  91. }
  92. }
  93. private class ResetSource extends Source
  94. {
  95. private final float _val;
  96. private ResetSource(Random rnd) {
  97. _val = rnd.nextFloat();
  98. }
  99. @Override
  100. protected float getImpl() {
  101. reset();
  102. return _val;
  103. }
  104. }
  105. private class LastValSource extends Source
  106. {
  107. private LastValSource() {
  108. }
  109. @Override
  110. protected float getImpl() {
  111. return _lastVal;
  112. }
  113. }
  114. }