Example usage for org.springframework.util ObjectUtils isEmpty

List of usage examples for org.springframework.util ObjectUtils isEmpty

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils isEmpty.

Prototype

@SuppressWarnings("rawtypes")
public static boolean isEmpty(@Nullable Object obj) 

Source Link

Document

Determine whether the given object is empty.

Usage

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Append the given String to the given String array, returning a new array
 * consisting of the input array contents plus the given String.
 * @param array the array to append to (can be <code>null</code>)
 * @param str the String to append/*  w  ww  .jav  a 2 s . c  om*/
 * @return the new array (never <code>null</code>)
 */
public static String[] addStringToArray(String[] array, String str) {
    if (ObjectUtils.isEmpty(array)) {
        return new String[] { str };
    }
    String[] newArr = new String[array.length + 1];
    System.arraycopy(array, 0, newArr, 0, array.length);
    newArr[array.length] = str;
    return newArr;
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Concatenate the given String arrays into one,
 * with overlapping array elements included twice.
 * <p>The order of elements in the original arrays is preserved.
 * @param array1 the first array (can be <code>null</code>)
 * @param array2 the second array (can be <code>null</code>)
 * @return the new array (<code>null</code> if both given arrays were <code>null</code>)
 *//*from ww  w  . j  a  va  2s . c om*/
public static String[] concatenateStringArrays(String[] array1, String[] array2) {
    if (ObjectUtils.isEmpty(array1)) {
        return array2;
    }
    if (ObjectUtils.isEmpty(array2)) {
        return array1;
    }
    String[] newArr = new String[array1.length + array2.length];
    System.arraycopy(array1, 0, newArr, 0, array1.length);
    System.arraycopy(array2, 0, newArr, array1.length, array2.length);
    return newArr;
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Merge the given String arrays into one, with overlapping
 * array elements only included once./*from w w w. ja  v a2  s .  com*/
 * <p>The order of elements in the original arrays is preserved
 * (with the exception of overlapping elements, which are only
 * included on their first occurrence).
 * @param array1 the first array (can be <code>null</code>)
 * @param array2 the second array (can be <code>null</code>)
 * @return the new array (<code>null</code> if both given arrays were <code>null</code>)
 */
public static String[] mergeStringArrays(String[] array1, String[] array2) {
    if (ObjectUtils.isEmpty(array1)) {
        return array2;
    }
    if (ObjectUtils.isEmpty(array2)) {
        return array1;
    }
    List<String> result = new ArrayList<String>();
    result.addAll(Arrays.asList(array1));
    for (String str : array2) {
        if (!result.contains(str)) {
            result.add(str);
        }
    }
    return toStringArray(result);
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Turn given source String array into sorted array.
 * @param array the source array/* ww w .  j a  v a 2 s.c om*/
 * @return the sorted array (never <code>null</code>)
 */
public static String[] sortStringArray(String[] array) {
    if (ObjectUtils.isEmpty(array)) {
        return new String[0];
    }
    Arrays.sort(array);
    return array;
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Trim the elements of the given String array,
 * calling <code>String.trim()</code> on each of them.
 * @param array the original String array
 * @return the resulting array (of the same size) with trimmed elements
 *//*from  ww w . ja v a 2  s .  co m*/
public static String[] trimArrayElements(String[] array) {
    if (ObjectUtils.isEmpty(array)) {
        return new String[0];
    }
    String[] result = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        String element = array[i];
        result[i] = (element != null ? element.trim() : null);
    }
    return result;
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Remove duplicate Strings from the given array.
 * Also sorts the array, as it uses a TreeSet.
 * @param array the String array// w ww  .j  a  v a  2s .c  o  m
 * @return an array without duplicates, in natural sort order
 */
public static String[] removeDuplicateStrings(String[] array) {
    if (ObjectUtils.isEmpty(array)) {
        return array;
    }
    Set<String> set = new TreeSet<String>();
    for (String element : array) {
        set.add(element);
    }
    return toStringArray(set);
}

From source file:com.gst.infrastructure.core.data.DataValidatorBuilder.java

public DataValidatorBuilder arrayNotEmpty() {
    if (this.value == null && this.ignoreNullValue) {
        return this;
    }/*ww  w  . ja va 2  s. co  m*/

    final Object[] array = (Object[]) this.value;
    if (ObjectUtils.isEmpty(array)) {
        final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource)
                .append(".").append(this.parameter).append(".cannot.be.empty");
        final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter ").append(this.parameter)
                .append(" cannot be empty. You must select at least one.");
        final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(),
                defaultEnglishMessage.toString(), this.parameter);
        this.dataValidationErrors.add(error);
    }
    return this;
}

From source file:com.gohighedu.platform.framework.utils.common.StringUtils.java

/**
 * Take an array Strings and split each element based on the given delimiter.
 * A <code>Properties</code> instance is then generated, with the left of the
 * delimiter providing the key, and the right of the delimiter providing the value.
 * <p>Will trim both the key and value before adding them to the
 * <code>Properties</code> instance.
 * @param array the array to process/*from  w w w.  ja v a2s.  c  o  m*/
 * @param delimiter to split each element using (typically the equals symbol)
 * @param charsToDelete one or more characters to remove from each element
 * prior to attempting the split operation (typically the quotation mark
 * symbol), or <code>null</code> if no removal should occur
 * @return a <code>Properties</code> instance representing the array contents,
 * or <code>null</code> if the array to process was <code>null</code> or empty
 */
public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter,
        String charsToDelete) {

    if (ObjectUtils.isEmpty(array)) {
        return null;
    }
    Properties result = new Properties();
    for (String element : array) {
        if (charsToDelete != null) {
            element = deleteAny(element, charsToDelete);
        }
        String[] splittedElement = split(element, delimiter);
        if (splittedElement == null) {
            continue;
        }
        result.setProperty(splittedElement[0].trim(), splittedElement[1].trim());
    }
    return result;
}

From source file:com.gst.portfolio.group.service.GroupingTypesWritePlatformServiceJpaRepositoryImpl.java

private Set<Client> assembleSetOfClients(final Long groupOfficeId, final JsonCommand command) {

    final Set<Client> clientMembers = new HashSet<>();
    final String[] clientMembersArray = command
            .arrayValueOfParameterNamed(GroupingTypesApiConstants.clientMembersParamName);

    if (!ObjectUtils.isEmpty(clientMembersArray)) {
        for (final String clientId : clientMembersArray) {
            final Long id = Long.valueOf(clientId);
            final Client client = this.clientRepositoryWrapper.findOneWithNotFoundDetection(id);
            if (!client.isOfficeIdentifiedBy(groupOfficeId)) {
                final String errorMessage = "Client with identifier " + clientId
                        + " must have the same office as group.";
                throw new InvalidOfficeException("client", "attach.to.group", errorMessage, clientId,
                        groupOfficeId);// w  w  w  .  j  a  v a  2 s.  c o m
            }
            clientMembers.add(client);
        }
    }

    return clientMembers;
}

From source file:com.gst.portfolio.group.service.GroupingTypesWritePlatformServiceJpaRepositoryImpl.java

private Set<Group> assembleSetOfChildGroups(final Long officeId, final JsonCommand command) {

    final Set<Group> childGroups = new HashSet<>();
    final String[] childGroupsArray = command
            .arrayValueOfParameterNamed(GroupingTypesApiConstants.groupMembersParamName);

    if (!ObjectUtils.isEmpty(childGroupsArray)) {
        for (final String groupId : childGroupsArray) {
            final Long id = Long.valueOf(groupId);
            final Group group = this.groupRepository.findOneWithNotFoundDetection(id);

            if (!group.isOfficeIdentifiedBy(officeId)) {
                final String errorMessage = "Group and child groups must have the same office.";
                throw new InvalidOfficeException("group", "attach.to.parent.group", errorMessage);
            }//from   w  ww.  j a  v a2  s .c o  m

            childGroups.add(group);
        }
    }

    return childGroups;
}