Example usage for java.lang Short valueOf

List of usage examples for java.lang Short valueOf

Introduction

In this page you can find the example usage for java.lang Short valueOf.

Prototype

@HotSpotIntrinsicCandidate
public static Short valueOf(short s) 

Source Link

Document

Returns a Short instance representing the specified short value.

Usage

From source file:com.wabacus.system.datatype.ShortType.java

public Object getColumnValue(ResultSet rs, String column, AbsDatabaseType dbtype) throws SQLException {
    return Short.valueOf(rs.getShort(column));
}

From source file:Main.java

/**
 * convert value to given type.//from  w w w. ja  v  a 2  s  . com
 * null safe.
 *
 * @param value value for convert
 * @param type  will converted type
 * @return value while converted
 */
public static Object convertCompatibleType(Object value, Class<?> type) {

    if (value == null || type == null || type.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (value instanceof String) {
        String string = (String) value;
        if (char.class.equals(type) || Character.class.equals(type)) {
            if (string.length() != 1) {
                throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!"
                        + " when convert String to char, the String MUST only 1 char.", string));
            }
            return string.charAt(0);
        } else if (type.isEnum()) {
            return Enum.valueOf((Class<Enum>) type, string);
        } else if (type == BigInteger.class) {
            return new BigInteger(string);
        } else if (type == BigDecimal.class) {
            return new BigDecimal(string);
        } else if (type == Short.class || type == short.class) {
            return Short.valueOf(string);
        } else if (type == Integer.class || type == int.class) {
            return Integer.valueOf(string);
        } else if (type == Long.class || type == long.class) {
            return Long.valueOf(string);
        } else if (type == Double.class || type == double.class) {
            return Double.valueOf(string);
        } else if (type == Float.class || type == float.class) {
            return Float.valueOf(string);
        } else if (type == Byte.class || type == byte.class) {
            return Byte.valueOf(string);
        } else if (type == Boolean.class || type == boolean.class) {
            return Boolean.valueOf(string);
        } else if (type == Date.class) {
            try {
                return new SimpleDateFormat(DATE_FORMAT).parse((String) value);
            } catch (ParseException e) {
                throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT
                        + ", cause: " + e.getMessage(), e);
            }
        } else if (type == Class.class) {
            return forName((String) value);
        }
    } else if (value instanceof Number) {
        Number number = (Number) value;
        if (type == byte.class || type == Byte.class) {
            return number.byteValue();
        } else if (type == short.class || type == Short.class) {
            return number.shortValue();
        } else if (type == int.class || type == Integer.class) {
            return number.intValue();
        } else if (type == long.class || type == Long.class) {
            return number.longValue();
        } else if (type == float.class || type == Float.class) {
            return number.floatValue();
        } else if (type == double.class || type == Double.class) {
            return number.doubleValue();
        } else if (type == BigInteger.class) {
            return BigInteger.valueOf(number.longValue());
        } else if (type == BigDecimal.class) {
            return BigDecimal.valueOf(number.doubleValue());
        } else if (type == Date.class) {
            return new Date(number.longValue());
        }
    } else if (value instanceof Collection) {
        Collection collection = (Collection) value;
        if (type.isArray()) {
            int length = collection.size();
            Object array = Array.newInstance(type.getComponentType(), length);
            int i = 0;
            for (Object item : collection) {
                Array.set(array, i++, item);
            }
            return array;
        } else if (!type.isInterface()) {
            try {
                Collection result = (Collection) type.newInstance();
                result.addAll(collection);
                return result;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else if (type == List.class) {
            return new ArrayList<>(collection);
        } else if (type == Set.class) {
            return new HashSet<>(collection);
        }
    } else if (value.getClass().isArray() && Collection.class.isAssignableFrom(type)) {
        Collection collection;
        if (!type.isInterface()) {
            try {
                collection = (Collection) type.newInstance();
            } catch (Throwable e) {
                collection = new ArrayList<>();
            }
        } else if (type == Set.class) {
            collection = new HashSet<>();
        } else {
            collection = new ArrayList<>();
        }
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++) {
            collection.add(Array.get(value, i));
        }
        return collection;
    }
    return value;
}

From source file:com.linkedin.databus2.producers.gg.GGEventGenerationFactory.java

/**
 * Given a logical source config, create a partition function.
 *
 * @param sourceConfig// ww  w  .  j a va 2s.com
 * @return the partition function
 * @throws InvalidConfigException
 */
public static PartitionFunction buildPartitionFunction(LogicalSourceStaticConfig sourceConfig)
        throws InvalidConfigException {
    String partitionFunction = sourceConfig.getPartitionFunction();
    if (partitionFunction.startsWith("constant:")) {
        try {
            String numberPart = partitionFunction.substring("constant:".length()).trim();
            short constantPartitionNumber = Short.valueOf(numberPart);
            return new ConstantPartitionFunction(constantPartitionNumber);
        } catch (Exception ex) {
            // Could be a NumberFormatException, IndexOutOfBoundsException or other exception when trying
            // to parse the partition number.
            throw new InvalidConfigException("Invalid partition configuration (" + partitionFunction + "). "
                    + "Could not parse the constant partition number.");
        }
    } else {
        throw new InvalidConfigException("Invalid partition configuration (" + partitionFunction + ").");
    }
}

