1 11 package org.eclipse.core.internal.expressions; 12 13 import java.util.Collection ; 14 15 import org.w3c.dom.Element ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 20 import org.eclipse.core.expressions.EvaluationResult; 21 import org.eclipse.core.expressions.Expression; 22 import org.eclipse.core.expressions.ExpressionInfo; 23 import org.eclipse.core.expressions.ICountable; 24 import org.eclipse.core.expressions.IEvaluationContext; 25 26 27 public class CountExpression extends Expression { 28 29 private static final int ANY_NUMBER= 5; 30 private static final int EXACT= 4; 31 private static final int ONE_OR_MORE= 3; 32 private static final int NONE_OR_ONE= 2; 33 private static final int NONE= 1; 34 private static final int UNKNOWN= 0; 35 36 39 private static final int HASH_INITIAL= CountExpression.class.getName().hashCode(); 40 41 private int fMode; 42 private int fSize; 43 44 public CountExpression(IConfigurationElement configElement) { 45 String size = configElement.getAttribute(ATT_VALUE); 46 initializeSize(size); 47 } 48 49 public CountExpression(Element element) { 50 String size = element.getAttribute(ATT_VALUE); 51 initializeSize(size.length() > 0 ? size : null); 52 } 53 54 public CountExpression(String size) { 55 initializeSize(size); 56 } 57 58 private void initializeSize(String size) { 59 if (size == null) 60 size= "*"; if (size.equals("*")) fMode= ANY_NUMBER; 63 else if (size.equals("?")) fMode= NONE_OR_ONE; 65 else if (size.equals("!")) fMode= NONE; 67 else if (size.equals("+")) fMode= ONE_OR_MORE; 69 else { 70 try { 71 fSize= Integer.parseInt(size); 72 fMode= EXACT; 73 } catch (NumberFormatException e) { 74 fMode= UNKNOWN; 75 } 76 } 77 } 78 79 public EvaluationResult evaluate(IEvaluationContext context) throws CoreException { 80 Object var= context.getDefaultVariable(); 81 int size; 82 if (var instanceof Collection ) { 83 size= ((Collection )var).size(); 84 } else { 85 ICountable countable= Expressions.getAsICountable(var, this); 86 if (countable == null) 87 return EvaluationResult.NOT_LOADED; 88 size= countable.count(); 89 } 90 switch (fMode) { 91 case UNKNOWN: 92 return EvaluationResult.FALSE; 93 case NONE: 94 return EvaluationResult.valueOf(size == 0); 95 case NONE_OR_ONE: 96 return EvaluationResult.valueOf(size == 0 || size == 1); 97 case ONE_OR_MORE: 98 return EvaluationResult.valueOf(size >= 1); 99 case EXACT: 100 return EvaluationResult.valueOf(fSize == size); 101 case ANY_NUMBER: 102 return EvaluationResult.TRUE; 103 } 104 return EvaluationResult.FALSE; 105 } 106 107 public void collectExpressionInfo(ExpressionInfo info) { 108 info.markDefaultVariableAccessed(); 109 } 110 111 public boolean equals(final Object object) { 112 if (!(object instanceof CountExpression)) 113 return false; 114 115 final CountExpression that= (CountExpression)object; 116 return (this.fMode == that.fMode) && (this.fSize == that.fSize); 117 } 118 119 protected int computeHashCode() { 120 return HASH_INITIAL * HASH_FACTOR + fMode 121 * HASH_FACTOR + fSize; 122 } 123 } 124 | Popular Tags |