Example usage for org.apache.commons.collections ComparatorUtils min

List of usage examples for org.apache.commons.collections ComparatorUtils min

Introduction

In this page you can find the example usage for org.apache.commons.collections ComparatorUtils min.

Prototype

public static Object min(Object o1, Object o2, Comparator comparator) 

Source Link

Document

Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.

Usage

From source file:me.philnate.textmanager.updates.Updater.java

/**
 * checks what the actual db version is, if an old version is encountered
 * appropriate updates are performed to get the db to the latest version
 *///from  w ww  .  ja va  2  s  .co  m
public static void checkUpdateNeeded(String packageName) {
    checkArgument(StringUtils.isNotBlank(packageName), "You must insert a packageName");
    TreeMap<Version, Class<? extends Update>> updates = createUpdateList(packageName);
    Setting v = Setting.find("version");
    // check that an version is set, if none was found set it to 1
    LOG.info(format("Database version is %s", v.getValue()));
    if (StringUtils.isBlank(v.getValue())) {
        Version db = (Version) ComparatorUtils.min(startVersion, updates.lastKey(), null);
        LOG.debug(String.format("No Version set, assuming []", db.toString()));
        v = new Setting("version", db);
        ds.save(v);
    }
    LOG.info(format("Found these Database upgrades: '%s'", updates.keySet()));
    for (Version vers : updates.keySet()) {
        if (vers.compareTo(new Version(v.getValue())) < Version.AFTER) {
            // if version is smaller than actual db version we have nothing
            // todo here
            LOG.debug(format("Database is already newer than '%s'", vers));
            continue;
        }
        try {
            LOG.info(format("Going to update Database to version '%s'", vers));
            backUp();
            // create new Instance
            Update up = updates.get(vers).newInstance();
            // verify that everything is met for this update
            up.preCheck();
            // do the actual update
            up.upgrade();
            // verify that everything is as expected
            up.postCheck();
            // update the version
            v.setValue(vers.toString()).save();
        } catch (Exception e) {
            // in case of an exception stop further rollback and stop
            // further updates
            LOG.error("Update process caused an exception going to rollback", e);
            rollback();
            return;
        } finally {
            // finally drop backup directory to avoid to get conflicting
            // data versions
            try {
                FileUtils.deleteDirectory(backUpPath);
            } catch (IOException e) {
                LOG.error("Could not remove file", e);
            }
        }
    }
}

From source file:com.facultyshowcase.app.cmscomp.professor.list.ProfessorProfileListGenerator.java

public String getIdentity(CmsRequest<ProfessorProfileListCMSBean> request) {

    Date lastModifiedDate = (Date) ComparatorUtils.min(request.getPageElement().getLastModified(),
            _ProfessorProfileDAO.getLastModifiedDate(),
            ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator()));

    return Long.toString(lastModifiedDate.getTime());

}

From source file:com.facultyshowcase.app.cmscomp.professor.viewer.ProfessorProfileGenerator.java

@Override
public String getIdentity(CmsRequest<ProfessorProfileCMSBean> request) {

    String slug = getSlug(request);

    if (StringFactory.trimToNull(slug) == null) {
        return super.getIdentity(request);
    }// w w  w .  j  av a2s  .co m

    Date lastModifiedDate = (Date) ComparatorUtils.min(request.getPageElement().getLastModified(),
            _ProfessorProfileDAO.getLastModifiedDate(slug),
            ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator()));

    return slug + " " + lastModifiedDate.getTime();

}

From source file:acromusashi.stream.ml.clustering.kmeans.KmeansCalculator.java

/**
 * Kmeans??<br>//  w w w. j  a v a2s .co  m
 * ???<br>
* <ol>
* <li>????????(?n????n?????)</li>
* <li>n?????????????????????????????</li>
* <li>???????</li>
* </ol>
 * 
 * @param baseKmeans Kmeans
 * @param targetKmeans Kmeans
 * @return ?
 */
public static final KmeansDataSet mergeKmeans(KmeansDataSet baseKmeans, KmeansDataSet targetKmeans) {
    KmeansDataSet merged = new KmeansDataSet();
    int centroidNum = (int) ComparatorUtils.min(baseKmeans.getCentroids().length,
            targetKmeans.getCentroids().length, ComparatorUtils.NATURAL_COMPARATOR);

    // ???????
    List<CentroidMapping> allDistance = calculateDistances(baseKmeans.getCentroids(),
            targetKmeans.getCentroids(), centroidNum);

    // n?????????????????
    Collections.sort(allDistance, new CentroidsComparator());
    Map<Integer, Integer> resultMapping = createCentroidMappings(centroidNum, allDistance);

    // ??
    double[][] mergedCentroids = mergeCentroids(baseKmeans.getCentroids(), targetKmeans.getCentroids(),
            resultMapping);
    merged.setCentroids(mergedCentroids);

    return merged;
}