01 /*
02 * Java Genetic Algorithm Library (jenetics-3.5.0).
03 * Copyright (c) 2007-2016 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.collection;
21
22 import static java.util.Arrays.copyOfRange;
23 import static java.util.Objects.requireNonNull;
24
25 import java.io.Serializable;
26
27 import org.jenetics.internal.collection.Array.Store;
28
29 /**
30 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
31 * @version 3.4
32 * @since 3.4
33 */
34 public final class ObjectStore<T> implements Store<T>, Serializable {
35
36 private static final long serialVersionUID = 1L;
37
38 private final Object[] _array;
39
40 private ObjectStore(final Object[] array) {
41 _array = requireNonNull(array);
42 }
43
44 @Override
45 public void set(final int index, T value) {
46 _array[index] = value;
47 }
48
49 @Override
50 @SuppressWarnings("unchecked")
51 public T get(final int index) {
52 return (T)_array[index];
53 }
54
55 @Override
56 public ObjectStore<T> copy(final int from, final int until) {
57 return new ObjectStore<>(copyOfRange(_array, from, until));
58 }
59
60 @Override
61 public ObjectStore<T> newInstance(final int length) {
62 return ofLength(length);
63 }
64
65 @Override
66 public int length() {
67 return _array.length;
68 }
69
70 public static <T> ObjectStore<T> of(final Object[] array) {
71 return new ObjectStore<>(array);
72 }
73
74 public static <T> ObjectStore<T> ofLength(final int length) {
75 return new ObjectStore<>(new Object[length]);
76 }
77
78 }
|