Adding PacketLevelSignatureExtractor.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / comparison / seqalignment / AlignmentPricer.java
1 package edu.uci.iotproject.comparison.seqalignment;
2
3 import java.util.function.ToIntBiFunction;
4 import java.util.function.ToIntFunction;
5
6 /**
7  * Provides a generic implementation for the calculation of the cost of aligning two elements of a sequence as part of
8  * the sequence alignment algorithm (the algorithm is implemented in {@link SequenceAlignment}).
9  *
10  * @param <T> The type of the elements that are being aligned.
11  *
12  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
14  */
15 public class AlignmentPricer<T> {
16
17     /**
18      * A function that provides the cost of aligning a {@link T} with a gap.
19      */
20     private final ToIntFunction<T> mGapCostFunction;
21
22     /**
23      * A function that provides the cost of aligning a {@link T} with some other {@link T}.
24      */
25     private final ToIntBiFunction<T,T> mAlignmentCostFunction;
26
27     /**
28      * Constructs a new {@link AlignmentPricer}.
29      *
30      * @param alignmentCostFunction A function that specifies the cost of aligning a {@link T} with some other {@link T}
31      *                              (e.g., based on the values of the properties of the two instances).
32      * @param gapCostFunction A function that specifies the cost of aligning a {@link T} with a gap. Note that the
33      *                        function is free to specify <em>different</em> gap costs for different {@link T}s.
34      */
35     public AlignmentPricer(ToIntBiFunction<T,T> alignmentCostFunction, ToIntFunction<T> gapCostFunction) {
36         mAlignmentCostFunction = alignmentCostFunction;
37         mGapCostFunction = gapCostFunction;
38     }
39
40     /**
41      * Calculate the cost of aligning {@code item1} with {@code item2}. If either of the two arguments is set to
42      * {@code null}, the cost of aligning the other argument with a gap will be returned. Note that both arguments
43      * cannot be {@code null} at the same time as that translates to aligning a gap with a gap, which is pointless.
44      *
45      * @param item1 The first of the two aligned objects. Set to {@code null} to calculate the cost of aligning
46      *              {@code item2} with a gap.
47      * @param item2 The second of the two aligned objects. Set to {@code null} to calculate the cost of aligning
48      *              {@code item2} with a gap.
49      * @return The cost of aligning {@code item1} with {@code item2}.
50      */
51     public int alignmentCost(T item1, T item2) {
52         // If both arguments are null, the caller is aligning a gap with a gap which is pointless might as well remove
53         // both gaps in that case!)
54         if (item1 == null && item2 == null) {
55             throw new IllegalArgumentException("Both arguments cannot be null: you are aligning a gap with a gap!");
56         }
57         // If one item is null, it means we're aligning an int with a gap.
58         // Invoke the provided gap cost function to get the gap cost.
59         if (item1 == null) {
60             return mGapCostFunction.applyAsInt(item2);
61         }
62         if (item2 == null) {
63             return mGapCostFunction.applyAsInt(item1);
64         }
65         // If both arguments are present, we simply delegate the task of calculating the cost of aligning the two items
66         // to the provided alignment cost function.
67         return mAlignmentCostFunction.applyAsInt(item1, item2);
68     }
69
70 }