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