Java List Move Item moveLeft(int offset, T element, List list)

Here you can find the source of moveLeft(int offset, T element, List list)

Description

This method returns a new List instance containing the same element objects as the provided list, but with the specified element having been moved left by the specified offset.

License

Open Source License

Parameter

Parameter Description
offset the number of places over which to move the specified element.
element the element to be moved.
list the list to be reordered.

Exception

Parameter Description
NoSuchElementException if the list does not contain an element equal to the one specified.

Return

a new List instance containing the ordered elements.

Declaration

public static <T> List<T> moveLeft(int offset, T element, List<T> list) 

Method Source Code

//package com.java2s;
/*/*from  ww w .j a  va  2  s. c o  m*/
 * Copyright (C) 2005-2014 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

import java.util.NoSuchElementException;

public class Main {
    /**
     * This method returns a new List instance containing the same element objects as the provided
     * list, but with the specified element having been moved left by the specified offset.
     * <p/>
     * If the offset would mean that the element would move beyond the start or end of the list, it will
     * move only to the end.
     * 
     * @param offset the number of places over which to move the specified element.
     * @param element the element to be moved.
     * @param list the list to be reordered.
     * @return a new List instance containing the ordered elements.
     * @throws NoSuchElementException if the list does not contain an element equal to the one specified.
     */
    public static <T> List<T> moveLeft(int offset, T element, List<T> list) {
        return moveRight(-offset, element, list);
    }

    /**
     * This method does the same as {@link #moveLeft(int, Object, List)} but it moves the specified element
     * to the right instead of the left.
     */
    public static <T> List<T> moveRight(int offset, T element, List<T> list) {
        final int elementIndex = list.indexOf(element);

        if (elementIndex == -1) {
            throw new NoSuchElementException("Element not found in provided list.");
        }

        if (offset == 0) {
            return list;
        } else {
            int newElementIndex = elementIndex + offset;

            // Ensure that the element will not move off the end of the list.
            if (newElementIndex >= list.size()) {
                newElementIndex = list.size() - 1;
            } else if (newElementIndex < 0) {
                newElementIndex = 0;
            }

            List<T> result = new ArrayList<>(list);
            result.remove(element);

            result.add(newElementIndex, element);

            return result;
        }
    }
}

Related

  1. moveElementToIndex( List list, ELEMENT fromElement, ELEMENT toElement)
  2. moveInList(List list, int index, T listItem)
  3. moveItem(List list, Object oldItem, int newIndex)
  4. moveItemIdInSecond(final List list)
  5. moveItems(List list, int[] indices, int moveby, boolean up)
  6. moveOnePositionUp(final List list, final T object)
  7. moveRight(int offset, T element, List list)
  8. moveRows(final List allRows, final List rowsToMove, final int index)
  9. moveToFront(List aList, Object anObj)