Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd17f93071f22f726f26cf3a9ff6b55b84ca8957 (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
/********************************************************************************
 * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/

package org.eclipse.mdm.api.base.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.mdm.api.base.model.Deletable;

/**
 * Holds related entities of any kind and keeps track of modifications.
 */
public final class ChildrenStore {

	private final Map<Class<? extends Deletable>, List<? extends Deletable>> current = new HashMap<>(0);
	private final Map<Class<? extends Deletable>, List<? extends Deletable>> removed = new HashMap<>(0);

	/**
	 * Returns current set of related children mapped by their type.
	 *
	 * @return Returned {@code Map} is unmodifiable.
	 */
	public Map<Class<? extends Deletable>, List<? extends Deletable>> getCurrent() {
		return Collections.unmodifiableMap(current);
	}

	/**
	 * Returns current set of removed related children mapped by their type.
	 *
	 * @return Returned {@code Map} is unmodifiable.
	 */
	public Map<Class<? extends Deletable>, List<? extends Deletable>> getRemoved() {
		return Collections.unmodifiableMap(removed);
	}

	/**
	 * Returns related child entities of given type.
	 *
	 * @param <T>
	 *            Desired entity type.
	 * @param entityClass
	 *            Used as identifier.
	 * @return Returned {@code List} is unmodifiable.
	 */
	@SuppressWarnings("unchecked")
	public <T extends Deletable> List<T> get(Class<T> entityClass) {
		return Collections.unmodifiableList((List<T>) current.computeIfAbsent(entityClass, k -> new ArrayList<>()));
	}

	/**
	 * Sorts the child entities with given {@code Comparator}.
	 *
	 * @param <T>
	 *            Desired entity type.
	 * @param entityClass
	 *            Used as identifier.
	 * @param comparator
	 *            Used for sorting.
	 */
	@SuppressWarnings("unchecked")
	public <T extends Deletable> void sort(Class<T> entityClass, Comparator<? super T> comparator) {
		List<T> children = (List<T>) current.get(entityClass);
		if (children != null) {
			children.sort(comparator);
		}
	}

	/**
	 * Adds given child entity.
	 *
	 * @param child
	 *            The new child.
	 */
	@SuppressWarnings("unchecked")
	public void add(Deletable child) {
		removed.getOrDefault(child.getClass(), new ArrayList<>()).remove(child);
		((List<Deletable>) current.computeIfAbsent(child.getClass(), k -> new ArrayList<>())).add(child);
	}

	/**
	 * Removes given child entity.
	 *
	 * @param child
	 *            The child which will be removed.
	 */
	@SuppressWarnings("unchecked")
	public void remove(Deletable child) {
		List<Deletable> children = (List<Deletable>) current.getOrDefault(child.getClass(), new ArrayList<>());
		if (children.remove(child) && child.getID() != null && child.getID().length() > 0) {
			((List<Deletable>) removed.computeIfAbsent(child.getClass(), k -> new ArrayList<>())).add(child);
		}
	}

	/**
	 * Clean up list of removed entities.
	 */
	void apply() {
		removed.clear();
	}

}

Back to the top