autodetection reworked
[fedora-idea.git] / source / com / intellij / facet / impl / autodetecting / FacetDetectionIndexEntry.java
blobe0cdc84da3fbffcac93bac7816043665a4a4a44a
1 /*
2 * Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
3 */
5 package com.intellij.facet.impl.autodetecting;
7 import com.intellij.facet.Facet;
8 import com.intellij.facet.impl.autodetecting.model.DetectedFacetInfo;
9 import com.intellij.facet.impl.autodetecting.model.FacetInfo2;
10 import com.intellij.facet.impl.autodetecting.model.FacetInfoBackedByFacet;
11 import com.intellij.facet.pointers.FacetPointer;
12 import com.intellij.facet.pointers.FacetPointersManager;
13 import com.intellij.openapi.module.Module;
14 import com.intellij.util.SmartList;
15 import com.intellij.util.fileIndex.FileIndexEntry;
16 import org.jetbrains.annotations.Nullable;
18 import java.io.DataInputStream;
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
26 /**
27 * @author nik
29 public class FacetDetectionIndexEntry extends FileIndexEntry {
30 private SmartList<FacetPointer> myFacets;
31 private SmartList<Integer> myDetectedFacetIds;
33 public FacetDetectionIndexEntry(final long timestamp) {
34 super(timestamp);
37 public FacetDetectionIndexEntry(final DataInputStream stream, final FacetPointersManager facetPointersManager) throws IOException {
38 super(stream);
39 int size = stream.readInt();
40 if (size > 0) {
41 while (size-- > 0) {
42 String id = stream.readUTF();
43 if (id.startsWith("/")) {
44 if (myDetectedFacetIds == null) {
45 myDetectedFacetIds = new SmartList<Integer>();
47 myDetectedFacetIds.add(Integer.parseInt(id.substring(1)));
49 else {
50 if (myFacets == null) {
51 myFacets = new SmartList<FacetPointer>();
53 myFacets.add(facetPointersManager.create(id));
59 @Nullable
60 public SmartList<FacetPointer> getFacets() {
61 return myFacets;
64 @Nullable
65 public SmartList<Integer> getDetectedFacetIds() {
66 return myDetectedFacetIds;
69 public void write(final DataOutputStream stream) throws IOException {
70 super.write(stream);
71 int number = (myFacets != null ? myFacets.size() : 0) + (myDetectedFacetIds != null ? myDetectedFacetIds.size() : 0);
72 stream.writeInt(number);
73 if (myFacets != null) {
74 for (FacetPointer facetPointer : myFacets) {
75 stream.writeUTF(facetPointer.getId());
78 if (myDetectedFacetIds != null) {
79 for (Integer id : myDetectedFacetIds) {
80 stream.writeUTF("/" + id);
85 @Nullable
86 public Collection<Integer> update(final FacetPointersManager facetPointersManager, final @Nullable List<FacetInfo2<Module>> detectedFacets) {
87 if (detectedFacets == null || detectedFacets.isEmpty()) {
88 SmartList<Integer> old = myDetectedFacetIds;
89 myFacets = null;
90 myDetectedFacetIds = null;
91 return old;
94 if (myFacets == null) {
95 myFacets = new SmartList<FacetPointer>();
97 else {
98 myFacets.clear();
101 if (myDetectedFacetIds == null) {
102 myDetectedFacetIds = new SmartList<Integer>();
105 Set<Integer> toRemove = new HashSet<Integer>(myDetectedFacetIds);
106 for (FacetInfo2<Module> info : detectedFacets) {
107 if (info instanceof FacetInfoBackedByFacet) {
108 FacetPointer<Facet> pointer = facetPointersManager.create(((FacetInfoBackedByFacet)info).getFacet());
109 myFacets.add(pointer);
111 else {
112 Integer id = ((DetectedFacetInfo)info).getId();
113 boolean removed = toRemove.remove(id);
114 if (!removed) {
115 myDetectedFacetIds.add(id);
119 myDetectedFacetIds.removeAll(toRemove);
120 return toRemove;
123 public void remove(final FacetPointer facetPointer) {
124 if (myFacets != null) {
125 myFacets.remove(facetPointer);
129 public void remove(final Integer id) {
130 if (myDetectedFacetIds != null) {
131 myDetectedFacetIds.remove(id);
135 public void add(final FacetPointer<Facet> pointer) {
136 if (myFacets == null) {
137 myFacets = new SmartList<FacetPointer>();
139 myFacets.add(pointer);