Example usage for java.util Objects toString

List of usage examples for java.util Objects toString

Introduction

In this page you can find the example usage for java.util Objects toString.

Prototype

public static String toString(Object o, String nullDefault) 

Source Link

Document

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Usage

From source file:com.yinghua.translation.rest.PhoneResourceRESTService.java

/**
 * ping++//from w  w  w.j a  v a2 s  .  c  o  m
 * 
 * @param params
 * @return
 */
@POST
@Path("/webhooks")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> webhooks(String params) {
    Event event = Webhooks.eventParse(params);
    Map<String, Object> req = new HashMap<>();
    if ("charge.succeeded".equals(event.getType())) {
        @SuppressWarnings("unchecked")
        Map<String, Object> object = (Map<String, Object>) event.getData().get("object");
        if (object != null && object.size() > 0) {
            String orderNo = Objects.toString(object.get("order_no"), "0");
            MemberOrder order = memberOrderBean.findByPayNo(orderNo);
            if (order != null) {
                if (order.getState() != OrderStatus.FINISHED) {
                    order.setState(OrderStatus.FINISHED);
                    List<PackageProductContent> ppcs = packageProductContentBean
                            .findByPackageNo(order.getPackageNo());
                    switch (order.getPackageType()) {
                    case "1":
                        //
                        for (PackageProductContent packageProductContent : ppcs) {
                            MemberOrderUse mou = new MemberOrderUse();
                            mou.setOrderNo(order.getOrderNo());
                            mou.setProductNo(packageProductContent.getProductNo());
                            mou.setTimes(packageProductContent.getTimes());
                            memberOrderUseBean.createMemberOrderUse(mou);
                        }
                        break;
                    case "2":
                        //
                        for (PackageProductContent packageProductContent : ppcs) {
                            MemberOrderUse mou = new MemberOrderUse();
                            mou.setOrderNo(order.getOrderNo());
                            mou.setProductNo(packageProductContent.getProductNo());
                            mou.setTimes(packageProductContent.getTimes());
                            memberOrderUseBean.createMemberOrderUse(mou);
                        }
                        List<MemberOrder> moList = memberOrderBean.findUsingOrderByUno(order.getMemberNumber(),
                                "2");
                        for (MemberOrder memberOrder : moList) {
                            if (memberOrder.getServiceEndTime().getTime() < order.getServiceEndTime()
                                    .getTime()) {
                                memberOrder.setServiceEndTime(order.getServiceEndTime());
                                memberOrderBean.updateOrder(memberOrder);
                            }
                        }

                        break;
                    case "3":
                        //?
                        JSONObject obj = JSONObject.parseObject(order.getRemark());
                        for (String key : obj.keySet()) {
                            MemberOrderUse mou = new MemberOrderUse();
                            mou.setOrderNo(order.getOrderNo());
                            mou.setProductNo(key);
                            mou.setTimes(obj.getIntValue(key));
                            memberOrderUseBean.createMemberOrderUse(mou);
                        }
                        List<MemberOrder> moList2 = memberOrderBean.findUsingOrderByUno(order.getMemberNumber(),
                                "3");
                        for (MemberOrder memberOrder : moList2) {
                            if (memberOrder.getServiceEndTime().getTime() < order.getServiceEndTime()
                                    .getTime()) {
                                memberOrder.setServiceEndTime(order.getServiceEndTime());
                                memberOrderBean.updateOrder(memberOrder);
                            }
                        }
                        break;
                    }
                    if (System.currentTimeMillis() >= order.getServiceTime().getTime()) {
                        //                      Account account = accountBean.findByMemberNo(order.getMemberNumber());
                        //                      int addCall = order.getSurplusCallDuration()+account.getSurplusCallDuration();
                        //                      account.setSurplusCallDuration(addCall);
                        //                      accountBean.updateAccount(account);
                        order.setUseState(OrderUseStatus.USING);
                    }
                    memberOrderBean.updateOrder(order);
                    System.out.println("orderNo:" + order.getOrderNo() + "|orderState:" + order.getState()
                            + "|orderUseState:" + order.getUseState());
                }
            } else {
                //
            }
        }
    }
    req.put("result", "ok");
    return req;
}

From source file:com.norconex.commons.lang.map.Properties.java

@Override
public final List<String> get(Object key) {
    if (!caseSensitiveKeys) {
        return super.get(key);
    }/*from   w  ww. ja v a 2s  .  c  om*/
    List<String> values = new ArrayList<String>();
    for (String k : keySet()) {
        if (StringUtils.equalsIgnoreCase(k, Objects.toString(key, null))) {
            values.addAll(super.get(k));
        }
    }
    return values;
}

From source file:com.norconex.commons.lang.map.Properties.java

