Example usage for java.util Collection clear

List of usage examples for java.util Collection clear

Introduction

In this page you can find the example usage for java.util Collection clear.

Prototype

void clear();

Source Link

Document

Removes all of the elements from this collection (optional operation).

Usage

From source file:och.util.ConcurrentUtil.java

public static void getAndClearAllFutures(Collection<Future<?>> futures, boolean logOnError) {
    if (isEmpty(futures))
        return;//from w w  w  . ja  v  a 2s  .c  o m
    for (Future<?> future : futures) {
        try {
            future.get();
        } catch (Exception e) {
            if (logOnError)
                log.error("can't get future", e);
        }
    }
    futures.clear();
}

From source file:com.google.code.guice.repository.spi.TypeUtil.java

/**
 * Finds and returns class of specified generic parametrization class.
 *
 * @param aClass                generic class
 * @param genericParameterClass generic parametrization class
 *
 * @return parameter class or {@code null} if parameter can't be found
 *
 * @throws IllegalArgumentException if specified {@code aClass}  or {@code genericParameterClass} is null
 *///from   w w w . j  av a2s  . co m
public static <T> Class<T> getTypeParameterClass(Class aClass, Class<T> genericParameterClass) {
    Assert.noNullElements(new Object[] { aClass, genericParameterClass });

    Collection<Type> types = new ArrayList<Type>();

    // check interfaces
    getGenericInterfacesActualTypes(types, aClass);

    Class result = findAppropriateType(types, genericParameterClass);
    if (result == null) {
        types.clear();
        // check superclasses
        getGenericSuperclassActualTypes(types, aClass);
    }
    return findAppropriateType(types, genericParameterClass);
}

From source file:edu.illinois.ncsa.springdata.SpringData.java

/**
 * Check each field in object to see if it is already seen. This will modify
 * collections, lists and arrays as well as the fields in an object.
 * /*from   w  w w . ja v  a2  s  .c o  m*/
 * @param obj
 *            the object to be checked for duplicates.
 * @param seen
 *            objects that have already been checked.
 * @return the object with all duplicates removed
 */
