Java Reference
In-Depth Information
Next, the new password is verified as having an acceptable format that is
within the minimum and maximum size limits and contains at least one
numeric value. The verifyFormat() method of the Password class, which is dis-
cussed later in the chapter, completes this verification. Because only encrypted
password values are stored in the password history, the encrypt() method of
the Password class then encrypts the new password. The encrypt() method is
discussed later in this chapter.
As noted in the requirements document, new passwords cannot match any
contained in the active history. The set() method thus calls the contains()
method of the ArrayList object to determine whether the encrypted password
value is in the password history list. If the encrypted password value is in the
password history list, an exception is thrown.
If a new password passes all of these conditions, it is ready to be added to
the password history list. If the list already is at its maximum size, the oldest
password is removed. The new password now is added to the list by calling the
ArrayList add() method , which returns a boolean value indicating that the item
was added. A collection that supports an add() method must return false only
if the collection does not allow duplicate items and the item is already in the
collection. Because an ArrayList does allow duplicates, this method always
should return true. If it does not, this indicates an internal error in the imple-
mentation of the ArrayList collection, which is unlikely. The set() method then
must determine if the password should expire automatically. If so, the set()
method must set the remaining uses to the maximum uses for a new password.
When a user enters a password, the validate() method of the Password class
validates the password by comparing it to the current password and, if it
matches, by determining that the password has not expired. Additionally, if the
password is set to expire automatically upon each successful use, the validate()
method decrements the remaining uses. Figure 9-30 displays the pseudocode
that outlines the logic for the validate() method.
The validate() method is similar to the set() method, in that it must verify
and encrypt the password value provided as a parameter. The validate() method,
however, does not trim the entered password to eliminate leading and trailing
white space, because a user always is required to supply exactly the correct
password.
The remainder of the logic for these two methods is rather different. The
set() method must determine that the supplied password value is not in the
password history list and then, if it is not, add it to the password history list as
the current password. The validate() method, however, not only must determine
that the supplied password value is in the password history list, but also that it is
the latest, or current, entry. This validation can be completed only if the history
J ava 2
J ava 2
v 5 . 0
JAVA UPDATE
v 5 . 0
Autoboxing/unboxing
To use a primitive data
type where you need
an object requires con-
verting the primitive by
boxing it with its corre-
sponding wrapper class,
as in converting from
an int to an Integer to
add it to an ArrayList.
To obtain the int value
from the Integer object,
you would unbox it by
using the intValue()
method. Autoboxing
and unboxing auto-
mate this process.
Although primitives
appear as interchange-
able with objects, the
conversions are still tak-
ing place. When used
with generics (see page
574), the coding is
greatly simplified.
The following code
compares adding and
then getting an item
from an ArrayList of
Integers with and with-
out autoboxing/un-
boxing.
//Without Autoboxing/auto-unboxing:
list.add ( O, new Integer ( 5 )) ;
int number = ( list.get ( O )) .intValue () ;
// boxing an int with Integer
// unboxing int value from Integer
//With Autoboxing/auto-unboxing:
list.add ( O, 10 ) ;
int number2 = list.get ( O ) ;
// autoboxing int to Integer
//auto-unboxing int from Integer
Search WWH ::




Custom Search