Passed
Branch master (697a4f)
by P.R.
01:30
created

Allen.relation()   F

Complexity

Conditions 10

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 3.1304
cc 10

How to fix   Complexity   

Complexity

Complex classes like Allen.relation() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
ETLT
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
9
10
class Allen:
11
    """
12
    Utility class for Allen's interval algebra, https://en.wikipedia.org/wiki/Allen%27s_interval_algebra.
13
    """
14
    # ------------------------------------------------------------------------------------------------------------------
15
    X_BEFORE_Y = 1
16
    X_MEETS_Y = 2
17
    X_OVERLAPS_WITH_Y = 3
18
    X_STARTS_Y = 4
19
    X_DURING_Y = 5
20
    X_FINISHES_Y = 6
21
    X_EQUAL_Y = 0
22
    X_BEFORE_Y_INVERSE = -1
23
    X_MEETS_Y_INVERSE = -2
24
    X_OVERLAPS_WITH_Y_INVERSE = -3
25
    X_STARTS_Y_INVERSE = -4
26
    X_DURING_Y_INVERSE = -5
27
    X_FINISHES_Y_INVERSE = -6
28
29
    # ------------------------------------------------------------------------------------------------------------------
30
    @staticmethod
31
    def relation(x_start, x_end, y_start, y_end):
32
        """
33
        Returns the relation between two intervals.
34
35
        :param int x_start: The start point of the first interval.
36
        :param int x_end: The end point of the first interval.
37
        :param int y_start: The start point of the second interval.
38
        :param int y_end: The end point of the second interval.
39
40
        :rtype: int
41
        """
42
        diff_start = y_start - x_start
43
        diff_end = y_end - x_end
44
        gab = y_start - x_end
45
46
        if diff_end == 0:
47
            if diff_start == 0:
48
                return Allen.X_EQUAL_Y
49
50
            if diff_start < 0:
51
                return Allen.X_FINISHES_Y
52
53
            return Allen.X_FINISHES_Y_INVERSE
54
55
        if diff_end < 0:
56
            return -Allen.relation(y_start, y_end, x_start, x_end)
57
58
        if gab > 1:
59
            return Allen.X_BEFORE_Y
60
61
        if gab == 1:
62
            return Allen.X_MEETS_Y
63
64
        if diff_start > 0:
65
            return Allen.X_OVERLAPS_WITH_Y
66
67
        if diff_start == 0:
68
            return Allen.X_STARTS_Y
69
70
        if diff_start < 0:
71
            return Allen.X_DURING_Y
72
73
# ----------------------------------------------------------------------------------------------------------------------
74