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:ayushi.view.adapter.ShoppingListAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {

    Collections.swap(productList, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    return true;/*from   w w  w  .  j av  a  2  s  . c  o m*/
}

From source file:br.com.hslife.orcamento.controller.RelatorioCustomizadoController.java

public void subirNivel() {
    // Adiciona o contedo do Set de colunas na lista temporria
    List<RelatorioColuna> listaColunas = new LinkedList<RelatorioColuna>();
    listaColunas.addAll(entity.getColunasRelatorio());

    // Variveis auxiliares para guardar do ndice
    int indiceAtual = 0;
    int indiceAnterior = 0;

    // Varre a lista a procura do elemento selecionado
    for (int i = 0; i < listaColunas.size(); i++) {
        if (listaColunas.get(i).equals(colunaRelatorioTemp)) {
            indiceAtual = i;/*ww w .j a  v  a2 s. c  o  m*/
            indiceAnterior = indiceAtual - 1;
            break;
        }
    }

    // Realiza o swap dos elementos da lista caso indice anterior ser
    // igual ou maior que zero
    if (indiceAnterior >= 0) {
        Collections.swap(listaColunas, indiceAtual, indiceAnterior);
    }

    // Limpa o Set da entidade e adiciona o contedo da lista
    entity.getColunasRelatorio().clear();
    entity.getColunasRelatorio().addAll(listaColunas);

    // Renumera a numerao da ordem das colunas
    int i = 1;
    for (RelatorioColuna coluna : entity.getColunasRelatorio()) {
        coluna.setOrdem(i);
        i++;
    }
}

From source file:com.gm.grecyclerview.SimpleAdapter.java

@Override
public void onItemMoved(int fromPosition, int toPosition) {
    Collections.swap(cells, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
}

From source file:br.com.hslife.orcamento.controller.RelatorioCustomizadoController.java

public void descerNivel() {
    // Adiciona o contedo do Set de colunas na lista temporria
    List<RelatorioColuna> listaColunas = new LinkedList<RelatorioColuna>();
    listaColunas.addAll(entity.getColunasRelatorio());

    // Variveis auxiliares para guardar do ndice
    int indiceAtual = 0;
    int indicePosterior = 0;

    // Varre a lista a procura do elemento selecionado
    for (int i = 0; i < listaColunas.size(); i++) {
        if (listaColunas.get(i).equals(colunaRelatorioTemp)) {
            indiceAtual = i;// ww  w  .j  a  va2  s  .  c  o m
            indicePosterior = indiceAtual + 1;
            break;
        }
    }

    // Realiza o swap dos elementos da lista caso indice anterior ser
    // igual ou maior que zero
    if (indicePosterior < listaColunas.size()) {
        Collections.swap(listaColunas, indiceAtual, indicePosterior);
    }

    // Limpa o Set da entidade e adiciona o contedo da lista
    entity.getColunasRelatorio().clear();
    entity.getColunasRelatorio().addAll(listaColunas);

    // Renumera a numerao da ordem das colunas
    int i = 1;
    for (RelatorioColuna coluna : entity.getColunasRelatorio()) {
        coluna.setOrdem(i);
        i++;
    }
}

From source file:org.gbif.ipt.model.Resource.java

/**
 * Delete a Resource's mapping. If the mapping gets successfully deleted, and the mapping is a core type mapping,
 * and there are no additional core type mappings, all other mappings are also cleared.
 *
 * @param mapping ExtensionMapping//from  ww  w  . jav  a 2s.co  m
 *
 * @return if deletion was successful or not
 */
public boolean deleteMapping(ExtensionMapping mapping) {
    if (mapping != null && mappings.contains(mapping)) {
        // what's the core row type?
        String coreRowType = getCoreRowType();
        // is this the resource's core mapping?
        if (coreRowType != null && coreRowType.equalsIgnoreCase(mapping.getExtension().getRowType())) {
            // are there multiple core mappings?
            List<ExtensionMapping> coreMappings = getMappings(coreRowType);
            if (coreMappings.size() > 1) {
                // if it's the first mapping in the list, swap it with next mapping to retain coreType
                if (mappings.indexOf(mapping) == 0) {
                    ExtensionMapping next = coreMappings.get(1);
                    int nextIndex = mappings.indexOf(next);
                    log.debug("Swapping first core mapping with next core mapping with index#"
                            + String.valueOf(nextIndex));
                    Collections.swap(mappings, 0, nextIndex);
                }
                log.debug("Deleting core mapping...");
                return mappings.remove(mapping);
            }
            // if this was the only core mapping, delete all mappings
            else {
                log.debug("Deleting only core mapping and thus clearing all mappings...");
                mappings.clear();
                return true;
            }
        } else {
            log.debug("Deleting non-core mapping...");
            return mappings.remove(mapping);
        }
    }
    log.debug("Mapping was null, or resource no longer has this mapping, thus it could not be deleted!");
    return false;
}

From source file:main.Content.java

private void Rendit() {
    for (int i = 1; i < data.size(); i++) {
        for (int j = i; j > 0; j--) {
            if (data.get(j).getParentValue() == data.get(j - 1).getParentValue()
                    && data.get(j).getParentValue() != 0) {
                if (data.get(j - 1).getID() > data.get(j).getID()) {
                    Collections.swap(data, j, j - 1);
                }/*w w w  .java2  s .com*/
            } else if (Double.parseDouble(data.get(j - 1).getIdString()) > Double
                    .parseDouble(data.get(j).getIdString())) {
                Collections.swap(data, j, j - 1);
            }
        }
        System.out.println("Renditja: " + data.get(i).getIdString());
    }
}

From source file:org.theospi.portfolio.presentation.tool.DecoratedPage.java

public String moveUp() {
    if (getBase().getSequence() != 0) {
        Collections.swap(getParent().getPresentation().getPages(), getBase().getSequence(),
                getBase().getSequence() - 1);
        getParent().reorderPages();//from w ww. j a  v  a2  s.  com
    }
    return null;
}

From source file:org.theospi.portfolio.presentation.tool.DecoratedPage.java

public String moveDown() {
    if (getBase().getSequence() < getParent().getPresentation().getPages().size() - 1) {
        Collections.swap(getParent().getPresentation().getPages(), getBase().getSequence(),
                getBase().getSequence() + 1);
        getParent().reorderPages();/*from  ww  w. j a  va2s.c om*/
    }
    return null;
}

From source file:org.eclipse.wb.internal.core.databinding.ui.editor.contentproviders.ChooseClassAndPropertiesUiContentProvider.java

protected final void handleMoveElement(boolean up, StructuredViewer viewer, List<?> properties) {
    // prepare move indexes
    IStructuredSelection selection = UiUtils.getSelection(viewer);
    int elementIndex = properties.indexOf(selection.getFirstElement());
    int newElementIndex = up ? elementIndex - 1 : elementIndex + 1;
    // move elements
    Collections.swap(properties, elementIndex, newElementIndex);
    // update viewer
    viewer.refresh();//from w  ww . j  a v  a2  s.  c o m
    calculateUpDowButtons();
}

From source file:org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage.java

private void swapUserNameWithAnonymousInTabList() {
    if (compositeContainer != null && anonymousButton != null && repositoryUserNameEditor != null) {
        List<Control> tabList = Arrays.asList(compositeContainer.getTabList());
        if (tabList.contains(repositoryUserNameEditor.getTextControl(compositeContainer))
                && tabList.contains(anonymousButton)) {
            int userNameIndex = tabList.indexOf(repositoryUserNameEditor.getTextControl(compositeContainer));
            int anonymousIndex = tabList.indexOf(anonymousButton);
            Collections.swap(tabList, userNameIndex, anonymousIndex);
            compositeContainer.setTabList(tabList.toArray(new Control[tabList.size()]));
        }//from   ww  w.  j av a  2s .co m
    }
}