From source file:com.wabacus.system.datatype.ShortType.java

public Object getColumnValue(ResultSet rs, int iindex, AbsDatabaseType dbtype) throws SQLException {
    return Short.valueOf(rs.getShort(iindex));
}

From source file:Main.java

/**
 * parse a string to an Object of given data type. <br>
 * @param strValue the string value/*from   w w w.jav a  2 s.com*/
 * @param valueType the full java class name which could be <br>
 * java.util.Date   (default format is yyyy-MM-dd) <br>
 * java.sql.Date   (default format is yyyy-MM-dd) <br>
 * java.sql.Timestamp   (default format is yyyy-MM-dd HH:mm:ss) <br>
 * java.sql.Time   (default format is HH:mm:ss) <br>
 * java.lang.String   <br>
 * java.lang.Boolean   <br>
 * java.lang.Double   <br>
 * java.lang.Long   <br>
 * java.lang.Short   <br>
 * java.lang.Integer   <br>
 * java.lang.Byte   <br>
 * java.lang.Float   <br>
 * java.math.BigInteger   <br>
 * java.math.BigDecimal   <br>
 * 
 * @param format SimpleDateFormat allowed standard formats.
 * @return Object
 * @throws Exception
 */
public static Object parseStringToObject(String strValue, String valueType, String format) throws Exception {
    if ("java.util.Date".equals(valueType)) {
        // default format yyyy-MM-dd
        SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd");
        return (fmt.parse(strValue));
    } else if ("java.sql.Date".equals(valueType)) {
        // default format yyyy-MM-dd
        SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd");
        return (new java.sql.Date(fmt.parse(strValue).getTime()));
    } else if ("java.sql.Timestamp".equals(valueType)) {
        // default format yyyy-MM-dd HH:mm:ss
        SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd HH:mm:ss");
        return (new java.sql.Timestamp(fmt.parse(strValue).getTime()));
    } else if ("java.sql.Time".equals(valueType)) {
        // default format HH:mm:ss
        SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "HH:mm:ss");
        return (new java.sql.Time(fmt.parse(strValue).getTime()));
    } else if ("java.lang.Boolean".equals(valueType)) {
        return (Boolean.valueOf(strValue));
    } else if ("java.lang.Double".equals(valueType)) {
        return (Double.valueOf(strValue));
    } else if ("java.lang.Long".equals(valueType)) {
        return (Long.valueOf(strValue));
    } else if ("java.lang.Short".equals(valueType)) {
        return (Short.valueOf(strValue));
    } else if ("java.lang.Integer".equals(valueType)) {
        return (Integer.valueOf(strValue));
    } else if ("java.lang.Byte".equals(valueType)) {
        return (Byte.valueOf(strValue));
    } else if ("java.lang.Float".equals(valueType)) {
        return (Float.valueOf(strValue));
    } else if ("java.math.BigInteger".equals(valueType)) {
        return (new java.math.BigInteger(strValue));
    } else if ("java.math.BigDecimal".equals(valueType)) {
        return (new java.math.BigDecimal(strValue));
    } else {
        return (strValue);
    }
}

From source file:com.glaf.jbpm.util.CustomFieldInstantiator.java

public static Object getValue(Class<?> type, Element propertyElement) {
    Object value = null;/*www. j a va2 s . c  om*/
    if (type == String.class) {
        value = propertyElement.getText();
    } else if ((type == Integer.class) || (type == int.class)) {
        value = Integer.parseInt(propertyElement.getTextTrim());
    } else if ((type == Long.class) || (type == long.class)) {
        value = Long.parseLong(propertyElement.getTextTrim());
    } else if ((type == Float.class) || (type == float.class)) {
        value = new Float(propertyElement.getTextTrim());
    } else if ((type == Double.class) || (type == double.class)) {
        value = Double.parseDouble(propertyElement.getTextTrim());
    } else if ((type == Boolean.class) || (type == boolean.class)) {
        value = Boolean.valueOf(propertyElement.getTextTrim());
    } else if ((type == Character.class) || (type == char.class)) {
        value = Character.valueOf(propertyElement.getTextTrim().charAt(0));
    } else if ((type == Short.class) || (type == short.class)) {
        value = Short.valueOf(propertyElement.getTextTrim());
    } else if ((type == Byte.class) || (type == byte.class)) {
        value = Byte.valueOf(propertyElement.getTextTrim());
    } else if (type.isAssignableFrom(java.util.Date.class)) {
        value = DateUtils.toDate(propertyElement.getTextTrim());
    } else if (type.isAssignableFrom(List.class)) {
        value = getCollectionValue(propertyElement, new java.util.ArrayList<Object>());
    } else if (type.isAssignableFrom(Set.class)) {
        value = getCollectionValue(propertyElement, new LinkedHashSet<Object>());
    } else if (type.isAssignableFrom(Collection.class)) {
        value = getCollectionValue(propertyElement, new java.util.ArrayList<Object>());
    } else if (type.isAssignableFrom(Map.class)) {
        value = getMapValue(propertyElement, new LinkedHashMap<Object, Object>());
    } else if (type == Element.class) {
        value = propertyElement;
    } else {
        try {
            Constructor<?> constructor = type.getConstructor(new Class[] { String.class });
            if ((propertyElement.isTextOnly()) && (constructor != null)) {
                value = constructor.newInstance(new Object[] { propertyElement.getTextTrim() });
            }
        } catch (Exception ex) {
            logger.error("couldn't parse the bean property value '" + propertyElement.asXML() + "' to a '"
                    + type.getName() + "'");
            throw new RuntimeException(ex);
        }
    }

    return value;
}

