Example usage for java.util Collections reverse

List of usage examples for java.util Collections reverse

Introduction

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

Prototype

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void reverse(List<?> list) 

Source Link

Document

Reverses the order of the elements in the specified list.

This method runs in linear time.

Usage

From source file:de.blizzy.documentr.page.CherryPicker.java

@Override
public List<String> getCommitsList(String projectName, String branchName, String path, String version1,
        String version2) throws IOException {

    List<PageVersion> pageVersions = Lists
            .newArrayList(pageStore.listPageVersions(projectName, branchName, path));
    boolean foundVersion1 = false;
    boolean foundVersion2 = false;
    for (PageVersion pageVersion : pageVersions) {
        String commit = pageVersion.getCommitName();
        if (!foundVersion1 && commit.equals(version1)) {
            foundVersion1 = true;//from  w  w w. j av a  2  s.c om
        }
        if (!foundVersion2 && commit.equals(version2)) {
            foundVersion2 = true;
        }
        if (foundVersion1 && foundVersion2) {
            break;
        }
    }
    if (!foundVersion1 || !foundVersion2) {
        throw new IllegalArgumentException("one of version1 or version2 not found in version history of page"); //$NON-NLS-1$
    }

    Collections.reverse(pageVersions);
    boolean include = false;
    for (Iterator<PageVersion> iter = pageVersions.iterator(); iter.hasNext();) {
        PageVersion version = iter.next();
        if (!include) {
            iter.remove();
        }

        String commit = version.getCommitName();
        if (commit.equals(version1)) {
            include = true;
        } else if (commit.equals(version2)) {
            include = false;
        }
    }
    Function<PageVersion, String> function = new Function<PageVersion, String>() {
        @Override
        public String apply(PageVersion version) {
            return version.getCommitName();
        }
    };
    return Lists.transform(pageVersions, function);
}

From source file:org.kuali.maven.plugins.externals.MojoHelper.java

protected List<Project> getProjectPath(DefaultMutableTreeNode node) {
    Object[] projectObjects = node.getUserObjectPath();
    List<Project> projects = new ArrayList<Project>();
    for (Object projectObject : projectObjects) {
        projects.add((Project) projectObject);
    }//  w ww  . j  a v  a 2 s .c om
    Collections.reverse(projects);
    return projects;
}

From source file:com.sun.labs.aura.fb.DataManager.java

private void sortByArtistPopularity(List<Scored<Artist>> scoredArtists) {
    Collections.sort(scoredArtists, new ArtistPopularitySorter());
    Collections.reverse(scoredArtists);
}

From source file:ch.ksfx.util.calc.MovingAverageCalculator.java

public static Double calcExponentialMovingAverageObservation(List<Observation> assetPrices, boolean askPrice) {
    Collections.sort(assetPrices, new ObservationDateComparator());
    Collections.reverse(assetPrices);

    //this is just a test
    //System.out.println("-- Test Start");
    //for (AssetPrice ap : assetPrices) {
    //    System.out.println(ap.getPricingTime());
    //}/*from w w  w.  j a va2  s . c o  m*/
    //System.out.println("-- Test End");
    //Test end

    Double average = calculateMovingAverageObservation(assetPrices, true);

    Double k = 2. / (assetPrices.size() + 1.);

    Double ema = 0.0;
    Double emaYesterday = average;

    for (Integer iK = 2; iK <= assetPrices.size(); iK++) {
        ema = Double.parseDouble(assetPrices.get(assetPrices.size() - iK).getScalarValue()) * k
                + emaYesterday * (1. - k);
        emaYesterday = ema;
    }

    return ema;
}

From source file:com.redhat.jenkins.nodesharing.NodeSharingJenkinsRule.java

protected List<ReservationTask> getQueuedReservations() {
    ArrayList<ReservationTask> out = new ArrayList<>();
    for (Queue.Item item : jenkins.getQueue().getItems()) {
        if (item.task instanceof ReservationTask) {
            out.add((ReservationTask) item.task);
        }//from   w  w  w. jav a2  s  .c o m
    }
    Collections.reverse(out);
    return out;
}

From source file:es.alvsanand.webpage.services.cms.CmsServiceImpl.java

