mirror of
https://github.com/apache/commons-math.git
synced 2025-02-06 01:59:13 +00:00
Start work on JIRA: MATH-575. Made InvalidRepresentationException extend MathIllegalArgumentException and localized error messages for this exception.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1135025 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b5a03457a5
commit
046a2ed6db
@ -131,6 +131,7 @@ public enum LocalizedFormats implements Localizable {
|
||||
INSUFFICIENT_ROWS_AND_COLUMNS("insufficient data: only {0} rows and {1} columns."),
|
||||
INTEGRATION_METHOD_NEEDS_AT_LEAST_TWO_PREVIOUS_POINTS("{0} method needs at least two previous points"),
|
||||
INTERNAL_ERROR("internal error, please fill a bug report at {0}"),
|
||||
INVALID_BINARY_DIGIT("invalid binary digit: {0}"),
|
||||
INVALID_BRACKETING_PARAMETERS("invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}"),
|
||||
INVALID_INTERVAL_INITIAL_VALUE_PARAMETERS("invalid interval, initial value parameters: lower={0}, initial={1}, upper={2}"),
|
||||
INVALID_ITERATIONS_LIMITS("invalid iteration limits: min={0}, max={1}"),
|
||||
|
@ -38,11 +38,7 @@ public abstract class AbstractListChromosome<T> extends Chromosome {
|
||||
* @param representation inner representation of the chromosome
|
||||
*/
|
||||
public AbstractListChromosome(final List<T> representation) {
|
||||
try {
|
||||
checkValidity(representation);
|
||||
} catch (InvalidRepresentationException e) {
|
||||
throw new IllegalArgumentException(String.format("Invalid representation for %s", getClass().getSimpleName()), e);
|
||||
}
|
||||
checkValidity(representation);
|
||||
this.representation = Collections.unmodifiableList(new ArrayList<T> (representation));
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ package org.apache.commons.math.genetics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
|
||||
|
||||
/**
|
||||
@ -51,7 +52,8 @@ public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
|
||||
protected void checkValidity(List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
|
||||
for (int i : chromosomeRepresentation) {
|
||||
if (i < 0 || i >1)
|
||||
throw new InvalidRepresentationException("Elements can be only 0 or 1.");
|
||||
throw new InvalidRepresentationException(
|
||||
LocalizedFormats.INVALID_BINARY_DIGIT, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,48 +16,29 @@
|
||||
*/
|
||||
package org.apache.commons.math.genetics;
|
||||
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math.exception.util.Localizable;
|
||||
|
||||
/**
|
||||
* Exception indicating that the representation of a chromosome is not valid.
|
||||
*
|
||||
* @version $Id$
|
||||
* @since 2.0
|
||||
*/
|
||||
public class InvalidRepresentationException extends Exception {
|
||||
public class InvalidRepresentationException extends MathIllegalArgumentException {
|
||||
|
||||
/** Serialization version id */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public InvalidRepresentationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an InvalidRepresentationException
|
||||
* @param arg0 exception message
|
||||
*/
|
||||
public InvalidRepresentationException(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an InvalidRepresentationException
|
||||
* @param arg0 cause
|
||||
*/
|
||||
public InvalidRepresentationException(Throwable arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an InvalidRepresentationException
|
||||
* Construct an InvalidRepresentationException with a specialized message.
|
||||
*
|
||||
* @param arg0 exception message
|
||||
* @param arg1 cause
|
||||
* @param specialized Message pattern.
|
||||
* @param args Arguments.
|
||||
*/
|
||||
public InvalidRepresentationException(String arg0, Throwable arg1) {
|
||||
super(arg0, arg1);
|
||||
public InvalidRepresentationException(Localizable pattern,
|
||||
Object ... args) {
|
||||
super(pattern, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import org.apache.commons.math.exception.OutOfRangeException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -175,7 +177,8 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
||||
protected void checkValidity(java.util.List<Double> chromosomeRepresentation) throws InvalidRepresentationException {
|
||||
for (double val : chromosomeRepresentation) {
|
||||
if (val < 0 || val > 1) {
|
||||
throw new InvalidRepresentationException("Values of representation must be in [0,1] interval");
|
||||
throw new InvalidRepresentationException(
|
||||
LocalizedFormats.OUT_OF_RANGE_SIMPLE, val, 0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,6 +102,7 @@ INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE = l''\u00e9chantillon ne contient que {0}
|
||||
INSUFFICIENT_ROWS_AND_COLUMNS = donn\u00e9es insuffisantes : seulement {0} lignes et {1} colonnes.
|
||||
INTEGRATION_METHOD_NEEDS_AT_LEAST_TWO_PREVIOUS_POINTS = la m\u00e9thode {0} n\u00e9cessite au moins deux points pr\u00e9c\u00e9dents
|
||||
INTERNAL_ERROR = erreur interne, veuillez signaler l''erreur \u00e0 {0}
|
||||
INVALID_BINARY_DIGIT = chiffre binaire invalide : {0}
|
||||
INVALID_BRACKETING_PARAMETERS = param\u00e8tres d''encadrement invalides : borne inf\u00e9rieure = {0}, valeur initiale = {1}, borne sup\u00e9rieure = {2}
|
||||
INVALID_INTERVAL_INITIAL_VALUE_PARAMETERS = param\u00e8tres de l''intervalle initial invalides : borne inf = {0}, valeur initiale = {1}, borne sup = {2}
|
||||
INVALID_ITERATIONS_LIMITS = limites d''it\u00e9rations invalides : min = {0}, max = {1}
|
||||
|
@ -34,7 +34,7 @@ public class BinaryChromosomeTest {
|
||||
new DummyBinaryChromosome(repr);
|
||||
Assert.fail("Exception not caught");
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,16 @@ public class RandomKeyTest {
|
||||
Assert.assertEquals("c", decoded.get(3));
|
||||
Assert.assertEquals("d", decoded.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidRepresentation() {
|
||||
try {
|
||||
DummyRandomKey drk = new DummyRandomKey(new Double[] {0.1, 0.1, 2d, 0.8, 0.2});
|
||||
Assert.fail("Expecting InvalidRepresentationException");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomPermutation() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user