@Override
public final List<String> remove(Object key) {

    if (!caseSensitiveKeys) {
        return super.remove(key);
    }/*from www . ja va2 s.c om*/

    List<String> oldValues = null;
    List<String> keysToRemove = new ArrayList<String>();
    for (Iterator<String> it = keySet().iterator(); it.hasNext();) {
        String k = it.next();
        if (StringUtils.equalsIgnoreCase(k, Objects.toString(key, null))) {
            keysToRemove.add(k);
        }
    }
    for (String k : keysToRemove) {
        List<String> previous = super.remove(k);
        if (previous != null) {
            if (oldValues == null) {
                oldValues = new ArrayList<>();
            }
            oldValues.addAll(previous);
        }
    }
    return oldValues;
}

From source file:com.norconex.commons.lang.map.Properties.java

private String[] toStringArray(Object[] array) {
    if (array == null) {
        return null;
    }/*from   w w  w  .j av  a2 s  .c om*/
    String[] strArray = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        strArray[i] = Objects.toString(array[i], StringUtils.EMPTY);

    }
    return strArray;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Joins the elements of the provided {@code Iterator} into
 * a single String containing the provided elements.</p>
 *
 * <p>No delimiter is added before or after the list. Null objects or empty
 * strings within the iteration are represented by empty strings.</p>
 *
 * <p>See the examples here: {@link #join(Object[],char)}. </p>
 *
 * @param iterator  the {@code Iterator} of values to join together, may be null
 * @param separator  the separator character to use
 * @return the joined String, {@code null} if null iterator input
 * @since 2.0//from ww  w  .j a v a 2  s.  c  om
 */
public static String join(final Iterator<?> iterator, final char separator) {

    // handle null, zero and one elements before building a buffer
    if (iterator == null) {
        return null;
    }
    if (!iterator.hasNext()) {
        return EMPTY;
    }
    final Object first = iterator.next();
    if (!iterator.hasNext()) {
        final String result = Objects.toString(first, "");
        return result;
    }

    // two or more elements
    final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
    if (first != null) {
        buf.append(first);
    }

    while (iterator.hasNext()) {
        buf.append(separator);
        final Object obj = iterator.next();
        if (obj != null) {
            buf.append(obj);
        }
    }

    return buf.toString();
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Joins the elements of the provided {@code Iterator} into
 * a single String containing the provided elements.</p>
 *
 * <p>No delimiter is added before or after the list.
 * A {@code null} separator is the same as an empty String ("").</p>
 *
 * <p>See the examples here: {@link #join(Object[],String)}. </p>
 *
 * @param iterator  the {@code Iterator} of values to join together, may be null
 * @param separator  the separator character to use, null treated as ""
 * @return the joined String, {@code null} if null iterator input
 *//*  w  ww. j ava2 s  .c o  m*/
public static String join(final Iterator<?> iterator, final String separator) {

    // handle null, zero and one elements before building a buffer
    if (iterator == null) {
        return null;
    }
    if (!iterator.hasNext()) {
        return EMPTY;
    }
    final Object first = iterator.next();
    if (!iterator.hasNext()) {
        final String result = Objects.toString(first, "");
        return result;
    }

    // two or more elements
    final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
    if (first != null) {
        buf.append(first);
    }

    while (iterator.hasNext()) {
        if (separator != null) {
            buf.append(separator);
        }
        final Object obj = iterator.next();
        if (obj != null) {
            buf.append(obj);
        }
    }
    return buf.toString();
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Joins the elements of the provided varargs into a
 * single String containing the provided elements.</p>
 *
 * <p>No delimiter is added before or after the list.
 * {@code null} elements and separator are treated as empty Strings ("").</p>
 *
 * <pre>//from w  ww .  j a  v  a2 s. c o m
 * StringUtils.joinWith(",", {"a", "b"})        = "a,b"
 * StringUtils.joinWith(",", {"a", "b",""})     = "a,b,"
 * StringUtils.joinWith(",", {"a", null, "b"})  = "a,,b"
 * StringUtils.joinWith(null, {"a", "b"})       = "ab"
 * </pre>
 *
 * @param separator the separator character to use, null treated as ""
 * @param objects the varargs providing the values to join together. {@code null} elements are treated as ""
 * @return the joined String.
 * @throws IllegalArgumentException if a null varargs is provided
 * @since 3.5
 */
public static String joinWith(final String separator, final Object... objects) {
    if (objects == null) {
        throw new IllegalArgumentException("Object varargs must not be null");
    }

    final String sanitizedSeparator = defaultString(separator, StringUtils.EMPTY);

    final StringBuilder result = new StringBuilder();

    final Iterator<Object> iterator = Arrays.asList(objects).iterator();
    while (iterator.hasNext()) {
        final String value = Objects.toString(iterator.next(), "");
        result.append(value);

        if (iterator.hasNext()) {
            result.append(sanitizedSeparator);
        }
    }

    return result.toString();
}