001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer;
017
018 import java.io.Serializable;
019
020 /**
021 * Represents an immutable result of an evaluation.
022 * <p>
023 * <i>Note: a instance is considered immutable, so you <b>should not modify</b>
024 * the contained individual. See {@link #getIndividual()} for additional
025 * hints.</i>
026 *
027 * @param <I>
028 * the individuals' type
029 *
030 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
031 */
032 public final class EvaluationResult<I> implements
033 Comparable<EvaluationResult<I>>, Serializable {
034 private static final long serialVersionUID = 1L;
035
036 private final I individual;
037 private final double evaluation;
038
039 /**
040 * Creates a new {@link EvaluationResult} with the given
041 * {@code individual} and its {@code evaluation}.
042 *
043 * @throws NullPointerException
044 * if {@code individual} is <code>null</code>
045 */
046 public EvaluationResult(final I individual, final double evaluation) {
047 if (null == individual) {
048 throw new NullPointerException("individual may not be null");
049 }
050
051 this.individual = individual;
052 this.evaluation = evaluation;
053 }
054
055 /**
056 * The calculated evaluation for the individual.
057 *
058 * @return the calculated evaluation-value (fitness)
059 */
060 public double getEvaluation() {
061 return evaluation;
062 }
063
064 /**
065 * Return the individual for which the evaluation was calculated.
066 * <p>
067 * <i>Note: An instance of {@link EvaluationResult} is considered to be
068 * immutable, but for performance reasons the contained individual is not
069 * copied. So <b>don't modify</b> the returned individual.</i>
070 *
071 * @return the individual; cannot be <code>null</code>
072 */
073 public I getIndividual() {
074 assert null != individual;
075
076 return individual;
077 }
078
079 @Override
080 public int hashCode() {
081 final int prime = 31;
082 int result = 1;
083 long temp;
084 temp = Double.doubleToLongBits(evaluation);
085 result = prime * result + (int) (temp ^ temp >>> 32);
086 return result;
087 }
088
089 /**
090 * Two {@link EvaluationResult}s are equal, if their
091 * {@link #getEvaluation() evaluations} are equal.
092 */
093 @Override
094 public boolean equals(final Object obj) {
095 if (this == obj) {
096 return true;
097 }
098 if (obj == null) {
099 return false;
100 }
101 if (!(obj instanceof EvaluationResult)) {
102 return false;
103 }
104 final EvaluationResult<?> other = (EvaluationResult<?>) obj;
105 return Double.doubleToLongBits(evaluation) == Double
106 .doubleToLongBits(other.evaluation);
107 }
108
109 /**
110 * Compares two {@link EvaluationResult}s.
111 * <p>
112 * The natural order of two {@link EvaluationResult}s is the order of their
113 * {@link #getEvaluation() evaluation}s.
114 *
115 * @see Comparable#compareTo(Object)
116 */
117 @Override
118 public int compareTo(final EvaluationResult<I> other) {
119 return Double.compare(evaluation, other.evaluation);
120 }
121
122 @Override
123 public String toString() {
124 return "EvaluationResult [individual=" + individual + ", evaluation="
125 + evaluation + "]";
126 }
127 }