1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-18 20:41:37 +02:00

Correct code format for file DescParseTickFormat

This commit is contained in:
snowleo
2011-08-08 17:46:12 +02:00
parent b07ba21659
commit f75390bd3f

View File

@@ -6,6 +6,7 @@ import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* This utility class is used for converting between the ingame * This utility class is used for converting between the ingame
* time in ticks to ingame time as a friendly string. * time in ticks to ingame time as a friendly string.
@@ -15,25 +16,23 @@ import java.util.logging.Logger;
* *
* @author Olof Larsson * @author Olof Larsson
*/ */
public class DescParseTickFormat { public class DescParseTickFormat
{
// ============================================ // ============================================
// First some information vars. TODO: Should this be in a config file? // First some information vars. TODO: Should this be in a config file?
// -------------------------------------------- // --------------------------------------------
public static final Map<String, Integer> nameToTicks = new LinkedHashMap<String, Integer>(); public static final Map<String, Integer> nameToTicks = new LinkedHashMap<String, Integer>();
public static final Set<String> resetAliases = new HashSet<String>(); public static final Set<String> resetAliases = new HashSet<String>();
public static final int ticksAtMidnight = 18000; public static final int ticksAtMidnight = 18000;
public static final int ticksPerDay = 24000; public static final int ticksPerDay = 24000;
public static final int ticksPerHour = 1000; public static final int ticksPerHour = 1000;
public static final double ticksPerMinute = 1000d / 60d; public static final double ticksPerMinute = 1000d / 60d;
public static final double ticksPerSecond = 1000d / 60d / 60d; public static final double ticksPerSecond = 1000d / 60d / 60d;
private static final SimpleDateFormat SDFTwentyFour = new SimpleDateFormat("HH:mm", Locale.ENGLISH); private static final SimpleDateFormat SDFTwentyFour = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
private static final SimpleDateFormat SDFTwelve = new SimpleDateFormat("h:mmaa", Locale.ENGLISH); private static final SimpleDateFormat SDFTwelve = new SimpleDateFormat("h:mmaa", Locale.ENGLISH);
static { static
{
nameToTicks.put("sunrise", 22000); nameToTicks.put("sunrise", 22000);
nameToTicks.put("rise", 22000); nameToTicks.put("rise", 22000);
@@ -68,7 +67,6 @@ public class DescParseTickFormat {
// ============================================ // ============================================
// PARSE. From describing String to int // PARSE. From describing String to int
// -------------------------------------------- // --------------------------------------------
public static long parse(String desc) throws NumberFormatException public static long parse(String desc) throws NumberFormatException
{ {
Long ret; Long ret;
@@ -77,16 +75,40 @@ public class DescParseTickFormat {
desc = desc.toLowerCase().replaceAll("[^A-Za-z0-9]", ""); desc = desc.toLowerCase().replaceAll("[^A-Za-z0-9]", "");
// Detect ticks format // Detect ticks format
try { return parseTicks(desc); } catch (Exception e) {} try
{
return parseTicks(desc);
}
catch (Exception e)
{
}
// Detect 24-hour format // Detect 24-hour format
try { return parse24(desc); } catch (Exception e) {} try
{
return parse24(desc);
}
catch (Exception e)
{
}
// Detect 12-hour format // Detect 12-hour format
try { return parse12(desc); } catch (Exception e) {} try
{
return parse12(desc);
}
catch (Exception e)
{
}
// Detect aliases // Detect aliases
try { return parseAlias(desc); } catch (Exception e) {} try
{
return parseAlias(desc);
}
catch (Exception e)
{
}
// Well we failed to understand... // Well we failed to understand...
throw new NumberFormatException(); throw new NumberFormatException();
@@ -94,7 +116,7 @@ public class DescParseTickFormat {
public static long parseTicks(String desc) throws NumberFormatException public static long parseTicks(String desc) throws NumberFormatException
{ {
if ( ! desc.matches("^[0-9]+ti?c?k?s?$")) if (!desc.matches("^[0-9]+ti?c?k?s?$"))
{ {
throw new NumberFormatException(); throw new NumberFormatException();
} }
@@ -106,7 +128,7 @@ public class DescParseTickFormat {
public static long parse24(String desc) throws NumberFormatException public static long parse24(String desc) throws NumberFormatException
{ {
if ( ! desc.matches("^[0-9]{2}[^0-9]?[0-9]{2}$")) if (!desc.matches("^[0-9]{2}[^0-9]?[0-9]{2}$"))
{ {
throw new NumberFormatException(); throw new NumberFormatException();
} }
@@ -126,7 +148,7 @@ public class DescParseTickFormat {
public static long parse12(String desc) throws NumberFormatException public static long parse12(String desc) throws NumberFormatException
{ {
if ( ! desc.matches("^[0-9]{1,2}([^0-9]?[0-9]{2})?(pm|am)$")) if (!desc.matches("^[0-9]{1,2}([^0-9]?[0-9]{2})?(pm|am)$"))
{ {
throw new NumberFormatException(); throw new NumberFormatException();
} }
@@ -185,7 +207,7 @@ public class DescParseTickFormat {
public static long parseAlias(String desc) throws NumberFormatException public static long parseAlias(String desc) throws NumberFormatException
{ {
Integer ret = nameToTicks.get(desc); Integer ret = nameToTicks.get(desc);
if ( ret == null) if (ret == null)
{ {
throw new NumberFormatException(); throw new NumberFormatException();
} }
@@ -201,7 +223,6 @@ public class DescParseTickFormat {
// ============================================ // ============================================
// FORMAT. From int to describing String // FORMAT. From int to describing String
// -------------------------------------------- // --------------------------------------------
public static String format(long ticks) public static String format(long ticks)
{ {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
@@ -220,7 +241,7 @@ public class DescParseTickFormat {
public static String formatTicks(long ticks) public static String formatTicks(long ticks)
{ {
return ""+ticks%ticksPerDay+"ticks"; return "" + ticks % ticksPerDay + "ticks";
} }
public static String format24(long ticks) public static String format24(long ticks)
@@ -254,11 +275,11 @@ public class DescParseTickFormat {
ticks = ticks - hours * ticksPerHour; ticks = ticks - hours * ticksPerHour;
// How many minutes on the last day? // How many minutes on the last day?
long minutes = (long) Math.floor(ticks / ticksPerMinute); long minutes = (long)Math.floor(ticks / ticksPerMinute);
double dticks = ticks - minutes*ticksPerMinute; double dticks = ticks - minutes * ticksPerMinute;
// How many seconds on the last day? // How many seconds on the last day?
long seconds = (long) Math.floor(dticks / ticksPerSecond); long seconds = (long)Math.floor(dticks / ticksPerSecond);
// Now we create an english GMT calendar (We wan't no daylight savings) // Now we create an english GMT calendar (We wan't no daylight savings)
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
@@ -269,7 +290,7 @@ public class DescParseTickFormat {
cal.add(Calendar.DAY_OF_YEAR, (int)days); cal.add(Calendar.DAY_OF_YEAR, (int)days);
cal.add(Calendar.HOUR_OF_DAY, (int)hours); cal.add(Calendar.HOUR_OF_DAY, (int)hours);
cal.add(Calendar.MINUTE, (int)minutes); cal.add(Calendar.MINUTE, (int)minutes);
cal.add(Calendar.SECOND, (int)seconds+1); // To solve rounding errors. cal.add(Calendar.SECOND, (int)seconds + 1); // To solve rounding errors.
return cal.getTime(); return cal.getTime();
} }