CharStore.java
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.Objects.requireNonNull;
23 
24 import java.io.Serializable;
25 
26 /**
27  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
28  * @since 3.0
29  * @version 3.4
30  */
31 public final class CharStore implements Array.Store<Character>, Serializable {
32     private static final long serialVersionUID = 1L;
33 
34     public final char[] array;
35 
36     private CharStore(final char[] chars) {
37         array = requireNonNull(chars);
38     }
39 
40     public CharStore(final int length) {
41         this(new char[length]);
42     }
43 
44     @Override
45     public Character get(final int index) {
46         return array[index];
47     }
48 
49     @Override
50     public void set(final int index, final Character value) {
51         array[index] = value;
52     }
53 
54     @Override
55     public CharStore copy(final int from, final int until) {
56         final char[] array = new char[until - from];
57         System.arraycopy(this.array, from, array, 0, until - from);
58         return new CharStore(array);
59     }
60 
61     @Override
62     public CharStore newInstance(final int length) {
63         return new CharStore(length);
64     }
65 
66     @Override
67     public int length() {
68         return array.length;
69     }
70 
71     public static CharStore of(final char[] chars) {
72         return new CharStore(chars);
73     }
74 
75 }