Make original ClusterMatcher inherit from AbstractClusterMatcher. Reorganize code...
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / detection / AbstractClusterMatcher.java
index 4c74eb86649f594555ee3bfb168dfa32f160586b..bda6493cb04978f2eafc178ff27d15b27821871e 100644 (file)
@@ -1,32 +1,43 @@
 package edu.uci.iotproject.detection;
 
 package edu.uci.iotproject.detection;
 
-import edu.uci.iotproject.Conversation;
 import org.pcap4j.core.PcapPacket;
 
 import java.util.List;
 import java.util.Objects;
 
 /**
 import org.pcap4j.core.PcapPacket;
 
 import java.util.List;
 import java.util.Objects;
 
 /**
- * TODO add class documentation.
+ * Base class for classes that search a traffic trace for sequences of packets that "belong to" a given cluster (in
+ * other words, classes that attempt to classify traffic as pertaining to a given cluster).
  *
  *
- * @author Janus Varmarken
+ * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
+ * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
  */
 abstract public class AbstractClusterMatcher {
 
  */
 abstract public class AbstractClusterMatcher {
 
+    /**
+     * The cluster that describes the sequence of packets that this {@link AbstractClusterMatcher} is trying to detect
+     * in the observed traffic.
+     */
     protected final List<List<PcapPacket>> mCluster;
 
     protected final List<List<PcapPacket>> mCluster;
 
-
     protected AbstractClusterMatcher(List<List<PcapPacket>> cluster) {
         // ===================== PRECONDITION SECTION =====================
         cluster = Objects.requireNonNull(cluster, "cluster cannot be null");
         if (cluster.isEmpty() || cluster.stream().anyMatch(inner -> inner.isEmpty())) {
             throw new IllegalArgumentException("cluster is empty (or contains an empty inner List)");
         }
     protected AbstractClusterMatcher(List<List<PcapPacket>> cluster) {
         // ===================== PRECONDITION SECTION =====================
         cluster = Objects.requireNonNull(cluster, "cluster cannot be null");
         if (cluster.isEmpty() || cluster.stream().anyMatch(inner -> inner.isEmpty())) {
             throw new IllegalArgumentException("cluster is empty (or contains an empty inner List)");
         }
+        for (List<PcapPacket> clusterMember : cluster) {
+            if (clusterMember.size() != cluster.get(0).size()) {
+                throw new IllegalArgumentException("All sequences in cluster must contain the same number of packets");
+            }
+        }
+        // ================================================================
+        // Let the subclass prune the provided cluster
         mCluster = pruneCluster(cluster);
     }
 
     /**
         mCluster = pruneCluster(cluster);
     }
 
     /**
-     * Allows subclasses to specify how to prune input cluster provided to the constructor.
+     * Allows subclasses to specify how to prune the input cluster provided to the constructor.
      * @param cluster The input cluster provided to the constructor.
      * @return The pruned cluster to use in place of the input cluster.
      */
      * @param cluster The input cluster provided to the constructor.
      * @return The pruned cluster to use in place of the input cluster.
      */