/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.p4java.extension.utility;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
*
*/
public class FormatUtility {
public static int UNSET_INT_VALUE = -1;
public static String UNSET_STRING_VALUE = "unset";
/**
* @param longDate
* date in ms since epoch, or null.
* @return the Date, or null.
*/
public static Date getDateFromLong(Object longDate) {
Date date;
try {
date = longDate == null ? null : new Date(
Long.parseLong((String) longDate));
} catch (Exception e) {
date = null;
}
return date;
}
public static Date getDateFromLongInSeconds(Object longDate) {
if (longDate == null)
return null;
Date date;
try {
long time = Long.parseLong((String) longDate);
time = time * 1000;
date = new Date(time);
} catch (Exception e) {
date = null;
}
return date;
}
/**
* @param stringDate
* date String of format YYYY/MM/DD HH:MM:SS, or null.
* @return the Date, or null.
*/
public static Date getDateFromString(Object stringDate) {
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = null;
try {
if (stringDate != null)
date = formatter.parse((String) stringDate);
} catch (Exception e) {
date = null;
}
return date;
}
/**
* Turns a String or Long formated date representation into a bona fide
* Date.
*
* @param source
* the Object to transform into a date, either in String or Long
* (epoch) format
* @return a Date, or null
*/
public static Date getDate(Object source) {
Date retval = getDateFromString(source);
if (retval == null)
retval = getDateFromLong(source);
return retval;
}
/**
* Transforms an object that could be an integer value or the string 'unset'
* into an integer value.
*
* @param source
* an Object that might be 'unset'. null is ok.
* @return the integer value of Object, or the value of UNSET_INT_VALUE if
* source is 'unset', null, or unable to be parsed.
*/
public static int transformUnsetToIntAsNeeded(Object source) {
if (source == null
|| ((String) source).equalsIgnoreCase(UNSET_STRING_VALUE))
return UNSET_INT_VALUE;
int retval;
try {
retval = Integer.parseInt((String) source);
} catch (NumberFormatException e) {
retval = UNSET_INT_VALUE;
}
return retval;
}
public static String intToUnsetAsNeeded(int source) {
if (source == UNSET_INT_VALUE)
return UNSET_STRING_VALUE;
return String.valueOf(source);
}
/**
* Finds each index suffixed key ("key0", "key1", etc) in a
* Map<String,Object> and adds it to a List<String>
*
* @param map
* a Map<String,Object> containing the index suffixed keys.
* non-null.
* @param keyRoot
* the root string common to each key. non-null.
* @param max
* the maximum number of keys expected, to prevent infinite loops
* @return a List<String> containing the String cast of the values of each
* index suffixed key, in index order. May be empty, never null.
*/
public static List<String> stringListFromIndexedKeySet(
Map<String, Object> map, String keyRoot, int max) {
Validate.notNull(map,
"map must not be null to FormatUtility.stringListFromIndexedKeySet");
Validate.notNull(keyRoot,
"keyRoot must not be null to FormatUtility.stringListFromIndexedKeySet");
List<String> retval = new ArrayList<String>();
int index = 0;
while (index < max) {
String element = (String) map.get(keyRoot + index);
if (element == null)
break;
retval.add(element);
index += 1;
}
return retval;
}
}