Example usage for org.apache.commons.lang3 ObjectUtils toString

List of usage examples for org.apache.commons.lang3 ObjectUtils toString

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ObjectUtils toString.

Prototype

@Deprecated
public static String toString(final Object obj, final String nullStr) 

Source Link

Document

Gets the toString of an Object returning a specified text if null input.

 ObjectUtils.toString(null, null)           = null ObjectUtils.toString(null, "null")         = "null" ObjectUtils.toString("", "null")           = "" ObjectUtils.toString("bat", "null")        = "bat" ObjectUtils.toString(Boolean.TRUE, "null") = "true" 

Usage

From source file:info.donsun.core.utils.Values.java

/**
 * ??//from  w ww  . j av a  2 s. c o  m
 * 
 * @param obj 
 * @param defaultValue 
 * @return 
 */
public static String getString(Object obj, String defaultValue) {
    return ObjectUtils.toString(obj, defaultValue);
}

From source file:com.nesscomputing.config.ConfigMagicDynamicMBean.java

private static Map<String, Object> toMap(Object configBean) {
    PropertyDescriptor[] props = ReflectUtils.getBeanGetters(configBean.getClass());

    Map<String, Object> result = Maps.newHashMap();

    for (PropertyDescriptor prop : props) {
        if (CONFIG_MAGIC_CALLBACKS_NAME.equals(prop.getName())) {
            continue;
        }/* w  w  w  . j a  v  a 2 s  . co  m*/

        try {
            result.put(prop.getName(), ObjectUtils.toString(prop.getReadMethod().invoke(configBean), null));
        } catch (Exception e) {
            LOG.error(String.format("For class %s, unable to find config property %s", configBean.getClass(),
                    prop), e);
        }
    }

    return result;
}

From source file:com.nesscomputing.service.discovery.announce.GalaxyAnnouncementModule.java

@Provides
@Singleton//from  www.j  av  a 2s  .  c o m
AnnouncementConfig getAnnouncementConfig(final Config config) {
    final AnnouncementConfig baseConfig = config.getBean(AnnouncementConfig.class);

    return new AnnouncementConfig(baseConfig) {
        @Override
        public String getServiceName() {
            return ObjectUtils.toString(super.getServiceName(), serviceName);
        }

        @Override
        public String getServiceType() {
            return ObjectUtils.toString(super.getServiceType(), serviceType);
        }
    };
}

From source file:com.github.htfv.maven.plugins.buildconfigurator.core.el.PropertyAccessor.java

@Override
public String get(final Object key) {
    return ObjectUtils.toString(resolver.resolve(context, key.toString()), null);
}

From source file:com.nesscomputing.exception.NessApiException.java

protected String getValue(String field) {
    return ObjectUtils.toString(fields.get(field), null);
}

From source file:com.nesscomputing.jms.activemq.ServiceDiscoveryTransportFactoryTest.java

private void consumeTestMessage() throws Exception {
    final Connection connection = factory.createConnection();
    connection.start();//w w  w . j  a va2 s . c om
    try {
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageConsumer consumer = session.createConsumer(session.createQueue(QNAME));
        final Message message = consumer.receive(1000);

        LOG.info(ObjectUtils.toString(message, "<no message>"));

        Assert.assertEquals(uniqueId, ((TextMessage) message).getText());
    } finally {
        connection.stop();
        connection.close();
    }
}

From source file:com.opencsv.ResultSetHelperService.java

private String getColumnValue(ResultSet rs, int colType, int colIndex, boolean trim, String dateFormatString,
        String timestampFormatString) throws SQLException, IOException {

    String value = "";

    switch (colType) {
    case Types.BIT:
    case Types.JAVA_OBJECT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getObject(colIndex), "");
        value = ObjectUtils.toString(rs.getObject(colIndex), "");
        break;//from   w w  w.  ja v  a  2  s  .  co m
    case Types.BOOLEAN:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getBoolean(colIndex));
        value = ObjectUtils.toString(rs.getBoolean(colIndex));
        break;
    case Types.NCLOB: // todo : use rs.getNClob
    case Types.CLOB:
        Clob c = rs.getClob(colIndex);
        if (c != null) {
            StrBuilder sb = new StrBuilder();
            sb.readFrom(c.getCharacterStream());
            value = sb.toString();
        }
        break;
    case Types.BIGINT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getLong(colIndex));
        value = ObjectUtils.toString(rs.getLong(colIndex));
        break;
    case Types.DECIMAL:
    case Types.REAL:
    case Types.NUMERIC:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getBigDecimal(colIndex), "");
        value = ObjectUtils.toString(rs.getBigDecimal(colIndex), "");
        break;
    case Types.DOUBLE:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getDouble(colIndex));
        value = ObjectUtils.toString(rs.getDouble(colIndex));
        break;
    case Types.FLOAT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getFloat(colIndex));
        value = ObjectUtils.toString(rs.getFloat(colIndex));
        break;
    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getInt(colIndex));
        value = ObjectUtils.toString(rs.getInt(colIndex));
        break;
    case Types.DATE:
        java.sql.Date date = rs.getDate(colIndex);
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(dateFormatString);
            value = df.format(date);
        }
        break;
    case Types.TIME:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getTime(colIndex), "");
        value = ObjectUtils.toString(rs.getTime(colIndex), "");
        break;
    case Types.TIMESTAMP:
        value = handleTimestamp(rs.getTimestamp(colIndex), timestampFormatString);
        break;
    case Types.NVARCHAR: // todo : use rs.getNString
    case Types.NCHAR: // todo : use rs.getNString
    case Types.LONGNVARCHAR: // todo : use rs.getNString
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.CHAR:
        String columnValue = rs.getString(colIndex);
        if (trim && columnValue != null) {
            value = columnValue.trim();
        } else {
            value = columnValue;
        }
        break;
    default:
        value = "";
    }

    if (rs.wasNull() || value == null) {
        value = "";
    }

    return value;
}

