jps: CompiledClass accepts multiple source files
[idea/community.git] / jps / jps-builders / src / org / jetbrains / jps / incremental / CompiledClass.java
1 /*
2  * Copyright 2000-2012 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.jetbrains.jps.incremental;
17
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.util.UserDataHolderBase;
20 import com.intellij.openapi.util.io.FileUtil;
21 import com.intellij.util.Function;
22 import com.intellij.util.containers.ContainerUtil;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.Collection;
29 import java.util.Collections;
30
31 /**
32  * @author Eugene Zhuravlev
33  *         Date: 11/18/12
34  */
35 public class CompiledClass extends UserDataHolderBase{
36   private final static Logger LOG = Logger.getInstance(CompiledClass.class);
37
38   @NotNull
39   private final File myOutputFile;
40   @NotNull
41   private final Collection<File> mySourceFiles;
42   @Nullable
43   private final String myClassName;
44   @NotNull
45   private BinaryContent myContent;
46
47   private boolean myIsDirty = false;
48
49   public CompiledClass(@NotNull File outputFile, @NotNull Collection<File> sourceFiles, @Nullable String className, @NotNull BinaryContent content) {
50     myOutputFile = outputFile;
51     mySourceFiles = sourceFiles;
52     myClassName = className;
53     myContent = content;
54     LOG.assertTrue(!mySourceFiles.isEmpty());
55   }
56
57   public CompiledClass(@NotNull File outputFile, @NotNull File sourceFile, @Nullable String className, @NotNull BinaryContent content) {
58     this(outputFile, Collections.singleton(sourceFile), className, content);
59   }
60
61   public  void save() throws IOException {
62     myContent.saveToFile(myOutputFile);
63     myIsDirty = false;
64   }
65
66   @NotNull
67   public File getOutputFile() {
68     return myOutputFile;
69   }
70
71   @NotNull
72   public Collection<File> getSourceFiles() {
73     return mySourceFiles;
74   }
75
76   @NotNull
77   public Collection<String> getSourceFilesPaths() {
78     return ContainerUtil.map(mySourceFiles, new Function<File, String>() {
79       @Override
80       public String fun(File file) {
81         return file.getPath();
82       }
83     });
84   }
85
86   /**
87    * @deprecated use {@link CompiledClass#getSourceFiles()} or {{@link CompiledClass#getSourceFilesPaths()}
88    */
89   @Deprecated
90   @NotNull
91   public File getSourceFile() {
92     //noinspection ConstantConditions
93     return ContainerUtil.getFirstItem(getSourceFiles());
94   }
95
96   @Nullable
97   public String getClassName() {
98     return myClassName;
99   }
100
101   @NotNull
102   public BinaryContent getContent() {
103     return myContent;
104   }
105
106   public void setContent(@NotNull BinaryContent content) {
107     myContent = content;
108     myIsDirty = true;
109   }
110
111   public boolean isDirty() {
112     return myIsDirty;
113   }
114
115   @Override
116   public boolean equals(Object o) {
117     if (this == o) return true;
118     if (o == null || getClass() != o.getClass()) return false;
119
120     CompiledClass aClass = (CompiledClass)o;
121
122     if (!FileUtil.filesEqual(myOutputFile, aClass.myOutputFile)) return false;
123
124     return true;
125   }
126
127   @Override
128   public int hashCode() {
129     return FileUtil.fileHashCode(myOutputFile);
130   }
131
132   @Override
133   public String toString() {
134     return "CompiledClass{" +
135            "myOutputFile=" + myOutputFile +
136            ", mySourceFiles=" + mySourceFiles +
137            ", myIsDirty=" + myIsDirty +
138            '}';
139   }
140 }