Skip to content

Package: EMFStoreDirtyDecoratorCachedTree$1

EMFStoreDirtyDecoratorCachedTree$1

nameinstructionbranchcomplexitylinemethod
isExcluded(Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2012 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar Mueller - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.emfstore.internal.ui.decorator;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19: import java.util.Set;
20:
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecp.common.spi.cachetree.AbstractCachedTree;
23: import org.eclipse.emf.ecp.common.spi.cachetree.CachedTreeNode;
24: import org.eclipse.emf.ecp.common.spi.cachetree.IExcludedObjectsCallback;
25: import org.eclipse.emf.ecp.core.ECPProject;
26: import org.eclipse.emf.ecp.spi.core.InternalProject;
27:
28: /**
29: * Cached tree implementation for dirty decorators of model elements managed by EMFStore.
30: *
31: * @author emueller
32: *
33: */
34: public final class EMFStoreDirtyDecoratorCachedTree extends AbstractCachedTree<Integer> {
35:
36:         private static Map<ECPProject, EMFStoreDirtyDecoratorCachedTree> cashedTrees = new HashMap<ECPProject, EMFStoreDirtyDecoratorCachedTree>();
37:
38:         /**
39:          * Removes an ECPProject from the Cache.
40:          *
41:          * @param project the project
42:          */
43:         public static void removeProject(ECPProject project) {
44:                 cashedTrees.remove(project);
45:         }
46:
47:         /**
48:          * Static {@link EMFStoreDirtyDecoratorCachedTree} singleton.
49:          *
50:          * @param project the {@link ECPProject} to initialize this CashedTree on
51:          * @return Static instance of the {@link EMFStoreDirtyDecoratorCachedTree}
52:          */
53:         public static EMFStoreDirtyDecoratorCachedTree getInstance(final ECPProject project) {
54:                 if (!cashedTrees.containsKey(project)) {
55:                         cashedTrees.put(project, new EMFStoreDirtyDecoratorCachedTree(new IExcludedObjectsCallback() {
56:
57:                                 @Override
58:                                 public boolean isExcluded(Object object) {
59:                                         return ((InternalProject) project).isModelRoot(object);
60:                                 }
61:                         }));
62:                 }
63:                 return cashedTrees.get(project);
64:         }
65:
66:         /**
67:          * Private constructor.
68:          */
69:         private EMFStoreDirtyDecoratorCachedTree(IExcludedObjectsCallback callback) {
70:                 super(callback);
71:         }
72:
73:         /**
74:          * Cached tree node that stores the dirty state of a model element managed by EMFStore.
75:          */
76:         public class CachedDirtyStateTreeNode extends CachedTreeNode<Integer> {
77:
78:                 /**
79:                  * Constructor.
80:                  *
81:                  * @param value
82:                  * the initial value for this entry
83:                  */
84:                 public CachedDirtyStateTreeNode(Integer value) {
85:                         super(0);
86:                         setChildValue(getDefaultValue());
87:                 }
88:
89:                 /**
90:                  * {@inheritDoc}
91:                  */
92:                 @Override
93:                 public void update() {
94:                         setChildValue(getDefaultValue());
95:                         for (final Integer value : values()) {
96:                                 if (value > getChildValue()) {
97:                                         setChildValue(value);
98:                                         break;
99:                                 }
100:                         }
101:                 }
102:
103:                 /*
104:                  * (non-Javadoc)
105:                  * @see org.eclipse.emf.ecp.ui.common.CachedTreeNode#getDisplayValue()
106:                  */
107:                 @Override
108:                 public Integer getDisplayValue() {
109:                         return getChildValue() > 0 || getOwnValue() > 0 ? 1 : 0;
110:                 }
111:         }
112:
113:         /**
114:          * {@inheritDoc}
115:          */
116:         @Override
117:         public Integer getDefaultValue() {
118:                 return 0;
119:         }
120:
121:         /**
122:          * {@inheritDoc}
123:          */
124:         @Override
125:         public CachedTreeNode<Integer> createdCachedTreeNode(Integer t) {
126:                 return new CachedDirtyStateTreeNode(t);
127:         }
128:
129:         /**
130:          * Call to indicate that an {@link EObject} was added.
131:          *
132:          * @param eObject the new {@link EObject}
133:          * @return the {@link Set} of {@link EObject} affected by this change
134:          */
135:         public Set<EObject> addOperation(EObject eObject) {
136:                 int value = 0;
137:                 final CachedTreeNode<Integer> node = getNodes().get(eObject);
138:                 if (node != null) {
139:                         value = node.getOwnValue();
140:                 }
141:                 return update(eObject, ++value);
142:         }
143:
144:         /**
145:          * Call to indicate that an {@link EObject} was removed.
146:          *
147:          * @param eObject the removed {@link EObject}
148:          * @return the {@link Set} of {@link EObject} affected by this change
149:          */
150:         public Set<EObject> removeOperation(EObject eObject) {
151:                 int value = 0;
152:                 final CachedTreeNode<Integer> node = getNodes().get(eObject);
153:                 if (node != null) {
154:                         value = node.getOwnValue();
155:                 }
156:
157:                 return update(eObject, Math.max(0, --value));
158:         }
159:
160:         /**
161:          * Sets the operation count for the given object.
162:          *
163:          * @param eObject the eobject
164:          * @param count the new count
165:          * @return the set of affected elements
166:          */
167:         public Set<EObject> setOperationCount(EObject eObject, int count) {
168:                 return update(eObject, count);
169:         }
170:
171:         /**
172:          * Gets the own cached value of the given object.
173:          *
174:          * @param eObject the e object
175:          * @return the own cached value
176:          */
177:         public int getOwnValue(EObject eObject) {
178:                 int value = 0;
179:                 final CachedTreeNode<Integer> node = getNodes().get(eObject);
180:                 if (node != null) {
181:                         value = node.getOwnValue();
182:                 }
183:                 return value;
184:         }
185: }