KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > CountExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.expressions;
12
13 import java.util.Collection JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
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     /**
37      * The seed for the hash code for all count expressions.
38      */

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 JavaDoc size = configElement.getAttribute(ATT_VALUE);
46         initializeSize(size);
47     }
48
49     public CountExpression(Element JavaDoc element) {
50         String JavaDoc size = element.getAttribute(ATT_VALUE);
51         initializeSize(size.length() > 0 ? size : null);
52     }
53
54     public CountExpression(String JavaDoc size) {
55         initializeSize(size);
56     }
57     
58     private void initializeSize(String JavaDoc size) {
59         if (size == null)
60             size= "*"; //$NON-NLS-1$
61
if (size.equals("*")) //$NON-NLS-1$
62
fMode= ANY_NUMBER;
63         else if (size.equals("?")) //$NON-NLS-1$
64
fMode= NONE_OR_ONE;
65         else if (size.equals("!")) //$NON-NLS-1$
66
fMode= NONE;
67         else if (size.equals("+")) //$NON-NLS-1$
68
fMode= ONE_OR_MORE;
69         else {
70             try {
71                 fSize= Integer.parseInt(size);
72                 fMode= EXACT;
73             } catch (NumberFormatException JavaDoc e) {
74                 fMode= UNKNOWN;
75             }
76         }
77     }
78
79     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
80         Object JavaDoc var= context.getDefaultVariable();
81         int size;
82         if (var instanceof Collection JavaDoc) {
83             size= ((Collection JavaDoc)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 JavaDoc 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