« Many/Scheduler/UniquenessScheduler/WeightedRandom.pm
↑ Summary
Many/Scheduler/UniquenessScheduler/WRR.pm »
| 1 | package Geo::Coder::Many::Scheduler::OrderedList; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Geo::Coder::Many::Scheduler'; | ||
| 7 | |||
| 8 | our $VERSION = '0.01'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Geo::Coder::Many::Scheduler::OrderedList - Ordered list scheduler | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This is a scheduler representing a strict preferential order - it will always | ||
| 17 | use the first geocoder in the list, unless it fails, in which case it'll fall | ||
| 18 | back to the second in the list, and then the third, and so on. | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =head2 new | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $ra_items = shift; | ||
| 29 | |||
| 30 | my @names = | ||
| 31 | map { $_->{name} } | ||
| 32 | sort { $b->{weight} <=> $a->{weight}; } @$ra_items; | ||
| 33 | |||
| 34 | my $self = { items => \@names }; | ||
| 35 | bless $self, $class; | ||
| 36 | |||
| 37 | $self->reset_available(); | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | =head2 reset_available | ||
| 43 | |||
| 44 | Resets the list back to how it was initially set. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub reset_available { | ||
| 49 | my $self = shift; | ||
| 50 | @{$self->{ available_items }} = @{$self->{items}}; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 get_next_unique | ||
| 55 | |||
| 56 | Returns the next item in the list. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub get_next_unique { | ||
| 61 | my $self = shift; | ||
| 62 | return shift @{$self->{ available_items }}; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head2 next_available | ||
| 66 | |||
| 67 | Returns zero if there are items available, and | ||
| 68 | -1 if there aren't. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub next_available { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | if ( 0 < @{$self->{ available_items }} ) { | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |