update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / find / ngrams / TrigramIndex.java
blob9fc580f9bffac263ea74cc18d8fb2a2f13ec92ff
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author max
20 package com.intellij.find.ngrams;
22 import com.intellij.openapi.util.text.TrigramBuilder;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.util.indexing.*;
25 import com.intellij.util.io.EnumeratorIntegerDescriptor;
26 import com.intellij.util.io.KeyDescriptor;
27 import gnu.trove.THashMap;
28 import gnu.trove.TIntHashSet;
29 import gnu.trove.TIntProcedure;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.Map;
34 public class TrigramIndex extends ScalarIndexExtension<Integer> {
35 public static final boolean ENABLED = "true".equals(System.getProperty("idea.internal.trigramindex.enabled"));
37 public static final ID<Integer,Void> INDEX_ID = ID.create("Trigram.Index");
39 private static final FileBasedIndex.InputFilter INPUT_FILTER = new FileBasedIndex.InputFilter() {
40 public boolean acceptInput(VirtualFile file) {
41 return !file.getFileType().isBinary();
44 private static final FileBasedIndex.InputFilter NO_FILES = new FileBasedIndex.InputFilter() {
45 public boolean acceptInput(VirtualFile file) {
46 return false;
50 public ID<Integer, Void> getName() {
51 return INDEX_ID;
54 public DataIndexer<Integer, Void, FileContent> getIndexer() {
55 return new DataIndexer<Integer, Void, FileContent>() {
56 @NotNull
57 public Map<Integer, Void> map(FileContent inputData) {
58 final Map<Integer, Void> result = new THashMap<Integer, Void>();
59 TIntHashSet built = TrigramBuilder.buildTrigram(inputData.getContentAsText());
60 built.forEach(new TIntProcedure() {
61 public boolean execute(int value) {
62 result.put(value, null);
63 return true;
65 });
66 return result;
71 public KeyDescriptor<Integer> getKeyDescriptor() {
72 return new EnumeratorIntegerDescriptor();
75 public FileBasedIndex.InputFilter getInputFilter() {
76 if (ENABLED) {
77 return INPUT_FILTER;
79 else {
80 return NO_FILES;
84 public boolean dependsOnFileContent() {
85 return true;
88 public int getVersion() {
89 return ENABLED ? 2 : 1;