You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanItemContainerGenerator.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.vaadin.data.util;
  2. import java.util.Date;
  3. import java.util.concurrent.atomic.AtomicLong;
  4. public class BeanItemContainerGenerator {
  5. public static class PortableRandom {
  6. private final static long multiplier = 0x5DEECE66DL;
  7. private final static long addend = 0xBL;
  8. private final static long mask = (1L << 48) - 1;
  9. private AtomicLong seed;
  10. public PortableRandom(long seed) {
  11. this.seed = new AtomicLong(0L);
  12. setSeed(seed);
  13. }
  14. synchronized public void setSeed(long seed) {
  15. seed = (seed ^ multiplier) & mask;
  16. this.seed.set(seed);
  17. }
  18. public int nextInt(int n) {
  19. if (n <= 0) {
  20. throw new IllegalArgumentException("n must be positive");
  21. }
  22. if ((n & -n) == n) {
  23. return (int) ((n * (long) next(31)) >> 31);
  24. }
  25. int bits, val;
  26. do {
  27. bits = next(31);
  28. val = bits % n;
  29. } while (bits - val + (n - 1) < 0);
  30. return val;
  31. }
  32. protected int next(int bits) {
  33. long oldseed, nextseed;
  34. AtomicLong seed = this.seed;
  35. do {
  36. oldseed = seed.get();
  37. nextseed = (oldseed * multiplier + addend) & mask;
  38. } while (!seed.compareAndSet(oldseed, nextseed));
  39. return (int) (nextseed >>> (48 - bits));
  40. }
  41. public boolean nextBoolean() {
  42. return next(1) != 0;
  43. }
  44. }
  45. public static BeanItemContainer<TestBean> createContainer(int size) {
  46. return createContainer(size, new Date().getTime());
  47. }
  48. public static BeanItemContainer<TestBean> createContainer(int size,
  49. long seed) {
  50. BeanItemContainer<TestBean> container = new BeanItemContainer<TestBean>(
  51. TestBean.class);
  52. PortableRandom r = new PortableRandom(seed);
  53. for (int i = 0; i < size; i++) {
  54. container.addBean(new TestBean(r));
  55. }
  56. return container;
  57. }
  58. public static class TestBean {
  59. private String name, address, city, country;
  60. private int age, shoesize;
  61. public int getAge() {
  62. return age;
  63. }
  64. public void setAge(int age) {
  65. this.age = age;
  66. }
  67. public int getShoesize() {
  68. return shoesize;
  69. }
  70. public void setShoesize(int shoesize) {
  71. this.shoesize = shoesize;
  72. }
  73. public TestBean(PortableRandom r) {
  74. age = r.nextInt(100) + 5;
  75. shoesize = r.nextInt(10) + 35;
  76. name = createRandomString(r, r.nextInt(5) + 5);
  77. address = createRandomString(r, r.nextInt(15) + 5) + " "
  78. + r.nextInt(100) + 1;
  79. city = createRandomString(r, r.nextInt(7) + 3);
  80. if (r.nextBoolean()) {
  81. country = createRandomString(r, r.nextInt(4) + 4);
  82. }
  83. }
  84. public String getName() {
  85. return name;
  86. }
  87. public void setName(String name) {
  88. this.name = name;
  89. }
  90. public String getAddress() {
  91. return address;
  92. }
  93. public void setAddress(String address) {
  94. this.address = address;
  95. }
  96. public String getCity() {
  97. return city;
  98. }
  99. public void setCity(String city) {
  100. this.city = city;
  101. }
  102. public String getCountry() {
  103. return country;
  104. }
  105. public void setCountry(String country) {
  106. this.country = country;
  107. }
  108. }
  109. public static String createRandomString(PortableRandom r, int len) {
  110. StringBuilder b = new StringBuilder();
  111. for (int i = 0; i < len; i++) {
  112. b.append((char) (r.nextInt('z' - 'a') + 'a'));
  113. }
  114. return b.toString();
  115. }
  116. }