1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.collect;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 
24 /**
25  * Provides static methods for creating {@code List} instances easily, and other
26  * utility methods for working with lists.
27  */
28 @android.ravenwood.annotation.RavenwoodKeepWholeClass
29 public class Lists {
30 
31     /**
32      * Creates an empty {@code ArrayList} instance.
33      *
34      * <p><b>Note:</b> if you only need an <i>immutable</i> empty List, use
35      * {@link Collections#emptyList} instead.
36      *
37      * @return a newly-created, initially-empty {@code ArrayList}
38      */
39     @UnsupportedAppUsage
newArrayList()40     public static <E> ArrayList<E> newArrayList() {
41         return new ArrayList<E>();
42     }
43 
44     /**
45      * Creates a resizable {@code ArrayList} instance containing the given
46      * elements.
47      *
48      * <p><b>Note:</b> due to a bug in javac 1.5.0_06, we cannot support the
49      * following:
50      *
51      * <p>{@code List<Base> list = Lists.newArrayList(sub1, sub2);}
52      *
53      * <p>where {@code sub1} and {@code sub2} are references to subtypes of
54      * {@code Base}, not of {@code Base} itself. To get around this, you must
55      * use:
56      *
57      * <p>{@code List<Base> list = Lists.<Base>newArrayList(sub1, sub2);}
58      *
59      * @param elements the elements that the list should contain, in order
60      * @return a newly-created {@code ArrayList} containing those elements
61      */
62     @UnsupportedAppUsage
newArrayList(E... elements)63     public static <E> ArrayList<E> newArrayList(E... elements) {
64         int capacity = (elements.length * 110) / 100 + 5;
65         ArrayList<E> list = new ArrayList<E>(capacity);
66         Collections.addAll(list, elements);
67         return list;
68     }
69 }
70