View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.genetics;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  /**
23   * Implementation of ListChromosome for testing purposes
24   */
25  public class DummyListChromosome extends AbstractListChromosome<Integer> {
26      public DummyListChromosome(final Integer[] representation) {
27          super(representation);
28      }
29  
30      public DummyListChromosome(final List<Integer> representation) {
31          super(representation);
32      }
33  
34      @Override
35      public double fitness() {
36          // Not important.
37          return 0;
38      }
39  
40      @Override
41      protected void checkValidity(final List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
42          // Not important.
43      }
44  
45      @Override
46      public AbstractListChromosome<Integer> newFixedLengthChromosome(final List<Integer> chromosomeRepresentation) {
47          return new DummyListChromosome(chromosomeRepresentation);
48      }
49  
50      @Override
51      public int hashCode() {
52          final int prime = 31;
53          int result = 1;
54          result = prime * result + (getRepresentation() == null ? 0 : getRepresentation().hashCode());
55          return result;
56      }
57  
58      @Override
59      public boolean equals(final Object obj) {
60          if (this == obj) {
61              return true;
62          }
63          if (obj == null) {
64              return false;
65          }
66          if (!(obj instanceof DummyListChromosome)) {
67              return false;
68          }
69          final DummyListChromosome other = (DummyListChromosome) obj;
70          if (getRepresentation() == null) {
71              if (other.getRepresentation() != null) {
72                  return false;
73              }
74          }
75          final Integer[] rep = getRepresentation().toArray(new Integer[getRepresentation().size()]);
76          final Integer[] otherRep = other.getRepresentation().toArray(new Integer[other.getRepresentation().size()]);
77          return Arrays.equals(rep, otherRep);
78      }
79  }