Java Queue Usage scramble(Object object)

Here you can find the source of scramble(Object object)

Description

Will alter the order of the letters in the String representation of an Object.

License

Open Source License

Parameter

Parameter Description
object the object.

Return

a scrambled string representation of the object.

Declaration

public static String scramble(Object object) 

Method Source Code

//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;
    }
}

Related

  1. offerUntilSuccess(T entry, Queue queue)
  2. parsePathToStack(String path)
  3. pollWhileNotEmpty(Queue queue)
  4. queue(final Queue queue, final Object o, final Class clazz, final boolean removeNull)
  5. randomizeIndex(char[] array)
  6. setQueueValue(final Queue queue, final Object obj, final Class clazz, final boolean removeNull)
  7. toArrayTest(Queue q)
  8. transferAllElementsFromListToQueue(final List elements, Queue queue)

  9. HOME | Copyright © www.java2s.com 2016