SimpleDoubleVec.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-7.1.2).
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  */
020 package io.jenetics.ext.moea;
021 
022 import static io.jenetics.internal.util.SerialIO.readDoubleArray;
023 import static io.jenetics.internal.util.SerialIO.writeDoubleArray;
024 
025 import java.io.DataInput;
026 import java.io.DataOutput;
027 import java.io.IOException;
028 import java.io.InvalidObjectException;
029 import java.io.ObjectInputStream;
030 import java.io.Serial;
031 import java.io.Serializable;
032 import java.util.Arrays;
033 import java.util.Comparator;
034 
035 /**
036  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
037  @version 5.2
038  @since 5.2
039  */
040 final class SimpleDoubleVec implements Vec<double[]>, Serializable {
041 
042     @Serial
043     private static final long serialVersionUID = 2L;
044 
045     private final double[] _data;
046 
047     SimpleDoubleVec(final double[] data) {
048         Vecs.checkVecLength(data.length);
049         _data = data;
050     }
051 
052     @Override
053     public double[] data() {
054         return _data;
055     }
056 
057     @Override
058     public int length() {
059         return _data.length;
060     }
061 
062     @Override
063     public ElementComparator<double[]> comparator() {
064         return SimpleDoubleVec::cmp;
065     }
066 
067     private static int cmp(final double[] u, final double[] v, final int i) {
068         return Double.compare(u[i], v[i]);
069     }
070 
071     @Override
072     public ElementDistance<double[]> distance() {
073         return SimpleDoubleVec::dist;
074     }
075 
076     private static double dist(final double[] u, final double[] v, final int i) {
077         return u[i- v[i];
078     }
079 
080     @Override
081     public Comparator<double[]> dominance() {
082         return Pareto::dominance;
083     }
084 
085     @Override
086     public int hashCode() {
087         return Arrays.hashCode(_data);
088     }
089 
090     @Override
091     public boolean equals(final Object obj) {
092         return obj == this ||
093             obj instanceof SimpleDoubleVec other &&
094             Arrays.equals(other._data, _data);
095     }
096 
097     @Override
098     public String toString() {
099         return Arrays.toString(_data);
100     }
101 
102 
103     /* *************************************************************************
104      *  Java object serialization
105      * ************************************************************************/
106 
107     @Serial
108     private Object writeReplace() {
109         return new SerialProxy(SerialProxy.SIMPLE_DOUBLE_VEC, this);
110     }
111 
112     @Serial
113     private void readObject(final ObjectInputStream stream)
114         throws InvalidObjectException
115     {
116         throw new InvalidObjectException("Serialization proxy required.");
117     }
118 
119     void write(final DataOutput outthrows IOException {
120         writeDoubleArray(_data,out);
121     }
122 
123     static SimpleDoubleVec read(final DataInput inthrows IOException {
124         return new SimpleDoubleVec(readDoubleArray(in));
125     }
126 }