Java Random String randomUnicodeString(Random r)

Here you can find the source of randomUnicodeString(Random r)

Description

Returns random string, including full unicode range.

License

Apache License

Declaration

public static String randomUnicodeString(Random r) 

Method Source Code

//package com.java2s;
/**//from w w w . j  a v a2 s.  c o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /** Returns random string, including full unicode range. */
    public static String randomUnicodeString(Random r) {
        return randomUnicodeString(r, 20);
    }

    /**
     * Returns a random string up to a certain length.
     */
    public static String randomUnicodeString(Random r, int maxLength) {
        final int end = nextInt(r, 0, maxLength);
        if (end == 0) {
            // allow 0 length
            return "";
        }
        final char[] buffer = new char[end];
        randomFixedLengthUnicodeString(r, buffer, 0, buffer.length);
        return new String(buffer, 0, end);
    }

    /** start and end are BOTH inclusive */
    public static int nextInt(Random r, int start, int end) {
        return start + r.nextInt(end - start + 1);
    }

    /**
     * Fills provided char[] with valid random unicode code
     * unit sequence.
     */
    public static void randomFixedLengthUnicodeString(Random random,
            char[] chars, int offset, int length) {
        int i = offset;
        final int end = offset + length;
        while (i < end) {
            final int t = random.nextInt(5);
            if (0 == t && i < length - 1) {
                // Make a surrogate pair
                // High surrogate
                chars[i++] = (char) nextInt(random, 0xd800, 0xdbff);
                // Low surrogate
                chars[i++] = (char) nextInt(random, 0xdc00, 0xdfff);
            } else if (t <= 1) {
                chars[i++] = (char) random.nextInt(0x80);
            } else if (2 == t) {
                chars[i++] = (char) nextInt(random, 0x80, 0x7ff);
            } else if (3 == t) {
                chars[i++] = (char) nextInt(random, 0x800, 0xd7ff);
            } else if (4 == t) {
                chars[i++] = (char) nextInt(random, 0xe000, 0xfffe);
            }
        }
    }
}

Related

  1. randomString(int stringLength)
  2. randomString(Integer len)
  3. randomString(Random r, int minCount, int maxCount)
  4. randomSubset(String[] list, int count)
  5. randomSuffix(final String name)
  6. Shuffle(ArrayList list, Random randomNumberGenerator)