You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2010/10/03 22:53:11 UTC
svn commit: r1004046 [3/4] - in /mahout/trunk:
core/src/main/java/org/apache/mahout/cf/taste/impl/common/
core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/
core/src/main/java/org/apache/mahout/cf/taste/impl/eval/
core/src/main/java/org/ap...
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/Builder.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/Builder.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/Builder.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/Builder.java Sun Oct 3 20:53:07 2010
@@ -37,6 +37,8 @@ import org.apache.mahout.df.data.Dataset
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* Base class for Mapred DecisionForest builders. Takes care of storing the parameters common to the mapred
* implementations.<br>
@@ -191,10 +193,8 @@ public abstract class Builder {
* if (nbTrees <= 0)
*/
public static void setNbTrees(Configuration conf, int nbTrees) {
- if (nbTrees <= 0) {
- throw new IllegalArgumentException("nbTrees should be greater than 0");
- }
-
+ Preconditions.checkArgument(nbTrees > 0, "nbTrees should be greater than 0");
+
conf.setInt("mahout.rf.nbtrees", nbTrees);
}
@@ -297,13 +297,11 @@ public abstract class Builder {
* @throws IOException
* if anything goes wrong while parsing the output
*/
- protected abstract DecisionForest parseOutput(Job job, PredictionCallback callback) throws IOException,
- ClassNotFoundException,
- InterruptedException;
-
- public DecisionForest build(int nbTrees, PredictionCallback callback) throws IOException,
- ClassNotFoundException,
- InterruptedException {
+ protected abstract DecisionForest parseOutput(Job job, PredictionCallback callback)
+ throws IOException, ClassNotFoundException, InterruptedException;
+
+ public DecisionForest build(int nbTrees, PredictionCallback callback)
+ throws IOException, ClassNotFoundException, InterruptedException {
// int numTrees = getNbTrees(conf);
Path outputPath = getOutputPath(conf);
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/MapredMapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/MapredMapper.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/MapredMapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/MapredMapper.java Sun Oct 3 20:53:07 2010
@@ -24,6 +24,8 @@ import org.apache.hadoop.mapreduce.Mappe
import org.apache.mahout.df.builder.TreeBuilder;
import org.apache.mahout.df.data.Dataset;
+import com.google.common.base.Preconditions;
+
/**
* Base class for Mapred mappers. Loads common parameters from the job
*/
@@ -73,21 +75,12 @@ public class MapredMapper<KEYIN,VALUEIN,
/**
* Useful for testing
- *
- * @param noOutput
- * @param oobEstimate
- * @param treeBuilder
- * @param dataset
*/
protected void configure(boolean noOutput, boolean oobEstimate, TreeBuilder treeBuilder, Dataset dataset) {
+ Preconditions.checkArgument(treeBuilder != null, "TreeBuilder not found in the Job parameters");
this.noOutput = noOutput;
this.oobEstimate = oobEstimate;
-
- if (treeBuilder == null) {
- throw new IllegalArgumentException("TreeBuilder not found in the Job parameters");
- }
this.treeBuilder = treeBuilder;
-
this.dataset = dataset;
}
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/InterResults.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/InterResults.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/InterResults.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/InterResults.java Sun Oct 3 20:53:07 2010
@@ -24,6 +24,8 @@ import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.mahout.df.node.Node;
+import com.google.common.base.Preconditions;
+
/**
* Stores/Loads the intermediate results of step1 needed by step2.<br>
* This class should not be needed outside of the partial package, so all its methods are protected.<br>
@@ -58,15 +60,13 @@ public final class InterResults {
int partition,
TreeID[] keys,
Node[] trees) throws IOException {
- if (keys.length != trees.length) {
- throw new IllegalArgumentException("keys.length != trees.length");
- }
-
+ Preconditions.checkArgument(keys.length == trees.length, "keys.length != trees.length");
+
FSDataInputStream in = fs.open(forestPath);
-
+
TreeID key = new TreeID();
int numInstances = -1;
-
+
try {
// get current partition's size
for (int p = 0; p < numMaps; p++) {
@@ -76,12 +76,12 @@ public final class InterResults {
in.readInt();
}
}
-
+
// load (key, tree)
int current = 0;
for (int index = 0; index < numTrees; index++) {
key.readFields(in);
-
+
if (key.partition() == partition) {
// skip the trees of the current partition
Node.read(in);
@@ -91,14 +91,14 @@ public final class InterResults {
current++;
}
}
-
+
if (current != keys.length) {
throw new IllegalStateException("loaded less keys/trees than expected");
}
} finally {
in.close();
}
-
+
return numInstances;
}
@@ -120,9 +120,7 @@ public final class InterResults {
TreeID[] keys,
Node[] trees,
int[] sizes) throws IOException {
- if (keys.length != trees.length) {
- throw new IllegalArgumentException("keys.length != trees.length");
- }
+ Preconditions.checkArgument(keys.length == trees.length, "keys.length != trees.length");
int numTrees = keys.length;
int numMaps = sizes.length;
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/PartialBuilder.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/PartialBuilder.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/PartialBuilder.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/PartialBuilder.java Sun Oct 3 20:53:07 2010
@@ -41,6 +41,8 @@ import org.apache.mahout.df.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* Builds a random forest using partial data. Each mapper uses only the data given by its InputSplit
*/
@@ -156,46 +158,43 @@ public class PartialBuilder extends Buil
TreeID[] keys,
Node[] trees,
PredictionCallback callback) throws IOException {
- if (((keys != null) && (trees == null)) || ((keys == null) && (trees != null))) {
- throw new IllegalArgumentException("if keys is null, trees should also be null");
- }
- if ((keys != null) && (keys.length != trees.length)) {
- throw new IllegalArgumentException("keys.length != trees.length");
- }
-
+ Preconditions.checkArgument((keys == null && trees == null) || (keys != null && trees != null),
+ "if keys is null, trees should also be null" );
+ Preconditions.checkArgument(keys == null || keys.length == trees.length, "keys.length != trees.length" );
+
Configuration conf = job.getConfiguration();
-
+
FileSystem fs = outputPath.getFileSystem(conf);
-
+
Path[] outfiles = DFUtils.listOutputFiles(fs, outputPath);
-
+
// read all the outputs
TreeID key = new TreeID();
MapredOutput value = new MapredOutput();
-
+
int index = 0;
for (Path path : outfiles) {
Reader reader = new Reader(fs, path, conf);
-
+
try {
while (reader.next(key, value)) {
if (keys != null) {
keys[index] = key.clone();
}
-
+
if (trees != null) {
trees[index] = value.getTree();
}
-
+
processOutput(firstIds, key, value, callback);
-
+
index++;
}
} finally {
reader.close();
}
}
-
+
// make sure we got all the keys/values
if ((keys != null) && (index != keys.length)) {
throw new IllegalStateException("Some key/values are missing from the output");
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step0Job.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step0Job.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step0Job.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step0Job.java Sun Oct 3 20:53:07 2010
@@ -46,6 +46,8 @@ import org.apache.mahout.df.DFUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* preparation step of the partial mapreduce builder. Computes some stats that will be used by the builder.
*/
@@ -212,9 +214,7 @@ public class Step0Job {
*/
protected void configure(int p) {
partition = p;
- if (partition < 0) {
- throw new IllegalArgumentException("Wrong partition id : " + partition);
- }
+ Preconditions.checkArgument(partition >= 0, "Wrong partition id: %d", partition);
}
@Override
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step1Mapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step1Mapper.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step1Mapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step1Mapper.java Sun Oct 3 20:53:07 2010
@@ -38,6 +38,8 @@ import org.apache.mahout.df.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* First step of the Partial Data Builder. Builds the trees using the data available in the InputSplit.
* Predict the oob classes for each tree in its growing partition (input split).
@@ -99,9 +101,7 @@ public class Step1Mapper extends MapredM
}
// mapper's partition
- if (partition < 0) {
- throw new IllegalArgumentException("Wrong partition ID");
- }
+ Preconditions.checkArgument(partition >= 0, "Wrong partition ID");
this.partition = partition;
// compute number of trees to build
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step2Mapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step2Mapper.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step2Mapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/Step2Mapper.java Sun Oct 3 20:53:07 2010
@@ -38,6 +38,8 @@ import org.apache.mahout.df.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* Second step of PartialBuilder. Using the trees of the first step, computes the oob predictions for each
* tree, except those of its own partition, on all instancesof the partition.
@@ -68,9 +70,7 @@ public class Step2Mapper extends Mapper<
log.info("DistributedCache.getCacheFiles(): {}", ArrayUtils.toString(files));
- if ((files == null) || (files.length < 2)) {
- throw new IllegalArgumentException("missing paths from the DistributedCache");
- }
+ Preconditions.checkArgument(files != null && files.length >= 2, "missing paths from the DistributedCache" );
Path datasetPath = new Path(files[0].getPath());
Dataset dataset = Dataset.load(conf, datasetPath);
@@ -80,9 +80,7 @@ public class Step2Mapper extends Mapper<
// total number of trees in the forest
int numTrees = Builder.getNbTrees(conf);
- if (numTrees == -1) {
- throw new IllegalArgumentException("numTrees not found !");
- }
+ Preconditions.checkArgument(numTrees != -1, "numTrees not found !");
int nbConcerned = nbConcerned(numMaps, numTrees, p);
keys = new TreeID[nbConcerned];
@@ -107,9 +105,7 @@ public class Step2Mapper extends Mapper<
* mapper's partition
*/
public static int nbConcerned(int numMaps, int numTrees, int partition) {
- if (partition < 0) {
- throw new IllegalArgumentException("partition < 0");
- }
+ Preconditions.checkArgument(partition >= 0, "partition < 0");
// the trees of the mapper's partition are not concerned
return numTrees - Step1Mapper.nbTrees(numMaps, numTrees, partition);
}
@@ -130,15 +126,11 @@ public class Step2Mapper extends Mapper<
*/
public void configure(int partition, Dataset dataset, TreeID[] keys, Node[] trees, int numInstances) {
this.partition = partition;
- if (partition < 0) {
- throw new IllegalArgumentException("Wrong partition id : " + partition);
- }
+ Preconditions.checkArgument(partition >= 0, "Wrong partition id : " + partition);
converter = new DataConverter(dataset);
-
- if (keys.length != trees.length) {
- throw new IllegalArgumentException("keys.length != trees.length");
- }
+
+ Preconditions.checkArgument(keys.length == trees.length, "keys.length != trees.length");
int nbConcerned = keys.length;
this.keys = keys;
@@ -146,9 +138,7 @@ public class Step2Mapper extends Mapper<
// make sure the trees are not from this partition
for (TreeID key : keys) {
- if (key.partition() == partition) {
- throw new IllegalArgumentException("a tree from this partition was found !");
- }
+ Preconditions.checkArgument(key.partition() != partition, "a tree from this partition was found !");
}
// init the callbacks
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/TreeID.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/TreeID.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/TreeID.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/mapreduce/partial/TreeID.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,8 @@ package org.apache.mahout.df.mapreduce.p
import org.apache.hadoop.io.LongWritable;
+import com.google.common.base.Preconditions;
+
/**
* Indicates both the tree and the data partition used to grow the tree
*/
@@ -29,12 +31,8 @@ public class TreeID extends LongWritable
public TreeID() { }
public TreeID(int partition, int treeId) {
- if (partition < 0) {
- throw new IllegalArgumentException("partition < 0");
- }
- if (treeId < 0) {
- throw new IllegalArgumentException("treeId < 0");
- }
+ Preconditions.checkArgument(partition >= 0, "partition < 0");
+ Preconditions.checkArgument(treeId >= 0, "treeId < 0");
set(partition, treeId);
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/df/tools/UDistrib.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/df/tools/UDistrib.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/df/tools/UDistrib.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/df/tools/UDistrib.java Sun Oct 3 20:53:07 2010
@@ -45,6 +45,8 @@ import org.apache.mahout.df.data.Instanc
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+
/**
* This tool is used to uniformly distribute the class of all the tuples of the dataset over a given number of
* partitions.
@@ -111,20 +113,15 @@ public final class UDistrib {
}
private static void runTool(String dataStr, String datasetStr, String output, int numPartitions) throws IOException {
-
Configuration conf = new Configuration();
-
- if (numPartitions <= 0) {
- throw new IllegalArgumentException("numPartitions <= 0");
- }
+
+ Preconditions.checkArgument(numPartitions > 0, "numPartitions <= 0");
// make sure the output file does not exist
Path outputPath = new Path(output);
FileSystem fs = outputPath.getFileSystem(conf);
-
- if (fs.exists(outputPath)) {
- throw new IllegalArgumentException("Output path already exists");
- }
+
+ Preconditions.checkArgument(!fs.exists(outputPath), "Output path already exists");
// create a new file corresponding to each partition
// Path workingDir = fs.getWorkingDirectory();
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/ep/Mapping.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/ep/Mapping.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/ep/Mapping.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/ep/Mapping.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,8 @@ package org.apache.mahout.ep;
import org.apache.mahout.math.function.UnaryFunction;
+import com.google.common.base.Preconditions;
+
/**
* Provides coordinate tranformations so that evolution can proceed on the entire space of
* reals but have the output limited and squished in convenient (and safe) ways.
@@ -127,12 +129,8 @@ public abstract class Mapping implements
* @return A mapped value.
*/
public static Mapping logLimit(double low, double high) {
- if (low <= 0) {
- throw new IllegalArgumentException("Lower bound for log limit must be > 0 but was " + low);
- }
- if (high <= 0) {
- throw new IllegalArgumentException("Upper bound for log limit must be > 0 but was " + high);
- }
+ Preconditions.checkArgument(low > 0, "Lower bound for log limit must be > 0 but was %f", low);
+ Preconditions.checkArgument(high > 0, "Upper bound for log limit must be > 0 but was %f", high);
return new LogLimit(low, high);
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/EvalMapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/EvalMapper.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/EvalMapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/EvalMapper.java Sun Oct 3 20:53:07 2010
@@ -27,6 +27,8 @@ import org.apache.hadoop.mapreduce.Mappe
import org.apache.mahout.common.StringUtils;
import org.uncommons.watchmaker.framework.FitnessEvaluator;
+import com.google.common.base.Preconditions;
+
/**
* <p>
* Generic Mapper class for fitness evaluation. Works with the following : <code><key, candidate, key,
@@ -47,9 +49,7 @@ public class EvalMapper extends Mapper<L
public void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
String evlstr = conf.get(MAHOUT_GA_EVALUATOR);
- if (evlstr == null) {
- throw new IllegalArgumentException("'MAHOUT_GA_EVALUATOR' job parameter non found");
- }
+ Preconditions.checkArgument(evlstr != null, "'MAHOUT_GA_EVALUATOR' job parameter not found");
evaluator = StringUtils.fromString(evlstr);
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluator.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluator.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluator.java Sun Oct 3 20:53:07 2010
@@ -22,6 +22,8 @@ import java.util.List;
import org.uncommons.watchmaker.framework.FitnessEvaluator;
+import com.google.common.base.Preconditions;
+
/** Special Fitness Evaluator that evaluates all the population ones. */
public abstract class STFitnessEvaluator<T> implements FitnessEvaluator<T> {
@@ -39,9 +41,7 @@ public abstract class STFitnessEvaluator
}
int index = population.indexOf(candidate);
- if (index == -1) {
- throw new IllegalArgumentException("Candidate is not part of the population");
- }
+ Preconditions.checkArgument(index != -1, "Candidate is not part of the population");
return evaluations.get(index);
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/math/Varint.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/math/Varint.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/math/Varint.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/math/Varint.java Sun Oct 3 20:53:07 2010
@@ -21,6 +21,8 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
+import com.google.common.base.Preconditions;
+
/**
* <p>Encodes signed and unsigned values using a common variable-length
* scheme, found for example in
@@ -123,9 +125,7 @@ public final class Varint {
while (((b = in.readByte()) & 0x80L) != 0) {
value |= (b & 0x7F) << i;
i += 7;
- if (i > 63) {
- throw new IllegalArgumentException("Variable length quantity is too long");
- }
+ Preconditions.checkArgument(i <= 63, "Variable length quantity is too long");
}
return value | (b << i);
}
@@ -159,9 +159,7 @@ public final class Varint {
while (((b = in.readByte()) & 0x80) != 0) {
value |= (b & 0x7F) << i;
i += 7;
- if (i > 35) {
- throw new IllegalArgumentException("Variable length quantity is too long");
- }
+ Preconditions.checkArgument(i <= 35, "Variable length quantity is too long");
}
return value | (b << i);
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java Sun Oct 3 20:53:07 2010
@@ -20,6 +20,8 @@ package org.apache.mahout.math;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.Writable;
+import com.google.common.base.Preconditions;
+
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@@ -127,9 +129,7 @@ public final class VectorWritable extend
@Override
public void readFields(DataInput in) throws IOException {
int flags = in.readByte();
- if (flags >> NUM_FLAGS != 0) {
- throw new IllegalArgumentException("Unknown flags set: " + Integer.toString(flags, 2));
- }
+ Preconditions.checkArgument(flags >> NUM_FLAGS == 0, "Unknown flags set: %d", Integer.toString(flags, 2));
boolean dense = (flags & FLAG_DENSE) != 0;
boolean sequential = (flags & FLAG_SEQUENTIAL) != 0;
boolean named = (flags & FLAG_NAMED) != 0;
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/math/hadoop/TimesSquaredJob.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/math/hadoop/TimesSquaredJob.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/math/hadoop/TimesSquaredJob.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/math/hadoop/TimesSquaredJob.java Sun Oct 3 20:53:07 2010
@@ -41,6 +41,8 @@ import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
import org.apache.mahout.math.function.Functions;
+import com.google.common.base.Preconditions;
+
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
@@ -151,10 +153,8 @@ public final class TimesSquaredJob {
public void configure(JobConf conf) {
try {
URI[] localFiles = DistributedCache.getCacheFiles(conf);
- if (localFiles == null || localFiles.length < 1) {
- throw new IllegalArgumentException(
- "missing paths from the DistributedCache");
- }
+ Preconditions.checkArgument(localFiles != null && localFiles.length >= 1,
+ "missing paths from the DistributedCache" );
Path inputVectorPath = new Path(localFiles[0].getPath());
FileSystem fs = inputVectorPath.getFileSystem(conf);
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/DictionaryVectorizer.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/DictionaryVectorizer.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/DictionaryVectorizer.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/DictionaryVectorizer.java Sun Oct 3 20:53:07 2010
@@ -22,6 +22,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.List;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileStatus;
@@ -128,12 +129,12 @@ public final class DictionaryVectorizer
boolean sequentialAccess,
boolean namedVectors)
throws IOException, InterruptedException, ClassNotFoundException {
- if (normPower != PartialVectorMerger.NO_NORMALIZING && normPower < 0) {
- throw new IllegalArgumentException("normPower must either be -1 or >= 0");
- }
- if (normPower != PartialVectorMerger.NO_NORMALIZING && (normPower <= 1 || Double.isInfinite(normPower)) && logNormalize) {
- throw new IllegalArgumentException("normPower must be > 1 and not +infinity if log normalization is chosen");
- }
+ Preconditions.checkArgument(normPower == PartialVectorMerger.NO_NORMALIZING || normPower >= 0,
+ "If specified normPower must be nonnegative", normPower);
+ Preconditions.checkArgument(normPower == PartialVectorMerger.NO_NORMALIZING
+ || (normPower > 1 && !Double.isInfinite(normPower))
+ || !logNormalize,
+ "normPower must be > 1 and not infinite if log normalization is chosen", normPower);
if (chunkSizeInMegabytes < MIN_CHUNKSIZE) {
chunkSizeInMegabytes = MIN_CHUNKSIZE;
} else if (chunkSizeInMegabytes > MAX_CHUNKSIZE) { // 10GB
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/common/PartialVectorMerger.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/common/PartialVectorMerger.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/common/PartialVectorMerger.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/common/PartialVectorMerger.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,7 @@ package org.apache.mahout.vectorizer.com
import java.io.IOException;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
@@ -86,12 +87,12 @@ public final class PartialVectorMerger {
boolean namedVector,
int numReducers)
throws IOException, InterruptedException, ClassNotFoundException {
- if (normPower != NO_NORMALIZING && normPower < 0) {
- throw new IllegalArgumentException("normPower must either be -1 or >= 0");
- }
- if (normPower != NO_NORMALIZING && (normPower <= 1 || Double.isInfinite(normPower)) && logNormalize) {
- throw new IllegalArgumentException("normPower must be > 1 and not +infinity if log normalization is chosen");
- }
+ Preconditions.checkArgument(normPower == NO_NORMALIZING || normPower >= 0,
+ "If specified normPower must be nonnegative", normPower);
+ Preconditions.checkArgument(normPower == NO_NORMALIZING
+ || (normPower > 1 && !Double.isInfinite(normPower))
+ || !logNormalize,
+ "normPower must be > 1 and not infinite if log normalization is chosen", normPower);
Configuration conf = new Configuration();
// this conf parameter needs to be set enable serialisation of conf values
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/term/TFPartialVectorReducer.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/term/TFPartialVectorReducer.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/term/TFPartialVectorReducer.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/term/TFPartialVectorReducer.java Sun Oct 3 20:53:07 2010
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileSystem;
@@ -111,27 +112,24 @@ public class TFPartialVectorReducer exte
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
Configuration conf = context.getConfiguration();
- try {
- dimension = conf.getInt(PartialVectorMerger.DIMENSION, Integer.MAX_VALUE);
- sequentialAccess = conf.getBoolean(PartialVectorMerger.SEQUENTIAL_ACCESS, false);
- namedVector = conf.getBoolean(PartialVectorMerger.NAMED_VECTOR, false);
- maxNGramSize = conf.getInt(DictionaryVectorizer.MAX_NGRAMS, maxNGramSize);
- URI[] localFiles = DistributedCache.getCacheFiles(conf);
- if (localFiles == null || localFiles.length < 1) {
- throw new IllegalArgumentException("missing paths from the DistributedCache");
- }
- Path dictionaryFile = new Path(localFiles[0].getPath());
- FileSystem fs = dictionaryFile.getFileSystem(conf);
- SequenceFile.Reader reader = new SequenceFile.Reader(fs, dictionaryFile, conf);
- Writable key = new Text();
- IntWritable value = new IntWritable();
-
- // key is word value is id
- while (reader.next(key, value)) {
- dictionary.put(key.toString(), value.get());
- }
- } catch (IOException e) {
- throw new IllegalStateException(e);
+ URI[] localFiles = DistributedCache.getCacheFiles(conf);
+ Preconditions.checkArgument(localFiles != null && localFiles.length >= 1,
+ "missing paths from the DistributedCache");
+
+ dimension = conf.getInt(PartialVectorMerger.DIMENSION, Integer.MAX_VALUE);
+ sequentialAccess = conf.getBoolean(PartialVectorMerger.SEQUENTIAL_ACCESS, false);
+ namedVector = conf.getBoolean(PartialVectorMerger.NAMED_VECTOR, false);
+ maxNGramSize = conf.getInt(DictionaryVectorizer.MAX_NGRAMS, maxNGramSize);
+
+ Path dictionaryFile = new Path(localFiles[0].getPath());
+ FileSystem fs = dictionaryFile.getFileSystem(conf);
+ SequenceFile.Reader reader = new SequenceFile.Reader(fs, dictionaryFile, conf);
+ Writable key = new Text();
+ IntWritable value = new IntWritable();
+
+ // key is word value is id
+ while (reader.next(key, value)) {
+ dictionary.put(key.toString(), value.get());
}
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFConverter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFConverter.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFConverter.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFConverter.java Sun Oct 3 20:53:07 2010
@@ -22,6 +22,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.List;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileStatus;
@@ -123,19 +124,19 @@ public final class TFIDFConverter {
boolean sequentialAccessOutput,
boolean namedVector,
int numReducers) throws IOException, InterruptedException, ClassNotFoundException {
+ Preconditions.checkArgument(normPower == PartialVectorMerger.NO_NORMALIZING || normPower >= 0,
+ "If specified normPower must be nonnegative", normPower);
+ Preconditions.checkArgument(normPower == PartialVectorMerger.NO_NORMALIZING
+ || (normPower > 1 && !Double.isInfinite(normPower))
+ || !logNormalize,
+ "normPower must be > 1 and not infinite if log normalization is chosen", normPower);
+
if (chunkSizeInMegabytes < MIN_CHUNKSIZE) {
chunkSizeInMegabytes = MIN_CHUNKSIZE;
} else if (chunkSizeInMegabytes > MAX_CHUNKSIZE) { // 10GB
chunkSizeInMegabytes = MAX_CHUNKSIZE;
}
- if (normPower != PartialVectorMerger.NO_NORMALIZING && normPower < 0) {
- throw new IllegalArgumentException("normPower must either be -1 or >= 0");
- }
- if (normPower != PartialVectorMerger.NO_NORMALIZING && (normPower <= 1 || Double.isInfinite(normPower)) && logNormalize) {
- throw new IllegalArgumentException("normPower must be > 1 and not +infinity if log normalization is chosen");
- }
-
if (minDf < 1) {
minDf = 1;
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFPartialVectorReducer.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFPartialVectorReducer.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFPartialVectorReducer.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectorizer/tfidf/TFIDFPartialVectorReducer.java Sun Oct 3 20:53:07 2010
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileSystem;
@@ -100,32 +101,27 @@ public class TFIDFPartialVectorReducer e
@Override
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
- try {
- Configuration conf = context.getConfiguration();
- URI[] localFiles = DistributedCache.getCacheFiles(conf);
- if (localFiles == null || localFiles.length < 1) {
- throw new IllegalArgumentException("missing paths from the DistributedCache");
- }
-
- vectorCount = conf.getLong(TFIDFConverter.VECTOR_COUNT, 1);
- featureCount = conf.getLong(TFIDFConverter.FEATURE_COUNT, 1);
- minDf = conf.getInt(TFIDFConverter.MIN_DF, 1);
- maxDfPercent = conf.getInt(TFIDFConverter.MAX_DF_PERCENTAGE, 99);
- sequentialAccess = conf.getBoolean(PartialVectorMerger.SEQUENTIAL_ACCESS, false);
- namedVector = conf.getBoolean(PartialVectorMerger.NAMED_VECTOR, false);
-
- Path dictionaryFile = new Path(localFiles[0].getPath());
- FileSystem fs = dictionaryFile.getFileSystem(conf);
- SequenceFile.Reader reader = new SequenceFile.Reader(fs, dictionaryFile, conf);
- IntWritable key = new IntWritable();
- LongWritable value = new LongWritable();
-
- // key is feature, value is the document frequency
- while (reader.next(key, value)) {
- dictionary.put(key.get(), value.get());
- }
- } catch (IOException e) {
- throw new IllegalStateException(e);
+ Configuration conf = context.getConfiguration();
+ URI[] localFiles = DistributedCache.getCacheFiles(conf);
+ Preconditions.checkArgument(localFiles != null && localFiles.length >= 1,
+ "missing paths from the DistributedCache");
+
+ vectorCount = conf.getLong(TFIDFConverter.VECTOR_COUNT, 1);
+ featureCount = conf.getLong(TFIDFConverter.FEATURE_COUNT, 1);
+ minDf = conf.getInt(TFIDFConverter.MIN_DF, 1);
+ maxDfPercent = conf.getInt(TFIDFConverter.MAX_DF_PERCENTAGE, 99);
+ sequentialAccess = conf.getBoolean(PartialVectorMerger.SEQUENTIAL_ACCESS, false);
+ namedVector = conf.getBoolean(PartialVectorMerger.NAMED_VECTOR, false);
+
+ Path dictionaryFile = new Path(localFiles[0].getPath());
+ FileSystem fs = dictionaryFile.getFileSystem(conf);
+ SequenceFile.Reader reader = new SequenceFile.Reader(fs, dictionaryFile, conf);
+ IntWritable key = new IntWritable();
+ LongWritable value = new LongWritable();
+
+ // key is feature, value is the document frequency
+ while (reader.next(key, value)) {
+ dictionary.put(key.get(), value.get());
}
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingContinuousValueEncoder.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingContinuousValueEncoder.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingContinuousValueEncoder.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingContinuousValueEncoder.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,7 @@ package org.apache.mahout.vectors;
import org.apache.mahout.math.map.OpenIntIntHashMap;
+import com.google.common.base.Preconditions;
public class CachingContinuousValueEncoder extends ContinuousValueEncoder {
private final int dataSize;
@@ -48,10 +49,7 @@ public class CachingContinuousValueEncod
}
protected int hashForProbe(String originalForm, int dataSize, String name, int probe) {
- if (dataSize != this.dataSize) {
- throw new IllegalArgumentException("dataSize argument ["
- + dataSize + "] does not match expected dataSize [" + this.dataSize + ']');
- }
+ Preconditions.checkArgument(dataSize == this.dataSize, "dataSize argument [" + dataSize + "] does not match expected dataSize [" + this.dataSize + "]");
if (caches[probe].containsKey(originalForm.hashCode())) {
return caches[probe].get(originalForm.hashCode());
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingStaticWordValueEncoder.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingStaticWordValueEncoder.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingStaticWordValueEncoder.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectors/CachingStaticWordValueEncoder.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,8 @@ package org.apache.mahout.vectors;
import org.apache.mahout.math.map.OpenIntIntHashMap;
+import com.google.common.base.Preconditions;
+
public class CachingStaticWordValueEncoder extends StaticWordValueEncoder {
private final int dataSize;
private OpenIntIntHashMap[] caches;
@@ -48,10 +50,7 @@ public class CachingStaticWordValueEncod
}
protected int hashForProbe(String originalForm, int dataSize, String name, int probe) {
- if (dataSize != this.dataSize) {
- throw new IllegalArgumentException("dataSize argument ["
- + dataSize + "] does not match expected dataSize [" + this.dataSize + ']');
- }
+ Preconditions.checkArgument(dataSize == this.dataSize, "dataSize argument [" + dataSize + "] does not match expected dataSize [" + this.dataSize + "]");
if (caches[probe].containsKey(originalForm.hashCode())) {
return caches[probe].get(originalForm.hashCode());
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/vectors/InteractionValueEncoder.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/vectors/InteractionValueEncoder.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/vectors/InteractionValueEncoder.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/vectors/InteractionValueEncoder.java Sun Oct 3 20:53:07 2010
@@ -40,7 +40,7 @@ public class InteractionValueEncoder ext
*/
@Override
public void addToVector(String originalForm, double w, Vector data) {
- throw new IllegalArgumentException("addToVector is not supported for InteractionVectorEncoder");
+ throw new UnsupportedOperationException("addToVector is not supported for InteractionVectorEncoder");
}
/**
@@ -51,7 +51,7 @@ public class InteractionValueEncoder ext
*/
@Override
public void addToVector(byte[] originalForm, double w, Vector data) {
- throw new IllegalArgumentException("addToVector is not supported for InteractionVectorEncoder");
+ throw new UnsupportedOperationException("addToVector is not supported for InteractionVectorEncoder");
}
/**
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java Sun Oct 3 20:53:07 2010
@@ -17,6 +17,8 @@
package org.apache.mahout.ga.watchmaker.utils;
+import com.google.common.base.Preconditions;
+
import java.util.ArrayList;
import java.util.List;
@@ -57,9 +59,7 @@ public final class DummyCandidate {
}
public static List<DummyCandidate> generatePopulation(int size) {
- if (size <= 0) {
- throw new IllegalArgumentException("bad size");
- }
+ Preconditions.checkArgument(size > 0, "Size must be positive", size);
List<DummyCandidate> population = new ArrayList<DummyCandidate>();
for (int index = 0; index < size; index++) {
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java Sun Oct 3 20:53:07 2010
@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import com.google.common.base.Preconditions;
import org.apache.mahout.cf.taste.common.Refreshable;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
@@ -55,16 +56,11 @@ public final class NetflixDataModel impl
* @param dataDirectory root directory of Netflix data set, unpacked
* @param useSubset if true, will use only a small fraction of the data -- may be useful
* for quick experiments
- * @throws IOException
*/
public NetflixDataModel(File dataDirectory, boolean useSubset) throws IOException {
- if (dataDirectory == null) {
- throw new IllegalArgumentException("dataDirectory is null");
- }
- if (!dataDirectory.exists() || !dataDirectory.isDirectory()) {
- throw new FileNotFoundException(dataDirectory.toString());
- }
-
+ Preconditions.checkArgument(dataDirectory != null && dataDirectory.exists() && dataDirectory.isDirectory(),
+ "%s is not a directory", dataDirectory);
+
this.useSubset = useSubset;
log.info("Creating NetflixDataModel for directory: {}", dataDirectory);
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java Sun Oct 3 20:53:07 2010
@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
+import com.google.common.base.Preconditions;
import org.apache.mahout.cf.taste.common.Refreshable;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.AbstractLongPrimitiveIterator;
@@ -41,13 +42,9 @@ public final class NetflixFileDataModel
private final File dataDirectory;
- public NetflixFileDataModel(File dataDirectory) throws IOException {
- if (dataDirectory == null) {
- throw new IllegalArgumentException("dataDirectory is null");
- }
- if (!dataDirectory.exists() || !dataDirectory.isDirectory()) {
- throw new FileNotFoundException(dataDirectory.toString());
- }
+ public NetflixFileDataModel(File dataDirectory) {
+ Preconditions.checkArgument(dataDirectory != null && dataDirectory.exists() && dataDirectory.isDirectory(),
+ "%s is not a directory", dataDirectory);
this.dataDirectory = dataDirectory;
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/TransposeToByUser.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/TransposeToByUser.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/TransposeToByUser.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/TransposeToByUser.java Sun Oct 3 20:53:07 2010
@@ -28,6 +28,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import com.google.common.base.Preconditions;
import org.apache.commons.cli2.OptionException;
import org.apache.mahout.cf.taste.example.TasteOptionParser;
import org.apache.mahout.cf.taste.impl.common.FastMap;
@@ -47,16 +48,12 @@ public final class TransposeToByUser {
File dataDirectory = TasteOptionParser.getRatings(args);
File byItemDirectory = new File(dataDirectory, "training_set");
File byUserDirectory = new File(dataDirectory, "training_set_by_user");
-
- if (!dataDirectory.exists() || !dataDirectory.isDirectory()) {
- throw new IllegalArgumentException(dataDirectory + " is not a directory");
- }
- if (!byItemDirectory.exists() || !byItemDirectory.isDirectory()) {
- throw new IllegalArgumentException(byItemDirectory + " is not a directory");
- }
- if (byUserDirectory.exists()) {
- throw new IllegalArgumentException(byUserDirectory + " already exists");
- }
+
+ Preconditions.checkArgument(dataDirectory.exists() && dataDirectory.isDirectory(),
+ "%s is not a directory", dataDirectory);
+ Preconditions.checkArgument(byItemDirectory.exists() && byItemDirectory.isDirectory(),
+ "%s is not a directory", byItemDirectory);
+ Preconditions.checkArgument(!byUserDirectory.exists(), "%s already exists", byUserDirectory);
byUserDirectory.mkdirs();
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/PrepareTwentyNewsgroups.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/PrepareTwentyNewsgroups.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/PrepareTwentyNewsgroups.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/PrepareTwentyNewsgroups.java Sun Oct 3 20:53:07 2010
@@ -100,7 +100,7 @@ public final class PrepareTwentyNewsgrou
for (File dir : categoryDirs) {
if (dir.isDirectory()) {
if (!outputDir.exists() && !outputDir.mkdirs()) {
- throw new IllegalArgumentException("Can't create output directory");
+ throw new IllegalStateException("Can't create output directory");
}
File outputFile = new File(outputDir, dir.getName() + ".txt");
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java Sun Oct 3 20:53:07 2010
@@ -26,6 +26,7 @@ import java.io.Writer;
import java.nio.charset.Charset;
import java.util.BitSet;
+import com.google.common.base.Preconditions;
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.Option;
@@ -56,11 +57,11 @@ import org.slf4j.LoggerFactory;
* This class can be used to split directories of files or individual files into
* training and test sets using a number of different methods.
* <p>
- * When executed via {@link #splitDirectory(File)} or {@link #splitFile(File)},
+ * When executed via {@link #splitDirectory(Path)} or {@link #splitFile(Path)},
* the lines read from one or more, input files are written to files of the same
* name into the directories specified by the
- * {@link #setTestOutputDirectory(File)} and
- * {@link #setTrainingOutputDirectory(File)} methods.
+ * {@link #setTestOutputDirectory(Path)} and
+ * {@link #setTrainingOutputDirectory(Path)} methods.
* <p>
* The composition of the test set is determined using one of the following
* approaches:
@@ -117,9 +118,8 @@ public class SplitBayesInput {
private int testRandomSelectionSize = -1;
private int testRandomSelectionPct = -1;
private Charset charset = Charset.forName("UTF-8");
-
- private Configuration conf;
- private FileSystem fs;
+
+ private final FileSystem fs;
private Path inputDirectory;
private Path trainingOutputDirectory;
private Path testOutputDirectory;
@@ -134,7 +134,7 @@ public class SplitBayesInput {
}
public SplitBayesInput() throws IOException {
- conf = new Configuration();
+ Configuration conf = new Configuration();
fs = FileSystem.get(conf);
}
@@ -250,20 +250,15 @@ public class SplitBayesInput {
return true;
}
- /** Perform a split on directory specified by {@link #setInputDirectory(File)} by calling {@link #splitFile(File)}
+ /** Perform a split on directory specified by {@link #setInputDirectory(Path)} by calling {@link #splitFile(Path)}
* on each file found within that directory.
- *
- * @throws IOException
*/
public void splitDirectory() throws IOException {
this.splitDirectory(inputDirectory);
}
- /** Perform a split on the specified directory by calling {@link #splitFile(File)} on each file found within that
+ /** Perform a split on the specified directory by calling {@link #splitFile(Path)} on each file found within that
* directory.
- *
- * @param inputDir
- * @throws IOException
*/
public void splitDirectory(Path inputDir) throws IOException {
if (fs.getFileStatus(inputDir) == null) {
@@ -518,25 +513,20 @@ public class SplitBayesInput {
* if output directories do not exist or are not directories.
*/
public void validate() throws IOException {
- if ((testSplitSize < 1) && (testSplitSize != -1)) {
- throw new IllegalArgumentException("test split size must be 1 or greater");
- }
-
- if ((splitLocation < 0 || splitLocation > 100) && (splitLocation != -1)) {
- throw new IllegalArgumentException("test split percentage must be between 0 and 100");
- }
-
- if ((testSplitPct < 0 || testSplitPct > 100) && (testSplitPct != -1)) {
- throw new IllegalArgumentException("test split percentage must be between 0 and 100");
- }
-
- if ((splitLocation < 0 || splitLocation > 100) && (splitLocation != -1)) {
- throw new IllegalArgumentException("test split percentage must be between 0 and 100");
- }
+ Preconditions.checkArgument(testSplitSize >= 1 || testSplitSize == -1,
+ "Invalid testSplitSize", testSplitSize);
+ Preconditions.checkArgument((splitLocation >= 0 && splitLocation <= 100) || splitLocation == -1,
+ "Invalid splitLocation percentage", splitLocation);
+ Preconditions.checkArgument((testSplitPct >= 0 && testSplitPct <= 100) || testSplitPct == -1,
+ "Invalid testSplitPct percentage", testSplitPct);
+ Preconditions.checkArgument((splitLocation >= 0 && splitLocation <= 100) || splitLocation == -1,
+ "Invalid splitLocation percentage", splitLocation);
+ Preconditions.checkArgument((testRandomSelectionPct >= 0 && testRandomSelectionPct <= 100)
+ || testRandomSelectionPct == -1,
+ "Invalid testRandomSelectionPct percentage", testRandomSelectionPct);
- if ((testRandomSelectionPct < 0 || testRandomSelectionPct > 100) && (testRandomSelectionPct != -1)) {
- throw new IllegalArgumentException("test split percentage must be between 0 and 100");
- }
+ Preconditions.checkArgument(trainingOutputDirectory != null, "No training output directory was specified");
+ Preconditions.checkArgument(testOutputDirectory != null, "No test output directory was specified");
// only one of the following may be set, one must be set.
int count = 0;
@@ -553,35 +543,15 @@ public class SplitBayesInput {
count++;
}
- if (count == 0) {
- throw new IllegalArgumentException("either test split size, test split pct, "
- + "random selection size or random selection pct must be specified and a positive integer");
- } else if (count > 1) {
- throw new IllegalArgumentException("only test split size, test split pct, "
- + "random selection size or random selection pct may be specified");
- }
-
- if (trainingOutputDirectory == null) {
- throw new IllegalArgumentException("no training output directory was specified");
- }
-
- if (testOutputDirectory == null) {
- throw new IllegalArgumentException("no test output directory was specified");
- }
-
- if (fs.getFileStatus(trainingOutputDirectory) == null) {
- throw new IOException(trainingOutputDirectory + " does not exist");
- }
- else if (!fs.getFileStatus(trainingOutputDirectory).isDir()) {
- throw new IOException(trainingOutputDirectory + " is not a directory");
- }
+ Preconditions.checkArgument(count == 1,
+ "Exactly one of testSplitSize, testSplitPct, testRandomSelectionSize, testRandomSelectionPct should be set");
- if (fs.getFileStatus(testOutputDirectory) == null) {
- throw new IOException(testOutputDirectory + " does not exist");
- }
- else if (!fs.getFileStatus(testOutputDirectory).isDir()) {
- throw new IOException(testOutputDirectory + " is not a directory");
- }
+ FileStatus trainingOutputDirStatus = fs.getFileStatus(trainingOutputDirectory);
+ Preconditions.checkArgument(trainingOutputDirStatus != null && trainingOutputDirStatus.isDir(),
+ "%s is not a directory", trainingOutputDirectory);
+ FileStatus testOutputDirStatus = fs.getFileStatus(testOutputDirectory);
+ Preconditions.checkArgument(testOutputDirStatus != null && testOutputDirStatus.isDir(),
+ "%s is not a directory", testOutputDirectory);
}
/** Count the lines in the file specified as returned by <code>BufferedReader.readLine()</code>
@@ -598,12 +568,15 @@ public class SplitBayesInput {
* if there is a problem opening or reading the file.
*/
public static int countLines(FileSystem fs, Path inputFile, Charset charset) throws IOException {
- BufferedReader countReader = new BufferedReader(new InputStreamReader(fs.open(inputFile), charset));
int lineCount = 0;
- while (countReader.readLine() != null) {
- lineCount++;
+ BufferedReader countReader = new BufferedReader(new InputStreamReader(fs.open(inputFile), charset));
+ try {
+ while (countReader.readLine() != null) {
+ lineCount++;
+ }
+ } finally {
+ IOUtils.quietClose(countReader);
}
- IOUtils.quietClose(countReader);
return lineCount;
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java Sun Oct 3 20:53:07 2010
@@ -17,6 +17,7 @@
package org.apache.mahout.classifier.sgd;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -149,9 +150,11 @@ public class LogisticModelParameters {
*/
public static LogisticModelParameters loadFrom(File in) throws IOException {
InputStreamReader input = new FileReader(in);
- LogisticModelParameters r = loadFrom(input);
- input.close();
- return r;
+ try {
+ return loadFrom(input);
+ } finally {
+ input.close();
+ }
}
/**
@@ -161,11 +164,9 @@ public class LogisticModelParameters {
* @param predictorList The list of variable names.
* @param typeList The list of types in the format preferred by CsvRecordFactory.
*/
- public void setTypeMap(List<String> predictorList, List<String> typeList) {
+ public void setTypeMap(Iterable<String> predictorList, List<String> typeList) {
+ Preconditions.checkArgument(!typeList.isEmpty(), "Must have at least one type specifier");
typeMap = Maps.newHashMap();
- if (typeList.isEmpty()) {
- throw new IllegalArgumentException("Must have at least one type specifier");
- }
Iterator<String> iTypes = typeList.iterator();
String lastType = null;
for (Object x : predictorList) {
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java Sun Oct 3 20:53:07 2010
@@ -17,6 +17,8 @@
package org.apache.mahout.classifier.sgd;
+import com.google.common.base.Preconditions;
+
import java.io.BufferedReader;
/**
@@ -29,14 +31,15 @@ public final class PrintResourceOrFile {
}
public static void main(String[] args) throws Exception {
- if (args.length != 1) {
- throw new IllegalArgumentException("Must have a single argument that names a file or resource.");
- }
+ Preconditions.checkArgument(args.length == 1, "Must have a single argument that names a file or resource.");
BufferedReader in = TrainLogistic.open(args[0]);
- String line = in.readLine();
- while (line != null) {
- System.out.println(line);
- line = in.readLine();
+ try {
+ String line;
+ while ((line = in.readLine()) != null) {
+ System.out.println(line);
+ }
+ } finally {
+ in.close();
}
}
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDCrossover.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDCrossover.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDCrossover.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDCrossover.java Sun Oct 3 20:53:07 2010
@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
+import com.google.common.base.Preconditions;
import org.uncommons.maths.random.Probability;
import org.uncommons.watchmaker.framework.operators.AbstractCrossover;
@@ -39,9 +40,9 @@ public class CDCrossover extends Abstrac
@Override
protected List<CDRule> mate(CDRule parent1, CDRule parent2, int numberOfCrossoverPoints, Random rng) {
- if (parent1.getNbConditions() != parent2.getNbConditions()) {
- throw new IllegalArgumentException("Cannot perform cross-over with parents of different size.");
- }
+ Preconditions.checkArgument(
+ parent1.getNbConditions() == parent2.getNbConditions(),
+ "Cannot perform cross-over with parents of different size.");
CDRule offspring1 = new CDRule(parent1);
CDRule offspring2 = new CDRule(parent2);
// Apply as many cross-overs as required.
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDMutation.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDMutation.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDMutation.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDMutation.java Sun Oct 3 20:53:07 2010
@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
+import com.google.common.base.Preconditions;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
/**
@@ -50,16 +51,9 @@ public class CDMutation implements Evolu
* for more information about the parameters
*/
public CDMutation(double rate, double range, int k) {
- if (rate <= 0 || rate > 1) {
- throw new IllegalArgumentException("mutation rate must be in ]0, 1]");
- }
- if (range <= 0 || range > 1) {
- throw new IllegalArgumentException("mutation range must be in ]0, 1]");
- }
- if (k < 0) {
- throw new IllegalArgumentException("mutation precision must be >= 0");
- }
-
+ Preconditions.checkArgument(rate > 0.0 && rate <= 1.0, "mutation rate must be in (0, 1]");
+ Preconditions.checkArgument(range > 0.0 && range <= 1.0, "mutation range must be in (0, 1]");
+ Preconditions.checkArgument(k >= 0, "mutation precision must be nonnegative");
this.rate = rate;
this.range = range;
this.k = k;
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDRule.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDRule.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDRule.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/CDRule.java Sun Oct 3 20:53:07 2010
@@ -19,6 +19,7 @@ package org.apache.mahout.ga.watchmaker.
import java.util.Random;
+import com.google.common.base.Preconditions;
import org.uncommons.maths.binary.BitString;
/**
@@ -55,10 +56,8 @@ public final class CDRule implements Rul
*/
public CDRule(double threshold) {
// crossover needs at least 2 attributes
- if (!(threshold >= 0 && threshold <= 1)) {
- throw new IllegalArgumentException("bad threshold");
- }
-
+ Preconditions.checkArgument(threshold >= 0.0 && threshold <= 1.0, "Threshold must be in [0,1]");
+
this.threshold = threshold;
// the label is not included in the conditions
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/DataSet.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/DataSet.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/DataSet.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/DataSet.java Sun Oct 3 20:53:07 2010
@@ -17,6 +17,8 @@
package org.apache.mahout.ga.watchmaker.cd;
+import com.google.common.base.Preconditions;
+
import java.util.List;
/**
@@ -96,10 +98,7 @@ public class DataSet {
* @throws IllegalArgumentException if the attribute is nominal
*/
public double getMax(int index) {
- if (!isNumerical(index)) {
- throw new IllegalArgumentException("Nominal Attribute");
- }
-
+ Preconditions.checkArgument(isNumerical(index), "Nominal attribute");
return ((NumericalAttr) attributes.get(index)).getMax();
}
@@ -110,10 +109,7 @@ public class DataSet {
* @throws IllegalArgumentException if the attribute is nominal
*/
public double getMin(int index) {
- if (!isNumerical(index)) {
- throw new IllegalArgumentException("Nominal Attribute");
- }
-
+ Preconditions.checkArgument(isNumerical(index), "Nominal attribute");
return ((NumericalAttr) attributes.get(index)).getMin();
}
@@ -124,10 +120,7 @@ public class DataSet {
* @throws IllegalArgumentException if the attribute is numerical
*/
public int getNbValues(int index) {
- if (isNumerical(index)) {
- throw new IllegalArgumentException("Numerical Attribute");
- }
-
+ Preconditions.checkArgument(!isNumerical(index), "Numerical attribute");
return ((NominalAttr) attributes.get(index)).getNbvalues();
}
@@ -150,10 +143,7 @@ public class DataSet {
* @throws IllegalArgumentException if the value is not found.
*/
public int valueIndex(int index, String value) {
- if (isNumerical(index)) {
- throw new IllegalArgumentException("Numerical Attribute");
- }
-
+ Preconditions.checkArgument(!isNumerical(index), "Numerical attribute");
return ((NominalAttr) attributes.get(index)).valueIndex(value);
}
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/FileInfoParser.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/FileInfoParser.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/FileInfoParser.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/FileInfoParser.java Sun Oct 3 20:53:07 2010
@@ -25,6 +25,7 @@ import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -108,35 +109,18 @@ public final class FileInfoParser {
/**
* Prepares the path for the info file corresponding to the input path.
*
- * @param fs
- * file system
- * @param inpath
- * @throws IOException
+ * @param fs file system
*/
public static Path getInfoFile(FileSystem fs, Path inpath) throws IOException {
- if (inpath == null) {
- throw new IllegalArgumentException("null inpath parameter");
- }
- if (!fs.exists(inpath)) {
- throw new IllegalArgumentException("Input path does not exist: " + inpath);
- }
- if (!fs.getFileStatus(inpath).isDir()) {
- throw new IllegalArgumentException("Input path should be a directory: " + inpath);
- }
-
- // info file name
+ Preconditions.checkArgument(inpath != null && fs.exists(inpath) && fs.getFileStatus(inpath).isDir(),
+ "Input path should be a directory", inpath);
Path infoPath = new Path(inpath.getParent(), inpath.getName() + ".infos");
- if (!fs.exists(infoPath)) {
- throw new IllegalArgumentException("Info file does not exist");
- }
-
+ Preconditions.checkArgument(fs.exists(infoPath), "Info file does not exist", infoPath);
return infoPath;
}
/**
* Parse a nominal attribute.
- *
- * @param tokenizer
*/
private static NominalAttr parseNominal(StringTokenizer tokenizer) {
Collection<String> vlist = new ArrayList<String>();
@@ -152,16 +136,11 @@ public final class FileInfoParser {
/**
* Parse a numerical attribute.
- *
- * @param tokenizer
*/
private static NumericalAttr parseNumerical(StringTokenizer tokenizer) {
double min = nextDouble(tokenizer);
double max = nextDouble(tokenizer);
- if (min > max) {
- throw new IllegalArgumentException("min > max");
- }
-
+ Preconditions.checkArgument(min <= max, "min > max");
return new NumericalAttr(min, max);
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/NominalAttr.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/NominalAttr.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/NominalAttr.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/NominalAttr.java Sun Oct 3 20:53:07 2010
@@ -17,14 +17,14 @@
package org.apache.mahout.ga.watchmaker.cd;
+import com.google.common.base.Preconditions;
+
class NominalAttr implements Attribute {
private final String[] values;
NominalAttr(String[] values) {
- if (values.length <= 0) {
- throw new IllegalArgumentException("values is empty");
- }
+ Preconditions.checkArgument(values.length > 0, "values is empty");
this.values = values;
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMahoutEvaluator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMahoutEvaluator.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMahoutEvaluator.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMahoutEvaluator.java Sun Oct 3 20:53:07 2010
@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -63,22 +64,18 @@ public final class CDMahoutEvaluator {
* evaluated fitness for each candidate from the input population,
* sorted in the same order as the candidates.
* @param split DatasetSplit used to separate training and testing input
- * @throws IOException
- * @throws ClassNotFoundException
- * @throws InterruptedException
*/
- public static void evaluate(List<? extends Rule> rules, int target, Path inpath, Path output, List<CDFitness> evaluations,
- DatasetSplit split) throws IOException, InterruptedException, ClassNotFoundException {
+ public static void evaluate(List<? extends Rule> rules,
+ int target,
+ Path inpath,
+ Path output,
+ Collection<CDFitness> evaluations,
+ DatasetSplit split) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
-
- Job job = new Job(conf);
-
FileSystem fs = FileSystem.get(inpath.toUri(), conf);
+ Preconditions.checkArgument(fs.exists(inpath) && fs.getFileStatus(inpath).isDir(), "%s is not a directory", inpath);
- // check the input
- if (!fs.exists(inpath) || !fs.getFileStatus(inpath).isDir()) {
- throw new IllegalArgumentException("Input path not found or is not a directory");
- }
+ Job job = new Job(conf);
configureJob(job, rules, target, inpath, output, split);
job.waitForCompletion(true);
@@ -129,12 +126,13 @@ public final class CDMahoutEvaluator {
* @param evaluations <code>List<CDFitness></code> that contains the
* evaluated fitness for each candidate from the input population,
* sorted in the same order as the candidates.
- * @throws IOException
- * @throws ClassNotFoundException
- * @throws InterruptedException
*/
- public static void evaluate(List<? extends Rule> rules, int target, Path inpath, Path output, List<CDFitness> evaluations)
- throws IOException, InterruptedException, ClassNotFoundException {
+ public static void evaluate(List<? extends Rule> rules,
+ int target,
+ Path inpath,
+ Path output,
+ Collection<CDFitness> evaluations)
+ throws IOException, InterruptedException, ClassNotFoundException {
evaluate(rules, target, inpath, output, evaluations, new DatasetSplit(1));
}
@@ -147,9 +145,13 @@ public final class CDMahoutEvaluator {
* @param inpath input path (the dataset)
* @param outpath output <code>Path</code>
* @param split DatasetSplit used to separate training and testing input
- * @throws IOException
*/
- private static void configureJob(Job job, List<? extends Rule> rules, int target, Path inpath, Path outpath, DatasetSplit split) throws IOException {
+ private static void configureJob(Job job,
+ List<? extends Rule> rules,
+ int target,
+ Path inpath,
+ Path outpath,
+ DatasetSplit split) throws IOException {
split.storeJobParameters(job.getConfiguration());
FileInputFormat.setInputPaths(job, inpath);
@@ -182,12 +184,11 @@ public final class CDMahoutEvaluator {
* @param evaluations <code>List<Fitness></code> that contains the
* evaluated fitness for each candidate from the input population,
* sorted in the same order as the candidates.
- * @throws IOException
*/
private static void importEvaluations(FileSystem fs,
Configuration conf, Path outpath,
Collection<CDFitness> evaluations)
- throws IOException {
+ throws IOException {
Sorter sorter = new Sorter(fs, LongWritable.class, CDFitness.class, conf);
// merge and sort the outputs
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMapper.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMapper.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/CDMapper.java Sun Oct 3 20:53:07 2010
@@ -20,6 +20,7 @@ package org.apache.mahout.ga.watchmaker.
import java.io.IOException;
import java.util.List;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
@@ -60,19 +61,13 @@ public class CDMapper extends Mapper<Lon
super.setup(context);
Configuration conf = context.getConfiguration();
String rstr = conf.get(CLASSDISCOVERY_RULES);
- if (rstr == null) {
- throw new IllegalArgumentException("Job Parameter (" + CLASSDISCOVERY_RULES + ") not found!");
- }
+ Preconditions.checkArgument(rstr != null, "Job parameter %s not found", CLASSDISCOVERY_RULES);
String datastr = conf.get(CLASSDISCOVERY_DATASET);
- if (datastr == null) {
- throw new IllegalArgumentException("Job Parameter (" + CLASSDISCOVERY_DATASET + ") not found!");
- }
+ Preconditions.checkArgument(datastr != null, "Job parameter %s not found", CLASSDISCOVERY_DATASET);
int target = conf.getInt(CLASSDISCOVERY_TARGET_LABEL, -1);
- if (target == -1) {
- throw new IllegalArgumentException("Job Parameter (" + CLASSDISCOVERY_TARGET_LABEL + ") not found!");
- }
+ Preconditions.checkArgument(target != -1, "Job parameter %s not found", CLASSDISCOVERY_TARGET_LABEL);
initializeDataSet(StringUtils.<DataSet> fromString(datastr));
configure(StringUtils.<List<Rule>> fromString(rstr), target);
@@ -86,16 +81,10 @@ public class CDMapper extends Mapper<Lon
}
void configure(List<Rule> rules, int target) {
- if (rules == null || rules.isEmpty()) {
- throw new IllegalArgumentException("bad 'rules' configuration parameter");
- }
- if (target < 0) {
- throw new IllegalArgumentException("bad 'target' configuration parameter");
- }
-
+ Preconditions.checkArgument(rules != null && !rules.isEmpty(), "Bad rules", rules);
+ Preconditions.checkArgument(target >= 0, "Bad target", target);
this.rules = rules;
this.target = target;
-
}
/**
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java Sun Oct 3 20:53:07 2010
@@ -20,6 +20,7 @@ package org.apache.mahout.ga.watchmaker.
import java.io.IOException;
import java.util.Random;
+import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
@@ -94,27 +95,18 @@ public final class DatasetSplit {
static long getSeed(Configuration conf) {
String seedstr = conf.get(SEED);
- if (seedstr == null) {
- throw new IllegalArgumentException("SEED job parameter not found");
- }
-
+ Preconditions.checkArgument(seedstr != null, "Job parameter %s not found", SEED);
return StringUtils.<Long> fromString(seedstr);
}
static double getThreshold(Configuration conf) {
String thrstr = conf.get(THRESHOLD);
- if (thrstr == null) {
- throw new IllegalArgumentException("THRESHOLD job parameter not found");
- }
-
+ Preconditions.checkArgument(thrstr != null, "Job parameter %s not found", THRESHOLD);
return Double.parseDouble(thrstr);
}
static boolean isTraining(Configuration conf) {
- if (conf.get(TRAINING) == null) {
- throw new IllegalArgumentException("TRAINING job parameter not found");
- }
-
+ Preconditions.checkArgument(conf.get(TRAINING) != null, "Job parameter %s not found", TRAINING);
return conf.getBoolean(TRAINING, true);
}
@@ -137,14 +129,9 @@ public final class DatasetSplit {
private final Text v = new Text();
public RndLineRecordReader(RecordReader<LongWritable, Text> reader, Configuration conf) {
- if (reader == null) {
- throw new IllegalArgumentException("null reader");
- }
-
+ Preconditions.checkArgument(reader != null, "Null reader");
this.reader = reader;
-
DatasetSplit split = new DatasetSplit(conf);
-
rng = RandomUtils.getRandom(split.getSeed());
threshold = split.getThreshold();
training = split.isTraining();
@@ -160,7 +147,7 @@ public final class DatasetSplit {
try {
return reader.getProgress();
} catch (InterruptedException e) {
- return 0;
+ return 0.0f;
}
}
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java?rev=1004046&r1=1004045&r2=1004046&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java Sun Oct 3 20:53:07 2010
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Scanner;
+import com.google.common.base.Preconditions;
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.Option;
@@ -74,20 +75,15 @@ public final class CDInfosTool {
* @param inpath input path (the dataset)
* @param descriptions <code>List<String></code> that contains the
* generated descriptions for each non ignored attribute
- * @throws IOException
- * @throws ClassNotFoundException
- * @throws InterruptedException
*/
- public static void gatherInfos(Descriptors descriptors, Path inpath, Path output, List<String> descriptions) throws IOException,
- InterruptedException, ClassNotFoundException {
+ public static void gatherInfos(Descriptors descriptors, Path inpath, Path output, Collection<String> descriptions)
+ throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
- Job job = new Job(conf);
FileSystem fs = FileSystem.get(inpath.toUri(), conf);
+ Preconditions.checkArgument(fs.exists(inpath) && fs.getFileStatus(inpath).isDir(),
+ "%s is not a directory", inpath);
- // check the input
- if (!fs.exists(inpath) || !fs.getFileStatus(inpath).isDir()) {
- throw new IllegalArgumentException("Input path not found or is not a directory");
- }
+ Job job = new Job(conf);
configureJob(job, descriptors, inpath, output);
job.waitForCompletion(true);
@@ -98,11 +94,9 @@ public final class CDInfosTool {
/**
* Configure the job
*
- * @param job
* @param descriptors attributes's descriptors
* @param inpath input <code>Path</code>
* @param outpath output <code>Path</code>
- * @throws IOException
*/
private static void configureJob(Job job, Descriptors descriptors, Path inpath, Path outpath) throws IOException {
FileInputFormat.setInputPaths(job, inpath);
@@ -130,12 +124,11 @@ public final class CDInfosTool {
* @param conf job configuration
* @param outpath output <code>Path</code>
* @param descriptions List of attribute's descriptions
- * @throws IOException
*/
private static void importDescriptions(FileSystem fs,
Configuration conf, Path outpath,
Collection<String> descriptions)
- throws IOException {
+ throws IOException {
Sorter sorter = new Sorter(fs, LongWritable.class, Text.class, conf);
// merge and sort the outputs
@@ -223,8 +216,8 @@ public final class CDInfosTool {
GroupBuilder gbuilder = new GroupBuilder();
Option inputOpt = obuilder.withLongName("input").withRequired(true).withShortName("i").withArgument(
- abuilder.withName("input").withMinimum(1).withMaximum(1).create()).withDescription("The Path for input data directory.")
- .create();
+ abuilder.withName("input").withMinimum(1).withMaximum(1).create())
+ .withDescription("The Path for input data directory.").create();
Option helpOpt = DefaultOptionCreator.helpOption();