Here you can find the source of scramble(Object object)
Parameter | Description |
---|---|
object | the object. |
public static String scramble(Object object)
//package com.java2s; /*//from w ww. j av a2s . c om This file is part of Cuber. Cuber is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Cuber is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Cuber. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Main { /** * Will alter the order of the letters in the String representation of an Object. * * @param object * the object. * @return a scrambled string representation of the object. * @author wonka * @since 23/04/2013 */ public static String scramble(Object object) { String string = object.toString(); char[] array = string.toCharArray(); char[] randomArray = randomizeIndex(array); return new String(randomArray); } /** * @param array * the array to randomize. * @return an array that is a result of mixing the index values of the first array. * @author wonka * @since 23/04/2013 */ private static char[] randomizeIndex(char[] array) { char[] randomArray = new char[array.length]; List<Integer> set = new ArrayList<>(); while (set.size() < array.length) { int index = (int) (Math.random() * array.length); if (!set.contains(index)) { set.add(index); } } Queue<Integer> queue = new LinkedList<>(set); int i = 0; while (!queue.isEmpty()) { randomArray[i++] = array[queue.poll()]; } return randomArray; } }