Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2017, OpenCensus 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 io.opencensus.implcore.trace.internal;
     18 
     19 import java.security.SecureRandom;
     20 import java.util.Random;
     21 import javax.annotation.concurrent.ThreadSafe;
     22 
     23 /**
     24  * Abstract class to access the current {@link Random}.
     25  *
     26  * <p>Implementation can have a per thread instance or a single global instance.
     27  */
     28 @ThreadSafe
     29 public abstract class RandomHandler {
     30   /**
     31    * Returns the current {@link Random}.
     32    *
     33    * @return the current {@code Random}.
     34    */
     35   public abstract Random current();
     36 
     37   /** Implementation of the {@link RandomHandler} using {@link SecureRandom}. */
     38   @ThreadSafe
     39   public static final class SecureRandomHandler extends RandomHandler {
     40     private final Random random = new SecureRandom();
     41 
     42     /** Constructs a new {@link SecureRandomHandler}. */
     43     public SecureRandomHandler() {}
     44 
     45     @Override
     46     public Random current() {
     47       return random;
     48     }
     49   }
     50 }
     51