001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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.Arrays.copyOfRange; 023import static java.util.Objects.requireNonNull; 024import static io.jenetics.internal.util.SerialIO.readObjectArray; 025import static io.jenetics.internal.util.SerialIO.writeObjectArray; 026 027import java.io.IOException; 028import java.io.InvalidObjectException; 029import java.io.ObjectInput; 030import java.io.ObjectInputStream; 031import java.io.ObjectOutput; 032import java.io.Serial; 033import java.io.Serializable; 034import java.util.Arrays; 035import java.util.Comparator; 036 037import io.jenetics.internal.collection.Array.Store; 038 039/** 040 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 041 * @version 6.0 042 * @since 6.0 043 */ 044public final class ObjectStore<T> implements Store<T>, Serializable { 045 046 @Serial 047 private static final long serialVersionUID = 2L; 048 049 private final Object[] _array; 050 051 private ObjectStore(final Object[] array) { 052 _array = requireNonNull(array); 053 } 054 055 @Override 056 public void set(final int index, T value) { 057 _array[index] = value; 058 } 059 060 @Override 061 @SuppressWarnings("unchecked") 062 public T get(final int index) { 063 return (T)_array[index]; 064 } 065 066 @Override 067 @SuppressWarnings("unchecked") 068 public void sort( 069 final int from, 070 final int until, 071 final Comparator<? super T> comparator 072 ) { 073 Arrays.sort((T[])_array, from, until, comparator); 074 } 075 076 @Override 077 public ObjectStore<T> copy(final int from, final int until) { 078 return new ObjectStore<>(copyOfRange(_array, from, until)); 079 } 080 081 @Override 082 public ObjectStore<T> newInstance(final int length) { 083 return ofLength(length); 084 } 085 086 @Override 087 public int length() { 088 return _array.length; 089 } 090 091 public static <T> ObjectStore<T> of(final Object[] array) { 092 return new ObjectStore<>(array); 093 } 094 095 public static <T> ObjectStore<T> ofLength(final int length) { 096 return new ObjectStore<>(new Object[length]); 097 } 098 099 100 /* ************************************************************************* 101 * Java object serialization 102 * ************************************************************************/ 103 104 @Serial 105 private Object writeReplace() { 106 return new SerialProxy(SerialProxy.OBJECT_STORE, this); 107 } 108 109 @Serial 110 private void readObject(final ObjectInputStream stream) 111 throws InvalidObjectException 112 { 113 throw new InvalidObjectException("Serialization proxy required."); 114 } 115 116 void write(final ObjectOutput out) throws IOException { 117 writeObjectArray(_array, out); 118 } 119 120 @SuppressWarnings("rawtypes") 121 static Object read(final ObjectInput in) 122 throws IOException, ClassNotFoundException 123 { 124 return new ObjectStore(readObjectArray(in)); 125 } 126 127}