SimpleLongVec.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.readLongArray;
023 import static io.jenetics.internal.util.SerialIO.writeLongArray;
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 SimpleLongVec implements Vec<long[]>, Serializable {
041 
042     @Serial
043     private static final long serialVersionUID = 2L;
044 
045     private final long[] _data;
046 
047     SimpleLongVec(final long[] data) {
048         Vecs.checkVecLength(data.length);
049         _data = data;
050     }
051 
052     @Override
053     public long[] data() {
054         return _data;
055     }
056 
057     @Override
058     public int length() {
059         return _data.length;
060     }
061 
062     @Override
063     public ElementComparator<long[]> comparator() {
064         return SimpleLongVec::cmp;
065     }
066 
067     private static int cmp(final long[] u, final long[] v, final int i) {
068         return Long.compare(u[i], v[i]);
069     }
070 
071     @Override
072     public ElementDistance<long[]> distance() {
073         return SimpleLongVec::dist;
074     }
075 
076     private static double dist(final long[] u, final long[] v, final int i) {
077         return u[i- v[i];
078     }
079 
080     @Override
081     public Comparator<long[]> 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 SimpleLongVec 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_LONG_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         writeLongArray(_data, out);
121     }
122 
123     static SimpleLongVec read(final DataInput inthrows IOException {
124         return new SimpleLongVec(readLongArray(in));
125     }
126 }