summaryrefslogtreecommitdiff
path: root/java/compiler/impl/src/com/intellij/packaging/impl/compiler/ArtifactCompilerCompileItem.java
blob: b4e1865f46ff3ff933b7dfa67a19027844272a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.packaging.impl.compiler;

import com.intellij.openapi.compiler.generic.VirtualFileCompileItem;
import com.intellij.compiler.impl.packagingCompiler.DestinationInfo;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SmartList;
import com.intellij.util.io.DataExternalizer;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author nik
 */
public class ArtifactCompilerCompileItem extends VirtualFileCompileItem<ArtifactPackagingItemOutputState> {
  public static final DataExternalizer<ArtifactPackagingItemOutputState> OUTPUT_EXTERNALIZER = new ArtifactPackagingItemExternalizer();
  private final List<DestinationInfo> myDestinations = new SmartList<DestinationInfo>();

  public ArtifactCompilerCompileItem(VirtualFile file) {
    super(file);
  }

  public void addDestination(DestinationInfo info) {
    myDestinations.add(info);
  }

  public List<DestinationInfo> getDestinations() {
    return myDestinations;
  }

  @NotNull
  @Override
  public ArtifactPackagingItemOutputState computeOutputState() {
    final SmartList<Pair<String, Long>> pairs = new SmartList<Pair<String, Long>>();
    for (DestinationInfo destination : myDestinations) {
      destination.update();
      final VirtualFile outputFile = destination.getOutputFile();
      long timestamp = outputFile != null ? outputFile.getTimeStamp() : -1;
      pairs.add(Pair.create(destination.getOutputPath(), timestamp));
    }
    return new ArtifactPackagingItemOutputState(pairs);
  }

  @Override
  public boolean isOutputUpToDate(@NotNull ArtifactPackagingItemOutputState state) {
    final SmartList<Pair<String, Long>> cachedDestinations = state.myDestinations;
    if (cachedDestinations.size() != myDestinations.size()) {
      return false;
    }

    for (DestinationInfo info : myDestinations) {
      final VirtualFile outputFile = info.getOutputFile();
      long timestamp = outputFile != null ? outputFile.getTimeStamp() : -1;
      final String path = info.getOutputPath();
      boolean found = false;
      //todo[nik] use map if list contains many items
      for (Pair<String, Long> cachedDestination : cachedDestinations) {
        if (cachedDestination.first.equals(path)) {
          if (cachedDestination.second != timestamp) return false;
          found = true;
          break;
        }
      }
      if (!found) return false;
    }

    return true;
  }

}