Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package niche.newres.timedevents2owl.randomizer; import java.text.DecimalFormat; import java.util.Random; import org.joda.time.DateTime; /** * * @author newres */ public abstract class TimedEvents2OWLRandomizer { static Random random = new Random(); public DateTime currentDateTime; //JenaOWLfiable bean; public TimedEvents2OWLRandomizer() { random = new Random(); currentDateTime = new DateTime(); } public static Random getRandom() { return random; } public static void setRandom(Random random) { TimedEvents2OWLRandomizer.random = random; } public DateTime getCurrentDateTime() { return currentDateTime; } public void setCurrentDateTime(DateTime currentDateTime) { this.currentDateTime = currentDateTime; } public static DateTime minusRandomDays(DateTime dateTime, int minRange, int maxRange) { int randomDays = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.minusDays(randomDays); } public static DateTime minusRandomHours(DateTime dateTime, int minRange, int maxRange) { int randomHours = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.minusHours(randomHours); } public static DateTime minusRandomMinutes(DateTime dateTime, int minRange, int maxRange) { int randomMinutes = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.minusMinutes(randomMinutes); } public static DateTime minusRandomSeconds(DateTime dateTime, int minRange, int maxRange) { int randomSeconds = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.minusSeconds(randomSeconds); } public static DateTime minusRandomMilliSeconds(DateTime dateTime, int minRange, int maxRange) { int randomMilliSeconds = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.minusMillis(randomMilliSeconds); } public static DateTime plusRandomDays(DateTime dateTime, int minRange, int maxRange) { int randomDays = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusDays(randomDays); } public static DateTime plusRandomHours(DateTime dateTime, int minRange, int maxRange) { int randomHours = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusHours(randomHours); } public static DateTime plusRandomMinutes(DateTime dateTime, int minRange, int maxRange) { int randomMinutes = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusMinutes(randomMinutes); } public static DateTime plusRandomSeconds(DateTime dateTime, int minRange, int maxRange) { int randomSeconds = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusSeconds(randomSeconds); } public static DateTime plusRandomMilliSeconds(DateTime dateTime, int minRange, int maxRange) { int randomMilliSeconds = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusMillis(randomMilliSeconds); } public static DateTime shiftToPreviousDayTimeDateTime(DateTime dateTime) { //int currentHours = dateTime.getHourOfDay(); if (dateTime.getHourOfDay() > 8 && dateTime.getHourOfDay() < 22) { return dateTime; } else { return dateTime.minusHours(11); } } public static DateTime shiftToPreviousNightTimeDateTime(DateTime dateTime) { if (dateTime.getHourOfDay() < 8 || dateTime.getHourOfDay() > 22) { return dateTime; } else { return dateTime.minusHours(dateTime.getHourOfDay() - 5); } } public abstract void setMeasurementsDefault(); //public abstract JenaOWLfiable createRandom(); public static boolean randBoolean() { return Math.random() < 0.5; } public static int randInt(int min, int max) { int randomNum = random.nextInt((max - min) + 1) + min; return randomNum; } public static double randDouble(double min, double max) { double randomNum = min + (max - min) * random.nextDouble(); return randomNum; } public static double RoundTo2Decimals(double val) { DecimalFormat df2 = new DecimalFormat("###.##"); return Double.valueOf(df2.format(val)); } public static double randDouble2Decimals(double min, double max) { return TimedEvents2OWLRandomizer.RoundTo2Decimals(TimedEvents2OWLRandomizer.randDouble(min, max)); } public static String randString(String characters, int length) { char[] text = new char[length]; for (int i = 0; i < length; i++) { text[i] = characters.charAt(random.nextInt(characters.length())); } return new String(text); } public static DateTime randDateTime(long minTime, long maxTime) { long diff = maxTime - minTime + 1; long rand = (minTime + (long) (Math.random() * diff)); return new DateTime(rand); } }