/** * Convert object to boolean, or <code>false</code> if conversion * failed * * @param o * object to convert * @return converted boolean or <code>false</code> if conversion * failed */ public static boolean getBoolean(Object o) { return getBoolean(o, false); }
/** * Converts object to an integer, defaults to <code>0</code> if object is * not convertible or is <code>null</code>. * * @param o * object to convert * @return converted int, or <code>0</code> if conversion failed */ public static int getInt(Object o) { return getInt(o, 0); }
/** * Converts object to a long, defaults to <code>0</code> if object is not * convertible or is <code>null</code> * * @param o * object to convert * @return converted long or <code>0</code> if conversion failed */ public static long getLong(Object o) { return getLong(o, 0); }
/** * Format a date using a given pattern * * @param date * date to format * @param pattern * pattern to use for formatting * @return formatted date, or empty string if date was <code>null</code> * @throws IllegalArgumentException * when <code>pattern</code> is invalid */ public static String formatDate(String date, String pattern) { return formatDate(getCalendar(date, null), pattern); }
convertedValue = value; } else if (toType.equals(float.class) || toType.equals(Float.class)) { convertedValue = Float.valueOf(getFloat(value)); } else if (toType.equals(double.class) || toType.equals(Double.class)) { convertedValue = Double.valueOf(getDouble(value)); } else if (toType.equals(String.class)) { convertedValue = getString(value); } else if (toType.equals(int.class) || toType.equals(Integer.class)) { convertedValue = Integer.valueOf(getInt(value)); } else if (toType.equals(char.class) || toType.equals(Character.class)) { convertedValue = Character.valueOf(getChar(value)); } else if (toType.equals(long.class) || toType.equals(Long.class)) { convertedValue = Long.valueOf(getLong(value)); } else if (toType.equals(boolean.class) || toType.equals(Boolean.class)) { convertedValue = Boolean.valueOf(getBoolean(value)); } else if (toType.equals(java.util.Date.class)) { convertedValue = getDate(value); } else if (toType.equals(Calendar.class)) { convertedValue = getCalendar(value); } else if (toType.equals(File.class)) { convertedValue = getFile(value); } else if (toType.equals(Class.class)) { try {
/** * @param reader stream to read objects from * @param context context for xstream to allow nested objects * @param res sample result on which the attributes should be set */ protected void retrieveAttributes(HierarchicalStreamReader reader, UnmarshallingContext context, SampleResult res) { res.setSampleLabel(ConversionHelp.decode(reader.getAttribute(ATT_LABEL))); res.setDataEncoding(ConversionHelp.decode(reader.getAttribute(ATT_DATA_ENCODING))); res.setDataType(ConversionHelp.decode(reader.getAttribute(ATT_DATA_TYPE))); String oldrc=reader.getAttribute(ATT_RESPONSE_CODE_OLD); if (oldrc!=null) { res.setResponseCode(ConversionHelp.decode(oldrc)); } else { res.setResponseCode(ConversionHelp.decode(reader.getAttribute(ATT_RESPONSE_CODE))); } res.setResponseMessage(ConversionHelp.decode(reader.getAttribute(ATT_RESPONSE_MESSAGE))); res.setSuccessful(Converter.getBoolean(reader.getAttribute(ATT_SUCCESS), true)); res.setThreadName(ConversionHelp.decode(reader.getAttribute(ATT_THREADNAME))); res.setStampAndTime(Converter.getLong(reader.getAttribute(ATT_TIME_STAMP)), Converter.getLong(reader.getAttribute(ATT_TIME))); res.setIdleTime(Converter.getLong(reader.getAttribute(ATT_IDLETIME))); res.setLatency(Converter.getLong(reader.getAttribute(ATT_LATENCY))); res.setConnectTime(Converter.getLong(reader.getAttribute(ATT_CONNECT_TIME))); res.setBytes(Converter.getLong(reader.getAttribute(ATT_BYTES))); res.setSentBytes(Converter.getLong(reader.getAttribute(ATT_SENT_BYTES))); res.setSampleCount(Converter.getInt(reader.getAttribute(ATT_SAMPLE_COUNT),1)); // default is 1 res.setErrorCount(Converter.getInt(reader.getAttribute(ATT_ERROR_COUNT),0)); // default is 0 res.setGroupThreads(Converter.getInt(reader.getAttribute(ATT_GRP_THRDS))); res.setAllThreads(Converter.getInt(reader.getAttribute(ATT_ALL_THRDS))); }
/** * Convert object to char, or ' ' if no conversion can * be applied * * @param o * object to convert * @return converted char or ' ' if conversion failed */ public static char getChar(Object o) { return getChar(o, ' '); }
/** * Convert object to double, or <code>0</code> if conversion * failed * * @param o * object to convert * @return converted double or <code>0</code> if conversion * failed */ public static double getDouble(Object o) { return getDouble(o, 0); }
/** * Format a date using a given pattern * * @param date * date to format * @param pattern * pattern to use for formatting * @return formatted date, or empty string if date was <code>null</code> * @throws IllegalArgumentException * when <code>pattern</code> is invalid */ public static String formatDate(Calendar date, String pattern) { return formatCalendar(date, pattern); }
/** * Converts the given object to a calendar object. Defaults to a calendar * using the current time if the given object can't be converted. * * @param o * object that should be converted to a {@link Calendar} * @return {@link Calendar} representing the given <code>o</code> or a new * {@link GregorianCalendar} using the current time if conversion * failed */ public static Calendar getCalendar(Object o) { return getCalendar(o, new GregorianCalendar()); }
/** * Converts the given object to a {@link Date} object. Defaults to the * current time if the given object can't be converted. * * @param date * object that should be converted to a {@link Date} * @return {@link Date} representing the given <code>date</code> or * the current time if conversion failed */ public static Date getDate(Object date) { return getDate(date, Calendar.getInstance().getTime()); }
private static Object unwrapProperty(PropertyDescriptor desc, JMeterProperty jprop, Class<?> type) { Object value; if(jprop instanceof TestElementProperty) { TestElement te = ((TestElementProperty)jprop).getElement(); if(te instanceof TestBean) { prepare(te); } value = te; } else if(jprop instanceof MultiProperty) { value = unwrapCollection((MultiProperty)jprop,(String)desc.getValue(TableEditor.CLASSNAME)); } // value was not provided, and this is allowed else if (jprop instanceof NullProperty && // use negative condition so missing (null) value is treated as FALSE ! Boolean.TRUE.equals(desc.getValue(GenericTestBeanCustomizer.NOT_UNDEFINED))) { value=null; } else { value = Converter.convert(jprop.getStringValue(), type); } return value; }