Example usage for java.util Comparator Comparator

List of usage examples for java.util Comparator Comparator

Introduction

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

Prototype

Comparator

Source Link

Usage

From source file:eu.cloud4soa.frontend.commons.server.semanticdao.SoaManager.java

protected <E extends DisplayableKeyValueModelData, S extends Thing> List<E> map(List<S> rdfBeans,
        AbstractMapper<S, E> mapper) {
    List<E> entityModels = new ArrayList<E>();

    for (S rdfBean : rdfBeans)
        if (Strings.isNotEmpty(rdfBean.getUriId()))
            entityModels.add(mapper.from(rdfBean).toModel());

    Collections.sort(entityModels, new Comparator<E>() {
        @Override//from  ww  w  .j av  a2  s .c o m
        public int compare(E model1, E model2) {
            return Strings.defaultString(model1.getDisplayName())
                    .compareTo(Strings.defaultString(model2.getDisplayName()));
        }
    });

    return entityModels;
}

From source file:net.chrissearle.flickrvote.web.stats.ChallengeVsEntriesChartAction.java

public String stats() throws Exception {
    this.challenges = new ArrayList<Challenge>();
    this.imageCounts = new ArrayList<ImageStatistic>();
    this.imageVoteCounts = new ArrayList<ImageStatistic>();

    for (ChallengeSummary challenge : challengeService.getChallengesByType(ChallengeType.CLOSED)) {
        challenges.add(new DisplayChallengeSummary(challenge));

        final ChallengeItem challengeItem = photographyService.getChallengeImages(challenge.getTag());

        imageCounts.add(new ImageStatistic(challenge.getTag(), challengeItem.getDescription(),
                (float) challengeItem.getImages().size()));

        Long voteCount = 0L;//from   ww  w .j av a  2 s.c o m

        for (ImageItem imageItem : challengeItem.getImages()) {
            voteCount += imageItem.getVoteCount();
        }

        imageVoteCounts
                .add(new ImageStatistic(challenge.getTag(), challengeItem.getDescription(), (float) voteCount));
    }

    Collections.sort(this.challenges, new Comparator<Challenge>() {
        public int compare(Challenge o1, Challenge o2) {
            return o2.getChallengeTag().compareTo(o1.getChallengeTag());
        }
    });

    Collections.sort(this.imageCounts, new Comparator<ImageStatistic>() {
        public int compare(ImageStatistic o1, ImageStatistic o2) {
            return o2.getTag().compareTo(o1.getTag());
        }
    });

    Collections.sort(this.imageVoteCounts, new Comparator<ImageStatistic>() {
        public int compare(ImageStatistic o1, ImageStatistic o2) {
            return o2.getTag().compareTo(o1.getTag());
        }
    });

    this.height = (long) (30 * challenges.size()) + 140;

    return SUCCESS;
}

From source file:org.myjerry.evenstar.service.impl.ViewPostServiceImpl.java

@Override
public Map<String, Object> getPostsViewModel(Blog blog, List<BlogPost> posts, boolean fetchComments,
        Map<String, Object> model) {
    Long blogID = blog.getBlogID();

    if (posts.size() > 1) {
        // this collection is sorted chronologically
        // sort in other order
        Collections.sort(posts, new Comparator<BlogPost>() {

            @Override// w  ww  . ja  v  a 2 s .  c om
            public int compare(BlogPost o1, BlogPost o2) {
                if (o1 != null && o2 != null) {
                    return 0 - o1.getPostedDate().compareTo(o2.getPostedDate());
                } else if (o1 == null && o2 == null) {
                    return 0;
                } else if (o1 == null) {
                    return 1;
                }
                // o2 is null
                return -1;
            }

        });
    }

    String dateHeaderFormat = this.blogPreferenceService.getPreference(blogID,
            BlogPreferenceConstants.postDateHeaderFormat);
    String commentTimeStampFormat = this.blogPreferenceService.getPreference(blogID,
            BlogPreferenceConstants.commentTimeStampFormat);

    boolean quickEditing = StringUtils.getBoolean(
            this.blogPreferenceService.getPreference(blogID, BlogPreferenceConstants.quickEditing), true);
    boolean emailLinks = StringUtils.getBoolean(
            this.blogPreferenceService.getPreference(blogID, BlogPreferenceConstants.emailLinks), true);
    boolean commentsAllowed = StringUtils.getBoolean(
            this.blogPreferenceService.getPreference(blogID, BlogPreferenceConstants.showPostComments), true);

    EvenstarUser user = (EvenstarUser) model.get(EvenstarConstants.EVENSTAR_MODEL_ATTRIBUTE_NAME);
    boolean isUserBlogAdmin = false;
    if (user != null) {
        isUserBlogAdmin = this.blogUserService.isUserBlogAdmin(user.getUserID(), blogID);
    }

    Collection<BlogPostInfo> list = new ArrayList<BlogPostInfo>();
    if (posts != null) {
        for (BlogPost post : posts) {
            BlogPostInfo p = getBlogPostInfo(post, dateHeaderFormat, blog.getBlogID(), user,
                    blog.getRestrictedPostText());
            if (fetchComments) {
                if (p.isRestricted()) {
                    // get the comments
                    Collection<Comment> comments = this.commentService
                            .getPublishedCommentsForPost(post.getPostID(), blogID, 1000);
                    p.setComments(getCommentList(comments, blogID, user, commentTimeStampFormat));
                }
            }

            if (!(isUserBlogAdmin && quickEditing)) {
                p.setEditUrl(null);
            }

            if (p.isRestricted() || !emailLinks) {
                p.setEmailPostUrl(null);
            }

            if (p.isRestricted() || !commentsAllowed) {
                p.setAddCommentUrl(null);
            }

            list.add(p);
        }
    }

    BlogArchive archive = this.blogArchiveService.getBlogArchive(blogID);
    Collection<BlogLabel> labels = this.blogLabelService.getBlogLabels(blogID);

    model.put("blog", new BlogInfo(blog));
    model.put("title", blog.getTitle());
    model.put("description", blog.getDescription());
    model.put("posts", list);
    model.put("isBlogAdmin", GAEUserUtil.isCurrentUserHost());
    model.put("archive", archive);
    model.put("labels", labels);
    model.put("commentsAllowed", commentsAllowed);

    return model;
}

