package StatusLog;

sub new {
    my ($this) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};
    bless($self, $class);

    $self->{"log"} = {};
    $self->{"sorted"} = 1;
    return $self;
}


sub push {
    my( $self, $item, $entry ) = @_;

    unless( $self->{"log"}->{$item} ) {
	$self->{"log"}->{$item} = [];
    }
    push( @{$self->{"log"}->{$item}}, $entry );
    $self->{"sorted"} = 0;
}


sub get {
    my( $self, $item ) = @_;
    return unless( defined $self->{"log"}->{$item} );
    return( $self->{"log"}->{$item} );
}


sub put {
    my( $self, $item, $log ) = @_;
    $self->{"log"}->{$item} = $log;
    $self->{"sorted"} = 0;
}


sub delete {
    my( $self, $item ) = @_;

    delete $self->{"log"}->{$item};
}


sub get_items {
    my( $self ) = @_;
    return( keys %{$self->{"log"}} );
}


sub sort {
    my( $self ) = @_;

    return if( $self->{"sorted"} );
    foreach my $item ($self->get_items()) {
	$list = $self->get( $item );
	my @sorted = sort { $a->{"start"} <=> $b->{"start"} } @$list;
	$self->put( $item, \@sorted );
    }
    $self->{"sorted"} = 1;
}


1;
