Skip to content

Package: PropertiesElement$1

PropertiesElement$1

nameinstructionbranchcomplexitylinemethod
propertiesChanged(ECPProperties, Collection, Collection)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /********************************************************************************
2: * Copyright (c) 2011-2015 Eike Stepper (Berlin, Germany) 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: * Eike Stepper - initial API and implementation
13: ********************************************************************************/
14: package org.eclipse.emf.ecp.internal.core.util;
15:
16: import java.io.IOException;
17: import java.io.ObjectInput;
18: import java.io.ObjectOutput;
19: import java.util.Collection;
20: import java.util.Map.Entry;
21:
22: import org.eclipse.emf.ecp.core.util.ECPProperties;
23: import org.eclipse.emf.ecp.core.util.ECPPropertiesAware;
24: import org.eclipse.emf.ecp.core.util.observer.ECPPropertiesObserver;
25: import org.eclipse.emf.ecp.internal.core.util.PropertiesStore.StorableElement;
26:
27: /**
28: * An element holding {@link ECPProperties}.
29: *
30: * @author Eike Stepper
31: */
32: public abstract class PropertiesElement extends Element implements StorableElement, ECPPropertiesAware {
33:         private final ECPProperties properties;
34:         private ECPPropertiesObserver observer;
35:
36:         /**
37:          * Constructor.
38:          *
39:          * @param name the name of the elements
40:          * @param properties the initial properties
41:          */
42:         public PropertiesElement(String name, ECPProperties properties) {
43:                 super(name);
44:                 this.properties = properties;
45:                 observeProperties();
46:         }
47:
48:         /**
49:          * Create a {@link PropertiesElement} from an {@link ObjectInput}.
50:          *
51:          * @param in the {@link ObjectInput}
52:          * @throws IOException if there a problem while reading the input
53:          */
54:         public PropertiesElement(ObjectInput in) throws IOException {
55:                 super(in.readUTF());
56:                 properties = new Properties(in);
57:                 observeProperties();
58:         }
59:
60:         /** {@inheritDoc} */
61:         @Override
62:         public void write(ObjectOutput out) throws IOException {
63:                 out.writeUTF(getName());
64:                 ((Properties) properties).write(out);
65:         }
66:
67:         /** {@inheritDoc} */
68:         @Override
69:         public final ECPProperties getProperties() {
70:                 return properties;
71:         }
72:
73:         /**
74:          * Called if the properties of the element change. Can be implemented by subclasses
75:          *
76:          * @param oldProperties the old properties
77:          * @param newProperties the new properties
78:          */
79:         protected void propertiesChanged(Collection<Entry<String, String>> oldProperties,
80:                 Collection<Entry<String, String>> newProperties) {
81:                 // Do nothing
82:         }
83:
84:         private void observeProperties() {
85:                 if (observer != null) {
86:                         cleanup();
87:                 }
88:                 observer = new ECPPropertiesObserver() {
89:                         @Override
90:                         public void propertiesChanged(ECPProperties properties, Collection<Entry<String, String>> oldProperties,
91:                                 Collection<Entry<String, String>> newProperties) {
92:                                 PropertiesElement.this.propertiesChanged(oldProperties, newProperties);
93:                         }
94:                 };
95:                 properties.addObserver(observer);
96:         }
97:
98:         /**
99:          * Cleans up after the PropertiesElement. This call unregisters the {@link ECPPropertiesObserver} from the
100:          * {@link ECPProperties}.
101:          */
102:         protected void cleanup() {
103:                 if (properties == null || observer == null) {
104:                         return;
105:                 }
106:                 properties.removeObserver(observer);
107:         }
108: }