public List<Album> getAlbums() throws ServiceException {
    try {//from   w ww .j a  v a 2s  .co  m
        logger.debug("Getting gallery albums");

        List<Album> galleryAlbums = getAlbumsFromCache();
        if (galleryAlbums == null) {
            List<Album> albumEntriesTmp = imageAdminService.getAlbums();

            galleryAlbums = new ArrayList<Album>();
            if (albumEntriesTmp != null) {
                for (Album album : albumEntriesTmp) {
                    if (album.getDescription() != null
                            && album.getDescription().indexOf(Globals.GALLERY_ALBUM_MEDIA_KEYWORD) == 0) {
                        Album galleryAlbum = new Album();

                        galleryAlbum.setId(album.getId());
                        galleryAlbum.setDescription(
                                album.getDescription().substring(Globals.GALLERY_ALBUM_MEDIA_KEYWORD.length()));
                        galleryAlbum.setTitle(album.getTitle());
                        galleryAlbum.setDate(album.getDate());

                        List<Photo> photoEntries = imageAdminService.getPhotos(album);

                        List<Photo> galleryPhotos = new ArrayList<Photo>();
                        for (Photo photo : photoEntries) {
                            Photo galleryPhoto = new Photo();

                            galleryPhoto.setId(photo.getId());
                            galleryPhoto.setDescription(photo.getDescription());
                            galleryPhoto.setTitle(photo.getTitle());
                            galleryPhoto.setThumbnailUrl(photo.getThumbnailUrl());
                            galleryPhoto.setUrl(photo.getUrl());
                            galleryPhoto.setDate(photo.getDate());

                            galleryPhotos.add(galleryPhoto);
                        }

                        Collections.sort(galleryPhotos);
                        Collections.reverse(galleryPhotos);

                        galleryAlbum.setPhotos(galleryPhotos);

                        galleryAlbums.add(galleryAlbum);
                    }
                }

                Collections.sort(galleryAlbums);
                Collections.reverse(galleryAlbums);
            }

            putAlbumsFromCache(galleryAlbums);
        }

        return galleryAlbums;

    } catch (ServiceException e) {
        logger.error("Error getting gallery albums", e);

        throw new ServiceException(e);
    }
}

From source file:userInterface.cdcRole.DecisionChartJPanel.java

private PieDataset createDataset1() {
    ArrayList<hospQty> temp = new ArrayList<>();
    //temp = supplier.getProductCatalog().getProductCatalog();
    //temp = supplier.getProductCatalog().getProductCatalog();
    hospQty sq = null;//from www  .  j av a2s  . com
    Boolean flag = true;
    for (ClinicOrderRequest cor : e.getMasterOrderList()) {
        HospitalOrganization o = null;
        if (cor.getHospOrg() != null) {
            o = cor.getHospOrg();
        } else {
            o = (HospitalOrganization) cor.getSender();
        }
        if (temp.size() == 0) {
            sq = new hospQty();
            sq.setO(o);
            for (OrderItem oi : cor.getOrder().getOrder()) {
                sq.setQ(sq.getQ() + oi.getQuantity());
            }
            temp.add(sq);
        }
        for (hospQty sq1 : temp) {
            if (sq1.getO().equals(o)) {
                for (OrderItem oi : cor.getOrder().getOrder()) {
                    sq1.setQ(sq1.getQ() + oi.getQuantity());
                    flag = false;
                    break;
                }
            }
        }
        if (flag) {
            sq = new hospQty();
            sq.setO(o);
            for (OrderItem oi : cor.getOrder().getOrder()) {
                sq.setQ(sq.getQ() + oi.getQuantity());
            }
            temp.add(sq);
        }
    }

    Collections.sort(temp, new Comparator<hospQty>() {
        public int compare(hospQty one, hospQty other) {
            if (one.getQ() < other.getQ()) {
                return -1;
            }
            if (one.getQ() > other.getQ()) {
                return 1;
            }

            return 0;
        }
    });
    Collections.reverse(temp);
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    //defaultpiedataset.setValue("Java", 10);
    int i = 1;
    for (hospQty p : temp) {
        if (i == 6) {
            break;
        } else {
            defaultpiedataset.setValue(p.getO().getName() + "(" + p.getQ() + ")", p.getQ());
            i++;
        }
    }

    return defaultpiedataset;
}

