Example usage for java.util Collections swap

List of usage examples for java.util Collections swap

Introduction

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

Prototype

private static void swap(Object[] arr, int i, int j) 

Source Link

Document

Swaps the two specified elements in the specified array.

Usage

From source file:org.alfresco.rest.api.tests.TestPersonSites.java

@Test
public void testSitesWithSameTitles() throws Exception {
    // Creates 3 sites
    initializeSites();/*from   ww w .j a va  2  s  .  co  m*/

    final String site4_name = "d_" + GUID.generate();
    final String site4_title = site3_title; // Same title as site3
    final SiteRole site4_role = SiteRole.SiteCollaborator;

    TestSite site4 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {
        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site4_name, site4_title, site4_title,
                    SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            site.inviteToSite(person32.getId(), site4_role);
            return site;
        }
    }, person31.getId(), network1.getId());
    assertNotNull(site4);

    // paging
    int totalResults = 4;
    Paging paging = getPaging(null, null, totalResults, null);

    // get memberships
    ListResponse<MemberOfSite> resp = getSiteMembershipsForPerson32(null, null, false);

    // check results
    List<MemberOfSite> expectedList = new LinkedList<>();
    expectedList.add(new MemberOfSite(site2, site2_role));
    expectedList.add(new MemberOfSite(site3, site3_role));
    expectedList.add(new MemberOfSite(site4, site4_role));
    expectedList.add(new MemberOfSite(site1, site1_role));

    try {
        checkList(expectedList, paging.getExpectedPaging(), resp);
    } catch (AssertionError error) {
        // Site3 and Site4 have a same title, and as we are sorting on titles (default sorting),
        // we can't guarantee the order in which the sites will
        // return, hence swap the sites and compare again.
        Collections.swap(expectedList, 1, 2);
        checkList(expectedList, paging.getExpectedPaging(), resp);
    }
}

From source file:hydrograph.ui.propertywindow.widgets.dialogs.join.JoinMapDialog.java

private void moveRowUp() {

    Table table = mappingTableViewer.getTable();
    setValueForCellEditor();// w w  w . j a  v a  2s . c  om
    int[] indexes = table.getSelectionIndices();
    for (int index : indexes) {

        if (index > 0) {
            Collections.swap((List<LookupMapProperty>) mappingTableItemList, index, index - 1);
            mappingTableViewer.refresh();
        }
    }
    refreshButtonStatus();

}

From source file:hydrograph.ui.propertywindow.widgets.dialogs.join.JoinMapDialog.java

private void moveRowDown() {

    Table table = mappingTableViewer.getTable();
    setValueForCellEditor();/*from  w  ww. ja v  a2  s .  c  o  m*/
    int[] indexes = table.getSelectionIndices();
    for (int i = indexes.length - 1; i > -1; i--) {

        if (indexes[i] < mappingTableItemList.size() - 1) {
            Collections.swap((List<LookupMapProperty>) mappingTableItemList, indexes[i], indexes[i] + 1);
            mappingTableViewer.refresh();

        }
    }
    refreshButtonStatus();

}

From source file:org.jts.gui.importJSIDL.Import.java

private static void commitInOrder(List<org.jts.jsidl.binding.ServiceDef> list) {
    if (list.size() == 1) {
        ServiceDef.lookupOrCreate(list.remove(0));
        return;/*from   w ww  . j a  v  a2 s  . c  o  m*/
    }

    while (!list.isEmpty()) {
        boolean restart = false;
        for (int jj = 1; jj < list.size(); jj++) {
            if (compare(list.get(0), list.get(jj)) == 1) {
                Collections.swap(list, 0, jj);
                restart = true;
                break;
            }
        }

        if (!restart) {
            ServiceDef.lookupOrCreate(list.remove(0));
        }
    }
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void moveLookDataDown() {
    Collections.swap(lookDataList, selectedLookPosition + 1, selectedLookPosition);
    adapter.notifyDataSetChanged();
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void moveLookDataUp() {
    Collections.swap(lookDataList, selectedLookPosition - 1, selectedLookPosition);
    adapter.notifyDataSetChanged();
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void moveLookDataToBottom() {
    for (int i = selectedLookPosition; i < lookDataList.size() - 1; i++) {
        Collections.swap(lookDataList, i, i + 1);
    }/*from   ww w  . j  ava 2s.  c o  m*/
    adapter.notifyDataSetChanged();
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void moveLookDataToTop() {
    for (int i = selectedLookPosition; i > 0; i--) {
        Collections.swap(lookDataList, i, i - 1);
    }/*from www  .ja v  a2s.  c o m*/
    adapter.notifyDataSetChanged();
}

From source file:org.eclipse.emf.emfstore.client.test.integration.forward.IntegrationTestHelper.java

/**
 * Changes the index of a value in a multi-valued EAtrribute of this ME.
 * /*from w  w w. j a v a 2 s .c  o m*/
 * @param me ME model element to change
 * @param attribute an attribute with multiple values (isMany = true)
 */
public void moveMultiAttributeValue(EObject me, EAttribute attribute) {
    if (!attribute.isMany()) {
        throw new IllegalArgumentException("Given attribute must be multiple valued (isMany = true)");
    }

    Object object = me.eGet(attribute);
    EList<?> eList = (EList<?>) object;
    int position1 = getRandomPosition(eList.size());
    int position2 = getRandomPosition(eList.size());
    if (position1 == position2) {
        return;
    }
    Collections.swap(eList, position1, position2);

}

From source file:org.eclipse.emf.emfstore.client.test.integration.forward.IntegrationTestHelper.java

/**
 * Changes index of a reference in a many EReference of given ME.
 * /*from   w w  w.  j  a  va 2s  .  co m*/
 * @param me ME
 * @param ref EReference to change
 */
@SuppressWarnings("unchecked")
public void moveMultiReferenceValue(EObject me, EReference ref) {
    if (!ref.isMany()) {
        throw new IllegalArgumentException("Given reference must be multiple valued (isMany = true)");
    }
    Object object = me.eGet(ref);
    EList<EObject> eList = (EList<EObject>) object;
    if (eList == null) {
        throw new IllegalStateException("Null list return for feature " + ref.getName() + " on "
                + ModelUtil.getProject(me).getModelElementId(me).getId());
    } else {
        int position1 = getRandomPosition(eList.size());
        int position2 = getRandomPosition(eList.size());
        if (position1 == position2) {
            return;
        }
        Collections.swap(eList, position1, position2);
    }

}