dtu.ds.warnme.utils.RandomUtils.java Source code

Java tutorial

Introduction

Here is the source code for dtu.ds.warnme.utils.RandomUtils.java

Source

/**
 * Project:   warnme-server
 * File:      RandomUtils.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Apr 24, 2014
 */

package dtu.ds.warnme.utils;

import java.text.DecimalFormat;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;

import dtu.ds.warnme.model.impl.EventEntity;
import dtu.ds.warnme.model.impl.EventType;
import dtu.ds.warnme.model.impl.UserEntity;

/**
 * @author Bartosz Cichecki
 */
public class RandomUtils {

    public static boolean compareDouble(double a, double b, int precision) {
        return Math.abs(a - b) <= Math.pow(10, -precision);
    }

    public static boolean compareFloat(float a, float b, int precision) {
        return Math.abs(a - b) <= Math.pow(10, -precision);
    }

    public static String getBasicAuth(String username, String password) {
        return "Basic " + new String(Base64.encodeBase64((username + ":" + password).getBytes()));
    }

    public static float randomBearing() {
        float[] values = new float[] { 0f, 90f, 180f, 270f };
        int idx = new Random().nextInt(values.length);
        return values[idx];
    }

    public static EventEntity randomEvent() throws Exception {
        EventEntity event = new EventEntity();
        event.setBearing(randomBearing());
        event.setEventType(randomEventType());
        event.setLatitude(randomFloat(-90, 90));
        event.setLongitude(randomFloat(-180, 180));
        event.setNote(randomString(0, 25));
        event.setReportsCount(randomInt(0, 100));
        return event;
    }

    public static EventType randomEventType() {
        int pick = new Random().nextInt(EventType.values().length);
        return EventType.values()[pick];
    }

    public static float randomFloat(float min, float max) {
        Float f = (float) (min + Math.random() * (max - min + 1));
        return Float.valueOf(new DecimalFormat("#.##").format(f));
    }

    public static int randomInt(int min, int max) {
        return (int) (min + Math.random() * (max - min + 1));
    }

    public static String randomString(int minChars, int maxChars) {
        char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(),./;'[]\\<>?:\"{}|-=_+".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < randomInt(minChars, maxChars); i++) {
            char c = chars[random.nextInt(chars.length)];
            sb.append(c);
        }
        String output = sb.toString();
        return output;
    }

    public static UserEntity randomUser() throws Exception {
        UserEntity user = new UserEntity();
        user.setUsername("user" + System.nanoTime());
        user.setPassword(SecurityUtils.hashSHA512Base64("pass" + System.nanoTime()));
        user.setEmail("email" + System.nanoTime() + "@email.com");
        return user;
    }
}