Skip to content

Commit

Permalink
#527 Immutable IgnoreEntryOccurrence with immutable items, fixed hash…
Browse files Browse the repository at this point in the history
…Code calculation
  • Loading branch information
hsz committed Apr 11, 2018
1 parent 0ab0dea commit 41787c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 67 deletions.
76 changes: 24 additions & 52 deletions src/mobi/hsz/idea/gitignore/indexing/IgnoreEntryOccurrence.java
Expand Up @@ -27,6 +27,8 @@
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.ImmutableList;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.jetbrains.annotations.NotNull;
Expand All @@ -36,6 +38,7 @@
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.regex.Pattern;

/**
Expand All @@ -55,51 +58,31 @@ public class IgnoreEntryOccurrence implements Serializable {
private VirtualFile file;

/** Collection of ignore entries converted to {@link Pattern}. */
@Nullable
private Pair<Pattern, Boolean>[] items;

/**
* Constructor.
*
* @param url current file
*/
public IgnoreEntryOccurrence(@NotNull String url) {
this(url, null);
}

/**
* Constructor.
*
* @param file current file
*/
public IgnoreEntryOccurrence(@NotNull VirtualFile file) {
this(file.getUrl(), file);
}
@NotNull
private ImmutableList<Pair<Pattern, Boolean>> items;

/**
* Constructor.
*
* @param file current file
* @param url current file path
* @param url entry URL
* @param items parsed entry items
*/
public IgnoreEntryOccurrence(@NotNull String url, @Nullable VirtualFile file) {
public IgnoreEntryOccurrence(@NotNull String url, @NotNull ImmutableList<Pair<Pattern, Boolean>> items) {
this.url = url;
this.file = file;
this.items = items;
}

/**
* Calculates hashCode with {@link #file} and {@link #items} hashCodes.
* Calculates hashCode with {@link #url} and {@link #items} hashCodes.
*
* @return entry hashCode
*/
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder().append(url);

if (items != null) {
for (Pair<Pattern, Boolean> item : items) {
builder = builder.append(item.first).append(item.second);
}
for (Pair<Pattern, Boolean> item : items) {
builder.append(item.first.toString()).append(item.second);
}

return builder.toHashCode();
Expand All @@ -118,19 +101,17 @@ public boolean equals(@Nullable Object obj) {
}

final IgnoreEntryOccurrence entry = (IgnoreEntryOccurrence) obj;
if (items == null && entry.items == null) {
return true;
}
if (items == null || entry.items == null) {
if (!url.equals(entry.url) || items.size() != entry.items.size()) {
return false;
}

boolean equals = url.equals(entry.url) && items.length == entry.items.length;
for (int i = 0; i < items.length; i++) {
equals = equals && items[i].toString().equals(entry.items[i].toString());
for (int i = 0; i < items.size(); i++) {
if (!items.get(i).toString().equals(entry.items.get(i).toString())) {
return false;
}
}

return equals;
return true;
}

/**
Expand All @@ -152,15 +133,10 @@ public VirtualFile getFile() {
* @return entries
*/
@Nullable
public Pair<Pattern, Boolean>[] getItems() {
public ImmutableList<Pair<Pattern, Boolean>> getItems() {
return items;
}

/** Set entries for current file. */
public void setItems(@Nullable Pair<Pattern, Boolean>[] items) {
this.items = items;
}

/**
* Static helper to write given {@link IgnoreEntryOccurrence} to the output stream.
*
Expand All @@ -171,7 +147,7 @@ public void setItems(@Nullable Pair<Pattern, Boolean>[] items) {
public static synchronized void serialize(@NotNull DataOutput out, @NotNull IgnoreEntryOccurrence entry)
throws IOException {
out.writeUTF(entry.url);
out.writeInt(entry.items == null ? 0 : entry.items.length);
out.writeInt(entry.items.size());
for (Pair<Pattern, Boolean> item : entry.items) {
out.writeUTF(item.first.pattern());
out.writeBoolean(item.second);
Expand All @@ -191,18 +167,14 @@ public static synchronized IgnoreEntryOccurrence deserialize(@NotNull DataInput
return null;
}

final IgnoreEntryOccurrence entry = new IgnoreEntryOccurrence(url);
int size = in.readInt();

@SuppressWarnings("unchecked")
Pair<Pattern, Boolean>[] items = (Pair<Pattern, Boolean>[]) new Pair[size];
final int size = in.readInt();
final ArrayList<Pair<Pattern, Boolean>> items = ContainerUtil.newArrayList();
for (int i = 0; i < size; i++) {
final Pattern pattern = Pattern.compile(in.readUTF());
Boolean isNegated = in.readBoolean();
items[i] = Pair.create(pattern, isNegated);
items.add(Pair.create(pattern, isNegated));
}
entry.setItems(items);

return entry;
return new IgnoreEntryOccurrence(url, ContainerUtil.immutableList(items));
}
}
22 changes: 7 additions & 15 deletions src/mobi/hsz/idea/gitignore/indexing/IgnoreFilesIndex.java
Expand Up @@ -48,10 +48,7 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -91,13 +88,9 @@ public Map<IgnoreFileTypeKey, IgnoreEntryOccurrence> map(@NotNull final FileCont
if (!(inputData.getPsiFile() instanceof IgnoreFile)) {
return Collections.emptyMap();
}
final IgnoreEntryOccurrence result = new IgnoreEntryOccurrence(inputData.getFile());
final IgnoreFileType type = (IgnoreFileType) inputData.getFileType();

IgnoreFile ignoreFile = (IgnoreFile) inputData.getPsiFile();
final List<Pair<Pattern, Boolean>> items = ContainerUtil.newArrayList();

ignoreFile.acceptChildren(new IgnoreVisitor() {
final ArrayList<Pair<Pattern, Boolean>> items = ContainerUtil.newArrayList();
inputData.getPsiFile().acceptChildren(new IgnoreVisitor() {
@Override
public void visitEntry(@NotNull IgnoreEntry entry) {
final Pattern pattern = Glob.createPattern(entry);
Expand All @@ -107,11 +100,10 @@ public void visitEntry(@NotNull IgnoreEntry entry) {
}
});

@SuppressWarnings("unchecked")
Pair<Pattern, Boolean>[] arr = (Pair<Pattern, Boolean>[]) new Pair[0];
result.setItems(items.toArray(arr));

return Collections.singletonMap(new IgnoreFileTypeKey(type), result);
return Collections.singletonMap(
new IgnoreFileTypeKey((IgnoreFileType) inputData.getFileType()),
new IgnoreEntryOccurrence(inputData.getFile().getUrl(), ContainerUtil.immutableList(items))
);
}

/**
Expand Down

0 comments on commit 41787c8

Please sign in to comment.