mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 13:17:41 +00:00
GUAC-830: Move parsing/formatting into field types.
This commit is contained in:
@@ -22,6 +22,11 @@
|
||||
|
||||
package org.glyptodon.guacamole.form;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Represents a date field. The field may contain only date values which
|
||||
* conform to a standard pattern, defined by DateField.FORMAT.
|
||||
@@ -45,4 +50,48 @@ public class DateField extends Field {
|
||||
super(name, Field.Type.DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given date into a string which follows the format used by
|
||||
* date fields.
|
||||
*
|
||||
* @param date
|
||||
* The date value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted date, or null if the provided time was null.
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return date == null ? null : dateFormat.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding date. The string must
|
||||
* follow the standard format used by date fields, as defined by FORMAT
|
||||
* and as would be produced by format().
|
||||
*
|
||||
* @param dateString
|
||||
* The date string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The date corresponding to the given date string, or null if the
|
||||
* provided date string was null or blank.
|
||||
*
|
||||
* @throws ParseException
|
||||
* If the given date string does not conform to the standard format
|
||||
* used by date fields.
|
||||
*/
|
||||
public static Date parse(String dateString)
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no date provided
|
||||
if (dateString == null || dateString.isEmpty())
|
||||
return null;
|
||||
|
||||
// Parse date according to format
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return dateFormat.parse(dateString);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,4 +39,46 @@ public class NumericField extends Field {
|
||||
super(name, Field.Type.NUMERIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given integer in the format required by a numeric field.
|
||||
*
|
||||
* @param i
|
||||
* The integer to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* A string representation of the given integer, or null if the given
|
||||
* integer was null.
|
||||
*/
|
||||
public static String format(Integer i) {
|
||||
|
||||
if (i == null)
|
||||
return null;
|
||||
|
||||
return i.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string as an integer, where the given string is in the
|
||||
* format required by a numeric field.
|
||||
*
|
||||
* @param str
|
||||
* The string to parse as an integer, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The integer representation of the given string, or null if the given
|
||||
* string was null.
|
||||
*
|
||||
* @throws NumberFormatException
|
||||
* If the given string is not in a parseable format.
|
||||
*/
|
||||
public static Integer parse(String str) throws NumberFormatException {
|
||||
|
||||
if (str == null || str.isEmpty())
|
||||
return null;
|
||||
|
||||
return new Integer(str);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -22,6 +22,11 @@
|
||||
|
||||
package org.glyptodon.guacamole.form;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Represents a time field. The field may contain only time values which
|
||||
* conform to a standard pattern, defined by TimeField.FORMAT.
|
||||
@@ -45,4 +50,48 @@ public class TimeField extends Field {
|
||||
super(name, Field.Type.TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding time. The string must
|
||||
* follow the standard format used by time fields, as defined by
|
||||
* FORMAT and as would be produced by format().
|
||||
*
|
||||
* @param timeString
|
||||
* The time string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The time corresponding to the given time string, or null if the
|
||||
* provided time string was null or blank.
|
||||
*
|
||||
* @throws ParseException
|
||||
* If the given time string does not conform to the standard format
|
||||
* used by time fields.
|
||||
*/
|
||||
public static Date parse(String timeString)
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no time provided
|
||||
if (timeString == null || timeString.isEmpty())
|
||||
return null;
|
||||
|
||||
// Parse time according to format
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return timeFormat.parse(timeString);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given time into a string which follows the format used by
|
||||
* time fields.
|
||||
*
|
||||
* @param time
|
||||
* The time value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted time, or null if the provided time was null.
|
||||
*/
|
||||
public static String format(Date time) {
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return time == null ? null : timeFormat.format(time);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -40,4 +40,27 @@ public class TimeZoneField extends Field {
|
||||
super(name, Field.Type.TIMEZONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a time zone ID string. As these strings are
|
||||
* equivalent, the only transformation currently performed by this function
|
||||
* is to ensure that a blank time zone string is parsed into null.
|
||||
*
|
||||
* @param timeZone
|
||||
* The time zone string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The ID of the time zone corresponding to the given string, or null
|
||||
* if the given time zone string was null or blank.
|
||||
*/
|
||||
public static String parse(String timeZone) {
|
||||
|
||||
// Return null if no time zone provided
|
||||
if (timeZone == null || timeZone.isEmpty())
|
||||
return null;
|
||||
|
||||
// Otherwise, assume time zone is valid
|
||||
return timeZone;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user