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 com
.intellij
.openapi
.vcs
.changes
;
18 import com
.intellij
.openapi
.util
.Comparing
;
19 import com
.intellij
.openapi
.util
.Pair
;
20 import com
.intellij
.openapi
.vcs
.FilePath
;
21 import com
.intellij
.openapi
.vcs
.FileStatus
;
22 import com
.intellij
.openapi
.vcs
.VcsKey
;
23 import com
.intellij
.openapi
.vfs
.VirtualFile
;
24 import org
.jetbrains
.annotations
.Nullable
;
27 import java
.util
.ArrayList
;
28 import java
.util
.HashMap
;
29 import java
.util
.List
;
32 public class ChangeListsIndexes
{
33 private final Map
<String
, FileStatus
> myFileToStatus
;
34 private final Map
<String
, VcsKey
> myFileToVcs
;
36 ChangeListsIndexes() {
37 myFileToStatus
= new HashMap
<String
, FileStatus
>();
38 myFileToVcs
= new HashMap
<String
, VcsKey
>();
41 ChangeListsIndexes(final ChangeListsIndexes idx
) {
42 myFileToStatus
= new HashMap
<String
, FileStatus
>(idx
.myFileToStatus
);
43 myFileToVcs
= new HashMap
<String
, VcsKey
>(idx
.myFileToVcs
);
46 void add(final FilePath file
, final FileStatus status
, final VcsKey key
) {
47 final String fileKey
= file
.getIOFile().getAbsolutePath();
48 myFileToStatus
.put(fileKey
, status
);
49 myFileToVcs
.put(fileKey
, key
);
52 void remove(final FilePath file
) {
53 final String fileKey
= file
.getIOFile().getAbsolutePath();
54 myFileToStatus
.remove(fileKey
);
55 myFileToVcs
.remove(fileKey
);
58 public FileStatus
getStatus(final VirtualFile file
) {
59 return myFileToStatus
.get(new File(file
.getPath()).getAbsolutePath());
62 public void changeAdded(final Change change
, final VcsKey key
) {
63 addChangeToIdx(change
, key
);
66 public void changeRemoved(final Change change
) {
67 final ContentRevision afterRevision
= change
.getAfterRevision();
68 final ContentRevision beforeRevision
= change
.getBeforeRevision();
70 if (afterRevision
!= null) {
71 remove(afterRevision
.getFile());
73 if (beforeRevision
!= null) {
74 remove(beforeRevision
.getFile());
78 public VcsKey
getVcsFor(final Change change
) {
79 VcsKey key
= getVcsForRevision(change
.getAfterRevision());
80 if (key
!= null) return key
;
81 return getVcsForRevision(change
.getBeforeRevision());
85 private VcsKey
getVcsForRevision(final ContentRevision revision
) {
86 if (revision
!= null) {
87 final String fileKey
= revision
.getFile().getIOFile().getAbsolutePath();
88 return myFileToVcs
.get(fileKey
);
93 private void addChangeToIdx(final Change change
, final VcsKey key
) {
94 final ContentRevision afterRevision
= change
.getAfterRevision();
95 if (afterRevision
!= null) {
96 add(afterRevision
.getFile(), change
.getFileStatus(), key
);
98 final ContentRevision beforeRevision
= change
.getBeforeRevision();
99 if (beforeRevision
!= null) {
100 if (afterRevision
!= null) {
101 if (! Comparing
.equal(beforeRevision
.getFile(), afterRevision
.getFile())) {
102 add(beforeRevision
.getFile(), FileStatus
.DELETED
, key
);
105 add(beforeRevision
.getFile(), change
.getFileStatus(), key
);
110 public List
<Pair
<String
, VcsKey
>> getAffectedFilesUnderVcs() {
111 final ArrayList
<Pair
<String
, VcsKey
>> result
= new ArrayList
<Pair
<String
, VcsKey
>>();
112 for (Map
.Entry
<String
, VcsKey
> entry
: myFileToVcs
.entrySet()) {
113 result
.add(new Pair
<String
, VcsKey
>(entry
.getKey(), entry
.getValue()));
118 public List
<File
> getAffectedPaths() {
119 final List
<File
> result
= new ArrayList
<File
>(myFileToStatus
.size());
120 for (String path
: myFileToStatus
.keySet()) {
121 result
.add(new File(path
));