首页

关于instinct源码包定义RandomProvider随机数实现类提供区间范围、任意基础数据类型随机数生成源码实现

标签:instinct,随机数,RandomProviderImpl,googlecode     发布时间:2018-07-08   

一、前言

关于instinct源码包com.googlecode.instinct.internal.actor.RandomProvider、com.googlecode.instinct.internal.actor.RandomProviderImpl随机数接口定义及实现类,可以生成对指定任意数据类型随机数,对指定最小最大值范围区间生成随机数,参见下面源码说明部分。

二、源码说明

1.RandomProvider接口

package com.googlecode.instinct.internal.actor;@b@@b@public abstract interface RandomProvider@b@{@b@  public abstract <T> T randomValue(Class<T> paramClass);@b@@b@  public abstract int randomIntInRange(int paramInt1, int paramInt2);@b@}

2.RandomProviderImpl实现类

package com.googlecode.instinct.internal.actor;@b@@b@import java.util.Random;@b@@b@public final class RandomProviderImpl@b@  implements RandomProvider@b@{@b@  private Random random;@b@@b@  public RandomProviderImpl()@b@  {@b@    this.random = new Random();@b@  }@b@@b@  public <T> T randomValue(Class<T> type)@b@  {@b@    if (type == Boolean.class)@b@      return randomBoolean();@b@@b@    if (type == Integer.class)@b@      return randomInteger();@b@@b@    if (type == Long.class)@b@      return randomLong();@b@@b@    if (type == Float.class)@b@      return randomFloat();@b@@b@    if (type == Double.class)@b@      return randomDouble();@b@@b@    if (type == Byte.class)@b@      return randomByte();@b@@b@    if (type == String.class)@b@      return randomString();@b@@b@    if (type == Class.class)@b@      return randomClass();@b@@b@    if (type == Object.class)@b@      return randomObject();@b@@b@    throw new UnsupportedOperationException("Hmm.  I cannot provide an instance of '" + type + "'.  " + "Might be worth edgifying (hiding behind an interface) this type or talking to the boosters!");@b@  }@b@@b@  public int randomIntInRange(int min, int max)@b@  {@b@    if (min >= max) {@b@      throw new IllegalArgumentException("min of range must be less than max");@b@    }@b@@b@    int partitionSize = max - min + 1;@b@    int intInPartition = this.random.nextInt(partitionSize);@b@    return (intInPartition + min);@b@  }@b@@b@  private Object randomClass()@b@  {@b@    return InternalInterface.class;@b@  }@b@@b@  private Object randomString() {@b@    return "Some random string " + this.random.nextLong();@b@  }@b@@b@  private Object randomBoolean() {@b@    return Boolean.valueOf(this.random.nextBoolean());@b@  }@b@@b@  private Object randomInteger() {@b@    return Integer.valueOf(this.random.nextInt());@b@  }@b@@b@  private Object randomLong() {@b@    return Long.valueOf(this.random.nextLong());@b@  }@b@@b@  private Object randomFloat() {@b@    return Float.valueOf(this.random.nextFloat());@b@  }@b@@b@  private Object randomDouble() {@b@    return Double.valueOf(this.random.nextDouble());@b@  }@b@@b@  private Object randomByte() {@b@    byte[] bytes = new byte[1];@b@    this.random.nextBytes(bytes);@b@    return Byte.valueOf(bytes[0]);@b@  }@b@@b@  private Object randomObject() {@b@    return new Object();@b@  }@b@@b@  private static abstract interface InternalInterface@b@  {@b@  }@b@}