Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 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;
        }
    }
}