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:edu.chalmers.dat255.audiobookplayer.model.Book.java

public void swapTracks(int firstIndex, int secondIndex) {
    checkTrackIndexLegal(firstIndex);//from w  w w .  ja v a2s  .  co m
    checkTrackIndexLegal(secondIndex);

    Collections.swap(tracks, firstIndex, secondIndex);
}

From source file:com.formkiq.core.service.workflow.WorkflowEditorServiceImpl.java

/**
 * Move Step Down 1./*  w  ww.  j  av  a  2s  .com*/
 * @param flow {@link WebFlow}
 * @param fieldids String[]
 * @param minimum int
 * @param steps boolean whether using steps or printsteps
 */
private void actionStep(final WebFlow flow, final String[] fieldids, final int minimum, final boolean steps) {

    Object obj = flow.getCurrentState().getData();
    if (!(obj instanceof Workflow)) {
        return;
    }

    Workflow workflow = (Workflow) obj;

    List<String> list = steps ? workflow.getSteps() : workflow.getPrintsteps();

    for (String s : fieldids) {

        int pos = NumberUtils.toInt(s, -1);

        try {
            Collections.swap(list, pos, pos + minimum * -1);
        } catch (IndexOutOfBoundsException e) {
            // ignore..
        }
    }
}

From source file:com.rks.musicx.ui.adapters.QueueAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < 0 || fromPosition >= data.size() || toPosition < 0 || toPosition >= data.size()) {
        return false;
    }/*from  ww w  .  j a  v a2  s.  co  m*/
    Collections.swap(data, fromPosition, toPosition);

    if (pos == fromPosition) {
        pos = toPosition;
    } else if (pos == toPosition) {
        pos = fromPosition;
    }
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

From source file:com.predic8.membrane.core.RuleManager.java

public synchronized void ruleUp(Rule rule) {
    int index = rules.indexOf(rule);
    if (index <= 0)
        return;/* w w  w  . j ava 2 s.c om*/
    Collections.swap(rules, index, index - 1);
    Collections.swap(ruleSources, index, index - 1);
    for (IRuleChangeListener listener : listeners) {
        listener.rulePositionsChanged();
    }
}

From source file:liam.franco.selene.fragments.SuperSheetsFragment.java

@Subscribe(queue = "note_updated")
public void noteUpdated(NoteUpdatingEvent noteUpdatingEvent) {
    boolean pastAdd = getTabTitle().equals(MainActivity.PAST) && noteUpdatingEvent.getNote().getArchive();
    boolean futureAdd = getTabTitle().equals(MainActivity.FUTURE) && noteUpdatingEvent.getNote().getReminder();
    boolean presentAdd = getTabTitle().equals(MainActivity.PRESENT)
            && (!noteUpdatingEvent.getNote().getReminder() && !noteUpdatingEvent.getNote().getArchive());

    boolean exists = false;
    for (int i = 0; i < getAdapter().getAdapterItems().size(); i++) {
        exists = noteUpdatingEvent.getNote().getUid() == getAdapter().getAdapterItem(i).getViewHolder()
                .getNote().getUid();/*from   w ww .ja v a 2  s  .c o  m*/

        if (exists) {
            break;
        }
    }

    if (!exists) {
        if (pastAdd || futureAdd || presentAdd) {
            addNote(noteUpdatingEvent.getNote());
        }
    } else {
        if (!pastAdd && !futureAdd && !presentAdd) {
            App.BUS.unregister(
                    getAdapter().getAdapterItem(noteUpdatingEvent.getLayoutPosition()).getViewHolder());
            getAdapter().remove(noteUpdatingEvent.getLayoutPosition());
        } else {
            Collections.swap(getAdapter().getAdapterItems(), noteUpdatingEvent.getLayoutPosition(), 0);
            getAdapter().notifyAdapterItemMoved(noteUpdatingEvent.getLayoutPosition(), 0);
        }
    }

    setEmptyView();
}