From source file:com.neocotic.bloggaer.account.dao.ofy.RoleDAOImplTest.java

/**
 * Tests {@link RoleDAOImpl#getAccounts(Role)} can retrieve existing {@link Account Accounts} for any given
 * {@link Role} while handling the optional parameter and sorting correctly.
 *//*from  www. j  ava  2 s.c o  m*/
@Test
public void getAccountsWithRole() {
    // Ensures no Roles with these names already exist.
    String[] names = { "foo_bar", "fu_baz", "bar_foo" };
    for (String name : names)
        assertNull(ofy.find(Key.create(Role.class, name)));

    // Create Roles with the names to be used later.
    List<Role> roles = new ArrayList<Role>();
    for (String name : names)
        roles.add(createRole(name, "Name " + roles.size()));
    Set<Key<Role>> roleKeys = ofy.put(roles).keySet();
    assertNotNull(roleKeys);
    for (String name : names)
        assertTrue(roleKeys.contains(Key.create(Role.class, name)));

    // Ensures no Accounts with these email addresses already exist.
    String[] emails = { "foo@bar.com", "fu@baz.com", "bar@foo.com", "baz@fu.com" };
    for (String email : emails)
        assertNull(ofy.find(Key.create(Account.class, email)));

    /*
     * Creates Accounts with the email addresses to be retrieved later.
     * 
     * The first Account won't be be associated with any Role, the last Account will be associated with the second
     * Role, and the other Accounts will be associated with the first Role.
     * 
     * dateCreated fields will be 1 year apart and List is reversed.
     */
    Date dateCreated = new Date();
    List<Account> accounts1 = new ArrayList<Account>();
    for (String email : emails) {
        int size = accounts1.size();
        Account account = createAccount(email, "Name " + size, Boolean.TRUE);
        if (size == emails.length - 1) {
            account.setRole(roles.get(1));
        } else if (size > 0) {
            account.setRole(roles.get(0));
        }
        dateCreated = DateUtils.addYears(dateCreated, 1);
        account.setDateCreated(dateCreated);
        accounts1.add(account);
    }
    Collections.reverse(accounts1);
    Set<Key<Account>> accountKeys = ofy.put(accounts1).keySet();
    assertNotNull(accountKeys);
    assertEquals(emails.length, accountKeys.size());
    for (String email : emails)
        assertTrue(accountKeys.contains(Key.create(Account.class, email)));

    // Reverses the List again so it's back to normal for testing sort order.
    Collections.reverse(accounts1);

    // Tests associated Accounts are retrieved from the datastore when providing the first Role.
    List<Account> accounts2 = impl.getAccounts(roles.get(0));
    assertNotNull(accounts2);
    assertArrayEquals(accounts1.subList(1, accounts1.size() - 1).toArray(), accounts2.toArray());

    // Tests associated Accounts are retrieved from the datastore when providing the second Role.
    accounts2 = impl.getAccounts(roles.get(1));
    assertNotNull(accounts2);
    assertArrayEquals(accounts1.subList(accounts1.size() - 1, accounts1.size()).toArray(), accounts2.toArray());

    // Tests no Accounts are retrieved from the datastore when providing a known unlinked Role.
    accounts2 = impl.getAccounts(roles.get(2));
    assertNotNull(accounts2);
    assertEquals(0, accounts2.size());

    // Tests an unlinked Account is retrieved from the datastore when providing no Role.
    accounts2 = impl.getAccounts((Role) null);
    assertNotNull(accounts2);
    assertArrayEquals(accounts1.subList(0, 1).toArray(), accounts2.toArray());
}

From source file:net.duckling.ddl.web.api.APIPageCopyController.java

private ResourceFolder parseFolderTree(List<Resource> rs, int rootRid) {
    ResourceFolder result = null;/*from w  w w. ja  va2s. com*/
    if (rs != null) {
        Map<Integer, ResourceFolder> map = new HashMap<Integer, ResourceFolder>();
        Collections.reverse(rs);
        for (Resource r : rs) {
            ResourceFolder rf = new ResourceFolder(r);
            map.put(r.getRid(), rf);
            ResourceFolder parent = map.get(r.getBid());
            if (parent != null) {
                parent.addChildren(rf);
            }
            if (rootRid == r.getRid()) {
                result = rf;
            }
        }
    }
    return result;
}