/**
*
* ObjectUtil.java
*
*/
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
/**
* A toolkit for Flight project
*
*
*/
public class ObjectUtil
{
/**
*
*/
public final static String P_HMM_AA = "h:mm a";
/**
*
*/
public final static String P_EEE = "EEE";
/**
*
*/
public final static String P_D_MMM = "d-MMM";
/**
*
*/
public static final String DAYS_OF_A_WEEK = "7";
/**
* Default constructor is made private to prevent class from being
* instantiated because it only has static methods.
*/
private ObjectUtil()
{
super();
}
/**
* If the parameter is empty.
*
* @param stringToTest
* @return if the parameter is empty.
*/
public static boolean isEmptyString(String stringToTest)
{
return (null == stringToTest || "".equals(stringToTest.trim()));
}
/**
* If a Boolean object is true.
*
* @param bValue
* @return if the parameter is true.
*/
public static boolean isTrue(Boolean bValue)
{
return (null != bValue && bValue);
}
/**
* compares if two strings are equal (case-sensitive), handling null
* properly
*
* @param value1
* String 1 to compare
* @param value2
* String 2 to compare
* @return if the two strings are equal
*/
public static boolean isStringEqual(String value1, String value2)
{
if (isEmptyString(value1) && isEmptyString(value2))
{
return true;
}
return isObjectEqual(value1, value2);
}
/**
* compares if two objects are equal , handling null properly
*
* @param value1
* Object 1 to compare
* @param value2
* Object 2 to compare
* @return if the two objects are equal
*/
public static boolean isObjectEqual(Object value1, Object value2)
{
if (null == value1)
{
return null == value2;
}
return value1.equals(value2);
}
/**
* compares if two strings are equal (not case-sensitive), handling null
* properly
*
* @param value1
* String 1 to compare
* @param value2
* String 2 to compare
* @return if the two strings are equal
*/
public static boolean isStringEqualIgnoreCase(String value1, String value2)
{
if (null == value1)
{
// If both are null, they are equal
return null == value2;
}
return value1.equalsIgnoreCase(value2);
}
/**
* Convert a String to a int Value;
*
* @param inputString
* @return the converted int value from a input String;
*/
public static int strToInt(String inputString)
{
// default value will be 0;
return strToInt(inputString, 0);
}
/**
* Convert a String to a int Value, If any exception found, the specified
* default value will be returned;
*
* @param inputString
* @param defaultValue
* @return the converted int value from a input String,defaultValue will be
* returned when Exception found during parsing;
*/
public static int strToInt(String inputString, int defaultValue)
{
try
{
return Integer.parseInt(inputString);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* Convert a date into formated date of Week Month-Day-Year
*
* @param date
* @return Formated date
* @throws Exception
*/
public static String convertDateToWeekMonthDayYear(String date) throws Exception
{
if (isEmptyString(date))
{
return "";
}
String formatedDate;
final String weeks[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Calendar cal = null;
try
{
cal = getCalInstance(date);
}
catch (Exception e)
{
return "";
}
String week = weeks[cal.get(Calendar.DAY_OF_WEEK) - 1];
formatedDate = week + " " + convertDateToMonthDayYear(date);
return formatedDate;
}
/**
* Convert a date into formated date of Month-Day-Year
*
* @param date
* @return Formated date
* @throws Exception
*/
public static String convertDateToMonthDayYear(String date) throws Exception
{
if (isEmptyString(date))
{
return "";
}
String formatedDate;
final String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" };
Calendar cal = null;
try
{
cal = getCalInstance(date);
}
catch (Exception e)
{
return "";
}
String month = months[cal.get(Calendar.MONTH)];
int day = cal.get(Calendar.DATE);
int year = cal.get(Calendar.YEAR);
formatedDate = month + "-" + day + "-" + year;
return formatedDate;
}
private static Calendar getCalInstance(String date) throws Exception
{
// The data source just surpport two formats (yyyy-mm-dd and
// mm/dd/yyyy)
DateFormat df = null;
if ((date.split("-")).length == 3)
{
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
}
if ((date.split("/")).length == 3)
{
df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
}
Calendar cal = Calendar.getInstance(Locale.US);
cal.setTime(df.parse(date));
return cal;
}
/**
* Simply date formmat conversion to java recognized format
*
* @param date
* @return ret
*/
public static String convertToJavaDateFormat(String date)
{
String ret = date.toLowerCase(Locale.US).replace("mm", "MM");
if (ret.indexOf("yy") == ret.lastIndexOf("yy"))
{
ret = ret.replace("yy", "yyyy");
}
return ret;
}
/**
* re-encode rawString, convert the quotation character
*
* @param rawString
* @return Auotation string
*/
public static String convertQuotation(String rawString)
{
if (isEmptyString(rawString))
{
return "";
}
return rawString.replaceAll("'", "'");
}
/**
* day in week, example Tue
*
* @param date
* @return day
*/
public static String formatDay(Date date)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
return dateFormat.format(date);
}
/**
* Format dd-MMM, eg: 19-Oct
*
* @param date
* @return date
*/
public static String formatDate(Date date)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM", Locale.US);
return dateFormat.format(date);
}
/**
* round the data of amountType
*
* @param decimal
* @param decimalPlaceCount
* @param remainPlaceCount
* @return amount
*/
public static Double roundAmoutType(int decimal, long decimalPlaceCount, int remainPlaceCount)
{
// construct the decimal with the value of decimalPlaceCount
String amount = String.valueOf(decimal);
int amountLength = amount.length();
int placeCount = (int) decimalPlaceCount;
if (amountLength > placeCount)
{
amount = amount.substring(0, amountLength - placeCount) + "."
+ amount.substring(amountLength - placeCount, amountLength);
}
BigDecimal vaule = new BigDecimal(amount);
vaule = vaule.setScale(remainPlaceCount, BigDecimal.ROUND_HALF_UP);
return vaule.doubleValue();
}
/**
* Parse the data of amountType
*
* @param decimal
* @param decimalPlaceCount
* @param remainPlaceCount
* @return value of the amount
*/
public static Double parseAmountType(int decimal, long decimalPlaceCount, int remainPlaceCount)
{
// construct the decimal with the value of decimalPlaceCount
String amount = String.valueOf(decimal);
int amountLength = amount.length();
int placeCount = (int) decimalPlaceCount;
if (amountLength > placeCount)
{
amount = amount.substring(0, amountLength - placeCount) + "."
+ amount.substring(amountLength - placeCount, amountLength);
}
// construct the format such as "#.00"
String decimalFormat = "#";
if (remainPlaceCount > 0)
{
decimalFormat = decimalFormat + ".";
}
for (; remainPlaceCount > 0; remainPlaceCount--)
{
decimalFormat = decimalFormat + "0";
}
// format the decimal with the formater
DecimalFormat df = new DecimalFormat(decimalFormat);
amount = df.format(Double.valueOf(amount));
return Double.valueOf(amount);
}
/**
* Parse the data of amountType
*
* @param decimal
* @param decimalPlaceCount
* @param remainPlaceCount
* @return value of the amount
*/
public static Double parseAmountTypeNoRound(int decimal, long decimalPlaceCount,
int remainPlaceCount)
{
// construct the decimal with the value of decimalPlaceCount
String amount = String.valueOf(decimal);
int amountLength = amount.length();
int placeCount = (int) decimalPlaceCount;
if (amountLength > placeCount)
{
String tailStr = amount.substring(amountLength - placeCount, amountLength);
if (tailStr.length() > remainPlaceCount)
{
tailStr = tailStr.substring(0, remainPlaceCount);
}
amount = amount.substring(0, amountLength - placeCount) + "." + tailStr;
}
// construct the format such as "#.00"
String decimalFormat = "#";
if (remainPlaceCount > 0)
{
decimalFormat = decimalFormat + ".";
}
for (; remainPlaceCount > 0; remainPlaceCount--)
{
decimalFormat = decimalFormat + "0";
}
// format the decimal with the formater
DecimalFormat df = new DecimalFormat(decimalFormat);
amount = df.format(Double.valueOf(amount));
return Double.valueOf(amount);
}
/**
* Change single number like "9" into "09"
*
* @param sourceNumber
* @return NonSingleLetter
*/
public static String convertNonSingleLetter(String sourceNumber)
{
String result = "";
if (!ObjectUtil.isEmptyString(sourceNumber))
{
Integer inputNumber = Integer.valueOf(sourceNumber);
if (inputNumber >= 0 && inputNumber <= 9)
{
result = "0" + String.valueOf(inputNumber);
}
else if (inputNumber > 9)
{
result = String.valueOf(inputNumber);
}
}
return result;
}
/**
* Change the date format "yyyy-MM-dd" into "MM/dd/yyyy"
*
* @param initialDate
* @return "MM/dd/yyyy" format date string
*/
public static String changeDateStringFormat1(String initialDate)
{
String resultDate = "";
String departureMonth = "";
String departureYear = "";
String departureDay = "";
if (!ObjectUtil.isEmptyString(initialDate))
{
String[] arr = initialDate.split("-");
if (3 == arr.length)
{
departureYear = arr[0];
departureMonth = arr[1];
departureDay = arr[2];
}
resultDate = convertNonSingleLetter(departureMonth) + "/"
+ convertNonSingleLetter(departureDay) + "/"
+ convertNonSingleLetter(departureYear);
}
return resultDate;
}
/**
* Change the date format "MM/dd/yyyy" into "yyyy-MM-dd"
*
* @param initialDate
* @return "yyyy-MM-dd" format date String
*/
public static String changeDateStringFormat2(String initialDate)
{
String resultDate = "";
String departureMonth = "";
String departureYear = "";
String departureDay = "";
if (!ObjectUtil.isEmptyString(initialDate))
{
String[] arr = initialDate.split("/");
if (3 == arr.length)
{
departureMonth = arr[0];
departureDay = arr[1];
departureYear = arr[2];
}
resultDate = convertNonSingleLetter(departureYear) + "-"
+ convertNonSingleLetter(departureMonth) + "-"
+ convertNonSingleLetter(departureDay);
}
return resultDate;
}
/**
* DateTime formatting
*
* @param pattern
* @param dateTime
* @return formatDate
*/
public static String formatDateTime(String pattern, DateTime dateTime)
{
String formatDate = "";
if (null == dateTime)
{
return formatDate;
}
DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.US);
formatDate = dateFormat.format(dateTime.toCalendar().getTime());
return formatDate;
}
/**
* convert number String
*
* @param rawString
* @return Number
*/
public static Integer convertStringNumber(String rawString)
{
Integer ret = null;
try
{
ret = Integer.valueOf(rawString);
}
catch (NumberFormatException e)
{
return -1;
}
return ret;
}
/**
* extract airport code from airport name example: give airportName:
* Seattle, WA (SEA-Seattle Tacoma) you will get: SEA
*
* @param airportName
* @return AirportCode
*/
public static String getAirportCode(String airportName)
{
if (isEmptyString(airportName))
{
return airportName;
}
String code = "";
int beginIndex = airportName.indexOf('(');
if (beginIndex != -1)
{
beginIndex++;
code = airportName.substring(beginIndex, beginIndex + 3);
return code.toUpperCase(Locale.US);
}
return airportName;
}
/**
* construct flex departure month
*
* @param strDate
* @return FlexDepartureMonth
*/
public static String parseFlexDepartureMonth(String strDate)
{
Calendar cal = Calendar.getInstance();
try
{
Date departureDate = new SimpleDateFormat("MM/dd/yy", Locale.US).parse(strDate);
cal.setTime(departureDate);
}
catch (ParseException e)
{
cal.setTime(new Date());
}
return (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR);
}
/**
* Parse string to java.util.Date object , allowing lenient mode.
*
* @param sdf
* @param dateString
* @return date
* @throws ParseException
*/
public static Date lenientParse(final SimpleDateFormat sdf, final String dateString)
throws ParseException
{
sdf.setLenient(true);
return sdf.parse(dateString);
}
/**
* Parse string to java.util.Date object , not allowing lenient mode.
*
* @param sdf
* @param dateString
* @return date
* @throws ParseException
*/
public static Date strictParse(final SimpleDateFormat sdf, final String dateString)
throws ParseException
{
sdf.setLenient(false);
return sdf.parse(dateString);
}
/**
* Generate trip length.
*
* @param tripLengthString
* @return string trip length
*
*/
public static String genTripLength(String tripLengthString)
{
if (isEmptyString(tripLengthString))
{
return null;
}
List<String> arr = new ArrayList<String>(3);
StringTokenizer st = new StringTokenizer(",");
while (st.hasMoreElements())
{
String elem = (String) st.nextElement();
arr.add(elem);
}
if (arr.size() != 2)
{
return null;
}
String flag = arr.get(0);
String begin = arr.get(1);
if (flag != null && flag.equals("1"))
{
return DAYS_OF_A_WEEK;
}
else
{
return begin;
}
}
}