Skip to main content
summaryrefslogtreecommitdiffstats
blob: 611c8bbe42b03847740c5d63dfe6845d26f81d20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jem.internal.instantiation.base;
/*
 *  $RCSfile: FeatureValueProvider.java,v $
 *  $Revision: 1.8 $  $Date: 2005/08/24 20:20:24 $ 
 */

import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * Implementers of this interface (an EMF EObject subclass) provide a visiter
 * capability to the set features on the EObject. This way only the set features
 * are presented to the visitor. This can save a lot of time.
 * 
 * @since 1.1.0
 */
public interface FeatureValueProvider {
	
	/**
	 * A helper class for FeatureValueProvider.
	 * 
	 * @see FeatureValueProviderHelper#visitSetFeatures(EObject, Visitor)
	 * 
	 * @since 1.1.0
	 */
	public static class FeatureValueProviderHelper {
		
		/**
		 * A helper to handle where the object may or may not be
		 * implement FeatureValueProvider. This way it can be a
		 * common access to do it.
		 * 
		 * @param eobject
		 * @param visitor
		 * @return
		 * 
		 * @since 1.1.0
		 */
		public static Object visitSetFeatures(EObject eobject, Visitor visitor) {
			if (eobject instanceof FeatureValueProvider)
				return ((FeatureValueProvider)eobject).visitSetFeatures(visitor);
			else {
				// Not a FeatureValueProvider, so do normal.
				Iterator features = eobject.eClass().getEAllStructuralFeatures().iterator();
				while(features.hasNext()){
					EStructuralFeature sf = (EStructuralFeature)features.next();
					if(eobject.eIsSet(sf)){
						Object result = visitor.isSet(sf, eobject.eGet(sf));
						if (result != null)
							return result;
					}
				}
				return null;
			}
		}
		
		/**
		 * Answers whether any feature is set or not.
		 * <p>
		 * <b>Note:</b> This SHOULD NOT be used as a test before deciding whether to do visitSetFeatures or not. It is more efficient to just call
		 * visitSetFeatures. It should be used only to see if any features are set.
		 * 
		 * @param eobject
		 * @return
		 * 
		 * @since 1.1.0
		 */
		public static boolean isAnyFeatureSet(EObject eobject) {
			if (eobject instanceof FeatureValueProvider)
				return ((FeatureValueProvider)eobject).isAnyFeatureSet();
			else {
				// Not a FeatureValueProvider, so do normal.
				Iterator features = eobject.eClass().getEAllStructuralFeatures().iterator();
				while(features.hasNext()){
					EStructuralFeature sf = (EStructuralFeature)features.next();
					if(eobject.eIsSet(sf)){
						return true;
					}
				}
				return false;
			}			
		}
		
		private FeatureValueProviderHelper() {
		}
	}
	
	/**
	 * The interface for the visiter callback.
	 * 
	 * @since 1.1.0
	 */
	public interface Visitor{
		/**
		 * Called for each set feature on the FeatureValueProvider.
		 * 
		 * @param feature
		 * @param value
		 * @return <code>null</code> to continue to next setting, or a value to stop visiting and return that value be the real exception.
		 * @since 1.1.0
		 */
		Object isSet(EStructuralFeature feature, Object value);
	}	

	/**
	 * Visit the set features.
	 * @param aVisitor
	 * @param <code>null</code> if all settings visited, or the value returned from the visit (isSet) that returned a non-nullSe.
	 * @since 1.1.0
	 */
	public Object visitSetFeatures(Visitor aVisitor);
	
	/**
	 * Answers whether any feature is set or not.
	 * <p>
	 * <b>Note:</b> This SHOULD NOT be used as a test before deciding whether to do visitSetFeatures or not. It is
	 * more efficient to just call visitSetFeatures. It should be used only to see if any features are set.
	 * 
	 * @return <code>true</code> if any features are set.
	 * 
	 * @since 1.1.0
	 */
	public boolean isAnyFeatureSet();
}

Back to the top