001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Iterator;
018    import java.util.LinkedHashSet;
019    import java.util.List;
020    import java.util.Random;
021    import java.util.Set;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class Randomizer extends Random {
027    
028            public static Randomizer getInstance() {
029                    return _instance;
030            }
031    
032            public Randomizer() {
033                    super();
034            }
035    
036            public Randomizer(long seed) {
037                    super(seed);
038            }
039    
040            public int[] nextInt(int n, int size) {
041                    if (size > n) {
042                            size = n;
043                    }
044    
045                    Set<Integer> set = new LinkedHashSet<Integer>();
046    
047                    for (int i = 0; i < size; i++) {
048                            while (true) {
049                                    Integer value = new Integer(nextInt(n));
050    
051                                    if (!set.contains(value)) {
052                                            set.add(value);
053    
054                                            break;
055                                    }
056                            }
057                    }
058    
059                    int[] array = new int[set.size()];
060    
061                    Iterator<Integer> itr = set.iterator();
062    
063                    for (int i = 0; i < array.length; i++) {
064                            array[i] = itr.next().intValue();
065                    }
066    
067                    return array;
068            }
069    
070            public void randomize(char[] array) {
071                    int length = array.length;
072    
073                    for (int i = 0; i < length - 1; i++) {
074                            int x = nextInt(length);
075                            char y = array[i];
076    
077                            array[i] = array[i + x];
078                            array[i + x] = y;
079    
080                            length--;
081                    }
082            }
083    
084            public void randomize(int[] array) {
085                    int length = array.length;
086    
087                    for (int i = 0; i < length - 1; i++) {
088                            int x = nextInt(length);
089                            int y = array[i];
090    
091                            array[i] = array[i + x];
092                            array[i + x] = y;
093    
094                            length--;
095                    }
096            }
097    
098            public void randomize(List<Object> list) {
099                    int size = list.size();
100    
101                    for (int i = 0; i <= size; i++) {
102                            Object obj = list.get(i);
103    
104                            int j = nextInt(size);
105    
106                            list.set(i, list.get(i + j));
107                            list.set(i + j, obj);
108    
109                            size--;
110                    }
111            }
112    
113            public void randomize(Object[] array) {
114                    int length = array.length;
115    
116                    for (int i = 0; i < length - 1; i++) {
117                            int x = nextInt(length);
118                            Object y = array[i];
119    
120                            array[i] = array[i + x];
121                            array[i + x] = y;
122    
123                            length--;
124                    }
125            }
126    
127            public String randomize(String s) {
128                    if (s == null) {
129                            return null;
130                    }
131    
132                    char[] array = s.toCharArray();
133    
134                    randomize(array);
135    
136                    return new String(array);
137            }
138    
139            private static Randomizer _instance = new Randomizer();
140    
141    }