/*
 * Copyright 2014 Giannis Dzegoutanis
 *
 * 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 org.bitcoinj.params;

import org.bitcoinj.core.NetworkParameters;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

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

/**
 * Utility class that holds all the registered {@link NetworkParameters} types used for address auto discovery.
 * By default only {@link MainNetParams} and {@link TestNet3Params} are used. If you want to use {@link RegTestParams}
 * or {@link UnitTestParams} use {@code register} and then {@code unregister} the {@code TestNet3Params} as they don't
 * have their own Base58 version/type code (although for {@link org.bitcoinj.core.SegwitAddress} the human readable
 * parts for RegTest and TestNet are different.)
 */
public class Networks {
    /** Registered networks */
    private static Set<NetworkParameters> networks = ImmutableSet.<NetworkParameters>of(TestNet3Params.get(), MainNetParams.get());

    public static Set<NetworkParameters> get() {
        return networks;
    }

    /**
     * Register a single network type by adding it to the {@code Set}.
     *
     * @param network Network to register/add.
     */
    public static void register(NetworkParameters network) {
        register(Lists.newArrayList(network));
    }

    /**
     * Register a collection of additional network types by adding them
     * to the {@code Set}.
     *
     * @param networks Networks to register/add.
     */
    public static void register(Collection<? extends NetworkParameters> networks) {
        ImmutableSet.Builder<NetworkParameters> builder = ImmutableSet.builder();
        builder.addAll(Networks.networks);
        builder.addAll(networks);
        Networks.networks = builder.build();
    }

    /**
     * Unregister a network type.
     *
     * @param network Network type to unregister/remove.
     */
    public static void unregister(NetworkParameters network) {
        if (networks.contains(network)) {
            ImmutableSet.Builder<NetworkParameters> builder = ImmutableSet.builder();
            for (NetworkParameters parameters : networks) {
                if (parameters.equals(network))
                    continue;
                builder.add(parameters);
            }
            networks = builder.build();
        }
    }
}

@@ -674,96 +674,93 @@
 rs;%0A
+%0A
 import 
-com.google.common.collect.ImmutableSet;%0Aimport com.google.common.c
+java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.C
 ollect
-.List
+ion
 s;%0A
-%0A
 impo
@@ -768,34 +768,31 @@
 t java.util.
-Collection
+HashSet
 ;%0Aimport jav
@@ -1439,42 +1439,23 @@
 s = 
-ImmutableSet.%3CNetworkParameters%3Eof
+unmodifiableSet
 (Tes
@@ -1800,26 +1800,29 @@
 ter(
-Lists.newArrayList
+Collections.singleton
 (net
@@ -2054,18 +2054,8 @@
 ion%3C
-? extends 
 Netw
@@ -2093,201 +2093,67 @@
     
-ImmutableSet.Builder%3CNetworkParameters%3E builder = ImmutableSet.builder();%0A        builder.addAll(Networks.networks);%0A        builder.addAll(networks);%0A        Networks.networks = builder.build(
+Networks.networks = combinedSet(Networks.networks, networks
 );%0A 
@@ -2347,21 +2347,17 @@
     
-if (n
+N
 etworks.
 cont
@@ -2356,357 +2356,849 @@
 rks.
-contains(network)) %7B
+networks = removeFromSet(networks, network);
 %0A    
+%7D%0A%0A
     
-    ImmutableSet.Builder%3CNetworkParameters%3E builder = ImmutableSet.builder();%0A            for (NetworkParameters parameters : networks) %7B%0A                if (parameters.equals(network))%0A                    continue;%0A                builder.add(parameters);%0A            %7D%0A            networks = builder.build();%0A        %7D
+// Create an unmodifiable set of NetworkParameters from an array/varargs%0A    private static Set%3CNetworkParameters%3E unmodifiableSet(NetworkParameters... ts) %7B%0A        return Collections.unmodifiableSet(new HashSet%3C%3E(Arrays.asList(ts)));%0A    %7D%0A%0A    // Create an unmodifiable set by combining two collections%0A    private static %3CT%3E Set%3CT%3E combinedSet(Collection%3CT%3E a, Collection%3CT%3E b) %7B%0A        Set%3CT%3E tempSet = new HashSet%3C%3E(a);%0A        tempSet.addAll(b);%0A        return Collections.unmodifiableSet(tempSet);%0A    %7D%0A%0A    // Create a new unmodifiable set by removing an item from an existing set%0A    private static %3CT%3E Set%3CT%3E removeFromSet(Set%3CT%3E set, T item) %7B%0A        Set%3CT%3E tempSet = new HashSet%3C%3E(set);%0A        tempSet.remove(item);%0A        return Collections.unmodifiableSet(tempSet);
 %0A   

Replace usage of Guava ImmutableSet with Collections.unmodifiableSet().