01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.texteditor.rulers;
11:
12: import java.util.HashSet;
13: import java.util.Iterator;
14: import java.util.Set;
15: import java.util.StringTokenizer;
16:
17: import org.eclipse.core.runtime.Assert;
18:
19: public final class StringSetSerializer {
20: private static final String DELIM = "\0"; //$NON-NLS-1$
21:
22: private StringSetSerializer() {
23: }
24:
25: public static String serialize(Set strings) {
26: Assert.isLegal(strings != null);
27: StringBuffer buf = new StringBuffer(strings.size() * 20);
28: for (Iterator it = strings.iterator(); it.hasNext();) {
29: buf.append((String) it.next());
30: if (it.hasNext())
31: buf.append(DELIM);
32: }
33: return buf.toString();
34: }
35:
36: public static Set deserialize(String serialized) {
37: Assert.isLegal(serialized != null);
38: Set marked = new HashSet();
39: StringTokenizer tok = new StringTokenizer(serialized, DELIM);
40: while (tok.hasMoreTokens())
41: marked.add(tok.nextToken());
42: return marked;
43: }
44:
45: public static String[] getDifference(String oldValue,
46: String newValue) {
47: Set oldSet = deserialize(oldValue);
48: Set newSet = deserialize(newValue);
49: Set intersection = new HashSet(oldSet);
50: intersection.retainAll(newSet);
51: oldSet.removeAll(intersection);
52: newSet.removeAll(intersection);
53: oldSet.addAll(newSet);
54: return (String[]) oldSet.toArray(new String[oldSet.size()]);
55: }
56: }
|