Skip to main content
summaryrefslogtreecommitdiffstats
blob: 96c8b060226c8c8e0841da5b7ad0cb63f7b73399 (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
/*******************************************************************************
 * Copyright (c) 2012, 2017 CEA LIST, Christian W. Damus, 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:
 *    Nicolas Bros (Mia-Software) - Bug 374758 - [Table] repair the table
 *    Gregoire Dupe (Mia-Software) - Bug 372626 - Aggregates
 *    Thomas Cicognani (Soft-Maint) - Bug 420192 - UnsupportedOperationException in a usefull method
 *    Christian W. Damus - bug 515913
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.custom.core.internal;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;

/** Represents a list of {@link Customization}s that exists as a subset of a delegate list of {@link FacetSet}s. */
public class CustomizationsDelegatingList extends BasicEList<Customization> {
	private static final long serialVersionUID = 1L;

	private final List<FacetSet> delegate;

	public CustomizationsDelegatingList(final List<FacetSet> delegate) {
		super(customizations(delegate));
		this.delegate = delegate;
	}

	/**
	 * @since 3.0
	 */
	@Override
	protected void didAdd(int index, Customization newObject) {
		// Insert at the corresponding location in the delegate
		if (index > 0) {
			index = delegate.indexOf(get(index - 1)) + 1;
		}

		delegate.add(index, newObject);
	}

	/**
	 * @since 3.0
	 */
	@Override
	protected void didSet(int index, Customization newObject, Customization oldObject) {
		index = delegate.indexOf(oldObject);
		delegate.set(index, newObject);
	}

	/**
	 * @since 3.0
	 */
	@Override
	protected void didRemove(int index, Customization oldObject) {
		delegate.remove(oldObject);
	}

	/**
	 * @since 3.0
	 */
	@Override
	protected void didClear(int size, Object[] oldObjects) {
		delegate.removeAll(Arrays.asList(oldObjects));
	}

	/**
	 * @since 3.0
	 */
	@Override
	protected void didMove(int index, Customization movedObject, int oldIndex) {
		// Move to the corresponding location in the delegate
		if (index > 0) {
			index = delegate.indexOf(get(index - 1)) + 1;
		}
		if (delegate instanceof EList<?>) {
			((EList<FacetSet>) delegate).move(index, movedObject);
		} else {
			// Do it the hard way
			delegate.remove(movedObject);
			delegate.add(index, movedObject);
		}
	}

	/**
	 * Obtains the subset of a list of facet-sets that are customizations.
	 * 
	 * @param facetSets
	 *            the superset
	 * @return the subset of customizations
	 * @since 3.0
	 */
	protected static List<Customization> customizations(List<FacetSet> facetSets) {
		return facetSets.stream()
				.filter(Customization.class::isInstance).map(Customization.class::cast)
				.collect(Collectors.toList());
	}
}

Back to the top