Example usage for org.apache.commons.collections4.comparators ReverseComparator ReverseComparator

List of usage examples for org.apache.commons.collections4.comparators ReverseComparator ReverseComparator

Introduction

In this page you can find the example usage for org.apache.commons.collections4.comparators ReverseComparator ReverseComparator.

Prototype

@SuppressWarnings("unchecked")
public ReverseComparator(final Comparator<? super E> comparator) 

Source Link

Document

Creates a comparator that inverts the comparison of the given comparator.

Usage

From source file:de.micromata.genome.gwiki.plugin.blog_1_0.GWikiBlogBaseActionBean.java

protected boolean init() {
    userLocale = wikiContext.getWikiWeb().getAuthorization().getCurrentUserLocale(wikiContext);
    userTimeZone = wikiContext.getUserTimeZone();
    if (StringUtils.isEmpty(blogPageId) == true) {
        blogPageId = wikiContext.getCurrentElement().getElementInfo().getId();
    }//from  w  w w. ja  va2  s .c  om
    blogPage = wikiContext.getWikiWeb().findElementInfo(blogPageId);
    if (blogPage == null) {
        // TODO return error;
        return false;
    }
    blogEntries = wikiContext.getElementFinder().getPageDirectPages(blogPageId);
    Collections.sort(blogEntries,
            new ReverseComparator<GWikiElementInfo>(new GWikiElementByPropComparator("CREATEDAT")));
    return true;
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.macros.GWikiPageCommentMacroActionBean.java

protected void collectComments() {
    List<GWikiElementInfo> l = getCommentsForPage(wikiContext, pageId);
    Collections.sort(l,//  w w  w.j  a  v a  2 s . c o m
            new ReverseComparator<GWikiElementInfo>(new GWikiElementByPropComparator(GWikiPropKeys.CREATEDAT)));
    fullList = l;
    // TODO find reply/to which are deleted.
    if (hierarchicThreadView == true) {
        List<GWikiElementInfo> nl = new ArrayList<GWikiElementInfo>();
        for (GWikiElementInfo ei : l) {
            if (StringUtils.isEmpty(ei.getProps().getStringValue(PROP_REPLY_TO)) == true) {
                nl.add(ei);
            }
        }
        commentElements = nl;
    } else {
        commentElements = l;
    }
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.macros.GWikiImageGalleryMacro.java

@Override
public boolean renderImpl(GWikiContext ctx, MacroAttributes attrs) {
    if (StringUtils.isEmpty(pageId) == true) {
        pageId = ctx.getCurrentElement().getElementInfo().getId();
    }//from www  .  java2  s  . c  o  m
    if (StringUtils.isEmpty(size) == true) {
        size = "medium";
    }
    List<GWikiElementInfo> childs = ctx.getElementFinder().getPageAttachments(pageId);
    if (StringUtils.isNotEmpty(include) == true) {
        Matcher<String> matcher = new BooleanListRulesFactory<String>().createMatcher(include);
        List<GWikiElementInfo> nch = new ArrayList<GWikiElementInfo>();
        for (GWikiElementInfo ci : childs) {
            if (matcher.match(ci.getId()) == true) {
                nch.add(ci);
            }
        }
        childs = nch;
    }
    Comparator<GWikiElementInfo> comparator = new GWikiElementByChildOrderComparator(
            new GWikiElementByOrderComparator(new GWikiElementByIntPropComparator("ORDER", 0)));
    if (StringUtils.isNotBlank(sort) == true) {

        if (sort.equals("title") == true) {
            comparator = new GWikiElementByPropComparator("TITLE");
        } else if (sort.equals("date") == true) {
            comparator = new GWikiElementByPropComparator("MODIFIEDAT");
        } else {
            comparator = new GWikiElementByPropComparator(sort);
        }
    }
    if (reverse == true) {
        comparator = new ReverseComparator<GWikiElementInfo>(comparator);
    }
    Collections.sort(childs, comparator);

    ctx.append("<table class=\"gwikiimagegallery\" border=\"1\" cellspacing=\"0\">");
    if (StringUtils.isEmpty(title) == false) {
        ctx.append("<tr><th colspan=\"").append(columns).append("\">").append(ctx.escape(title))
                .append("</th></tr>\n");
    }
    int i = 0;
    int colNo = 0;
    for (GWikiElementInfo c : childs) {
        if (colNo == 0) {
            ctx.append("<tr>");
        }
        ctx.append("<td valign=\"top\">");

        GWikiFragmentImage image = new GWikiFragmentImage(c.getId());
        image.setThumbnail(size);
        image.render(ctx);
        ctx.append("</td>");
        if (colNo + 1 >= columns) {
            ctx.append("</tr>");
            colNo = 0;
        } else {
            ++colNo;
        }
        ++i;
    }
    ctx.append("</tr></table>");
    return true;
}

From source file:net.ontopia.topicmaps.nav2.taglibs.logic.ForEachTag.java

/**
 * Process the start tag for this instance.
 *///  w ww . j  a  v  a2 s  . com
public int doStartTag() throws JspTagException {

    this.contextTag = FrameworkUtils.getContextTag(pageContext);
    this.ctxtMgr = contextTag.getContextManager();

    // get Collection to loop over
    Collection coll = null;
    if (collVariableName != null)
        coll = ctxtMgr.getValue(collVariableName);
    else
        coll = ctxtMgr.getDefaultValue();

    // if not maximum list length set by attribute get from configuration
    if (maxNumber <= 0)
        maxNumber = contextTag.getNavigatorConfiguration().getProperty(NavigatorConfigurationIF.MAX_LIST_LENGTH,
                NavigatorConfigurationIF.DEF_VAL_MAX_LIST_LENGTH);

    // establish new lexical scope for this loop
    ctxtMgr.pushScope();

    // do not proceed if no elements in collection at all
    if (coll.isEmpty())
        return SKIP_BODY;

    // WARN: This might be problematic when collection is of unknown size.
    this.items = coll.toArray();
    // DEFAULT: do *not* sort elements in collection
    if (sortItemsFlag) {
        try {
            listComparator = getComparator();
            // TODO: enhance with more comparators?
            // TODO: Why fallback to another comparator if it is null?
            if (listComparator == null)
                listComparator = DEF_TOPIC_COMPARATOR;

            if (sortOrder != null && sortOrder.equals(DEF_ORDER_DESCENDING))
                listComparator = new ReverseComparator(listComparator);

            Arrays.sort(items, listComparator);
        } catch (Throwable t) {
            log.warn("Sort of List (variable '" + (collVariableName != null ? collVariableName : "_default_")
                    + "') had problems: " + t.getMessage());
        }
    }

    // set first element of collection in beginning
    if (startNumber >= items.length)
        startNumber = items.length - 1;
    index = startNumber;
    setVariableValues(items[index]);

    // proceed
    index = startNumber + 1;
    //log.debug("doStartTag, itemsSize: " + items.length + ", index: " + index);
    //log.debug("  coll: " + coll);
    //log.debug("    first item: " + items[0] + " type: " + items[0].getClass().getName());

    return EVAL_BODY_BUFFERED;
}

From source file:com.sunchenbin.store.feilong.core.util.MapUtil.java

/**
 * Sort by key desc./*from w w w .j a v a2 s  .co m*/
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return the map< k, v>
 * @see ReverseComparator#ReverseComparator(Comparator)
 * @see PropertyComparator#PropertyComparator(String)
 * @since 1.2.0
 */
public static <K, V> Map<K, V> sortByKeyDesc(Map<K, V> map) {
    if (Validator.isNullOrEmpty(map)) {
        throw new NullPointerException("map can't be null/empty!");
    }
    PropertyComparator<Entry<K, V>> propertyComparator = new PropertyComparator<Map.Entry<K, V>>("key");
    Comparator<Entry<K, V>> comparator = new ReverseComparator<Map.Entry<K, V>>(propertyComparator);
    return sort(map, comparator);
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.macros.GWikiChildrenMacro.java

protected void renderChildToc(GWikiElementInfo ei, int level, GWikiContext ctx) {
    if (RenderModes.NoToc.isSet(ctx.getRenderMode()) == true) {
        return;//from  w w w.  ja  v  a 2  s  .c o  m
    }
    if (ctx.getWikiWeb().getAuthorization().isAllowToView(ctx, ei) == false) {
        return;
    }
    if (ei.isNoToc() == true) {
        return;
    }
    boolean allTypes = StringUtils.equals(type, "all");
    List<GWikiElementInfo> cl;
    if (viewAll == false) {
        cl = ctx.getElementFinder().getDirectChilds(ei);
    } else {
        cl = ctx.getElementFinder().getAllDirectChilds(ei);
    }
    if (allTypes == false) {
        List<GWikiElementInfo> ncl = new ArrayList<GWikiElementInfo>();
        for (GWikiElementInfo ci : cl) {
            if (StringUtils.equals(ci.getType(), type) == false) {
                continue;
            }
            if (viewAll == true || ci.isNoToc() == true) {
                continue;
            }
            ncl.add(ci);
        }
        cl = ncl;
    }
    Comparator<GWikiElementInfo> comparator = null;
    if (StringUtils.equalsIgnoreCase(sort, "title") == true) {
        comparator = new GWikiElementByOrderComparator(new GWikiElementByPropComparator("TITLE"));
    } else if (StringUtils.equalsIgnoreCase(sort, "modifiedat") == true) {
        comparator = new GWikiElementByOrderComparator(new GWikiElementByPropComparator("MODIFIEDAT"));
    } else {
        comparator = new GWikiElementByChildOrderComparator(
                new GWikiElementByOrderComparator(new GWikiElementByIntPropComparator("ORDER", 0)));
    }
    if (reverse == true) {
        comparator = new ReverseComparator<GWikiElementInfo>(comparator);
    }
    Collections.sort(cl, comparator);

    if (cl.isEmpty() == true) {
        return;
    }
    String xmlidattr = "";
    if (level == 1) {
        xmlidattr = " id='" + ctx.genHtmlId("childrentoc") + "'";
    }
    ctx.append("\n<ul" + xmlidattr + ">\n");
    for (GWikiElementInfo ci : cl) {
        if (ctx.getWikiWeb().getAuthorization().isAllowToView(ctx, ci) == false) {
            if (viewAll == true && withEditLinks == true) {
                if (ctx.getWikiWeb().getAuthorization().isAllowToEdit(ctx, ci) == false) {
                    continue;
                }
            } else {
                continue;
            }
        }
        renderChild(ci, ctx);

        if (level + 1 > depth) {
            ctx.append("</li>\n"); // close child
            continue;
        }
        renderChildToc(ci, level + 1, ctx);
        ctx.append("</li>\n"); // close child
    }
    ctx.append("\n</ul>\n");
}

From source file:com.sunchenbin.store.feilong.core.util.MapUtil.java

/**
 * ?value ???desc.//from   ww  w  .  j a  v a 2 s  .c  o m
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return the map< k, v>
 * @see ReverseComparator#ReverseComparator(Comparator)
 * @see PropertyComparator#PropertyComparator(String)
 * @see java.util.Map.Entry
 * @see #sortByValueAsc(Map)
 * @since 1.2.0
 */
public static <K, V extends Comparable<V>> Map<K, V> sortByValueDesc(Map<K, V> map) {
    PropertyComparator<Entry<K, V>> propertyComparator = new PropertyComparator<Map.Entry<K, V>>("value");
    Comparator<Entry<K, V>> comparator = new ReverseComparator<Map.Entry<K, V>>(propertyComparator);
    return sort(map, comparator);
}

From source file:com.feilong.core.util.SortUtil.java

/**
 * key desc ??./*w w w .ja v a  2 s  .c o  m*/
 * 
 * <h3>:</h3>
 * <blockquote>
 * 
 * <pre class="code">
 * Map{@code <String, Comparable>} map = new HashMap{@code <String, Comparable>}();
 * 
 * map.put("a", 123);
 * map.put("c", 345);
 * map.put("b", 8);
 * 
 * LOGGER.debug(JsonUtil.format(SortUtil.sortByKeyDesc(map)));
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 * {
 * "c": 345,
 * "b": 8,
 * "a": 123
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return  <code>map</code> null, {@link Collections#emptyMap()}<br>
 * @see ReverseComparator#ReverseComparator(Comparator)
 * @see PropertyComparator#PropertyComparator(String)
 * @see #sort(Map, Comparator)
 * @since 1.8.0 move from MapUtil
 */
public static <K, V> Map<K, V> sortByKeyDesc(Map<K, V> map) {
    if (null == map) {
        return emptyMap();
    }
    return sort(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("key")));
}

From source file:com.feilong.core.util.SortUtil.java

/**
 * ?value ???(desc)./*from   ww w.j av a 2s.  co  m*/
 * 
 * <h3>:</h3>
 * <blockquote>
 * 
 * <pre class="code">
 * Map{@code <String, Comparable>} map = new LinkedHashMap{@code <String, Comparable>}();
 * 
 * map.put("a", 123);
 * map.put("c", 345);
 * map.put("b", 8);
 * 
 * LOGGER.debug(JsonUtil.format(SortUtil.sortByValueDesc(map)));
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 * {
 * "c": 345,
 * "a": 123,
 * "b": 8
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return  <code>map</code> null, {@link Collections#emptyMap()}<br>
 * @see #sort(Map, Comparator)
 * @since 1.8.0 move from MapUtil
 */
public static <K, V extends Comparable<V>> Map<K, V> sortByValueDesc(Map<K, V> map) {
    if (null == map) {
        return emptyMap();
    }
    return sort(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("value")));
}

From source file:com.link_intersystems.lang.reflect.criteria.ClassCriteriaTest.java

@SuppressWarnings("unchecked")
@Test//from   ww  w . j  a  v a2  s  .  co m
public void interfacesComparator() {
    classCriteria.setTraverseStrategy(TraverseStrategy.DEPTH_FIRST);
    classCriteria.setSelection(ClassType.INTERFACES);
    classCriteria.setTraverseClassesUniquely(false);
    classCriteria.add(ReflectFacade.getIsInterfacePredicate());
    classCriteria.setInterfacesIterationOrder(
            new ReverseComparator(ReflectFacade.getCanonicalClassNameComparator()));
    Iterator<Class<?>> iterator = classCriteria.getIterable(ArrayList.class).iterator();
    List<Class<?>> interfaces = IteratorUtils.toList(iterator);

    List<Class<?>> expectedInterfaces = new ArrayList<Class<?>>(Arrays.asList(RandomAccess.class, List.class,
            Collection.class, Iterable.class, Cloneable.class, Serializable.class, List.class, Collection.class,
            Iterable.class, Collection.class, Iterable.class));

    for (int i = 0; i < interfaces.size(); i++) {
        Class<?> interfaceClass = interfaces.get(i);
        Class<?> expectedClass = expectedInterfaces.get(i);
        assertEquals(expectedClass, interfaceClass);
    }

}