Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

import java.util.RandomAccess;

public class Main {
    /**
     * Ensures the given is sortable, i.e. is a List and implements {@link RandomAccess}. If not
     * it will be copied to a collection that is sortable.
     * @param collection
     * @return
     */
    public static <T> List<T> ensureSortable(Collection<T> collection) {

        if (collection == null) {
            return null;
        }

        if (collection instanceof RandomAccess && collection instanceof List
                && !collection.getClass().getName().toLowerCase().contains("unmodifiable")) {
            return (List<T>) collection;
        }

        return new ArrayList<T>(collection);
    }
}