update copyright
[fedora-idea.git] / plugins / images / src / org / intellij / images / index / ImageInfoIndex.java
blob4b469d8646c628f8b0757269c038a4454d8be632
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.
16 package org.intellij.images.index;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.vfs.LocalFileSystem;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.openapi.vfs.ex.temp.TempFileSystem;
22 import com.intellij.psi.search.GlobalSearchScope;
23 import com.intellij.util.indexing.*;
24 import com.intellij.util.io.DataExternalizer;
25 import org.intellij.images.fileTypes.ImageFileTypeManager;
26 import org.intellij.images.util.ImageInfoReader;
27 import org.jetbrains.annotations.NotNull;
29 import java.io.DataInput;
30 import java.io.DataOutput;
31 import java.io.IOException;
33 /**
34 * @author spleaner
36 public class ImageInfoIndex extends SingleEntryFileBasedIndexExtension<ImageInfoIndex.ImageInfo> {
37 public static final ID<Integer, ImageInfo> INDEX_ID = ID.create("ImageFileInfoIndex");
39 private final FileBasedIndex.InputFilter myInputFilter = new FileBasedIndex.InputFilter() {
40 public boolean acceptInput(final VirtualFile file) {
41 return (file.getFileSystem() == LocalFileSystem.getInstance() || file.getFileSystem() instanceof TempFileSystem) &&
42 file.getFileType() == ImageFileTypeManager.getInstance().getImageFileType();
46 private final DataExternalizer<ImageInfo> myValueExternalizer = new DataExternalizer<ImageInfo>() {
47 public void save(final DataOutput out, final ImageInfo info) throws IOException {
48 out.writeInt(info.width);
49 out.writeInt(info.height);
50 out.writeInt(info.bpp);
53 public ImageInfo read(final DataInput in) throws IOException {
54 return new ImageInfo(in.readInt(), in.readInt(), in.readInt());
58 private final SingleEntryIndexer<ImageInfo> myDataIndexer = new SingleEntryIndexer<ImageInfo>(false) {
59 protected ImageInfo computeValue(@NotNull FileContent inputData) {
60 final ImageInfoReader.Info info = ImageInfoReader.getInfo(inputData.getContent());
61 return info != null? new ImageInfo(info.width, info.height, info.bpp) : null;
65 public ID<Integer, ImageInfo> getName() {
66 return INDEX_ID;
69 public SingleEntryIndexer<ImageInfo> getIndexer() {
70 return myDataIndexer;
73 public static void processValues(VirtualFile virtualFile, FileBasedIndex.ValueProcessor<ImageInfo> processor, Project project) {
74 FileBasedIndex.getInstance().processValues(INDEX_ID, Math.abs(FileBasedIndex.getFileId(virtualFile)), virtualFile, processor, GlobalSearchScope.allScope(project));
77 public DataExternalizer<ImageInfo> getValueExternalizer() {
78 return myValueExternalizer;
81 public FileBasedIndex.InputFilter getInputFilter() {
82 return myInputFilter;
85 public int getVersion() {
86 return 2;
89 public static class ImageInfo {
90 public int width;
91 public int height;
92 public int bpp;
94 public ImageInfo(int width, int height, int bpp) {
95 this.width = width;
96 this.height = height;
97 this.bpp = bpp;
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) return true;
103 if (o == null || getClass() != o.getClass()) return false;
105 ImageInfo imageInfo = (ImageInfo)o;
107 if (bpp != imageInfo.bpp) return false;
108 if (height != imageInfo.height) return false;
109 if (width != imageInfo.width) return false;
111 return true;
114 @Override
115 public int hashCode() {
116 int result = width;
117 result = 31 * result + height;
118 result = 31 * result + bpp;
119 return result;