You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2009/06/16 15:12:35 UTC
svn commit: r785197 -
/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java
Author: gsingers
Date: Tue Jun 16 13:12:35 2009
New Revision: 785197
URL: http://svn.apache.org/viewvc?rev=785197&view=rev
Log:
MAHOUT-134: Handle errors when decoding the clusters
Modified:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java?rev=785197&r1=785196&r2=785197&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java Tue Jun 16 13:12:35 2009
@@ -30,6 +30,8 @@
public class Cluster {
+ private static final String ERROR_UNKNOWN_CLUSTER_FORMAT="Unknown cluster format:\n";
+
public static final String DISTANCE_MEASURE_KEY = "org.apache.mahout.clustering.kmeans.measure";
public static final String CLUSTER_PATH_KEY = "org.apache.mahout.clustering.kmeans.path";
@@ -81,23 +83,30 @@
* Decodes and returns a Cluster from the formattedString
*
* @param formattedString a String produced by formatCluster
- * @return a new Canopy
+ * @return a decoded Cluster, not null
+ * @throws IllegalArgumentException when the string is wrongly formatted
*/
public static Cluster decodeCluster(String formattedString) {
- int beginIndex = formattedString.indexOf('[');
- String id = formattedString.substring(0, beginIndex);
- String center = formattedString.substring(beginIndex);
- char firstChar = id.charAt(0);
- boolean startsWithV = firstChar == 'V';
- if (firstChar == 'C' || startsWithV) {
- int clusterId = Integer.parseInt(formattedString.substring(1,
- beginIndex - 2));
- Vector clusterCenter = AbstractVector.decodeVector(center);
- Cluster cluster = new Cluster(clusterCenter, clusterId);
- cluster.converged = startsWithV;
- return cluster;
+ final int beginIndex = formattedString.indexOf('[');
+ final Cluster cluster;
+ if (beginIndex <= 0) {
+ throw new IllegalArgumentException(ERROR_UNKNOWN_CLUSTER_FORMAT + formattedString);
+ } else {
+ final String id = formattedString.substring(0, beginIndex);
+ final String center = formattedString.substring(beginIndex);
+ final char firstChar = id.charAt(0);
+ final boolean startsWithV = firstChar == 'V';
+ if (firstChar == 'C' || startsWithV) {
+ final int clusterId = Integer.parseInt(formattedString.substring(1,
+ beginIndex - 2));
+ final Vector clusterCenter = AbstractVector.decodeVector(center);
+ cluster = new Cluster(clusterCenter, clusterId);
+ cluster.converged = startsWithV;
+ } else {
+ throw new IllegalArgumentException(ERROR_UNKNOWN_CLUSTER_FORMAT + formattedString);
+ }
}
- return null;
+ return cluster;
}
/**