aboutsummaryrefslogtreecommitdiffstats
path: root/server/tests/src/com/vaadin/data/util/BeanItemContainerGenerator.java
blob: 7400f0efcf57ca4e74c3695d836dc261bb013466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.vaadin.data.util;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import org.junit.Ignore;

@Ignore
public class BeanItemContainerGenerator {

    public static class PortableRandom {
        private final static long multiplier = 0x5DEECE66DL;
        private final static long addend = 0xBL;
        private final static long mask = (1L << 48) - 1;
        private AtomicLong seed;

        public PortableRandom(long seed) {
            this.seed = new AtomicLong(0L);
            setSeed(seed);
        }

        synchronized public void setSeed(long seed) {
            seed = (seed ^ multiplier) & mask;
            this.seed.set(seed);
        }

        public int nextInt(int n) {
            if (n <= 0) {
                throw new IllegalArgumentException("n must be positive");
            }

            if ((n & -n) == n) {
                return (int) ((n * (long) next(31)) >> 31);
            }

            int bits, val;
            do {
                bits = next(31);
                val = bits % n;
            } while (bits - val + (n - 1) < 0);
            return val;
        }

        protected int next(int bits) {
            long oldseed, nextseed;
            AtomicLong seed = this.seed;
            do {
                oldseed = seed.get();
                nextseed = (oldseed * multiplier + addend) & mask;
            } while (!seed.compareAndSet(oldseed, nextseed));
            return (int) (nextseed >>> (48 - bits));
        }

        public boolean nextBoolean() {
            return next(1) != 0;
        }

    }

    public static BeanItemContainer<TestBean> createContainer(int size) {
        return createContainer(size, new Date().getTime());
    }

    public static BeanItemContainer<TestBean> createContainer(int size,
            long seed) {

        BeanItemContainer<TestBean> container = new BeanItemContainer<TestBean>(
                TestBean.class);
        PortableRandom r = new PortableRandom(seed);
        for (int i = 0; i < size; i++) {
            container.addBean(new TestBean(r));
        }

        return container;

    }

    public static class TestBean {
        private String name, address, city, country;
        private int age, shoesize;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public int getShoesize() {
            return shoesize;
        }

        public void setShoesize(int shoesize) {
            this.shoesize = shoesize;
        }

        public TestBean(PortableRandom r) {
            age = r.nextInt(100) + 5;
            shoesize = r.nextInt(10) + 35;
            name = createRandomString(r, r.nextInt(5) + 5);
            address = createRandomString(r, r.nextInt(15) + 5) + " "
                    + r.nextInt(100) + 1;
            city = createRandomString(r, r.nextInt(7) + 3);
            if (r.nextBoolean()) {
                country = createRandomString(r, r.nextInt(4) + 4);
            }
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

    }

    public static String createRandomString(PortableRandom r, int len) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < len; i++) {
            b.append((char) (r.nextInt('z' - 'a') + 'a'));
        }

        return b.toString();
    }

}