changeset 781:694942f9c83b nio2-b97

MembershipKey's getXXX methods inconsistent with SelectionKey/FileLock Also WatchEvent in file package.
author alanb
date Tue, 21 Oct 2008 18:25:15 +0100
parents cf485a78c523
children 6386808e17ca
files src/share/classes/java/nio/channels/MembershipKey.java src/share/classes/java/nio/channels/MulticastChannel.java src/share/classes/java/nio/file/Path.java src/share/classes/java/nio/file/StandardWatchEventKind.java src/share/classes/java/nio/file/WatchEvent.java src/share/classes/sun/nio/ch/DatagramChannelImpl.java src/share/classes/sun/nio/ch/MembershipKeyImpl.java src/share/classes/sun/nio/ch/MembershipRegistry.java src/share/classes/sun/nio/ch/SimpleAsynchronousDatagramChannelImpl.java src/share/classes/sun/nio/fs/AbstractWatchKey.java src/share/sample/nio/file/WatchDir.java test/java/nio/channels/AsynchronousDatagramChannel/Basic.java test/java/nio/channels/DatagramChannel/BasicMulticastTests.java test/java/nio/file/WatchService/Basic.java test/java/nio/file/WatchService/FileTreeModifier.java
diffstat 15 files changed, 87 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/channels/MembershipKey.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/java/nio/channels/MembershipKey.java	Tue Oct 21 18:25:15 2008 +0100
@@ -28,7 +28,6 @@
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.io.IOException;
-import java.util.List;
 
 /**
  * A token representing the membership of an Internet Protocol (IP) multicast
@@ -38,7 +37,7 @@
  * to the group, or it may be <em>source-specific</em>, meaning that it
  * represents a membership that receives only datagrams from a specific source
  * address. Whether or not a membership key is source-specific may be determined
- * by invoking its {@link #getSourceAddress() getSourceAddress} method.
+ * by invoking its {@link #sourceAddress() sourceAddress} method.
  *
  * <p> A membership key is valid upon creation and remains valid until the
  * membership is dropped by invoking the {@link #drop() drop} method, or
@@ -152,7 +151,7 @@
      *
      * @return  the channel
      */
-    public abstract MulticastChannel getChannel();
+    public abstract MulticastChannel channel();
 
     /**
      * Returns the multicast group for which this membership key was created.
@@ -161,7 +160,7 @@
      *
      * @return  the multicast group
      */
-    public abstract InetAddress getGroup();
+    public abstract InetAddress group();
 
     /**
      * Returns the network interface for which this membership key was created.
@@ -170,7 +169,7 @@
      *
      * @return  the network interface
      */
-    public abstract NetworkInterface getNetworkInterface();
+    public abstract NetworkInterface networkInterface();
 
     /**
      * Returns the source address if this membership key is source-specific,
@@ -179,5 +178,5 @@
      * @return  The source address if this membership key is source-specific,
      *          otherwise {@code null}
      */
-    public abstract InetAddress getSourceAddress();
+    public abstract InetAddress sourceAddress();
 }
--- a/src/share/classes/java/nio/channels/MulticastChannel.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/java/nio/channels/MulticastChannel.java	Tue Oct 21 18:25:15 2008 +0100
@@ -130,7 +130,7 @@
      * interface to receive all datagrams then the membership key, representing
      * that membership, is returned. Otherwise this channel joins the group and
      * the resulting new membership key is returned. The resulting membership key
