1 19 20 package org.openide.loaders; 21 22 import java.util.*; 23 24 import org.openide.filesystems.FileObject; 25 26 37 final class FilesSet implements Set<FileObject> { 38 39 40 private MultiDataObject mymdo; 41 42 43 private boolean lazyWorkDone; 44 45 46 private FileObject primaryFile; 47 48 51 private Map<FileObject,MultiDataObject.Entry> secondary; 52 53 56 private TreeSet<FileObject> delegate; 57 58 63 public FilesSet(MultiDataObject mdo) { 64 65 this.mymdo = mdo; 66 this.lazyWorkDone = false; 67 this.primaryFile = null; 68 this.secondary = null; 69 } 70 71 72 private void doLazyWork() { 73 synchronized (this) { 74 if (!lazyWorkDone) { 75 lazyWorkDone = true; 76 77 synchronized ( mymdo.synchObjectSecondary() ) { 78 mymdo.secondaryEntries(); 80 primaryFile = mymdo.getPrimaryFile(); 81 secondary = mymdo.getSecondary(); 82 } 83 } 84 } 85 } 86 87 89 private Set<FileObject> getDelegate() { 90 doLazyWork(); 91 synchronized (secondary) { 95 if (delegate == null) { 96 delegate = new TreeSet<FileObject>(new FilesComparator()); 97 delegate.add(primaryFile); 98 delegate.addAll(secondary.keySet()); 99 } 100 } 101 return delegate; 102 } 103 104 108 public boolean add(FileObject obj) { 109 return getDelegate().add(obj); 110 } 111 112 public boolean addAll(Collection<? extends FileObject> collection) { 113 return getDelegate().addAll(collection); 114 } 115 116 public void clear() { 117 getDelegate().clear(); 118 } 119 120 public boolean contains(Object obj) { 121 return getDelegate().contains(obj); 122 } 123 124 public boolean containsAll(Collection<?> collection) { 125 return getDelegate().containsAll(collection); 126 } 127 128 public boolean isEmpty() { 129 doLazyWork(); 130 synchronized (secondary) { 131 return (delegate == null) ? false : delegate.isEmpty(); 132 } 133 } 134 135 public Iterator<FileObject> iterator() { 136 doLazyWork(); 137 synchronized (secondary) { 138 return (delegate == null) ? new FilesIterator() : delegate.iterator(); 139 } 140 } 141 142 public boolean remove(Object obj) { 143 return getDelegate().remove(obj); 144 } 145 146 public boolean removeAll(Collection<?> collection) { 147 return getDelegate().removeAll(collection); 148 } 149 150 public boolean retainAll(Collection<?> collection) { 151 return getDelegate().retainAll(collection); 152 } 153 154 public int size() { 155 doLazyWork(); 156 synchronized (secondary) { 157 return (delegate == null) ? (secondary.size() + 1) : delegate.size(); 158 } 159 } 160 161 public Object [] toArray() { 162 return getDelegate().toArray(); 163 } 164 165 public <T> T[] toArray(T[] obj) { 166 return getDelegate().toArray(obj); 167 } 168 169 public boolean equals(Object obj) { 170 return getDelegate().equals(obj); 171 } 172 173 public String toString() { 174 return getDelegate().toString(); 175 } 176 177 public int hashCode() { 178 return getDelegate().hashCode(); 179 } 180 181 184 private final class FilesIterator implements Iterator<FileObject> { 185 187 private boolean first = true; 188 189 192 private Iterator<FileObject> itDelegate = null; 193 194 FilesIterator() {} 195 196 public boolean hasNext() { 197 return first ? true : getIteratorDelegate().hasNext(); 198 } 199 200 public FileObject next() { 201 if (first) { 202 first = false; 203 return FilesSet.this.primaryFile; 204 } 205 else { 206 return getIteratorDelegate().next(); 207 } 208 } 209 210 public void remove() { 211 getIteratorDelegate().remove(); 212 } 213 214 216 private Iterator<FileObject> getIteratorDelegate() { 217 if (itDelegate == null) { 218 itDelegate = FilesSet.this.getDelegate().iterator(); 220 itDelegate.next(); 222 } 223 return itDelegate; 224 } 225 } 226 227 231 private final class FilesComparator implements Comparator<FileObject> { 232 FilesComparator() {} 233 public int compare(FileObject f1, FileObject f2) { 234 if (f1 == f2) 235 return 0; 236 237 if (f1 == primaryFile) 238 return -1; 239 240 if (f2 == primaryFile) 241 return 1; 242 243 int res = f1.getNameExt().compareTo(f2.getNameExt()); 244 245 if (res == 0) { 246 try { 248 if (f1.getFileSystem() == f2.getFileSystem()) { 249 return 0; 250 } 251 return f1.getFileSystem().getSystemName().compareTo( 253 f2.getFileSystem().getSystemName()); 254 } catch (org.openide.filesystems.FileStateInvalidException fsie) { 255 return 0; 259 } 260 } 261 262 return res; 263 } 264 } 265 } 266 | Popular Tags |