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     * @deprecated As of 6.2.0
026     */
027    public class Randomizer extends Random {
028    
029            public static Randomizer getInstance() {
030                    return _instance;
031            }
032    
033            public Randomizer() {
034                    super();
035            }
036    
037            public Randomizer(long seed) {
038                    super(seed);
039            }
040    
041            public int[] nextInt(int n, int size) {
042                    if (size > n) {
043                            size = n;
044                    }
045    
046                    Set<Integer> set = new LinkedHashSet<Integer>();
047    
048                    for (int i = 0; i < size; i++) {
049                            while (true) {
050                                    Integer value = new Integer(nextInt(n));
051    
052                                    if (!set.contains(value)) {
053                                            set.add(value);
054    
055                                            break;
056                                    }
057                            }
058                    }
059    
060                    int[] array = new int[set.size()];
061    
062                    Iterator<Integer> itr = set.iterator();
063    
064                    for (int i = 0; i < array.length; i++) {
065                            array[i] = itr.next().intValue();
066                    }
067    
068                    return array;
069            }
070    
071            public void randomize(char[] array) {
072                    int length = array.length;
073    
074                    for (int i = 0; i < length - 1; i++) {
075                            int x = nextInt(length);
076                            char y = array[i];
077    
078                            array[i] = array[i + x];
079                            array[i + x] = y;
080    
081                            length--;
082                    }
083            }
084    
085            public void randomize(int[] array) {
086                    int length = array.length;
087    
088                    for (int i = 0; i < length - 1; i++) {
089                            int x = nextInt(length);
090                            int y = array[i];
091    
092                            array[i] = array[i + x];
093                            array[i + x] = y;
094    
095                            length--;
096                    }
097            }
098    
099            public void randomize(List<Object> list) {
100                    int size = list.size();
101    
102                    for (int i = 0; i <= size; i++) {
103                            Object obj = list.get(i);
104    
105                            int j = nextInt(size);
106    
107                            list.set(i, list.get(i + j));
108                            list.set(i + j, obj);
109    
110                            size--;
111                    }
112            }
113    
114            public void randomize(Object[] array) {
115                    int length = array.length;
116    
117                    for (int i = 0; i < length - 1; i++) {
118                            int x = nextInt(length);
119                            Object y = array[i];
120    
121                            array[i] = array[i + x];
122                            array[i + x] = y;
123    
124                            length--;
125                    }
126            }
127    
128            public String randomize(String s) {
129                    if (s == null) {
130                            return null;
131                    }
132    
133                    char[] array = s.toCharArray();
134    
135                    randomize(array);
136    
137                    return new String(array);
138            }
139    
140            private static Randomizer _instance = new Randomizer();
141    
142    }