Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b44ab4a6903ef87b0dee75b933b16e4b52fc11ec (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2012, 2013 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.persistence.history;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.tcf.te.runtime.events.ChangeEvent;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.persistence.activator.CoreBundleActivator;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;

/**
 * History manager implementation.
 */
public class HistoryManager {
	// the maximum length of the history per id
	private final static int HISTORY_LENGTH = 5;

	// The map maintaining the history
	private Map<String, List<String>> history = new HashMap<String, List<String>>();

	/*
	 * Thread save singleton instance creation.
	 */
	private static class LazyInstance {
		public static HistoryManager instance = new HistoryManager();
	}

	/**
	 * Returns the singleton instance of the history point manager.
	 */
	public static HistoryManager getInstance() {
		return LazyInstance.instance;
	}

	/**
	 * Constructor.
	 */
	HistoryManager() {
		super();
		initialize();
	}

	/**
	 * Initialize the history manager.
	 */
	private void initialize() {
		history.clear();
		try {
			// Get the persistence service
			IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
			if (uRIPersistenceService == null) {
				throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
			}
			// Save the history to the persistence storage
			history = (Map<String,List<String>>)uRIPersistenceService.read(history, getURI());
		} catch (IOException e) {
		}
	}

	// Get the URI for history persistence
	private URI getURI() {
		IPath pluginPath = CoreBundleActivator.getDefault().getStateLocation();
		pluginPath = pluginPath.append(".history"); //$NON-NLS-1$

		return pluginPath.toFile().toURI();
	}

	/**
	 * Write the history to disk.
	 */
	public void flush() {
		synchronized (history) {
			try {
				// Get the persistence service
				IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
				if (uRIPersistenceService == null) {
					throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
				}
				// Save the history to the persistence storage
				uRIPersistenceService.write(history, getURI());
			} catch (IOException e) {
			}
        }
	}

	/**
	 * Get the history for a given history id.
	 * @param historyId The history id.
	 * @return The list of ids within the history ids list or an empty list.
	 */
	public String[] getHistory(String historyId) {
		Assert.isNotNull(historyId);

		List<String> ids = history.get(historyId);
		if (ids == null) {
			ids = new ArrayList<String>();
		}

		return ids.toArray(new String[ids.size()]);
	}

	/**
	 * Get the fist entry of a history ids list.
	 * @param historyId The history id.
	 * @return The first entry of the history ids list or null if no history is available for that id.
	 */
	public String getFirst(String historyId) {
		String[] history = getHistory(historyId);
		return history.length > 0 ? history[0] : null;
	}

	/**
	 * Add a new history entry to the top of the history ids list.
	 * If the list size exceeds the HISTORY_LENGTH, the last element of the list will be removed.
	 * @param historyId The history id.
	 * @param id The id to be added to the top of history ids list.
	 * @return <code>true</code> if the id
	 */
	public boolean add(String historyId, String id) {
		Assert.isNotNull(historyId);
		Assert.isNotNull(id);

		List<String> ids = history.get(historyId);
		if (ids == null) {
			ids = new ArrayList<String>();
			history.put(historyId, ids);
		}
		if (ids.contains(id)) {
			ids.remove(id);
		}

		ids.add(0, id);

		while (ids.size() > HISTORY_LENGTH) {
			ids.remove(HISTORY_LENGTH);
		}

		flush();

		EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_ADDED, historyId, historyId));

		return true;
	}

	/**
	 * Set a new list of history entries to the history ids list.
	 * If the list size exceeds the HISTORY_LENGTH, the last element of the list will be removed.
	 * @param historyId The history id.
	 * @param ids The ids to be set to the history ids list.
	 * @return <code>true</code> if the id
	 */
	public void set(String historyId, String[] ids) {
		Assert.isNotNull(historyId);
		Assert.isNotNull(ids);

		history.put(historyId, Arrays.asList(ids));
		List<String> newIds = history.get(historyId);

		while (newIds.size() > HISTORY_LENGTH) {
			newIds.remove(HISTORY_LENGTH);
		}

		flush();

		EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_CHANGED, historyId, historyId));
	}

	/**
	 * Remove a id from the history ids list.
	 * @param historyId The history id.
	 * @param id The id to be removed from the history ids list.
	 * @return <code>true</code> if the id was removed from the history ids list.
	 */
	public boolean remove(String historyId, String id) {
		Assert.isNotNull(historyId);
		Assert.isNotNull(id);

		boolean removed = false;

		List<String> ids = history.get(historyId);
		if (ids != null) {
			removed |= ids.remove(id);
			if (ids.isEmpty()) {
				history.remove(historyId);
			}
		}

		if (removed) {
			flush();
			EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_REMOVED, historyId, historyId));
		}

		return removed;
	}

	/**
	 * Remove all ids from the history ids list.
	 * @param historyId The history id.
	 **/
	public void clear(String historyId) {
		Assert.isNotNull(historyId);

		List<String> ids = history.remove(historyId);

		if (ids != null) {
			flush();
			EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_REMOVED, historyId, historyId));
		}
	}

}

Back to the top