Java Reference
In-Depth Information
// return the number of available resources that
// have coordinate name equal to value
private int
sumAvailable(String name, Object value){
Iterator it
#
resources.iterator();
int
sum
#
0;
while
(it.hasNext()){
Resource resource
#
(Resource)it.next();
if
(resource.match(name,value)){
sum
!#
resource.available;
}
}
return
sum;
}
}
The class
ResourcePreference
represents the preferences for limited
resources. It inherits most of the methods and attributes of class
PreferenceMap
. In addition there is a link to the
ResourceAllocator
. In par-
ticular, the methods
decrease()
and
bestProposal()
are overridden; they call
the version defined in the superclass and then they return the closest valid
proposal. The key method is
closest()
: it finds the closest valid proposal.
package
booking;
import
Negotiation.*;
import
java.util.*;
public class
ResourcePreference
extends
PreferenceMap {
ResourceAllocation resources;
Specification spec;
public
ResourcePreference
(Specification spec,ResourceAllocation resources ){
super(spec);
this
.resources
#
resources;
this
.spec
#
spec;
}
// decrease the value of an attribute in the proposal
// (i.e. substitute the value with the one having rank
// immediately lower) return the closest valid proposal
public
Proposal decrease
(Attribute attribute, Proposal proposal){
super.decrease(attribute,proposal);
if
(resources.valid(proposal)){
return
proposal;
}
Proposal close
#
closest(proposal);
return
close;
}
// return the best valid proposal