-     * is not {@link MembershipKey#getSourceAddress source-specific}.
+     * is not {@link MembershipKey#sourceAddress source-specific}.
      *
      * <p> A multicast channel may join several multicast groups, including
      * the same group on more than one interface. An implementation may impose a
@@ -170,7 +170,7 @@
      * interface to receive datagrams from the given source address then the
      * membership key, representing that membership, is returned. Otherwise this
      * channel joins the group and the resulting new membership key is returned.
-     * The resulting membership key is {@link MembershipKey#getSourceAddress
+     * The resulting membership key is {@link MembershipKey#sourceAddress
      * source-specific}.
      *
      * <p> Membership is <em>cumulative</em> and this method may be invoked
--- a/src/share/classes/java/nio/file/Path.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/java/nio/file/Path.java	Tue Oct 21 18:25:15 2008 +0100
@@ -1427,7 +1427,7 @@
      *        entry in directory was modified</li>
      * </ul>
      *
-     * <p> The {@link WatchEvent#getContext context} for these events is the
+     * <p> The {@link WatchEvent#context context} for these events is the
      * relative path between the directory located by this path, and the path
      * that locates the directory entry that is created, deleted, or modified.
      *
--- a/src/share/classes/java/nio/file/StandardWatchEventKind.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/java/nio/file/StandardWatchEventKind.java	Tue Oct 21 18:25:15 2008 +0100
@@ -38,9 +38,9 @@
      * A special event to indicate that events may have been lost or
      * discarded.
      *
-     * <p> The {@link WatchEvent#getContext context} for this event is
+     * <p> The {@link WatchEvent#context context} for this event is
      * implementation specific and may be {@code null}. The event {@link
-     * WatchEvent#getCount count} may be greater than {@code 1}.
+     * WatchEvent#count count} may be greater than {@code 1}.
      *
      * @see WatchService
      */
@@ -52,7 +52,7 @@
      *
      * <p> When a directory is registered for this event then the {@link WatchKey}
      * is queued when it is observed that an entry is created in the directory
-     * or renamed into the directory. The event {@link WatchEvent#getCount count}
+     * or renamed into the directory. The event {@link WatchEvent#count count}
      * for this event is always {@code 1}.
      */
     public static final WatchEvent.Kind<Path> ENTRY_CREATE =
@@ -63,7 +63,7 @@
      *
      * <p> When a directory is registered for this event then the {@link WatchKey}
      * is queued when it is observed that an entry is deleted or renamed out of
-     * the directory. The event {@link WatchEvent#getCount count} for this event
+     * the directory. The event {@link WatchEvent#count count} for this event
      * is always {@code 1}.
      */
     public static final WatchEvent.Kind<Path> ENTRY_DELETE =
@@ -74,7 +74,7 @@
      *
      * <p> When a directory is registered for this event then the {@link WatchKey}
      * is queued when it is observed that an entry in the directory has been
-     * modified. The event {@link WatchEvent#getCount count} for this event is
+     * modified. The event {@link WatchEvent#count count} for this event is
      * {@code 1} or greater.
      */
     public static final WatchEvent.Kind<Path> ENTRY_MODIFY =
--- a/src/share/classes/java/nio/file/WatchEvent.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/java/nio/file/WatchEvent.java	Tue Oct 21 18:25:15 2008 +0100
@@ -29,13 +29,16 @@
  * An event or a repeated event for an object that is registered with a {@link
  * WatchService}.
  *
- * <p> An event is classified by its {@link #getKind() kind} and has a {@link
- * #getCount() count} to indicate the number of times that the event has been
+ * <p> An event is classified by its {@link #kind() kind} and has a {@link
+ * #count() count} to indicate the number of times that the event has been
  * observed. This allows for efficient representation of repeated events. The
- * {@link #getContext() getContext} method returns any context associated with
+ * {@link #context() context} method returns any context associated with
  * the event. In the case of a repeated event then the context is the same for
  * all events.
  *
+ * <p> Watch events are immutable and safe for use by multiple concurrent
+ * threads.
+ *
  * @param   <T>     The type of the context object associated with the event
  *
  * @since 1.7
@@ -56,7 +59,7 @@
         String name();
 
         /**
-         * Returns the type of the {@link WatchEvent#getContext context} value.
+         * Returns the type of the {@link WatchEvent#context context} value.
          */
         Class<T> type();
     }
@@ -87,7 +90,7 @@
      *
      * @return  The event kind
      */
-    public abstract Kind<T> getKind();
+    public abstract Kind<T> kind();
 
     /**
      * Returns the event count. If the event count is greater than {@code 1}
@@ -95,7 +98,7 @@
      *
      * @return  The event count
      */
-    public abstract int getCount();
+    public abstract int count();
 
     /**
      * Returns the context for the event.
@@ -109,5 +112,5 @@
      *
      * @return  The event context; may be {@code null}
      */
-    public abstract T getContext();
+    public abstract T context();
 }
--- a/src/share/classes/sun/nio/ch/DatagramChannelImpl.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/sun/nio/ch/DatagramChannelImpl.java	Tue Oct 21 18:25:15 2008 +0100
@@ -871,7 +871,7 @@
     void drop(MembershipKeyImpl key)
         throws IOException
     {
-        assert key.getChannel() == this;
+        assert key.channel() == this;
 
         synchronized (stateLock) {
             if (!key.isValid())
@@ -880,11 +880,11 @@
             if (family == StandardProtocolFamily.INET6) {
                 MembershipKeyImpl.Type6 key6 =
                     (MembershipKeyImpl.Type6)key;
-                Net.drop6(fd, key6.group(), key6.index(), key6.source());
+                Net.drop6(fd, key6.groupAddress(), key6.index(), key6.source());
             } else {
-                MembershipKeyImpl.Type4 key4 =
-                    (MembershipKeyImpl.Type4)key;
-                Net.drop4(fd, key4.group(), key4.interfaceAddress(), key4.source());
+                MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
+                Net.drop4(fd, key4.groupAddress(), key4.interfaceAddress(),
+                    key4.source());
             }
 
             key.invalidate();
@@ -899,8 +899,8 @@
     void block(MembershipKeyImpl key, InetAddress source)
         throws IOException
     {
-        assert key.getChannel() == this;
-        assert key.getSourceAddress() == null;
+        assert key.channel() == this;
+        assert key.sourceAddress() == null;
 
         synchronized (stateLock) {
             if (!key.isValid())
@@ -909,19 +909,19 @@
                 throw new IllegalArgumentException("Source address is a wildcard address");
             if (source.isMulticastAddress())
                 throw new IllegalArgumentException("Source address is multicast address");
-            if (source.getClass() != key.getGroup().getClass())
+            if (source.getClass() != key.group().getClass())
                 throw new IllegalArgumentException("Source address is different type to group");
 
             int n;
             if (family == StandardProtocolFamily.INET6) {
                  MembershipKeyImpl.Type6 key6 =
                     (MembershipKeyImpl.Type6)key;
-                n = Net.block6(fd, key6.group(), key6.index(),
+                n = Net.block6(fd, key6.groupAddress(), key6.index(),
                                Net.inet6AsByteArray(source));
             } else {
                 MembershipKeyImpl.Type4 key4 =
                     (MembershipKeyImpl.Type4)key;
-                n = Net.block4(fd, key4.group(), key4.interfaceAddress(),
+                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
                                Net.inet4AsInt(source));
             }
             if (n == IOStatus.UNAVAILABLE) {
@@ -937,8 +937,8 @@
     void unblock(MembershipKeyImpl key, InetAddress source)
         throws IOException
     {
-        assert key.getChannel() == this;
-        assert key.getSourceAddress() == null;
+        assert key.channel() == this;
+        assert key.sourceAddress() == null;
 
         synchronized (stateLock) {
             if (!key.isValid())
@@ -947,12 +947,12 @@
             if (family == StandardProtocolFamily.INET6) {
                 MembershipKeyImpl.Type6 key6 =
                     (MembershipKeyImpl.Type6)key;
-                Net.unblock6(fd, key6.group(), key6.index(),
+                Net.unblock6(fd, key6.groupAddress(), key6.index(),
                              Net.inet6AsByteArray(source));
             } else {
                 MembershipKeyImpl.Type4 key4 =
                     (MembershipKeyImpl.Type4)key;
-                Net.unblock4(fd, key4.group(), key4.interfaceAddress(),
+                Net.unblock4(fd, key4.groupAddress(), key4.interfaceAddress(),
                              Net.inet4AsInt(source));
             }
         }
--- a/src/share/classes/sun/nio/ch/MembershipKeyImpl.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/sun/nio/ch/MembershipKeyImpl.java	Tue Oct 21 18:25:15 2008 +0100
@@ -85,7 +85,7 @@
             this.sourceAddress = sourceAddress;
         }
 
-        int group() {
+        int groupAddress() {
             return groupAddress;
         }
 
@@ -120,7 +120,7 @@
             this.sourceAddress = sourceAddress;
         }
 
-        byte[] group() {
+        byte[] groupAddress() {
             return groupAddress;
         }
 
@@ -148,22 +148,22 @@
     }
 
     @Override
-    public MulticastChannel getChannel() {
+    public MulticastChannel channel() {
         return ch;
     }
 
     @Override
-    public InetAddress getGroup() {
+    public InetAddress group() {
         return group;
     }
 
     @Override
-    public NetworkInterface getNetworkInterface() {
+    public NetworkInterface networkInterface() {
         return interf;
     }
 
     @Override
-    public InetAddress getSourceAddress() {
+    public InetAddress sourceAddress() {
         return source;
     }
 
--- a/src/share/classes/sun/nio/ch/MembershipRegistry.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/sun/nio/ch/MembershipRegistry.java	Tue Oct 21 18:25:15 2008 +0100
@@ -55,20 +55,20 @@
             List<MembershipKeyImpl> keys = groups.get(group);
             if (keys != null) {
                 for (MembershipKeyImpl key: keys) {
-                    if (key.getNetworkInterface().equals(interf)) {
+                    if (key.networkInterface().equals(interf)) {
                         // already a member to receive all packets so return
                         // existing key or detect conflict
                         if (source == null) {
-                            if (key.getSourceAddress() == null)
+                            if (key.sourceAddress() == null)
                                 return key;
                             throw new IllegalStateException("Already a member to receive all packets");
                         }
 
                         // already have source-specific membership so return key
                         // or detect conflict
-                        if (key.getSourceAddress() == null)
+                        if (key.sourceAddress() == null)
                             throw new IllegalStateException("Already have source-specific membership");
-                        if (source.equals(key.getSourceAddress()))
+                        if (source.equals(key.sourceAddress()))
                             return key;
                     }
                 }
@@ -81,7 +81,7 @@
      * Add membership to the registry, returning a new membership key.
      */
     void add(MembershipKeyImpl key) {
-        InetAddress group = key.getGroup();
+        InetAddress group = key.group();
         List<MembershipKeyImpl> keys;
         if (groups == null) {
             groups = new HashMap<InetAddress,List<MembershipKeyImpl>>();
@@ -100,7 +100,7 @@
      * Remove a key from the registry
      */
     void remove(MembershipKeyImpl key) {
-        InetAddress group = key.getGroup();
+        InetAddress group = key.group();
         List<MembershipKeyImpl> keys = groups.get(group);
         if (keys != null) {
             Iterator<MembershipKeyImpl> i = keys.iterator();
--- a/src/share/classes/sun/nio/ch/SimpleAsynchronousDatagramChannelImpl.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/sun/nio/ch/SimpleAsynchronousDatagramChannelImpl.java	Tue Oct 21 18:25:15 2008 +0100
@@ -44,17 +44,18 @@
 class SimpleAsynchronousDatagramChannelImpl
     extends AsynchronousDatagramChannel implements MulticastChannel, Groupable
 {
+    private final DatagramChannel dc;
     private final AsynchronousChannelGroupImpl group;
-    private final DatagramChannel dc;
 
     SimpleAsynchronousDatagramChannelImpl(ProtocolFamily family,
                                           AsynchronousChannelGroupImpl group)
         throws IOException
     {
         super(group.provider());
-        this.group = group;
         this.dc = (family == null) ?
             DatagramChannel.open() : DatagramChannel.open(family);
+        this.group = group;
+        // FIXME: register this channel with group
     }
 
     @Override
@@ -69,6 +70,7 @@
 
     @Override
     public void close() throws IOException {
+        // FIXME - unregister this channel from the group
         dc.close();
     }
 
@@ -106,23 +108,23 @@
         }
 
         @Override
-        public MulticastChannel getChannel() {
+        public MulticastChannel channel() {
             return channel;
         }
 
         @Override
-        public InetAddress getGroup() {
-            return key.getGroup();
+        public InetAddress group() {
+            return key.group();
         }
 
         @Override
-        public NetworkInterface getNetworkInterface() {
-            return key.getNetworkInterface();
+        public NetworkInterface networkInterface() {
+            return key.networkInterface();
         }
 
         @Override
-        public InetAddress getSourceAddress() {
-            return key.getSourceAddress();
+        public InetAddress sourceAddress() {
+            return key.sourceAddress();
         }
 
         @Override
@@ -237,8 +239,8 @@
                                              A attachment,
                                              final CompletionHandler<SocketAddress,? super A> handler)
     {
-        if (dst == null)
-            throw new NullPointerException();
+        if (dst.isReadOnly())
+            throw new IllegalArgumentException("Read-only buffer");
         if (timeout < 0L)
             throw new IllegalArgumentException("Negative timeout");
         if (unit == null)
@@ -306,8 +308,8 @@
                                     A attachment,
                                     final CompletionHandler<Integer,? super A> handler)
     {
-        if (dst == null)
-            throw new NullPointerException();
+        if (dst.isReadOnly())
+            throw new IllegalArgumentException("Read-only buffer");
         if (timeout < 0L)
             throw new IllegalArgumentException("Negative timeout");
         if (unit == null)
--- a/src/share/classes/sun/nio/fs/AbstractWatchKey.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/classes/sun/nio/fs/AbstractWatchKey.java	Tue Oct 21 18:25:15 2008 +0100
@@ -93,12 +93,12 @@
 
                 // repeated event
                 WatchEvent<?> prev = events.get(size-1);
-                if (kind == prev.getKind()) {
+                if (kind == prev.kind()) {
                     boolean isRepeat;
                     if (context == null) {
-                        isRepeat = (prev.getContext() == null);
+                        isRepeat = (prev.context() == null);
                     } else {
-                        isRepeat = context.equals(prev.getContext());
+                        isRepeat = context.equals(prev.context());
                     }
                     if (isRepeat) {
                         ((Event)prev).increment();
@@ -154,17 +154,17 @@
         }
 
         @Override
-        public WatchEvent.Kind<T> getKind() {
+        public WatchEvent.Kind<T> kind() {
             return kind;
         }
 
         @Override
-        public T getContext() {
+        public T context() {
             return context;
         }
 
         @Override
-        public int getCount() {
+        public int count() {
             return count;
         }
 
--- a/src/share/sample/nio/file/WatchDir.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/src/share/sample/nio/file/WatchDir.java	Tue Oct 21 18:25:15 2008 +0100
@@ -124,7 +124,7 @@
             }
 
             for (WatchEvent<?> event: key.pollEvents()) {
-                WatchEvent.Kind kind = event.getKind();
+                WatchEvent.Kind kind = event.kind();
 
                 // TBD - provide example of how OVERFLOW event is handled
                 if (kind == OVERFLOW) {
@@ -132,11 +132,11 @@
                 }
 
                 // Context for directory entry event is the file name of entry
-                Path name = ((WatchEvent<Path>)event).getContext();
+                Path name = ((WatchEvent<Path>)event).context();
                 Path child = dir.resolve(name);
 
                 // print out event
-                System.out.format("%s: %s\n", event.getKind().name(), child);
+                System.out.format("%s: %s\n", event.kind().name(), child);
 
                 // if directory is created, and watching recursively, then
                 // register it and its sub-directories
--- a/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java	Tue Oct 21 18:25:15 2008 +0100
@@ -357,7 +357,7 @@
         MembershipKey key = ((MulticastChannel)ch).join(group, interf);
 
         // check key
-        if (key.getChannel() != ch)
+        if (key.channel() != ch)
             throw new RuntimeException("Not the expected channel");
 
         // send message to group
--- a/test/java/nio/channels/DatagramChannel/BasicMulticastTests.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/test/java/nio/channels/DatagramChannel/BasicMulticastTests.java	Tue Oct 21 18:25:15 2008 +0100
@@ -64,11 +64,11 @@
         // check key
         if (!key.isValid())
             throw new RuntimeException("key is not valid");
-        if (!key.getGroup().equals(group))
+        if (!key.group().equals(group))
             throw new RuntimeException("group is incorrect");
-        if (!key.getNetworkInterface().equals(nif))
+        if (!key.networkInterface().equals(nif))
             throw new RuntimeException("network interface is incorrect");
-        if (key.getSourceAddress() != null)
+        if (key.sourceAddress() != null)
             throw new RuntimeException("key is source specific");
 
         // drop membership
@@ -86,11 +86,11 @@
             }
             if (!key.isValid())
                 throw new RuntimeException("key is not valid");
-            if (!key.getGroup().equals(group))
+            if (!key.group().equals(group))
                 throw new RuntimeException("group is incorrect");
-            if (!key.getNetworkInterface().equals(nif))
+            if (!key.networkInterface().equals(nif))
                 throw new RuntimeException("network interface is incorrect");
-            if (!key.getSourceAddress().equals(source))
+            if (!key.sourceAddress().equals(source))
                 throw new RuntimeException("key's source address incorrect");
 
             // drop membership
--- a/test/java/nio/file/WatchService/Basic.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/test/java/nio/file/WatchService/Basic.java	Tue Oct 21 18:25:15 2008 +0100
@@ -59,15 +59,15 @@
     }
 
     static void checkExpectedEvent(Iterable<WatchEvent<?>> events,
-                                   WatchEvent.Kind<?> expectedType,
+                                   WatchEvent.Kind<?> expectedKind,
                                    Object expectedContext)
     {
         WatchEvent<?> event = events.iterator().next();
         System.out.format("got event: type=%s, count=%d, context=%s\n",
-            event.getKind(), event.getCount(), event.getContext());
-        if (event.getKind() != expectedType)
+            event.kind(), event.count(), event.context());
+        if (event.kind() != expectedKind)
             throw new RuntimeException("unexpected event");
-        if (!expectedContext.equals(event.getContext()))
+        if (!expectedContext.equals(event.context()))
             throw new RuntimeException("unexpected context");
     }
 
@@ -340,11 +340,6 @@
             } catch (NullPointerException x) {
             }
             try {
-                dir.register(watcher, null);
-                throw new RuntimeException("NullPointerException not thrown");
-            } catch (NullPointerException x) {
-            }
-            try {
                 dir.register(watcher, new WatchEvent.Kind<?>[]{ null });
                 throw new RuntimeException("NullPointerException not thrown");
             } catch (NullPointerException x) {
--- a/test/java/nio/file/WatchService/FileTreeModifier.java	Tue Oct 21 17:54:26 2008 +0100
+++ b/test/java/nio/file/WatchService/FileTreeModifier.java	Tue Oct 21 18:25:15 2008 +0100
@@ -50,10 +50,10 @@
         }
         WatchEvent<?> event = key.pollEvents().iterator().next();
         System.out.format("Event: type=%s, count=%d, context=%s\n",
-            event.getKind(), event.getCount(), event.getContext());
-        if (event.getKind() != expectedType)
+            event.kind(), event.count(), event.context());
+        if (event.kind() != expectedType)
             throw new RuntimeException("unexpected event");
-        if (!expectedContext.equals(event.getContext()))
+        if (!expectedContext.equals(event.context()))
             throw new RuntimeException("unexpected context");
     }