/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.facebook.buck.util.collect;

import com.google.common.collect.ImmutableSet;

/** Additional methods to deal with sets. */
public class MoreSets {
  private MoreSets() {}

  /** Creates a union of two immutable sets. */
  public static <T> ImmutableSet<T> union(ImmutableSet<T> set1, ImmutableSet<T> set2) {
    return ImmutableSet.<T>builderWithExpectedSize(set1.size() + set2.size())
        .addAll(set1)
        .addAll(set2)
        .build();
  }

  /** Creates a union of three immutable sets. */
  public static <T> ImmutableSet<T> union(
      ImmutableSet<T> set1, ImmutableSet<T> set2, ImmutableSet<T> set3) {
    return ImmutableSet.<T>builderWithExpectedSize(set1.size() + set2.size() + set3.size())
        .addAll(set1)
        .addAll(set2)
        .addAll(set3)
        .build();
  }
}

@@ -926,24 +926,136 @@
 %3CT%3E set2) %7B%0A
+    if (set1.isEmpty()) %7B%0A      return set2;%0A    %7D else if (set2.isEmpty()) %7B%0A      return set1;%0A    %7D else %7B%0A  
     return I
@@ -1120,32 +1120,34 @@
 size())%0A        
+  
 .addAll(set1)%0A  
@@ -1136,32 +1136,34 @@
   .addAll(set1)%0A
+  
         .addAll(
@@ -1160,32 +1160,34 @@
   .addAll(set2)%0A
+  
         .build()
@@ -1184,24 +1184,30 @@
   .build();%0A
+    %7D%0A
   %7D%0A%0A  /** C
@@ -1362,24 +1362,227 @@
 %3CT%3E set3) %7B%0A
+    if (set1.isEmpty()) %7B%0A      return union(set2, set3);%0A    %7D else if (set2.isEmpty()) %7B%0A      return union(set1, set3);%0A    %7D else if (set3.isEmpty()) %7B%0A      return union(set1, set2);%0A    %7D else %7B%0A  
     return I
@@ -1661,32 +1661,34 @@
 size())%0A        
+  
 .addAll(set1)%0A  
@@ -1677,32 +1677,34 @@
   .addAll(set1)%0A
+  
         .addAll(
@@ -1709,32 +1709,34 @@
 l(set2)%0A        
+  
 .addAll(set3)%0A  
@@ -1733,32 +1733,34 @@
 l(set3)%0A        
+  
 .build();%0A  %7D%0A%7D%0A
@@ -1753,14 +1753,20 @@
 uild();%0A
+    %7D%0A
   %7D%0A%7D%0A

Optimize MoreSets.union