Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a9cd2dc3b20ad33641fa2208734a4722abe65d6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.viewers.deferred;

import java.util.Collection;
import java.util.HashSet;

import org.eclipse.core.runtime.Assert;


/**
 * Trivial implementation of an <code>IConcurrentModel</code>. Implements 
 * an unordered set of elements that fires off change notifications whenever
 * elements are added or removed from the set. All notifications are sent
 * synchronously.
 * 
 * @since 3.1
 */
public class SetModel extends AbstractConcurrentModel {
    
    private HashSet data = new HashSet();
    
    /**
     * Return the contents of the model.
     * @return the array of elements
     * 
     */
    public Object[] getElements() {
        return data.toArray();
    }

    /**
     * Sets the contents to the given array of elements
     * 
     * @param newContents new contents of this set
     */
    public void set(Object[] newContents) {
    	Assert.isNotNull(newContents);
    	data.clear();
        for (int i = 0; i < newContents.length; i++) {
            Object object = newContents[i];
            
            data.add(object);
        }
    	
    	IConcurrentModelListener[] listeners = getListeners();
    	for (int i = 0; i < listeners.length; i++) {
			IConcurrentModelListener listener = listeners[i];
			
			listener.setContents(newContents);
		}
    }

    /**
     * Empties the set
     */
    public void clear() {
        Object[] removed = data.toArray();
        data.clear();
        fireRemove(removed);
    }
    
    /**
     * Adds the given elements to the set
     * 
     * @param toAdd elements to add
     */
    public void addAll(Object[] toAdd) {
    	Assert.isNotNull(toAdd);
        for (int i = 0; i < toAdd.length; i++) {
            Object object = toAdd[i];
            
            data.add(object);
        }
        
        fireAdd(toAdd);
    }
    
    /**
     * Adds the given elements to the set. Duplicate elements are ignored.
     * 
     * @param toAdd elements to add
     */
    public void addAll(Collection toAdd) {
    	Assert.isNotNull(toAdd);
        addAll(toAdd.toArray());
    }
    
    /**
     * Fires a change notification for all elements in the given array
     * 
     * @param changed array of elements that have changed
     */
    public void changeAll(Object[] changed) {
    	Assert.isNotNull(changed);
        fireUpdate(changed);
    }
    
    /**
     * Removes all of the given elements from the set.
     * 
     * @param toRemove elements to remove
     */
    public void removeAll(Object[] toRemove) {
    	Assert.isNotNull(toRemove);
        for (int i = 0; i < toRemove.length; i++) {
            Object object = toRemove[i];
            
            data.remove(object);
        }        
        
        fireRemove(toRemove);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.deferred.IConcurrentModel#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentModelListener)
     */
    @Override
	public void requestUpdate(IConcurrentModelListener listener) {
    	Assert.isNotNull(listener);
        listener.setContents(getElements());
    }
}

Back to the top