SelectionKey.java 4.51 KB
Newer Older
1
/* SelectionKey.java --
Michael Koch committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   Copyright (C) 2002 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.nio.channels;

40

41 42 43 44
/**
 * @author Michael Koch
 * @since 1.4
 */
Michael Koch committed
45 46
public abstract class SelectionKey
{
47
  public static final int OP_ACCEPT = 16;
48
  public static final int OP_CONNECT = 8;
49 50
  public static final int OP_READ = 1;
  public static final int OP_WRITE = 4;
Michael Koch committed
51
  Object attached;
52

53 54 55
  /**
   * Initializes the selection key.
   */
56
  protected SelectionKey()
Michael Koch committed
57 58 59
  {
  }

60 61 62
  /**
   * Attaches obj to the key and returns the old attached object.
   */
63
  public final Object attach(Object obj)
Michael Koch committed
64 65 66 67 68
  {
    Object old = attached;
    attached = obj;
    return old;
  }
69

70 71 72
  /**
   * Returns the object attached to the key.
   */
73
  public final Object attachment()
Michael Koch committed
74 75
  {
    return attached;
76
  }
Michael Koch committed
77 78

  /**
79 80
   * Tests if the channel attached to this key is ready to accept
   * a new socket connection.
81
   *
82
   * @exception CancelledKeyException If this key has been cancelled
Michael Koch committed
83
   */
84 85 86
  public final boolean isAcceptable()
  {
    return (readyOps() & OP_ACCEPT) != 0;
Michael Koch committed
87 88 89
  }

  /**
90 91
   * Tests whether this key's channel has either finished,
   * or failed to finish, its socket-connection operation.
92
   *
93
   * @exception CancelledKeyException If this key has been cancelled
Michael Koch committed
94
   */
95
  public final boolean isConnectable()
Michael Koch committed
96
  {
97 98 99
    return (readyOps() & OP_CONNECT) != 0;
  }

Michael Koch committed
100
  /**
101
   * Tests if the channel attached to the key is readable.
102
   *
103
   * @exception CancelledKeyException If this key has been cancelled
Michael Koch committed
104
   */
105
  public final boolean isReadable()
Michael Koch committed
106
  {
107
    return (readyOps() & OP_READ) != 0;
Michael Koch committed
108
  }
109

Michael Koch committed
110
  /**
111 112 113
   * Tests if the channel attached to the key is writable.
   *
   * @exception CancelledKeyException If this key has been cancelled
Michael Koch committed
114
   */
115
  public final boolean isWritable()
Michael Koch committed
116
  {
117
    return (readyOps() & OP_WRITE) != 0;
Michael Koch committed
118 119 120
  }

  /**
121 122
   * Requests that the registration of this key's channel with
   * its selector be cancelled.
Michael Koch committed
123
   */
124 125
  public abstract void cancel();

126 127 128
  /**
   * return the channel attached to the key.
   */
129 130
  public abstract SelectableChannel channel();

Michael Koch committed
131
  /**
132
   * Returns the key's interest set.
133
   *
134
   * @exception CancelledKeyException If this key has been cancelled
Michael Koch committed
135
   */
136 137
  public abstract int interestOps();

138 139
  /**
   * Sets this key's interest set to the given value.
140
   *
141 142 143 144 145
   * @exception CancelledKeyException If this key has been cancelled
   * @exception IllegalArgumentException If a bit in the set does not
   * correspond to an operation that is supported by this key's channel,
   * that is, if set & ~(channel().validOps()) != 0
   */
146 147
  public abstract SelectionKey interestOps(int ops);

Michael Koch committed
148
  /**
149
   * Tells whether or not this key is valid.
Michael Koch committed
150
   */
151 152
  public abstract boolean isValid();

153 154
  /**
   * Retrieves this key's ready-operation set.
155
   *
156 157
   * @exception CancelledKeyException If this key has been cancelled
   */
158 159
  public abstract int readyOps();

160 161 162
  /**
   * Returns the selector for which this key was created.
   */
163
  public abstract Selector selector();
Michael Koch committed
164
}