// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * A filterable load balancer list. This is immutable.
 *
 * @author mpolden
 */
public class LoadBalancerList implements Iterable<LoadBalancer> {

    private final List<LoadBalancer> loadBalancers;

    private LoadBalancerList(Collection<LoadBalancer> loadBalancers) {
        this.loadBalancers = List.copyOf(Objects.requireNonNull(loadBalancers, "loadBalancers must be non-null"));
    }

    /** Returns the subset of load balancers that are in given state */
    public LoadBalancerList in(LoadBalancer.State state) {
        return of(loadBalancers.stream().filter(lb -> lb.state() == state));
    }

    public List<LoadBalancer> asList() {
        return loadBalancers;
    }

    private static LoadBalancerList of(Stream<LoadBalancer> stream) {
        return new LoadBalancerList(stream.collect(Collectors.toUnmodifiableList()));
    }

    public static LoadBalancerList copyOf(Collection<LoadBalancer> loadBalancers) {
        return new LoadBalancerList(loadBalancers);
    }

    @Override
    public Iterator<LoadBalancer> iterator() {
        return loadBalancers.iterator();
    }

}

@@ -163,172 +163,81 @@
 ort 
-java.util.Collection;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Objects;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream
+com.yahoo.collections.AbstractFilteringList;%0A%0Aimport java.util.Collection
 ;%0A%0A/
@@ -354,63 +354,33 @@
 ist 
-implements Iterable%3CLoadBalancer%3E %7B%0A%0A    private final 
+extends AbstractFiltering
 List
@@ -384,35 +384,35 @@
 ist%3CLoadBalancer
-%3E l
+, L
 oadBalancers;%0A%0A 
@@ -410,18 +410,23 @@
 ncer
-s;
+List%3E %7B
 %0A%0A    pr
 ivat
@@ -421,21 +421,23 @@
 %0A%0A    pr
-ivate
+otected
  LoadBal
@@ -475,137 +475,83 @@
 er%3E 
-loadBalancers) %7B%0A        this.loadBalancers = List.copyOf(Objects.requireNonNull(loadBalancers, %22loadBalancers must be non-null%22)
+items, boolean negate) %7B%0A        super(items, negate, LoadBalancerList::new
 );%0A 
@@ -706,308 +706,42 @@
 urn 
-of(loadBalancers.stream().filter(lb -%3E lb.state() == state));%0A    %7D%0A%0A    public List%3CLoadBalancer%3E asList() %7B%0A        return loadBalancers;%0A    %7D%0A%0A    private static LoadBalancerList of(Stream%3CLoadBalancer%3E stream) %7B%0A        return new LoadBalancerList(stream.collect(Collectors.toUnmodifiableList())
+matching(lb -%3E lb.state() == state
 );%0A 
@@ -883,117 +883,15 @@
 cers
-);%0A    %7D%0A%0A    @Override%0A    public Iterator%3CLoadBalancer%3E iterator() %7B%0A        return loadBalancers.iterator(
+, false
 );%0A 

Extend AbstractFilteringList