/*******************************************************************************
* Copyright 2011 The Regents of the University of California
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.ohmage.conditionevaluator.comparator;
import java.util.Iterator;
import java.util.List;
import org.ohmage.conditionevaluator.DataPoint;
/**
* Comparator for the multi_choice_custom prompt type.
* Conditions on multi_choice_custom can only be "=="
* and "!=" so all other condition operators return false.
* In practice, the configuration validator doesn't even
* allow those operators in the condition.
*
* @author Mohamad Monibi
*
*/
public class MultiChoiceCustomDataPointComparator extends BasicDataPointComparator {
// The methods here all need to be updated to support conditions after changes made due to 2.9 api changes
// dataPoint.getValue() will now return a List of Strings instead of Integers
// the integer keys for those strings need to be looked up in the campaign config
// then the keys can be compared to the value parameter (after casting to Integer)
@Override
boolean equals(DataPoint dataPoint, String value) {
// Run through all the responses, if any of them are true assume the whole thing is true
Integer valueToCompare = Integer.parseInt(value);
Integer dataPointValue = null;
@SuppressWarnings("unchecked")
List<Integer> dataPointValues = (List<Integer>) dataPoint.getIndexes();
Iterator<Integer> dataPointValuesIterator = dataPointValues.iterator();
while (dataPointValuesIterator.hasNext()) {
dataPointValue = dataPointValuesIterator.next();
if (dataPointValue.compareTo(valueToCompare) == 0) {
return true;
}
}
return false;
}
@Override
boolean greaterThan(DataPoint dataPoint, String value) {
// not allowed
// throw exception instead?
return false;
}
@Override
boolean greaterThanOrEquals(DataPoint dataPoint, String value) {
// not allowed
// throw exception instead?
return false;
}
@Override
boolean lessThan(DataPoint dataPoint, String value) {
// not allowed
// throw exception instead?
return false;
}
@Override
boolean lessThanOrEquals(DataPoint dataPoint, String value) {
// not allowed
// throw exception instead?
return false;
}
@Override
boolean notEquals(DataPoint dataPoint, String value) {
// Run through all the responses, if any of them are true assume the whole thing is true
Integer valueToCompare = Integer.parseInt(value);
Integer dataPointValue = null;
@SuppressWarnings("unchecked")
List<Integer> dataPointValues = (List<Integer>) dataPoint.getIndexes();
Iterator<Integer> dataPointValuesIterator = dataPointValues.iterator();
while (dataPointValuesIterator.hasNext()) {
dataPointValue = dataPointValuesIterator.next();
if (dataPointValue.compareTo(valueToCompare) == 0) {
return false;
}
}
return true;
}
}