diff options
Diffstat (limited to 'src/jcgp/function')
-rw-r--r-- | src/jcgp/function/Addition.java | 14 | ||||
-rw-r--r-- | src/jcgp/function/Function.java | 4 | ||||
-rw-r--r-- | src/jcgp/function/InsufficientArgumentsException.java | 10 | ||||
-rw-r--r-- | src/jcgp/function/InvalidArgumentsException.java | 20 | ||||
-rw-r--r-- | src/jcgp/function/Subtraction.java | 14 |
5 files changed, 40 insertions, 22 deletions
diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java index 45a8d35..f40bc24 100644 --- a/src/jcgp/function/Addition.java +++ b/src/jcgp/function/Addition.java @@ -3,19 +3,23 @@ package jcgp.function; import jcgp.population.Connection; public class Addition extends Function { + + private int arity = 2; @Override - public int run(Connection... connections) { - if (connections.length > 0) { - return connections[0].getValue() + connections[1].getValue(); + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else if (connections[0].getValue() instanceof Integer) { + return ((Integer) connections[0].getValue()) + ((Integer) connections[1].getValue()); } else { - throw new InsufficientArgumentsException(); + throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); } } @Override public int getArity() { - return 2; + return arity; } } diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java index 4900c68..118131a 100644 --- a/src/jcgp/function/Function.java +++ b/src/jcgp/function/Function.java @@ -4,8 +4,8 @@ import jcgp.population.Connection; import jcgp.population.InsufficientConnectionsException; public abstract class Function { - - public abstract int run(Connection ... connections) throws InsufficientConnectionsException; + + public abstract Object run(Connection ... connections) throws InsufficientConnectionsException; public abstract int getArity(); diff --git a/src/jcgp/function/InsufficientArgumentsException.java b/src/jcgp/function/InsufficientArgumentsException.java deleted file mode 100644 index a591b20..0000000 --- a/src/jcgp/function/InsufficientArgumentsException.java +++ /dev/null @@ -1,10 +0,0 @@ -package jcgp.function; - -public class InsufficientArgumentsException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = 2675108124600817777L; - -} diff --git a/src/jcgp/function/InvalidArgumentsException.java b/src/jcgp/function/InvalidArgumentsException.java new file mode 100644 index 0000000..cf55937 --- /dev/null +++ b/src/jcgp/function/InvalidArgumentsException.java @@ -0,0 +1,20 @@ +package jcgp.function; + +public class InvalidArgumentsException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 2675108124600817777L; + private String reason; + + public InvalidArgumentsException(String reason) { + super(); + this.reason = reason; + } + + public String getReason() { + return reason; + } + +} diff --git a/src/jcgp/function/Subtraction.java b/src/jcgp/function/Subtraction.java index 8f107b1..d785614 100644 --- a/src/jcgp/function/Subtraction.java +++ b/src/jcgp/function/Subtraction.java @@ -4,18 +4,22 @@ import jcgp.population.Connection; public class Subtraction extends Function { + private int arity = 2; + @Override - public int run(Connection... connections) { - if (connections.length > 1) { - return connections[0].getValue() - connections[1].getValue(); + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else if (connections[0].getValue() instanceof Integer) { + return ((Integer) connections[0].getValue()) - ((Integer) connections[1].getValue()); } else { - throw new InsufficientArgumentsException(); + throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); } } @Override public int getArity() { - return 2; + return arity; } } |