// Copyright (C) 2018 The Android Open Source Project
//
// 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.google.gerrit.server.account.externalids;

import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.gerrit.reviewdb.client.Account.Id;

/**
 * Cache value containing all external IDs.
 *
 * <p>All returned fields are unmodifiable.
 */
@AutoValue
public abstract class AllExternalIds {
  static AllExternalIds create(Multimap<Id, ExternalId> byAccount) {
    SetMultimap<String, ExternalId> byEmailCopy =
        MultimapBuilder.hashKeys(byAccount.size()).hashSetValues(1).build();
    byAccount
        .values()
        .stream()
        .filter(e -> !Strings.isNullOrEmpty(e.email()))
        .forEach(e -> byEmailCopy.put(e.email(), e));

    return new AutoValue_AllExternalIds(
        Multimaps.unmodifiableSetMultimap(
            MultimapBuilder.hashKeys(byAccount.size()).hashSetValues(5).build(byAccount)),
        byEmailCopy);
  }

  public abstract SetMultimap<Id, ExternalId> byAccount();

  public abstract SetMultimap<String, ExternalId> byEmail();
}

@@ -736,16 +736,71 @@
 trings;%0A
+import com.google.common.collect.ImmutableSetMultimap;%0A
 import c
@@ -888,52 +888,8 @@
 er;%0A
-import com.google.common.collect.Multimaps;%0A
 impo
@@ -922,32 +922,82 @@
 ct.SetMultimap;%0A
+import com.google.gerrit.reviewdb.client.Account;%0A
 import com.googl
@@ -1037,19 +1037,16 @@
 Id;%0A%0A/**
-%0A *
  Cache v
@@ -1082,56 +1082,8 @@
 IDs.
-%0A *%0A * %3Cp%3EAll returned fields are unmodifiable.%0A
  */%0A
@@ -1542,26 +1542,13 @@
     
-Multimaps.unmodifi
+Immut
 able
@@ -1558,16 +1558,23 @@
 Multimap
+.copyOf
 (%0A      
@@ -1666,16 +1666,44 @@
         
+ImmutableSetMultimap.copyOf(
 byEmailC
@@ -1706,16 +1706,17 @@
 ailCopy)
+)
 ;%0A  %7D%0A%0A 
@@ -1724,32 +1724,41 @@
 public abstract 
+Immutable
 SetMultimap%3CId, 
@@ -1745,32 +1745,40 @@
 ableSetMultimap%3C
+Account.
 Id, ExternalId%3E 
@@ -1809,16 +1809,25 @@
 bstract 
+Immutable
 SetMulti

Make AllExternalIds fields immutable