01 /*
02 * Java Genetic Algorithm Library (jenetics-1.6.0).
03 * Copyright (c) 2007-2014 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 static java.util.Objects.requireNonNull;
23
24
25 /**
26 * {@code ArrayProxy} implementation which stores {@code Object}s.
27 *
28 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
29 * @since 1.4
30 * @version 1.5 — <em>$Date: 2013-12-18 $</em>
31 */
32 public final class ArrayProxyImpl<T> extends ArrayProxy<T> {
33
34 Object[] _array;
35 private boolean _sealed = false;
36
37 /**
38 * Create a new array proxy implementation.
39 *
40 * @param array the array where the elements are stored.
41 * @param start the start index of the array proxy, inclusively.
42 * @param end the end index of the array proxy, exclusively.
43 */
44 public ArrayProxyImpl(final Object[] array, final int start, final int end) {
45 super(start, end);
46 _array = requireNonNull(array, "Object array must not be null.");
47 }
48
49 /**
50 * Create a new array proxy implementation.
51 *
52 * @param length the length of the array proxy.
53 */
54 public ArrayProxyImpl(final int length) {
55 this(new Object[length], 0, length);
56 }
57
58 @SuppressWarnings("unchecked")
59 @Override
60 public T __get(final int absoluteIndex) {
61 return (T)_array[absoluteIndex];
62 }
63
64 @Override
65 public void __set(final int absoluteIndex, final T value) {
66 _array[absoluteIndex] = value;
67 }
68
69 @Override
70 public ArrayProxyImpl<T> slice(final int from, final int until) {
71 return new ArrayProxyImpl<>(_array, from + _start, until + _start);
72 }
73
74 @Override
75 public void cloneIfSealed() {
76 if (_sealed) {
77 _array = _array.clone();
78 _sealed = false;
79 }
80 }
81
82 @Override
83 public ArrayProxyImpl<T> seal() {
84 _sealed = true;
85 return new ArrayProxyImpl<>(_array, _start, _end);
86 }
87
88 @Override
89 public ArrayProxyImpl<T> copy() {
90 final ArrayProxyImpl<T> proxy = new ArrayProxyImpl<>(_length);
91 System.arraycopy(_array, _start, proxy._array, 0, _length);
92 return proxy;
93 }
94
95 }
|