resultDefaults[i+count] = otherDefaults[getIndex(otherHash,
name)];
resultValidValues[i+count] = otherPld.getParamValueRange(name);
}
ParameterListDescriptorImpl resultPLD =
new ParameterListDescriptorImpl(null,
resultNames,
resultClasses,
resultDefaults,
resultValidValues);
// Both NC's are preferences
result = new NegotiableCapability(category,
capabilityName,
resultGenerators,
resultPLD,
true);
for (int i=0; i<commonNames.size(); i++) {
currParam = (String)commonNames.elementAt(i);
thisValue = (Negotiable)getObjectParameter(currParam);
otherValue =
(Negotiable)capability.getObjectParameter(currParam);
// If one of the values is null, select the other one, and
// negotiation succeeds. Note that this also takes care
// of the scenario when both are null, therefore the result
// is null, and on a non-pref, this would have failed the
// negotiation, but on a pref, it doesn't, so we just set
// null (otherValue) as the result and allow negotiation to
// succeed.
if (thisValue == null) {
result.setParameter(currParam, otherValue);
continue;
}
if (otherValue == null) {
result.setParameter(currParam, thisValue);
continue;
}
// Following only gets executed if neither of the two is
// a null, and therefore both have set values. If negotiation
// fails, the negotiation as a whole is failed, otherwise
// set the result on the resultant NC.
resultValue = thisValue.negotiate(otherValue);
if (resultValue == null) {
return null;
}
result.setParameter(currParam, resultValue);
}
// Copy the extra ones directly into the result
for (int i=0; i<thisExtraLength; i++) {
currParam = (String)thisExtras.elementAt(i);
result.setParameter(currParam,
(Negotiable)getObjectParameter(currParam));
}
for (int i=0; i<otherExtraLength; i++) {
currParam = (String)otherExtras.elementAt(i);
result.setParameter(currParam,
(Negotiable)capability.getObjectParameter(currParam));
}
break;
case 1:
// The given capability is a pref, while this is a non-pref
commonNames = commonElements(thisHash, otherHash);
commonHash = hashNames(commonNames);
thisExtras = removeAll(thisHash, commonHash);
// Create a new PLD which is the amalgamation of the two
// NC's PLD's
resultParams = new Vector(commonNames);
resultParams.addAll(thisExtras);
resultLength = resultParams.size();
resultNames = new String[resultLength];
for (int i=0; i<resultLength; i++) {
resultNames[i] = (String)resultParams.elementAt(i);
}
resultClasses = new Class[resultLength];
resultDefaults = new Object[resultLength];
resultValidValues = new Object[resultLength];
count = 0;
for (count=0; count<commonNames.size(); count++) {
name = (String)commonNames.elementAt(count);
resultClasses[count] = thisClasses[getIndex(thisHash, name)];
resultDefaults[count] = thisDefaults[getIndex(thisHash, name)];
resultValidValues[count] = pld.getParamValueRange(name);
}
for (int i=0; i<thisExtras.size(); i++) {
name = (String)thisExtras.elementAt(i);
resultClasses[i+count] = thisClasses[getIndex(thisHash, name)];
resultDefaults[i+count] = thisDefaults[getIndex(thisHash,
name)];
resultValidValues[i+count] = pld.getParamValueRange(name);
}
resultPLD = new ParameterListDescriptorImpl(null,
resultNames,
resultClasses,
resultDefaults,
resultValidValues);
result = new NegotiableCapability(category,
capabilityName,
resultGenerators,
resultPLD,
false);
for (int i=0; i<commonNames.size(); i++) {
currParam = (String)commonNames.elementAt(i);
thisValue = (Negotiable)getObjectParameter(currParam);
otherValue =
(Negotiable)capability.getObjectParameter(currParam);
if (thisValue == null) {
// If non-pref doesn't have value, negotiation fails right
// away
return null;
}
if (otherValue == null) {
// If pref value is null, then non-pref's value wins.
// This needs to be done separately, since
// non-null.negotiate(null) returns null, which is *not*
// what we want.
result.setParameter(currParam, thisValue);
} else {
// Do the negotiation.
resultValue = thisValue.negotiate(otherValue);
if (resultValue == null) {
// Negotiation on one parameter failed, so negotiation
// on the entire NC has also failed, return null to
// signify this
return null;
} else {
result.setParameter(currParam, resultValue);
}
}
}
// Copy the extra ones directly into the result
for (int i=0; i<thisExtras.size(); i++) {
currParam = (String)thisExtras.elementAt(i);
resultValue = (Negotiable)getObjectParameter(currParam);
if (resultValue == null)
return null;
result.setParameter(currParam, resultValue);
}
break;
case 2:
// The given capability is a non-pref, while this is a pref
commonNames = commonElements(thisHash, otherHash);
commonHash = hashNames(commonNames);
otherExtras = removeAll(otherHash, commonHash);
// Create a new PLD which is the amalgamation of the two
// NC's PLD's
resultParams = new Vector(commonNames);
resultParams.addAll(otherExtras);
resultLength = resultParams.size();
resultNames = new String[resultLength];
for (int i=0; i<resultLength; i++) {
resultNames[i] = (String)resultParams.elementAt(i);
}
resultClasses = new Class[resultLength];
resultDefaults = new Object[resultLength];
resultValidValues = new Object[resultLength];
count = 0;
for (count=0; count<commonNames.size(); count++) {
name = (String)commonNames.elementAt(count);
resultClasses[count] = thisClasses[getIndex(thisHash, name)];
resultDefaults[count] = thisDefaults[getIndex(thisHash, name)];
resultValidValues[count] = pld.getParamValueRange(name);
}
for (int i=0; i<otherExtras.size(); i++) {
name = (String)otherExtras.elementAt(i);
resultClasses[i+count] = otherClasses[getIndex(otherHash,
name)];
resultDefaults[i+count] = otherDefaults[getIndex(otherHash,
name)];
resultValidValues[i+count] = otherPld.getParamValueRange(name);
}
resultPLD = new ParameterListDescriptorImpl(null,
resultNames,
resultClasses,
resultDefaults,
resultValidValues);