src/java.base/share/classes/sun/nio/ch/MembershipRegistry.java
changeset 59146 455612b3161a
parent 47216 71c04702a3d5
equal deleted inserted replaced
59145:ea044aedc2b6 59146:455612b3161a
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.nio.channels.*;
       
    29 import java.net.InetAddress;
    28 import java.net.InetAddress;
    30 import java.net.NetworkInterface;
    29 import java.net.NetworkInterface;
    31 import java.util.*;
    30 import java.nio.channels.MembershipKey;
       
    31 import java.util.HashMap;
       
    32 import java.util.Iterator;
       
    33 import java.util.LinkedList;
       
    34 import java.util.List;
       
    35 import java.util.Map;
    32 
    36 
    33 /**
    37 /**
    34  * Simple registry of membership keys for a MulticastChannel.
    38  * Simple registry of membership keys for a MulticastChannel.
    35  *
    39  *
    36  * Instances of this object are not safe by multiple concurrent threads.
    40  * Instances of this object are not safe by multiple concurrent threads.
    37  */
    41  */
    38 
    42 
    39 class MembershipRegistry {
    43 class MembershipRegistry {
    40 
    44 
    41     // map multicast group to keys
    45     // map multicast group to list of keys
    42     private Map<InetAddress,List<MembershipKeyImpl>> groups = null;
    46     private Map<InetAddress, List<MembershipKeyImpl>> groups;
    43 
    47 
    44     MembershipRegistry() {
    48     MembershipRegistry() {
    45     }
    49     }
    46 
    50 
    47     /**
    51     /**
   114                 groups.remove(group);
   118                 groups.remove(group);
   115             }
   119             }
   116         }
   120         }
   117     }
   121     }
   118 
   122 
       
   123     @FunctionalInterface
       
   124     interface ThrowingConsumer<T, X extends Throwable> {
       
   125         void accept(T action) throws X;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Invoke an action for each key in the registry
       
   130      */
       
   131      <X extends Throwable>
       
   132      void forEach(ThrowingConsumer<MembershipKeyImpl, X> action) throws X {
       
   133         if (groups != null) {
       
   134             for (List<MembershipKeyImpl> keys : groups.values()) {
       
   135                 for (MembershipKeyImpl key : keys) {
       
   136                     action.accept(key);
       
   137                 }
       
   138             }
       
   139         }
       
   140     }
       
   141 
   119     /**
   142     /**
   120      * Invalidate all keys in the registry
   143      * Invalidate all keys in the registry
   121      */
   144      */
   122     void invalidateAll() {
   145     void invalidateAll() {
   123         if (groups != null) {
   146         forEach(MembershipKeyImpl::invalidate);
   124             for (InetAddress group: groups.keySet()) {
       
   125                 for (MembershipKeyImpl key: groups.get(group)) {
       
   126                     key.invalidate();
       
   127                 }
       
   128             }
       
   129         }
       
   130     }
   147     }
   131 }
   148 }