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