Home | History | Annotate | Download | only in collect
      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
     23  * in seed from a benchmark parameter.
     24  *
     25  * TODO: Remove this class once Caliper has a better way.
     26  *
     27  * @author Nicholaus Shupe
     28  */
     29 public final class SpecialRandom extends Random {
     30   public static SpecialRandom valueOf(String s) {
     31     return (s.length() == 0)
     32         ? new SpecialRandom()
     33         : new SpecialRandom(Long.parseLong(s));
     34   }
     35 
     36   private final boolean hasSeed;
     37   private final long seed;
     38 
     39   public SpecialRandom() {
     40     this.hasSeed = false;
     41     this.seed = 0;
     42   }
     43 
     44   public SpecialRandom(long seed) {
     45     super(seed);
     46     this.hasSeed = true;
     47     this.seed = seed;
     48   }
     49 
     50   @Override public String toString() {
     51     return hasSeed ? "(seed:" + seed : "(default seed)";
     52   }
     53 
     54   private static final long serialVersionUID = 0;
     55 }
     56