aboutsummaryrefslogtreecommitdiff
path: root/challenge-092/athanasius/perl/Interval.pm
blob: aed117cd9628b11582dda8b5ddb84493f00af4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!perl

###############################################################################
##
## Perl Weekly Challenge 092, Task #2: Insert Interval
##
###############################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

package Interval;

use strict;
use warnings;
use Moo;
use Regexp::Common qw( number );

#------------------------------------------------------------------------------
has start =>
#------------------------------------------------------------------------------
(
    is  => 'ro',
    isa =>  sub
            {
                $_[0] =~ / \A $RE{num}{int} \z /x
                    or die qq[ERROR: "$_[0]" is not an integer];
            },
    required => 1,
);

#------------------------------------------------------------------------------
has end =>
#------------------------------------------------------------------------------
(
    is  => 'ro',
    isa =>  sub
            {
                $_[0] =~ / \A $RE{num}{int} \z /x
                    or die qq[ERROR: "$_[0]" is not an integer];
            },
    required => 1,
);

#------------------------------------------------------------------------------
sub BUILD
#------------------------------------------------------------------------------
{
    my ($self) = @_;

    die sprintf qq[ERROR: Start "%d" is greater than end "%d"\n],
                $self->start,   $self->end
        unless  $self->start <= $self->end;
}

#------------------------------------------------------------------------------
sub display
#------------------------------------------------------------------------------
{
    my ($self) = @_;

    return sprintf '(%d,%d)', $self->start, $self->end;
}

#------------------------------------------------------------------------------
sub precedes
#------------------------------------------------------------------------------
{
    my ($self, $rhs) = @_;

    return $self->end < $rhs->start;
}

#------------------------------------------------------------------------------
sub merge
#------------------------------------------------------------------------------
{
    my ($self, $rhs) = @_;

    my  $minimum = $self->start <= $rhs->start ? $self->start : $rhs->start;
    my  $maximum = $self->end   >= $rhs->end   ? $self->end   : $rhs->end;

    return Interval->new( start => $minimum, end => $maximum );
}

###############################################################################
1;
###############################################################################