} else {
negStatus = 3;
}
}
ParameterListDescriptor pld = getParameterListDescriptor();
ParameterListDescriptor otherPld =
capability.getParameterListDescriptor();
String thisNames[] = pld.getParamNames();
if (thisNames == null)
thisNames = new String[0];
String otherNames[] = otherPld.getParamNames();
if (otherNames == null)
otherNames = new String[0];
Hashtable thisHash = hashNames(thisNames);
Hashtable otherHash = hashNames(otherNames);
Class thisClasses[] = pld.getParamClasses();
Class otherClasses[] = otherPld.getParamClasses();
Object thisDefaults[] = pld.getParamDefaults();
Object otherDefaults[] = otherPld.getParamDefaults();
NegotiableCapability result = null;
String currParam;
Negotiable thisValue, otherValue, resultValue;
ArrayList resultGenerators = new ArrayList();
if (generators != null)
resultGenerators.addAll(generators);
if (capability.getGenerators() != null)
resultGenerators.addAll(capability.getGenerators());
switch (negStatus) {
case 0:
Vector commonNames = commonElements(thisHash, otherHash);
Hashtable commonHash = hashNames(commonNames);
Vector thisExtras = removeAll(thisHash, commonHash);
Vector otherExtras = removeAll(otherHash, commonHash);
int thisExtraLength = thisExtras.size();
int otherExtraLength = otherExtras.size();
// Create a new PLD which is the amalgamation of the two
// NC's PLD's
Vector resultParams = new Vector(commonNames);
resultParams.addAll(thisExtras);
resultParams.addAll(otherExtras);
int resultLength = resultParams.size();
String resultNames[] = new String[resultLength];
for (int i=0; i<resultLength; i++) {
resultNames[i] = (String)resultParams.elementAt(i);
}
Class resultClasses[] = new Class[resultLength];
Object resultDefaults[] = new Object[resultLength];
Object resultValidValues[] = new Object[resultLength];
String name;
int count;
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<thisExtraLength; i++) {
name = (String)thisExtras.elementAt(i);
resultClasses[count+i] = thisClasses[getIndex(thisHash, name)];
resultDefaults[count+i] = thisDefaults[getIndex(thisHash,
name)];
resultValidValues[count+i] = pld.getParamValueRange(name);
}
count += thisExtraLength;
for (int i=0; i<otherExtraLength; 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);
}
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,