Extend AbstractFilteringList
// 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 com.yahoo.collections.AbstractFilteringList;
+
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;
+public class LoadBalancerList extends AbstractFilteringList<LoadBalancer, LoadBalancerList> {
- private LoadBalancerList(Collection<LoadBalancer> loadBalancers) {
- this.loadBalancers = List.copyOf(Objects.requireNonNull(loadBalancers, "loadBalancers must be non-null"));
+ protected LoadBalancerList(Collection<LoadBalancer> items, boolean negate) {
+ super(items, negate, LoadBalancerList::new);
}
/** 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()));
+ return matching(lb -> lb.state() == state);
}
public static LoadBalancerList copyOf(Collection<LoadBalancer> loadBalancers) {
- return new LoadBalancerList(loadBalancers);
- }
-
- @Override
- public Iterator<LoadBalancer> iterator() {
- return loadBalancers.iterator();
+ return new LoadBalancerList(loadBalancers, false);
}
}
