// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.roots;

import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Objects;
import java.util.Set;

class ImmutableSyntheticLibrary extends SyntheticLibrary {

  private final Collection<VirtualFile> myBinaryRoots;
  private final Collection<VirtualFile> mySourceRoots;
  private final Set<VirtualFile> myExcludedRoots;
  private final Condition<VirtualFile> myExcludeCondition;

  ImmutableSyntheticLibrary(@NotNull Collection<VirtualFile> sourceRoots,
                            @NotNull Collection<VirtualFile> binaryRoots,
                            @NotNull Set<VirtualFile> excludedRoots,
                            @Nullable Condition<VirtualFile> excludeCondition) {
    mySourceRoots = sourceRoots;
    myBinaryRoots = binaryRoots;
    myExcludedRoots = excludedRoots;
    myExcludeCondition = excludeCondition;
  }

  @NotNull
  @Override
  public Collection<VirtualFile> getSourceRoots() {
    return mySourceRoots;
  }

  @NotNull
  @Override
  public Collection<VirtualFile> getBinaryRoots() {
    return myBinaryRoots;
  }

  @NotNull
  @Override
  public Set<VirtualFile> getExcludedRoots() {
    return myExcludedRoots;
  }

  @Nullable
  @Override
  public Condition<VirtualFile> getExcludeFileCondition() {
    return myExcludeCondition;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    ImmutableSyntheticLibrary library = (ImmutableSyntheticLibrary)o;
    if (!mySourceRoots.equals(library.getSourceRoots())) return false;
    if (!myBinaryRoots.equals(library.getBinaryRoots())) return false;
    if (!myExcludedRoots.equals(library.getExcludedRoots())) return false;
    if (!Objects.equals(myExcludeCondition, library.getExcludeFileCondition())) return false;
    return true;
  }

  @Override
  public int hashCode() {
    int result = mySourceRoots.hashCode();
    result = 31 * result + myBinaryRoots.hashCode();
    result = 31 * result + myExcludedRoots.hashCode();
    result = 31 * result + (myExcludeCondition != null ? myExcludeCondition.hashCode() : 0);
    return result;
  }
}

@@ -260,16 +260,67 @@
 alFile;%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import o
@@ -353,16 +353,16 @@
 otNull;%0A
-
 import o
@@ -1073,16 +1073,60 @@
 Roots = 
+ContainerUtil.unmodifiableOrEmptyCollection(
 sourceRo
@@ -1124,24 +1124,25 @@
 (sourceRoots
+)
 ;%0A    myBina
@@ -1151,16 +1151,60 @@
 Roots = 
+ContainerUtil.unmodifiableOrEmptyCollection(
 binaryRo
@@ -1202,24 +1202,25 @@
 (binaryRoots
+)
 ;%0A    myExcl
@@ -1231,16 +1231,53 @@
 Roots = 
+ContainerUtil.unmodifiableOrEmptySet(
 excluded
@@ -1277,24 +1277,25 @@
 xcludedRoots
+)
 ;%0A    myExcl
@@ -2359,265 +2359,94 @@
     
-int result = mySourceRoots.hashCode();%0A    result = 31 * result + myBinaryRoots.hashCode();%0A    result = 31 * result + myExcludedRoots.hashCode();%0A    result = 31 * result + (myExcludeCondition != null ? myExcludeCondition.hashCode() : 0);%0A    return result
+return Objects.hash(mySourceRoots, myBinaryRoots, myExcludedRoots, myExcludeCondition)
 ;%0A  

use unmodifiable collections and Objects.hash (IDEA-CR-29435)