org.jiemamy.utils.collection.ListUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.jiemamy.utils.collection.ListUtil.java

Source

/*
 * Copyright 2007-2012 Jiemamy Project and the Others.
 * Created on 2009/02/10
 *
 * This file is part of Jiemamy.
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.jiemamy.utils.collection;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.Validate;

/**
 * {@link List}?
 * 
 * @version $Id$
 * @author daisuke
 */
public final class ListUtil {

    /**
     * ???index???????
     * 
     * @param list 
     * @param index 
     * @throws IllegalArgumentException ?{@code null}???
     * @throws IllegalArgumentException index????
     * @throws IndexOutOfBoundsException if either index or index + 1 is out of range ((index < 0 || index + 1 >= list.size())).
     */
    public static void moveDown(List<?> list, int index) {
        Validate.notNull(list);
        Validate.isTrue(index >= 0);
        Collections.swap(list, index, index + 1);
    }

    /**
     * ???index????????
     * 
     * @param list 
     * @param index 
     * @throws IllegalArgumentException ?{@code null}???
     * @throws IndexOutOfBoundsException if either index or index - 1 is out of range ((index - 1 < 0 || index >= list.size())).
     */
    public static void moveUp(List<?> list, int index) {
        Validate.notNull(list);
        Collections.swap(list, index, index - 1);
    }

    /**
     * ??????
     * 
     * @param list 
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static void reverse(List<?> list) {
        Validate.notNull(list);
        int center = list.size() / 2;
        int end = list.size() - 1;

        for (int i = 0; i < center; i++) {
            Collections.swap(list, i, end - i);
        }
    }

    private ListUtil() {
    }

}