private static <T> T removeDuplicate(T obj, Map<String, Object> seen) {
    if (obj == null) {
        return null;
    }
    if (obj instanceof AbstractBean) {
        String id = ((AbstractBean) obj).getId();
        if (seen.containsKey(id)) {
            return (T) seen.get(id);
        }
        seen.put(id, obj);

        // check all subfields
        for (Field field : obj.getClass().getDeclaredFields()) {
            if ((field.getModifiers() & Modifier.FINAL) == 0) {
                try {
                    field.setAccessible(true);
                    field.set(obj, removeDuplicate(field.get(obj), seen));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    } else if (obj instanceof Collection<?>) {
        Collection col = (Collection) obj;
        ArrayList arr = new ArrayList(col);
        col.clear();
        for (int i = 0; i < arr.size(); i++) {
            Object x = removeDuplicate(arr.get(i), seen);
            col.add(x);
        }
    } else if (obj instanceof Map<?, ?>) {
        Map map = (Map) obj;
        ArrayList<Entry> arr = new ArrayList(map.entrySet());
        map.clear();
        for (int i = 0; i < arr.size(); i++) {
            Object k = removeDuplicate(arr.get(i).getKey(), seen);
            Object v = removeDuplicate(arr.get(i).getValue(), seen);
            map.put(k, v);
        }
    } else if (obj.getClass().isArray()) {
        Object[] arr = (Object[]) obj;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = removeDuplicate(arr[i], seen);
        }
    }

    return obj;
}

From source file:com.gm.bamboo.util.HibernateUtils.java

/**
 * ?ID?, ???.//from  w w w  .  j  ava 2  s.  co  m
 * 
 * ??????id,Hibernate??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update??.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?,IdEntity?
 */
public static <T extends BaseEntity> void mergeByCheckedIds(final Collection<T> srcObjects,
        final Collection<Long> checkedIds, final Class<T> clazz) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.notNull(clazz, "clazz?");

    //?, ???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?.
    //?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Long id = element.getId();

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (Long id : checkedIds) {
            T element = clazz.newInstance();
            element.setId(id);
            srcObjects.add(element);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.gm.wine.util.HibernateUtils.java

/**
 * ?ID?, ???./*from  www  . ja v a 2 s.c o  m*/
 * 
 * ??????id,Hibernate??????id?????.
 * ???id??,??id??. ?ID,
 * ??cascade-save-or-update??.
 * 
 * @param srcObjects
 *            ??,.
 * @param checkedIds
 *            ?,ID.
 * @param clazz
 *            ?,IdEntity?
 */
public static <T extends BaseEntity> void mergeByCheckedIds(final Collection<T> srcObjects,
        final Collection<Long> checkedIds, final Class<T> clazz) {

    // ?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.notNull(clazz, "clazz?");

    // ?, ???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    // ????,id?ID?.
    // ?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Long id = element.getId();

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        // ID??id????,,id??.
        for (Long id : checkedIds) {
            T element = clazz.newInstance();
            element.setId(id);
            srcObjects.add(element);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:org.oscarehr.web.admin.ProviderPreferencesUIBean.java

public static final ProviderPreference updateOrCreateProviderPreferences(HttpServletRequest request) {
    ProviderPreference providerPreference = getLoggedInProviderPreference();

    // update preferences based on request parameters
    String temp;/*from  w  w  w  .j av a2  s.com*/
    HttpSession session = request.getSession();

    // new tickler window
    temp = StringUtils.trimToNull(request.getParameter("new_tickler_warning_window"));
    if (temp != null) {
        providerPreference.setNewTicklerWarningWindow(temp);
    } else {
        temp = StringUtils.trimToNull((String) session.getAttribute("newticklerwarningwindow"));
        if (temp != null)
            providerPreference.setNewTicklerWarningWindow(temp);
    }

    // default pmm
    temp = StringUtils.trimToNull(request.getParameter("default_pmm"));
    if (temp != null) {
        providerPreference.setDefaultCaisiPmm(temp);
    } else {
        temp = StringUtils.trimToNull((String) session.getAttribute("default_pmm"));
        if (temp == null)
            providerPreference.setDefaultCaisiPmm("disabled");
        else
            providerPreference.setDefaultCaisiPmm(temp);
    }

    // default billing preference (edit or delete)
    temp = StringUtils.trimToNull(request.getParameter("caisiBillingPreferenceNotDelete"));
    if (temp != null) {
        try {
            providerPreference.setDefaultDoNotDeleteBilling(Integer.parseInt(temp));
        } catch (NumberFormatException e) {
            MiscUtils.getLogger().error("Error", e);
        }
    } else {
        temp = StringUtils.trimToNull(String.valueOf(session.getAttribute("caisiBillingPreferenceNotDelete")));
        if (temp == null)
            providerPreference.setDefaultDoNotDeleteBilling(0);
        else {
            int defBilling = 0;
            try {
                defBilling = Integer.parseInt(temp);
            } catch (NumberFormatException e) {
                Log.warn("warning", e);
            }
            providerPreference.setDefaultDoNotDeleteBilling(defBilling);
        }
    }

    // default billing dxCode 
    temp = StringUtils.trimToNull(request.getParameter("dxCode"));
    if (temp != null)
        providerPreference.setDefaultDxCode(temp);

    // rest
    temp = StringUtils.trimToNull(request.getParameter("start_hour"));
    if (temp != null)
        providerPreference.setStartHour(Integer.parseInt(temp));

    temp = StringUtils.trimToNull(request.getParameter("end_hour"));
    if (temp != null)
        providerPreference.setEndHour(Integer.parseInt(temp));

    temp = StringUtils.trimToNull(request.getParameter("every_min"));
    if (temp != null)
        providerPreference.setEveryMin(Integer.parseInt(temp));

    temp = StringUtils.trimToNull(request.getParameter("mygroup_no"));
    if (temp != null)
        providerPreference.setMyGroupNo(temp);

    temp = StringUtils.trimToNull(request.getParameter("default_servicetype"));
    if (temp != null)
        providerPreference.setDefaultServiceType(temp);

    temp = StringUtils.trimToNull(request.getParameter("color_template"));
    if (temp != null)
        providerPreference.setColourTemplate(temp);

    providerPreference.setPrintQrCodeOnPrescriptions(WebUtils.isChecked(request, "prescriptionQrCodes"));

    // get encounterForms for appointment screen
    temp = StringUtils.trimToNull(request.getParameter("appointmentScreenFormsNameDisplayLength"));
    if (temp != null)
        providerPreference.setAppointmentScreenLinkNameDisplayLength(Integer.parseInt(temp));

    String[] formNames = request.getParameterValues("encounterFormName");
    Collection<String> formNamesList = providerPreference.getAppointmentScreenForms();

    formNamesList.clear();
    if (formNames != null) {
        for (String formName : formNames) {
            formNamesList.add(formName);
        }
    }

    // get eForms for appointment screen
    String[] formIds = request.getParameterValues("eformId");
    Collection<Integer> eFormsIdsList = providerPreference.getAppointmentScreenEForms();

    eFormsIdsList.clear();
    if (formIds != null) {
        for (String formId : formIds) {
            eFormsIdsList.add(Integer.parseInt(formId));
        }
    }

    // external prescriber prefs
    providerPreference.setERxEnabled(WebUtils.isChecked(request, "erx_enable"));

    temp = StringUtils.trimToNull(request.getParameter("erx_username"));
    if (temp != null)
        providerPreference.setERxUsername(temp);

    temp = StringUtils.trimToNull(request.getParameter("erx_password"));
    if (temp != null)
        providerPreference.setERxPassword(temp);

    temp = StringUtils.trimToNull(request.getParameter("erx_facility"));
    if (temp != null)
        providerPreference.setERxFacility(temp);

    providerPreference.setERxTrainingMode(WebUtils.isChecked(request, "erx_training_mode"));

    temp = StringUtils.trimToNull(request.getParameter("erx_sso_url"));
    if (temp != null)
        providerPreference.setERx_SSO_URL(temp);

    providerPreferenceDao.merge(providerPreference);

    return (providerPreference);
}

From source file:com.framework.infrastructure.utils.HibernateUtils.java

/**
 * ID,./*ww w  . ja va  2 s . c  om*/
 * 
 * id,id.
 * id,id. ID,
 * cascade-save-or-update.
 * 
 * @param srcObjects
 *            ,.
 * @param checkedIds
 *            ,ID.
 * @param clazz
 *            
 * @param idName
 *            
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    // 
    Assert.notNull(srcObjects, "scrObjects");
    Assert.hasText(idName, "idName");
    Assert.notNull(clazz, "clazz");

    // , .
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    // ,idID,.
    // ,id,idid.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        // IDid,,id.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:org.springside.modules.orm.hibernate.HibernateUtils.java

/**
 * ?ID?,???.//www .j  a  v a  2 s.co m
 * 
 * ??????id,??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update??.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //?, ???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.thinkmore.framework.orm.hibernate.HibernateWebUtils.java

/**
 * ?ID?,???./*  w ww .j a v a2s.  c  o  m*/
 * 
 * ??????id,??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //?,???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtil.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.xyz.util.WebDataUtil.java

/**
 * ?ID?,???.//from   w w  w. j  a v  a 2s  .c  om
 * 
 * ??????id,??????id?????.
 * ???id?ID?,?ID?id??.
 * 
 * @param srcObjects ??
 * @param checkedIds  ID?
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //ID?,???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,ID???id,ID?id???ID.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtil.convertToUncheckedException(e);
    }
}