Java List Move Item moveTop(List list, int[] indices)

Here you can find the source of moveTop(List list, int[] indices)

Description

moves the selected items to the top.

License

Open Source License

Parameter

Parameter Description
indices the indices of the rows to move

Return

the updated indices of the selected rows

Declaration

public static int[] moveTop(List list, int[] indices) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w. jav  a 2 s  . c  o m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * moves the selected items to the top.
     *
     * @param indices   the indices of the rows to move
     * @return      the updated indices of the selected rows
     */
    public static int[] moveTop(List list, int[] indices) {
        int diff;

        if (canMoveUp(list, indices)) {
            diff = indices[0];
            indices = moveItems(list, indices, diff, true);
        }

        return indices;
    }

    /**
     * checks whether the selected items can be moved up.
     *
     * @param indices   the indices of the rows to move
     * @return      true if the selected items can be moved
     */
    public static boolean canMoveUp(List list, int[] indices) {
        boolean result;

        result = false;

        if (indices.length > 0) {
            if (indices[0] > 0)
                result = true;
        }

        return result;
    }

    /**
     * moves the selected items by a certain amount of items in a given direction.
     *
     * @param indices   the indices to move
     * @param moveby      the number of items to move by
     * @param up         if true then items are moved up, otherwise down
     * @return      the updated selected indices
     */
    protected static int[] moveItems(List list, int[] indices, int moveby, boolean up) {
        int i;
        int newIndex;

        if (up) {
            for (i = 0; i < indices.length; i++) {
                if (indices[i] == 0)
                    continue;
                newIndex = indices[i] - moveby;
                swap(list, indices[i], newIndex);
                indices[i] = newIndex;
            }
        } else {
            for (i = indices.length - 1; i >= 0; i--) {
                if (indices[i] == list.size() - 1)
                    continue;
                newIndex = indices[i] + moveby;
                swap(list, indices[i], newIndex);
                indices[i] = newIndex;
            }
        }

        return indices;
    }

    /**
     * Swaps the two rows.
     *
     * @param firstIndex   the index of the first row
     * @param secondIndex   the index of the second row
     */
    protected static void swap(List list, int firstIndex, int secondIndex) {
        Object backup;

        backup = list.get(firstIndex);
        list.set(firstIndex, list.get(secondIndex));
        list.set(secondIndex, backup);
    }
}

Related

  1. moveLeft(int offset, T element, List list)
  2. moveOnePositionUp(final List list, final T object)
  3. moveRight(int offset, T element, List list)
  4. moveRows(final List allRows, final List rowsToMove, final int index)
  5. moveToFront(List aList, Object anObj)
  6. moveUp(final List list, final T toMoveUp)
  7. moveUp(List list, List toMoveUp)
  8. moveUp(List list, int[] selectionIndexes)
  9. moveUpInList(List list, int... indices)

  10. HOME | Copyright © www.java2s.com 2016