From source file:com.linkedin.pinot.core.startree.StarTreeDataSorter.java

/**
 * Sort from to given start (inclusive) to end (exclusive) as per the provided sort order.
 * @param startRecordId inclusive/*w  w  w .  j a v  a 2s. co  m*/
 * @param endRecordId exclusive
 * @param sortOrder
 */
public void sort(int startRecordId, int endRecordId, final int[] sortOrder) {
    int length = endRecordId - startRecordId;
    final int startOffset = startRecordId * totalSizeInBytes;

    List<Integer> idList = new ArrayList<Integer>();
    for (int i = startRecordId; i < endRecordId; i++) {
        idList.add(i - startRecordId);
    }

    Comparator<Integer> comparator = new Comparator<Integer>() {
        byte[] buf1 = new byte[dimensionSizeInBytes];
        byte[] buf2 = new byte[dimensionSizeInBytes];

        @Override
        public int compare(Integer o1, Integer o2) {
            int pos1 = startOffset + (o1) * totalSizeInBytes;
            int pos2 = startOffset + (o2) * totalSizeInBytes;

            mappedByteBuffer.copyTo(pos1, buf1, 0, dimensionSizeInBytes);
            mappedByteBuffer.copyTo(pos2, buf2, 0, dimensionSizeInBytes);

            IntBuffer bb1 = ByteBuffer.wrap(buf1).asIntBuffer();
            IntBuffer bb2 = ByteBuffer.wrap(buf2).asIntBuffer();

            for (int dimIndex : sortOrder) {
                int v1 = bb1.get(dimIndex);
                int v2 = bb2.get(dimIndex);
                if (v1 != v2) {
                    return v1 - v2;
                }
            }
            return 0;
        }
    };

    Collections.sort(idList, comparator);
    int[] currentPositions = new int[length];
    int[] indexToRecordIdMapping = new int[length];

    byte[] buf1 = new byte[totalSizeInBytes];
    byte[] buf2 = new byte[totalSizeInBytes];

    for (int i = 0; i < length; i++) {
        currentPositions[i] = i;
        indexToRecordIdMapping[i] = i;
    }

    for (int i = 0; i < length; i++) {
        int thisRecordId = indexToRecordIdMapping[i];
        int thisRecordIdPos = currentPositions[thisRecordId];

        int thatRecordId = idList.get(i);
        int thatRecordIdPos = currentPositions[thatRecordId];

        // swap the buffers
        mappedByteBuffer.copyTo(startOffset + thisRecordIdPos * totalSizeInBytes, buf1, 0, totalSizeInBytes);
        mappedByteBuffer.copyTo(startOffset + thatRecordIdPos * totalSizeInBytes, buf2, 0, totalSizeInBytes);
        mappedByteBuffer.readFrom(buf2, 0, startOffset + thisRecordIdPos * totalSizeInBytes, totalSizeInBytes);
        mappedByteBuffer.readFrom(buf1, 0, startOffset + thatRecordIdPos * totalSizeInBytes, totalSizeInBytes);

        // update the positions
        indexToRecordIdMapping[i] = thatRecordId;
        indexToRecordIdMapping[thatRecordIdPos] = thisRecordId;

        currentPositions[thatRecordId] = i;
        currentPositions[thisRecordId] = thatRecordIdPos;
    }
}

