]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/PlatformRandomHelper.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / PlatformRandomHelper.java
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5
6 package org.rocksdb;
7
8 import java.util.Random;
9
10 /**
11 * Helper class to get the appropriate Random class instance dependent
12 * on the current platform architecture (32bit vs 64bit)
13 */
14 public class PlatformRandomHelper {
15 /**
16 * Determine if OS is 32-Bit/64-Bit
17 *
18 * @return boolean value indicating if operating system is 64 Bit.
19 */
20 public static boolean isOs64Bit(){
21 final boolean is64Bit;
22 if (System.getProperty("os.name").contains("Windows")) {
23 is64Bit = (System.getenv("ProgramFiles(x86)") != null);
24 } else {
25 is64Bit = (System.getProperty("os.arch").contains("64"));
26 }
27 return is64Bit;
28 }
29
30 /**
31 * Factory to get a platform specific Random instance
32 *
33 * @return {@link java.util.Random} instance.
34 */
35 public static Random getPlatformSpecificRandomFactory(){
36 if (isOs64Bit()) {
37 return new Random();
38 }
39 return new Random32Bit();
40 }
41
42 /**
43 * Random32Bit is a class which overrides {@code nextLong} to
44 * provide random numbers which fit in size_t. This workaround
45 * is necessary because there is no unsigned_int < Java 8
46 */
47 private static class Random32Bit extends Random {
48 @Override
49 public long nextLong(){
50 return this.nextInt(Integer.MAX_VALUE);
51 }
52 }
53
54 /**
55 * Utility class constructor
56 */
57 private PlatformRandomHelper() { }
58 }