4c74eb86649f594555ee3bfb168dfa32f160586b
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / detection / AbstractClusterMatcher.java
1 package edu.uci.iotproject.detection;
2
3 import edu.uci.iotproject.Conversation;
4 import org.pcap4j.core.PcapPacket;
5
6 import java.util.List;
7 import java.util.Objects;
8
9 /**
10  * TODO add class documentation.
11  *
12  * @author Janus Varmarken
13  */
14 abstract public class AbstractClusterMatcher {
15
16     protected final List<List<PcapPacket>> mCluster;
17
18
19     protected AbstractClusterMatcher(List<List<PcapPacket>> cluster) {
20         // ===================== PRECONDITION SECTION =====================
21         cluster = Objects.requireNonNull(cluster, "cluster cannot be null");
22         if (cluster.isEmpty() || cluster.stream().anyMatch(inner -> inner.isEmpty())) {
23             throw new IllegalArgumentException("cluster is empty (or contains an empty inner List)");
24         }
25         mCluster = pruneCluster(cluster);
26     }
27
28     /**
29      * Allows subclasses to specify how to prune input cluster provided to the constructor.
30      * @param cluster The input cluster provided to the constructor.
31      * @return The pruned cluster to use in place of the input cluster.
32      */
33     abstract protected List<List<PcapPacket>> pruneCluster(List<List<PcapPacket>> cluster);
34
35     // TODO: move Direction outside Conversation so that this is less confusing.
36 //    abstract protected Conversation.Direction[] getPacketDirections(List<PcapPacket> packets);
37
38 }