From source file:com.nesscomputing.httpclient.testing.JaxRsResponseHttpResponseGenerator.java

@Override
public HttpClientResponse respondTo(final HttpClientRequest<Object> request) {
    return new HttpClientResponse() {
        @Override/*from  ww  w.  j  a v  a  2  s. c o m*/
        public int getStatusCode() {
            return response.getStatus();
        }

        @Override
        public String getStatusText() {
            return Status.fromStatusCode(getStatusCode()).getReasonPhrase();
        }

        @Override
        public InputStream getResponseBodyAsStream() throws IOException {
            return responseBody;
        }

        @Override
        public URI getUri() {
            return request.getUri();
        }

        @Override
        public String getContentType() {
            return ObjectUtils.toString(contentType, null);
        }

        @Override
        public Long getContentLength() {
            return contentLength;
        }

        @Override
        public String getCharset() {
            return charset;
        }

        @Override
        public String getHeader(String name) {
            List<Object> headers = response.getMetadata().get(name);
            if (headers == null || headers.isEmpty()) {
                return null;
            }
            return headers.get(0).toString();
        }

        @Override
        public List<String> getHeaders(String name) {
            List<Object> headers = response.getMetadata().get(name);
            if (headers == null) {
                return null;
            }
            return ImmutableList.copyOf(Collections2.transform(headers, Functions.toStringFunction()));
        }

        @Override
        public Map<String, List<String>> getAllHeaders() {
            ImmutableMap.Builder<String, List<String>> result = ImmutableMap.builder();
            for (Entry<String, List<Object>> e : response.getMetadata().entrySet()) {
                result.put(e.getKey(), ImmutableList
                        .copyOf(Collections2.transform(e.getValue(), Functions.toStringFunction())));
            }
            return result.build();
        }

        @Override
        public boolean isRedirected() {
            return false;
        }
    };
}

From source file:com.nesscomputing.mojo.numbers.macros.ScmMacro.java

public String getConnectionUrl(final Scm scm, final Properties props) {
    final boolean requireDeveloperConnection = BooleanUtils
            .toBoolean(props.getProperty("requireDeveloperConnection"));

    if (StringUtils.isNotBlank(scm.getConnection()) && !requireDeveloperConnection) {
        final String url = ObjectUtils.toString(scm.getConnection(), scm.getDeveloperConnection());
        Preconditions.checkState(url != null, "no scm url found!");
        return url;
    }/*from  w  ww.  j a v  a2  s  . c om*/

    final String url = scm.getDeveloperConnection();
    Preconditions.checkState(url != null, "no scm developer url found!");
    return url;
}

From source file:com.opencsv.bean.StatefulBeanToCsv.java

/**
 * Writes a bean out to the {@link java.io.Writer} provided to the
 * constructor./*ww  w .  ja va  2s  . co  m*/
 * 
 * @param bean A bean to be written to a CSV destination
 * @throws CsvDataTypeMismatchException If a field of the bean is
 *   annotated improperly or an unsupported data type is supposed to be
 *   written
 * @throws CsvRequiredFieldEmptyException If a field is marked as required,
 *   but the source is null
 */
public void write(T bean) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
    if (bean != null) {
        ++lineNumber;
        beforeFirstWrite(bean);
        List<String> contents = new ArrayList<String>();
        int numColumns = mappingStrategy.findMaxFieldIndex();
        if (mappingStrategy.isAnnotationDriven()) {
            BeanField beanField;
            for (int i = 0; i <= numColumns; i++) {
                beanField = mappingStrategy.findField(i);
                try {
                    String s = beanField != null ? beanField.write(bean) : "";
                    contents.add(StringUtils.defaultString(s));
                }
                // Combine to a multi-catch once we support Java 7
                catch (CsvDataTypeMismatchException e) {
                    e.setLineNumber(lineNumber);
                    if (throwExceptions) {
                        throw e;
                    } else {
                        capturedExceptions.add(e);
                    }
                } catch (CsvRequiredFieldEmptyException e) {
                    e.setLineNumber(lineNumber);
                    if (throwExceptions) {
                        throw e;
                    } else {
                        capturedExceptions.add(e);
                    }
                }
            }
        } else {
            PropertyDescriptor desc;
            for (int i = 0; i <= numColumns; i++) {
                try {
                    desc = mappingStrategy.findDescriptor(i);
                    Object o = desc != null ? desc.getReadMethod().invoke(bean, (Object[]) null) : null;
                    contents.add(ObjectUtils.toString(o, ""));
                    // Once we support Java 7
                    //                        contents.add(Objects.toString(o, ""));
                }
                // Combine in a multi-catch with Java 7
                catch (IntrospectionException e) {
                    CsvBeanIntrospectionException csve = new CsvBeanIntrospectionException(bean, null,
                            INTROSPECTION_ERROR);
                    csve.initCause(e);
                    throw csve;
                } catch (IllegalAccessException e) {
                    CsvBeanIntrospectionException csve = new CsvBeanIntrospectionException(bean, null,
                            INTROSPECTION_ERROR);
                    csve.initCause(e);
                    throw csve;
                } catch (InvocationTargetException e) {
                    CsvBeanIntrospectionException csve = new CsvBeanIntrospectionException(bean, null,
                            INTROSPECTION_ERROR);
                    csve.initCause(e);
                    throw csve;
                }
            }
        }
        csvwriter.writeNext(contents.toArray(new String[contents.size()]));
    }
}