KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > rulers > StringSetSerializer


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.texteditor.rulers;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 public final class StringSetSerializer {
21     private static final String JavaDoc DELIM= "\0"; //$NON-NLS-1$
22
private StringSetSerializer() {
23     }
24
25     public static String JavaDoc serialize(Set JavaDoc strings) {
26         Assert.isLegal(strings != null);
27         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(strings.size() * 20);
28         for (Iterator JavaDoc it= strings.iterator(); it.hasNext();) {
29             buf.append((String JavaDoc) it.next());
30             if (it.hasNext())
31                 buf.append(DELIM);
32         }
33         return buf.toString();
34     }
35     
36     public static Set JavaDoc deserialize(String JavaDoc serialized) {
37         Assert.isLegal(serialized != null);
38         Set JavaDoc marked= new HashSet JavaDoc();
39         StringTokenizer JavaDoc tok= new StringTokenizer JavaDoc(serialized, DELIM);
40         while (tok.hasMoreTokens())
41             marked.add(tok.nextToken());
42         return marked;
43     }
44
45     public static String JavaDoc[] getDifference(String JavaDoc oldValue, String JavaDoc newValue) {
46         Set JavaDoc oldSet= deserialize(oldValue);
47         Set JavaDoc newSet= deserialize(newValue);
48         Set JavaDoc intersection= new HashSet JavaDoc(oldSet);
49         intersection.retainAll(newSet);
50         oldSet.removeAll(intersection);
51         newSet.removeAll(intersection);
52         oldSet.addAll(newSet);
53         return (String JavaDoc[]) oldSet.toArray(new String JavaDoc[oldSet.size()]);
54     }
55 }
56
Popular Tags