1 11 package org.eclipse.ui.internal.texteditor.rulers; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.core.runtime.Assert; 19 20 public final class StringSetSerializer { 21 private static final String DELIM= "\0"; 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, String newValue) { 46 Set oldSet= deserialize(oldValue); 47 Set newSet= deserialize(newValue); 48 Set intersection= new HashSet (oldSet); 49 intersection.retainAll(newSet); 50 oldSet.removeAll(intersection); 51 newSet.removeAll(intersection); 52 oldSet.addAll(newSet); 53 return (String []) oldSet.toArray(new String [oldSet.size()]); 54 } 55 } 56 | Popular Tags |