1 17 package org.eclipse.emf.ecore.resource.impl; 18 19 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.emf.common.util.BasicEList; 24 import org.eclipse.emf.common.util.BasicEMap; 25 import org.eclipse.emf.common.util.URI; 26 27 28 31 public class URIMappingRegistryImpl extends BasicEMap 32 { 33 37 public static final URIMappingRegistryImpl INSTANCE = new URIMappingRegistryImpl(); 38 39 43 protected BasicEList prefixMaps = new BasicEList(); 44 45 48 public URIMappingRegistryImpl() 49 { 50 } 51 52 55 protected Entry newEntry(int hash, Object key, Object value) 56 { 57 validateKey(key); 58 validateValue(value); 59 return new MappingEntryImpl(hash, key, value); 60 } 61 62 66 protected class MappingEntryImpl extends EntryImpl 67 { 68 71 public boolean isPrefixMapEntry; 72 73 76 public MappingEntryImpl(int hash, Object key, Object value) 77 { 78 super(hash, key, value); 79 determineEntryType(); 80 } 81 82 85 public void determineEntryType() 86 { 87 isPrefixMapEntry = ((URI)key).isPrefix() && ((URI)value).isPrefix(); 88 } 89 } 90 91 99 public URI getURI(URI uri) 100 { 101 URI result = (URI)get(uri); 102 if (result == null) 103 { 104 if (prefixMaps != null) 105 { 106 for (int i = Math.min(prefixMaps.size() - 1, uri.segmentCount()); i >= 0; --i) 107 { 108 List prefixes = (List )prefixMaps.get(i); 109 for (int j = prefixes.size() - 1; j >= 0; --j) 110 { 111 Entry entry = (Entry)prefixes.get(j); 112 result = uri.replacePrefix((URI)entry.getKey(), (URI)entry.getValue()); 113 114 if (result != null) 115 { 116 return result; 117 } 118 } 119 } 120 } 121 122 result = delegatedGetURI(uri); 123 } 124 125 return result; 126 } 127 128 137 protected URI delegatedGetURI(URI uri) 138 { 139 return uri; 140 } 141 142 145 protected class URIMapImpl extends DelegatingMap implements URIConverterImpl.URIMap 146 { 147 150 public URIMapImpl() 151 { 152 } 153 154 160 public URI getURI(URI uri) 161 { 162 return URIMappingRegistryImpl.this.getURI(uri); 163 } 164 } 165 166 169 public Map map() 170 { 171 if (view == null) 172 { 173 view = new View(); 174 } 175 if (view.map == null) 176 { 177 view.map = new URIMapImpl(); 178 } 179 180 return view.map; 181 } 182 183 186 protected void validateKey(Object key) 187 { 188 if (!(key instanceof URI)) 189 { 190 throw new IllegalArgumentException (); 191 } 192 } 193 194 197 protected void validateValue(Object value) 198 { 199 if (!(value instanceof URI)) 200 { 201 throw new IllegalArgumentException (); 202 } 203 } 204 205 208 protected void didAdd(Entry entry) 209 { 210 if (((MappingEntryImpl)entry).isPrefixMapEntry) 211 { 212 int length = ((URI)entry.getKey()).segmentCount(); 213 if (prefixMaps == null) 214 { 215 prefixMaps = new BasicEList(); 216 } 217 218 for (int i = prefixMaps.size() - 1; i <= length; ++i) 219 { 220 prefixMaps.add(new BasicEList()); 221 } 222 223 ((List )prefixMaps.get(length)).add(entry); 224 } 225 } 226 227 230 protected void didModify(Entry entry, Object oldValue) 231 { 232 didRemove(entry); 233 ((MappingEntryImpl)entry).determineEntryType(); 234 didAdd(entry); 235 } 236 237 240 protected void didRemove(Entry entry) 241 { 242 if (((MappingEntryImpl)entry).isPrefixMapEntry) 243 { 244 int length = ((URI)entry.getKey()).segmentCount(); 245 ((List )prefixMaps.get(length)).remove(entry); 246 } 247 } 248 249 252 protected void didClear(BasicEList [] oldEntryData) 253 { 254 prefixMaps = null; 255 } 256 } 257 | Popular Tags |