From source file:com.moscona.dataSpace.Factor.java

public Factor(String name, Set<T> values) throws DataSpaceException {
    init(name);/*from  ww w.  ja  va  2  s .co  m*/
    if (values == null || values.size() == 0) {
        throw new DataSpaceException("The factor has no values!!");
    }
    ArrayList<T> list = new ArrayList<T>(values);
    T first = list.get(0);
    if (first instanceof Comparable) {
        Collections.sort(list, new Comparator<T>() {
            @Override
            @SuppressWarnings({ "unchecked" })
            public int compare(T o1, T o2) {
                return o1.compareTo(o2);
            }
        });
    }
    setValues(list);
    validate();
}

From source file:com.xumpy.timesheets.services.TickedJobsDetailSrv.java

private static List<? extends TickedJobs> sortTickedJobs(List<? extends TickedJobs> tickedJobs) {
    Collections.sort(tickedJobs, new Comparator<TickedJobs>() {
        @Override/*from ww w.j a  va 2 s.com*/
        public int compare(TickedJobs o1, TickedJobs o2) {
            return o1.getTicked().compareTo(o2.getTicked());
        }
    });

    return tickedJobs;
}

From source file:ch.cyberduck.core.HistoryCollection.java

/**
 * Sort by timestamp of bookmark file./*from  w w w  .  ja  va 2 s.  c  om*/
 */
@Override
protected synchronized void sort() {
    Collections.sort(this, new Comparator<Host>() {
        @Override
        public int compare(Host o1, Host o2) {
            if (null == o1.getTimestamp() && null == o2.getTimestamp()) {
                return 0;
            }
            if (null == o1.getTimestamp()) {
                return 1;
            }
            if (null == o2.getTimestamp()) {
                return -1;
            }
            return -o1.getTimestamp().compareTo(o2.getTimestamp());
        }
    });
}

From source file:com.sixrr.metrics.ui.charts.PieChartDialog.java

private PieDataset createDataset() {
    final List<Pair<String, Double>> namedValues = new ArrayList<Pair<String, Double>>();
    double total = 0.0;
    for (int j = 0; j < values.length; j++) {
        final Double value = values[j];
        final String measuredItem = measuredItems[j];
        if (value != null && value != 0.0) {
            namedValues.add(new Pair<String, Double>(measuredItem, value));
            total += value;/*ww  w .  j  a  va 2  s .com*/
        }
    }
    Collections.sort(namedValues, new Comparator<Pair<String, Double>>() {
        @Override
        public int compare(Pair<String, Double> pair1, Pair<String, Double> pair2) {
            final Double value1 = pair1.getSecond();
            final Double value2 = pair2.getSecond();
            return -value1.compareTo(value2);
        }
    });
    final DefaultPieDataset dataset = new DefaultPieDataset();

    double totalForOther = 0.0;
    for (final Pair<String, Double> namedValue : namedValues) {
        final double value = namedValue.getSecond();
        if (value > total * SMALLEST_PIE_PIECE) {
            dataset.setValue(namedValue.getFirst(), value);
        } else {
            totalForOther += value;
        }
    }
    if (totalForOther != 0.0) {
        dataset.setValue(MetricsReloadedBundle.message("other"), totalForOther);
    }
    return dataset;
}

From source file:edu.uiowa.icts.bluebutton.json.view.StatsFinder.java

public Double getMin() {
    List<Double> dList = this.getDoubleList();
    Double min = Collections.min(dList, new Comparator<Double>() {
        public int compare(Double d, Double d2) {
            if (d > d2) {
                return 1;
            } else if (d < d2) {
                return -1;
            }// ww w  . j  a v  a  2  s .  c o m
            return 0;
        }
    });
    return min;
}

From source file:com.clustercontrol.infra.factory.SelectInfraManagement.java

public List<InfraManagementInfo> getList() throws InvalidRole, HinemosUnknown {
    List<InfraManagementInfo> list = QueryUtil.getAllInfraManagementInfo();
    for (InfraManagementInfo info : list) {
        // ?/*from  w  ww .  j av  a 2  s  .co  m*/
        info.setNotifyRelationList(new NotifyControllerBean().getNotifyRelation(info.getNotifyGroupId()));

        // FIXME:
        Collections.sort(info.getModuleList(), new Comparator<InfraModuleInfo<?>>() {
            @Override
            public int compare(InfraModuleInfo<?> o1, InfraModuleInfo<?> o2) {
                return o1.getOrderNo().compareTo(o2.getOrderNo());
            }
        });
    }
    return list;
}