Skip to main content
summaryrefslogtreecommitdiffstats
blob: d04722f9ffcf0a143631950bafd6349a0c01d7ce (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
/*******************************************************************************
 * Copyright (c) 2009 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.model.value;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.HashBag;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterators.ReadOnlyIterator;
import org.eclipse.jpt.common.utility.model.event.CollectionAddEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionChangeEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionClearEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionRemoveEvent;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;

/**
 * A <code>SetCollectionValueModel</code> wraps another
 * {@link CollectionValueModel} and returns the items in the collection
 * only once.
 */
public class SetCollectionValueModel<E>
	extends CollectionValueModelWrapper<E>
	implements CollectionValueModel<E>
{
	private final HashBag<E> bag = new HashBag<E>();


	// ********** constructors **********

	/**
	 * Construct a collection value model with the specified wrapped
	 * collection value model and a filter that simply accepts every object.
	 */
	public SetCollectionValueModel(CollectionValueModel<? extends E> collectionHolder) {
		super(collectionHolder);
	}

	/**
	 * Construct a collection value model with the specified wrapped
	 * list value model and a filter that simply accepts every object.
	 */
	public SetCollectionValueModel(ListValueModel<E> listHolder) {
		this(new ListCollectionValueModelAdapter<E>(listHolder));
	}


	// ********** CollectionValueModel implementation **********

	public Iterator<E> iterator() {
		return new ReadOnlyIterator<E>(this.bag.uniqueIterator());
	}

	public int size() {
		return this.bag.uniqueCount();
	}


	// ********** CollectionValueModelWrapper overrides/implementation **********

	@Override
	protected void engageModel() {
		super.engageModel();
		// synch our cache *after* we start listening to the nested collection,
		// since its value might change when a listener is added
		CollectionTools.addAll(this.bag, this.collectionHolder);
	}

	@Override
	protected void disengageModel() {
		super.disengageModel();
		// clear out the cache when we are not listening to the nested collection
		this.bag.clear();
	}

	@Override
	protected void itemsAdded(CollectionAddEvent event) {
		ArrayList<E> addedItems = new ArrayList<E>(event.getItemsSize());
		int uniqueCount = this.bag.uniqueCount();
		for (E item : this.getItems(event)) {
			this.bag.add(item);
			if (this.bag.uniqueCount() > uniqueCount) {
				uniqueCount = this.bag.uniqueCount();
				addedItems.add(item);
			}
		}
		this.fireItemsAdded(VALUES, addedItems);
	}

	@Override
	protected void itemsRemoved(CollectionRemoveEvent event) {
		ArrayList<E> removedItems = new ArrayList<E>(event.getItemsSize());
		int uniqueCount = this.bag.uniqueCount();
		for (E item : this.getItems(event)) {
			if (this.bag.remove(item)) {
				if (this.bag.uniqueCount() < uniqueCount) {
					uniqueCount = this.bag.uniqueCount();
					removedItems.add(item);
				}
			} else {
				throw new IllegalStateException("missing item: " + item); //$NON-NLS-1$
			}
		}
		this.fireItemsRemoved(VALUES, removedItems);
	}

	@Override
	protected void collectionCleared(CollectionClearEvent event) {
		this.clearCollection(this.bag, VALUES);
	}

	@Override
	protected void collectionChanged(CollectionChangeEvent event) {
		this.bag.clear();
		CollectionTools.addAll(this.bag, this.collectionHolder);
		this.fireCollectionChanged(VALUES, new HashSet<E>(this.bag));
	}

	@Override
	public void toString(StringBuilder sb) {
		StringTools.append(sb, this);
	}

}

Back to the top