Here you can find the source of moveOnePositionUp(final List
Parameter | Description |
---|---|
T | the generic type |
list | the list |
object | the object |
public static <T> List<T> moveOnePositionUp(final List<T> list, final T object)
//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; } }