lists.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-1.5.0).
03  * Copyright (c) 2007-2013 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.internal.util;
21 
22 import java.util.AbstractCollection;
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.List;
26 
27 import org.jenetics.util.Factory;
28 import org.jenetics.util.StaticObject;
29 
30 /**
31  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
32  @since 1.5
33  @version 1.5 &mdash; <em>$Date: 2013-11-18 $</em>
34  */
35 public final class lists extends StaticObject {
36     private lists() {}
37 
38     public static <T> void fill(
39         final List<? super T> list,
40         final Factory<? extends T> factory,
41         final int size
42     ) {
43         final Object[] array = arrays.fill(new Object[size], factory);
44 
45         // This will tricking the ArrayList.addAll method.
46         list.addAll(new AbstractCollection<T>() {
47             @SuppressWarnings("unchecked")
48             @Override
49             public Iterator<T> iterator() {
50                 return Arrays.asList((T[])array).iterator();
51             }
52             @Override
53             public int size() {
54                 return array.length;
55             }
56             @Override
57             public Object[] toArray() {
58                 return array;
59             }
60         });
61     }
62 }
63 
64 
65 
66