From source file:org.mifos.application.servicefacade.FormEnteredDataAssembler.java

public CollectionSheetFormEnteredDataDto toDto() {
    final OfficeDetailsDto office = collectionSheetFormDtoDecorator
            .findSelectedBranchOfficeById(Short.valueOf(collectionSheetForm.getOfficeId()));
    final PersonnelDto loanOfficer = collectionSheetFormDtoDecorator
            .findSelectedLoanOfficerById(Short.valueOf(collectionSheetForm.getLoanOfficerId()));
    final CustomerDto selectedCustomer = collectionSheetFormDtoDecorator
            .findSelectedCustomerById(Integer.valueOf(collectionSheetForm.getCustomerId()));
    final ListItem<Short> selectedPaymentType = collectionSheetFormDtoDecorator
            .findSelectedPaymentTypeById(Short.valueOf(collectionSheetForm.getPaymentId()));
    final java.sql.Date meetingDate = collectionSheetFormDtoDecorator.getMeetingDateAsSqlDate();

    final java.sql.Date receiptDate = determineReceiptDateIfPopulated();

    return new CollectionSheetFormEnteredDataDto(office, loanOfficer, selectedCustomer, selectedPaymentType,
            meetingDate, receiptDate, collectionSheetForm.getReceiptId());
}

From source file:com.github.lsiu.hkrestaurants.importer.RestaurantDataImporter.java

private void processData(Element root) throws Exception {

    NodeList nl = root.getElementsByTagName("LP");

    final int PAGE_SIZE = 30;
    List<Restaurant> restaurantPage = new ArrayList<Restaurant>();

    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from   w  w w .  j a  va 2s .  com

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) n;
            String type = getTagValue("TYPE", e);
            String dist = getTagValue("DIST", e);
            String licno = getTagValue("LICNO", e);
            String name = getTagValue("SS", e);
            String adr = getTagValue("ADR", e);
            String info = getTagValue("INFO", e);

            log.debug("Restaurant Name={}, license No={}", name, licno);

            Restaurant rs = new Restaurant();
            rs.address = adr;
            rs.districtCode = Short.valueOf(dist);
            rs.infoCode = info;
            rs.licenseNo = licno;
            rs.name = name;
            rs.typeCode = type;

            restaurantPage.add(rs);

            if (restaurantPage.size() > PAGE_SIZE) {
                commitPage(restaurantPage);
                restaurantPage.clear();
            }
        }
    }
}

From source file:com.albert.util.StringUtilCommon.java

/**
 * convert string to Short.//  w  w  w .ja v  a  2 s  . c o m
 * 
 * @param str
 * @return Short
 */
public static Short toShort(String str) {
    Short outShort = null;
    if (!isEmpty(str)) {
        outShort = Short.valueOf(str);
    }
    return outShort;
}

From source file:com.igorbaiborodine.example.mybatis.customer.CustomerServiceImpl.java

@Override
public short addCustomer(Customer customer_, Address address_) throws ServiceException {

    short newCustomerId = -1;
    try {//from   w ww  .j  a  v a2s.  c o m
        int count = _addressMapper.insert(address_);
        assert (count == 1);
        assert (Short.valueOf(address_.getAddressId()) > 0);

        //double d = 1/0; // to test transaction roll-back 

        customer_.setAddressId(address_.getAddressId());
        count = _customerMapper.insert(customer_);
        assert (count == 1);
        assert (Short.valueOf(customer_.getCustomerId()) > 0);

        newCustomerId = Short.valueOf(customer_.getCustomerId());
    } catch (Throwable t) {
        String msg = String.format("Cannot add %s with %s", customer_.toString(), address_.toString());
        throw new ServiceException(msg, t);
    }
    return newCustomerId;
}