• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import java.util.Random;
20 
21 /**
22  * Utility class for being able to seed a {@link Random} value with a passed in seed from a
23  * benchmark parameter.
24  *
25  * <p>TODO: Remove this class once Caliper has a better way.
26  *
27  * @author Nicholaus Shupe
28  */
29 public final class SpecialRandom extends Random {
valueOf(String s)30   public static SpecialRandom valueOf(String s) {
31     return (s.length() == 0) ? new SpecialRandom() : new SpecialRandom(Long.parseLong(s));
32   }
33 
34   private final boolean hasSeed;
35   private final long seed;
36 
SpecialRandom()37   public SpecialRandom() {
38     this.hasSeed = false;
39     this.seed = 0;
40   }
41 
SpecialRandom(long seed)42   public SpecialRandom(long seed) {
43     super(seed);
44     this.hasSeed = true;
45     this.seed = seed;
46   }
47 
48   @Override
toString()49   public String toString() {
50     return hasSeed ? "(seed:" + seed : "(default seed)";
51   }
52 
53   private static final long serialVersionUID = 0;
54 }
55