nah, check this out. this is a little hacky, but it's as efficient as
we can get.
the three methods below use this field:
private Object validators = null;
to manage an array or a single validator instance. this is ideal for
session size,
which is really all we care about here. i wouldn't worry about the
reallocation
overhead because (1) few people are going to add more than a couple
validators and (2) the reallocation is all going to occur in eden.
should be like
lightning. and then we end up with the absolute minimum size we can
have for
the validators attached to a form component! and that's what we want.
yeah, it's ugly because of the cast, but the implementation is dirt
simple and
no client will ever know about it!
/**
* @param validator
* The validator to add to the validators Object (which
may be an
* array of IValidators or a single instance, for efficiency)
*/
private void validators_add(final IValidator validator)
{
if (this.validators == null)
{
this.validators = validator;
}
else
{
// Get current list size
final int size = validators_size();
// Create array that holds size + 1 elements
final IValidator[] validators = new IValidator[size + 1];
// Loop through existing validators copying them
for (int i = 0; i < size; i++)
{
validators[i] = validators_get(i);
}
// Add new validator to the end
validators[size] = validator;
// Save new validator list
this.validators = validators;
}
}
/**
* Gets validator from validators Object (which may be an array of
* IValidators or a single instance, for efficiency) at the given index
*
* @param index
* The index of the validator to get
* @return The validator
*/
private IValidator validators_get(int index)
{
if (this.validators == null)
{
throw new IndexOutOfBoundsException();
}
if (this.validators instanceof IValidator[])
{
return ((IValidator[])validators)[index];
}
return (IValidator)validators;
}
/**
* @return The number of validators in the validators Object (which
may be
* an array of IValidators or a single instance, for efficiency)
*/
private int validators_size()
{
if (this.validators == null)
{
return 0;
}
if (this.validators instanceof IValidator[])
{
return ((IValidator[])validators).length;
}
return 1;
}
Phil Kulak wrote:
Yea, that's what I was saying about making our own implementation of
ArrayList. Arrays are more efficient, memory wise, then creating
objects. I think.
I would say, start it off at length == 2 (since I'd say 99% of the
time it's one or two), then bump it up (with array copying) by one
each time instead of doubling it each time like ArrayList does.
On 8/11/05, Jonathan Locke <[EMAIL PROTECTED]> wrote:
the one thing i can think of that would actually be more efficient that
what we've got now is to do a union using Object between IValidator
and IValidator[] and manage the array by hand. that would actually
get rid of all node objects in cases where we have more than 2. also,
the array object should be smaller, when trimmed to size than the current
node object.
Jonathan Locke wrote:
ooh. actually this is incorrect too.
for 1, it's the object directly
for 2, there is only ONE node (because of left/right pointers!)
for N, there are N - 1 node objects
and the cases of 1 or 2 validators is really the majority.
Phil Kulak wrote:
Well, right now you're doing exactly what LinkedList does. You're
wrapping every object in a node and including two object references.
Where's the space savings? You could get rid of half the objects by
using an array and essentially reimplementing ArrayList. The downside
here is that if I add a string validator to a RequiredTextField, I
have no gaurantees that the value I get will be non-null.
I do like what you did with the booleans though. That's tight. :)
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle
Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing
& QA
Security * Process Improvement & Measurement *
http://www.sqe.com/bsce5sf
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle
Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing
& QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop