Java List Move Item moveOnePositionUp(final List list, final T object)

Here you can find the source of moveOnePositionUp(final List list, final T object)

Description

Move one position up.

License

Open Source License

Parameter

Parameter Description
T the generic type
list the list
object the object

Return

the list

Declaration

public static <T> List<T> moveOnePositionUp(final List<T> list, final T object) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/

import java.util.List;

public class Main {
    /**/*from  ww w . j  a v  a  2s.  c o m*/
     * Move one position up.
     *
     * @param <T> the generic type
     * @param list the list
     * @param object the object
     * @return the list
     */
    public static <T> List<T> moveOnePositionUp(final List<T> list, final T object) {
        return changePosition(list, object, -1);
    }

    /**
     * Change the position of the given object in the provided list for the
     * specified number of positions.
     * 
     * Negative offset means move up, whereas Positive indicates move down.
     *
     * @param <T> the generic type
     * @param list the list
     * @param object the object
     * @param offset the offset
     * @return the list
     */
    public static <T> List<T> changePosition(final List<T> list, final T object, final int offset) {

        final int index = list.indexOf(object);
        if (index >= 0) {
            //found the object
            final int newIndex = index + offset;
            if (offset < 0 && newIndex >= 0 && list.remove(object)) {
                //move up
                list.add(newIndex, object);
            } else if (offset > 0 && newIndex < list.size() && list.remove(object)) {
                //move down
                list.add(newIndex, object);
            }
        }
        return list;
    }

    /**
     * Removes the.
     *
     * @param <T> the generic type
     * @param list the list
     * @param objects the objects
     * @return the list
     */
    public static <T> List<T> remove(final List<T> list, final T... objects) {
        for (final T object : objects)
            list.remove(object);
        return list;
    }

    /**
     * Adds the.
     *
     * @param <T> the generic type
     * @param list the list
     * @param objects the objects
     * @return the list
     */
    public static <T> List<T> add(final List<T> list, final T... objects) {
        if (objects == null)
            return list;
        for (final T object : objects)
            list.add(object);
        return list;
    }
}

Related

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