###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::Data::Integer - integer data type for PSGConf::Data
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Data::Integer;

use strict;
use integer;

#use overload
#	'""'		=> 'get',
##	'0+'		=> 'get',
#	'+'		=> 'numeric_add',
#	'='		=> 'set',
#	'eq'		=> 'equals',
##	'fallback'	=> 1
#	;

use PSGConf::Data;

our @ISA = qw(PSGConf::Data);


###############################################################################
###  equals method (for conditional expressions)
###############################################################################

sub equals
{
	my ($self, $value) = @_;

	return $self->eq($value);
}


sub eq
{
	my ($self, $value) = @_;

#	print "==> equals(" . ref($self) . "='$self->{'value'}', '$value')\n";

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() == $value);
}


###############################################################################
###  not-equals method (for conditional expressions)
###############################################################################

sub ne
{
	my ($self, $value) = @_;

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() != $value);
}


###############################################################################
###  greater-than method (for conditional expressions)
###############################################################################

sub gt
{
	my ($self, $value) = @_;

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() > $value);
}


###############################################################################
###  less-than method (for conditional expressions)
###############################################################################

sub lt
{
	my ($self, $value) = @_;

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() < $value);
}


###############################################################################
###  less-than-or-equal-to method (for conditional expressions)
###############################################################################

sub le
{
	my ($self, $value) = @_;

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() <= $value);
}


###############################################################################
###  greater-than-or-equal-to method (for conditional expressions)
###############################################################################

sub ge
{
	my ($self, $value) = @_;

	warn "comparing to \"$value\" when object is undefined\n"
		if ( ! defined $self->get() );

	return ($self->get() >= $value);
}


###############################################################################
###  set method
###############################################################################

sub set
{
	my ($self, $value) = @_;

#	print "==> Integer::set($value)\n";

	die "non-scalar value specified for integer variable\n"
		if (ref($value));

	### Support the use of octal and hex format
	### for numbers as well as decimal.
	if ( $value =~ m/^0x[0-9a-f]+$/i ) {
		$self->SUPER::set(hex($value));
	} elsif ( $value =~ m/^0[0-7]+$/ ) {
		$self->SUPER::set(oct($value));
	} elsif ( $value =~ m/^-?\d+$/) {
		$self->SUPER::set(int($value));
	} else {
		die "non-numeric value specified for integer variable\n";
	}
}


###############################################################################
###  add method
###############################################################################

sub add
{
	my ($self, $value) = @_;

	$self->set($self->get() + $value);
}


###############################################################################
###  subtract method
###############################################################################

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

	$self->set($self->get() - $value);
}


###############################################################################
###  divide method
###############################################################################

sub div
{
	my ($self, $value) = @_;

	$self->set(int($self->get()) / $value);
}


###############################################################################
###  multiply method
###############################################################################

sub mult
{
	my ($self, $value) = @_;

	$self->set($self->get() * $value);
}


###############################################################################
###  increment method
###############################################################################

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

	$self->set($self->get()+1);
}


###############################################################################
###  decrement method
###############################################################################

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

	$self->set($self->get()-1);
}


###############################################################################
###  documentation
###############################################################################

1;

__END__

=head1 NAME

PSGConf::Data::Integer - integer data type class for PSGConf

=head1 SYNOPSIS

  use PSGConf::Data::Integer;

  $psgconf->register_data(
		'intobj'	=> PSGConf::Data::Integer->new()
	);

=head1 DESCRIPTION

The B<PSGConf::Data::Integer> module provides a class that
represents an integer value in an object so that it can be used with
B<PSGConf>.  Its methods can be used to manipulate the
encapsulated integer value from the B<PSGConf> data store(s).

The B<PSGConf::Data::Integer> class is derived from the
B<<PSGConf::Data> class, but it defines/overrides the
following methods:

=over 4

=item set()

Sets the object's value to the supplied value.  The value must consist of
only digit characters, with an optional leading "-" character to denote
a negative value.

=item add()

Adds the supplied value to the object's value.

=item sub()

Subtracts the supplied value from the object's value.

=item div()

Divides the object's value by the supplied value.

=item mult()

Multiplies the object's value by the supplied value.

=item incr()

Increments the object's value by one.

=item decr()

Decrements the object's value by one.

=item equals()

Same as the eq() method.

=item eq()

Returns true if the object's value is equal to the supplied value.

=item ne()

Returns true if the object's value is not equal to the supplied value.

=item gt()

Returns true if the object's value is greater than the supplied value.

=item lt()

Returns true if the object's value is less than the supplied value.

=item ge()

Returns true if the object's value is greater than or equal to the
supplied value.

=item le()

Returns true if the object's value is less than or equal to the
supplied value.

=back

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<PSGConf::Data>

=cut



syntax highlighted by Code2HTML, v. 0.9.1