/** * Copyright (c) 2005-2010 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: DataUtils.java 1185 2010-08-29 15:56:19Z calvinxiu $ */ package org.springside.modules.test.utils; import java.util.Collections; import java.util.List; import java.util.Random; /** * 测试数据生成工具类. * * @author calvin */ public class DataUtils { private static Random random = new Random(); /** * 返回随机ID. */ public static long randomId() { return random.nextLong(); } /** * 返回随机名称, prefix字符串+5位随机数字. */ public static String randomName(String prefix) { return prefix + random.nextInt(10000); } /** * 从输入list中随机返回一个对象. */ public static <T> T randomOne(List<T> list) { return randomSome(list, 1).get(0); } /** * 从输入list中随机返回随机个对象. */ public static <T> List<T> randomSome(List<T> list) { return randomSome(list, random.nextInt(list.size())); } /** * 从输入list中随机返回count个对象. */ public static <T> List<T> randomSome(List<T> list, int count) { Collections.shuffle(list); return list.subList(0, count); } }