001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.collection;
021
022import static java.util.Objects.requireNonNull;
023import static io.jenetics.internal.util.SerialIO.readCharArray;
024import static io.jenetics.internal.util.SerialIO.writeCharArray;
025
026import java.io.IOException;
027import java.io.InvalidObjectException;
028import java.io.ObjectInput;
029import java.io.ObjectInputStream;
030import java.io.ObjectOutput;
031import java.io.Serial;
032import java.io.Serializable;
033import java.util.Arrays;
034import java.util.Comparator;
035
036/**
037 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
038 * @since 3.0
039 * @version 6.0
040 */
041public final class CharStore implements Array.Store<Character>, Serializable {
042
043        @Serial
044        private static final long serialVersionUID = 1L;
045
046        public final char[] array;
047
048        private CharStore(final char[] chars) {
049                array = requireNonNull(chars);
050        }
051
052        public CharStore(final int length) {
053                this(new char[length]);
054        }
055
056        @Override
057        public Character get(final int index) {
058                return array[index];
059        }
060
061        @Override
062        public void sort(
063                final int from,
064                final int until,
065                final Comparator<? super Character> comparator
066        ) {
067                if (comparator == null) {
068                        Arrays.sort(array, from, until);
069                } else {
070                        final Character[] chars = new Character[until - from];
071                        for (int i = from; i < until; ++i) {
072                                chars[i - from] = array[i];
073                        }
074                        Arrays.sort(chars, comparator);
075                        for (int i = from; i < until; ++i) {
076                                array[i] = chars[i - from];
077                        }
078                }
079        }
080
081        @Override
082        public void set(final int index, final Character value) {
083                array[index] = value;
084        }
085
086        @Override
087        public CharStore copy(final int from, final int until) {
088                final char[] array = new char[until - from];
089                System.arraycopy(this.array, from, array, 0, until - from);
090                return new CharStore(array);
091        }
092
093        @Override
094        public CharStore newInstance(final int length) {
095                return new CharStore(length);
096        }
097
098        @Override
099        public int length() {
100                return array.length;
101        }
102
103        public static CharStore of(final char[] chars) {
104                return new CharStore(chars);
105        }
106
107
108        /* *************************************************************************
109         *  Java object serialization
110         * ************************************************************************/
111
112        @Serial
113        private Object writeReplace() {
114                return new SerialProxy(SerialProxy.CHAR_STORE, this);
115        }
116
117        @Serial
118        private void readObject(final ObjectInputStream stream)
119                throws InvalidObjectException
120        {
121                throw new InvalidObjectException("Serialization proxy required.");
122        }
123
124        void write(final ObjectOutput out) throws IOException {
125                writeCharArray(array, out);
126        }
127
128        static CharStore read(final ObjectInput in) throws IOException {
129                return new CharStore(readCharArray(in));
130        }
131
132}