View Javadoc
1   package net.bmahe.genetics4j.core.chromosomes;
2   
3   import java.util.Arrays;
4   import java.util.Objects;
5   
6   import org.apache.commons.lang3.Validate;
7   
8   public class DoubleChromosome implements Chromosome {
9   
10  	private final int size;
11  	private final double minValue;
12  	private final double maxValue;
13  	private final double[] values;
14  
15  	public DoubleChromosome(final int _size, final double _minValue, final double _maxValue, final double[] _values) {
16  		Validate.isTrue(_size > 0);
17  		Validate.isTrue(_minValue <= _maxValue);
18  		Validate.notNull(_values);
19  		Validate.isTrue(_size == _values.length, "Provided size does not match the size of the content");
20  
21  		this.size = _size;
22  		this.minValue = _minValue;
23  		this.maxValue = _maxValue;
24  		this.values = Arrays.copyOf(_values, _size);
25  	}
26  
27  	@Override
28  	public int getNumAlleles() {
29  		return size;
30  	}
31  
32  	public double getAllele(final int index) {
33  		Validate.inclusiveBetween(0, size - 1, index);
34  
35  		return values[index];
36  	}
37  
38  	public int getSize() {
39  		return size;
40  	}
41  
42  	public double getMinValue() {
43  		return minValue;
44  	}
45  
46  	public double getMaxValue() {
47  		return maxValue;
48  	}
49  
50  	public double[] getValues() {
51  		return values;
52  	}
53  
54  	@Override
55  	public int hashCode() {
56  		final int prime = 31;
57  		int result = 1;
58  		result = prime * result + Arrays.hashCode(values);
59  		result = prime * result + Objects.hash(maxValue, minValue, size);
60  		return result;
61  	}
62  
63  	@Override
64  	public boolean equals(Object obj) {
65  		if (this == obj)
66  			return true;
67  		if (obj == null)
68  			return false;
69  		if (getClass() != obj.getClass())
70  			return false;
71  		DoubleChromosome other = (DoubleChromosome) obj;
72  		return Double.doubleToLongBits(maxValue) == Double.doubleToLongBits(other.maxValue)
73  				&& Double.doubleToLongBits(minValue) == Double.doubleToLongBits(other.minValue) && size == other.size
74  				&& Arrays.equals(values, other.values);
75  	}
76  
77  	@Override
78  	public String toString() {
79  		return "DoubleChromosome [size=" + size + ", minValue=" + minValue + ", maxValue=" + maxValue + ", values="
80  				+ Arrays.toString(values) + "]";
81  	}
82  }