001    /* SelectionKey.java --
002       Copyright (C) 2002, 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package java.nio.channels;
039    
040    
041    /**
042     * @author Michael Koch
043     * @since 1.4
044     */
045    public abstract class SelectionKey
046    {
047      public static final int OP_ACCEPT = 16;
048      public static final int OP_CONNECT = 8;
049      public static final int OP_READ = 1;
050      public static final int OP_WRITE = 4;
051      Object attached;
052    
053      /**
054       * Initializes the selection key.
055       */
056      protected SelectionKey()
057      {
058      }
059    
060      /**
061       * Attaches obj to the key and returns the old attached object.
062       */
063      public final synchronized Object attach(Object obj)
064      {
065        Object old = attached;
066        attached = obj;
067        return old;
068      }
069    
070      /**
071       * Returns the object attached to the key.
072       */
073      public final synchronized Object attachment()
074      {
075        return attached;
076      }
077    
078      /**
079       * Tests if the channel attached to this key is ready to accept
080       * a new socket connection.
081       *
082       * @exception CancelledKeyException If this key has been cancelled
083       */
084      public final boolean isAcceptable()
085      {
086        return (readyOps() & OP_ACCEPT) != 0;
087      }
088    
089      /**
090       * Tests whether this key's channel has either finished,
091       * or failed to finish, its socket-connection operation.
092       *
093       * @exception CancelledKeyException If this key has been cancelled
094       */
095      public final boolean isConnectable()
096      {
097        return (readyOps() & OP_CONNECT) != 0;
098      }
099    
100      /**
101       * Tests if the channel attached to the key is readable.
102       *
103       * @exception CancelledKeyException If this key has been cancelled
104       */
105      public final boolean isReadable()
106      {
107        return (readyOps() & OP_READ) != 0;
108      }
109    
110      /**
111       * Tests if the channel attached to the key is writable.
112       *
113       * @exception CancelledKeyException If this key has been cancelled
114       */
115      public final boolean isWritable()
116      {
117        return (readyOps() & OP_WRITE) != 0;
118      }
119    
120      /**
121       * Requests that the registration of this key's channel with
122       * its selector be cancelled.
123       */
124      public abstract void cancel();
125    
126      /**
127       * return the channel attached to the key.
128       */
129      public abstract SelectableChannel channel();
130    
131      /**
132       * Returns the key's interest set.
133       *
134       * @exception CancelledKeyException If this key has been cancelled
135       */
136      public abstract int interestOps();
137    
138      /**
139       * Sets this key's interest set to the given value.
140       *
141       * @exception CancelledKeyException If this key has been cancelled
142       * @exception IllegalArgumentException If a bit in the set does not
143       * correspond to an operation that is supported by this key's channel,
144       * that is, if set & ~(channel().validOps()) != 0
145       */
146      public abstract SelectionKey interestOps(int ops);
147    
148      /**
149       * Tells whether or not this key is valid.
150       */
151      public abstract boolean isValid();
152    
153      /**
154       * Retrieves this key's ready-operation set.
155       *
156       * @exception CancelledKeyException If this key has been cancelled
157       */
158      public abstract int readyOps();
159    
160      /**
161       * Returns the selector for which this key was created.
162       */
163      public abstract Selector selector();
164    }