From source file:org.mars3142.android.toaster.fragment.NavigationDrawerFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    switch (loader.getId()) {
    case DATA_LOADER:
        mNavList = new ArrayList<ToastCard>();

        ToastCard emptyCard = new ToastCard(getActionBar().getThemedContext());
        emptyCard.appName = getString(R.string.all_data);
        mNavList.add(emptyCard);/*www  .  j  av a 2  s .c o m*/

        if (data.moveToFirst()) {
            do {
                ToastCard packageCard = new ToastCard(getActionBar().getThemedContext());
                packageCard.packageName = data.getString(data.getColumnIndex(ToasterTable.PACKAGE));
                if (packageCard.packageName != null) {
                    packageCard.loadData();
                    mNavList.add(packageCard);
                }
            } while (data.moveToNext());
        }
        Collections.sort(mNavList, new ToastCardComparator());
        Collections.swap(mNavList, mNavList.indexOf(emptyCard), 0);

        mDrawerListView.setAdapter(new ToastArrayAdapter(getActionBar().getThemedContext(), mNavList));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        break;

    default:
        throw new IllegalArgumentException();
    }
}

From source file:com.predic8.membrane.core.RuleManager.java

public synchronized void ruleDown(Rule rule) {
    int index = rules.indexOf(rule);
    if (index < 0 || index == (rules.size() - 1))
        return;//from www.  j a  v  a2 s  .c  o  m
    Collections.swap(rules, index, index + 1);
    Collections.swap(ruleSources, index, index + 1);
    for (IRuleChangeListener listener : listeners) {
        listener.rulePositionsChanged();
    }
}

From source file:org.openmrs.web.controller.person.PersonAttributeTypeListController.java

/**
 * Moves the selected types up one in the order
 * //  w  w  w .  j  av  a  2s .  c  o  m
 * @param personAttributeTypeId list of ids to move up
 * @param httpSession the current session
 * @should move selected ids up one in the list
 * @should not fail if given first id
 * @should not fail if not given any ids
 */
@RequestMapping(method = RequestMethod.POST, params = "action=moveup")
public String moveUp(Integer[] personAttributeTypeId, HttpSession httpSession) {
    if (personAttributeTypeId == null) {
        httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "PersonAttributeType.select");
    } else {
        PersonService ps = Context.getPersonService();

        List<PersonAttributeType> attributes = ps.getAllPersonAttributeTypes();

        Set<PersonAttributeType> attributesToSave = new HashSet<PersonAttributeType>();

        List<Integer> selectedIds = Arrays.asList(personAttributeTypeId);

        // assumes attributes are returned in sortWeight order

        for (int i = 1; i < attributes.size(); i++) {
            PersonAttributeType current = attributes.get(i);
            if (selectedIds.contains(current.getPersonAttributeTypeId())) {
                PersonAttributeType above = attributes.get(i - 1);

                // swap current and the attribute above it
                double temp = current.getSortWeight();
                current.setSortWeight(above.getSortWeight());
                above.setSortWeight(temp);
                Collections.swap(attributes, i, i - 1); // move the actual elements in the list as well

                attributesToSave.add(current);
                attributesToSave.add(above);
            }
        }

        // now save things
        for (PersonAttributeType pat : attributesToSave) {
            ps.savePersonAttributeType(pat);
        }
    }

    return "redirect:/admin/person/personAttributeType.list";
}

From source file:edu.utexas.cs.tactex.utils.BrokerUtils.java

/**
 * @param list//from   w  w w . j  a  v  a2  s.  com
 * @param value
 */
public static <T> void insertToSortedArrayList(ArrayList<T> list, T value) {
    list.add(value);
    @SuppressWarnings("unchecked")
    Comparable<T> cmp = (Comparable<T>) value;
    for (int i = list.size() - 1; i > 0 && cmp.compareTo(list.get(i - 1)) < 0; i--)
        Collections.swap(list, i, i - 1);
}

From source file:io.syng.adapter.ProfileDrawerAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    int realFromPosition = fromPosition - 1;//-1 because of the header
    int realToPosition = toPosition - 1;

    Collections.swap(mDataSet, realFromPosition, realToPosition);
    notifyItemMoved(fromPosition, toPosition);
    ProfileManager.reorderProfiles(realFromPosition, realToPosition);
    return true;/*from  w w w .jav a  2 s .c o m*/
}