use of org.eclipse.core.expressions.ICountable in project eclipse.platform.runtime by eclipse.
the class CountExpression method evaluate.
@Override
public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
Object var = context.getDefaultVariable();
int size;
if (var instanceof Collection) {
size = ((Collection<?>) var).size();
} else {
ICountable countable = Expressions.getAsICountable(var, this);
if (countable == null)
return EvaluationResult.NOT_LOADED;
size = countable.count();
}
switch(fMode) {
case UNKNOWN:
return EvaluationResult.FALSE;
case NONE:
return EvaluationResult.valueOf(size == 0);
case NONE_OR_ONE:
return EvaluationResult.valueOf(size == 0 || size == 1);
case ONE_OR_MORE:
return EvaluationResult.valueOf(size >= 1);
case EXACT:
return EvaluationResult.valueOf(fSize == size);
case ANY_NUMBER:
return EvaluationResult.TRUE;
case LESS_THAN:
return EvaluationResult.valueOf(size < fSize);
case GREATER_THAN:
return EvaluationResult.valueOf(size > fSize);
}
return EvaluationResult.FALSE;
}
use of org.eclipse.core.expressions.ICountable in project eclipse.platform.runtime by eclipse.
the class Expressions method getAsICountable.
/**
* Converts the given variable into an <code>ICountable</code>. If a corresponding adapter can't be found an
* exception is thrown. If the corresponding adapter isn't loaded yet, <code>null</code> is returned.
*
* @param var the variable to turn into an <code>ICountable</code>
* @param expression the expression referring to the variable
*
* @return the <code>ICountable</code> or <code>null<code> if a corresponding adapter isn't loaded yet
*
* @throws CoreException if the var can't be adapted to an <code>ICountable</code>
*/
public static ICountable getAsICountable(Object var, Expression expression) throws CoreException {
if (var instanceof ICountable) {
return (ICountable) var;
} else {
IAdapterManager manager = Platform.getAdapterManager();
ICountable result = manager.getAdapter(var, ICountable.class);
if (result != null)
return result;
if (manager.queryAdapter(var, ICountable.class.getName()) == IAdapterManager.NOT_LOADED)
return null;
throw new CoreException(new ExpressionStatus(ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION, Messages.format(ExpressionMessages.Expression_variable_not_countable, expression.toString())));
}
}
Aggregations