001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package java.nio.channels;
018:
019: import java.nio.channels.Selector;
020:
021: /**
022: * A key that representing the relationship of a channel and the selector.
023: *
024: */
025: public abstract class SelectionKey {
026:
027: /**
028: * Interesting operation mask bit for socket-accept operations.
029: */
030: public static final int OP_ACCEPT = 16;
031:
032: /**
033: * Interesting operation mask bit for socket-connect operations.
034: */
035: public static final int OP_CONNECT = 8;
036:
037: /**
038: * Interesting operation mask bit for read operations.
039: */
040: public static final int OP_READ = 1;
041:
042: /**
043: * Interesting operation mask bit for write operations.
044: */
045: public static final int OP_WRITE = 4;
046:
047: private volatile Object attachment = null;
048:
049: /**
050: * The constructor.
051: *
052: */
053: protected SelectionKey() {
054: super ();
055: }
056:
057: /**
058: * Attaches an object to the key.
059: *
060: * @param anObject
061: * the object to attach
062: * @return the last attached object
063: */
064: public final Object attach(Object anObject) {
065: Object oldAttachment = attachment;
066: attachment = anObject;
067: return oldAttachment;
068: }
069:
070: /**
071: * Gets the attached object.
072: *
073: * @return the attached object or null if no object has been attached
074: */
075: public final Object attachment() {
076: return attachment;
077: }
078:
079: /**
080: * Cancels this key.
081: *
082: */
083: public abstract void cancel();
084:
085: /**
086: * Gets the channel of this key.
087: *
088: * @return the channel of this key
089: */
090: public abstract SelectableChannel channel();
091:
092: /**
093: * Gets the interesting operation of this key.
094: *
095: * @return the interesting operation of this key
096: * @throws CancelledKeyException
097: * If the key has been cancelled already
098: */
099: public abstract int interestOps();
100:
101: /**
102: * Sets the interesting operation for this key.
103: *
104: * @param operations
105: * the interesting operation to set
106: * @return this key
107: * @throws IllegalArgumentException
108: * if the given operation is not in the key's interesting
109: * operation set
110: * @throws CancelledKeyException
111: * If the key has been cancelled already
112: */
113: public abstract SelectionKey interestOps(int operations);
114:
115: /**
116: * Tells whether the channel of this key is interested in accept operation
117: * and ready for acceptation.
118: *
119: * @return true if the channel is interested in accept operation and ready
120: * for acceptation
121: * @throws CancelledKeyException
122: * If the key has been cancelled already
123: */
124: public final boolean isAcceptable() {
125: return (readyOps() & OP_ACCEPT) == OP_ACCEPT;
126: }
127:
128: /**
129: * Tells whether the channel of this key is interested in connect operation
130: * and ready for connection.
131: *
132: * @return true if the channel is interested in connect operation and ready
133: * for connection
134: * @throws CancelledKeyException
135: * If the key has been cancelled already
136: */
137: public final boolean isConnectable() {
138: return (readyOps() & OP_CONNECT) == OP_CONNECT;
139: }
140:
141: /**
142: * Tells whether the channel of this key is interested in read operation and
143: * ready for reading.
144: *
145: * @return true if the channel is interested in read operation and ready for
146: * reading
147: * @throws CancelledKeyException
148: * If the key has been cancelled already
149: */
150: public final boolean isReadable() {
151: return (readyOps() & OP_READ) == OP_READ;
152: }
153:
154: /**
155: * Tells whether the key is valid.
156: *
157: * @return true if the key has not been cancelled
158: */
159: public abstract boolean isValid();
160:
161: /**
162: * Tells whether the channel of this key is interested in write operation
163: * and ready for writing.
164: *
165: * @return true if the channel is interested in write operation and ready
166: * for writing
167: * @throws CancelledKeyException
168: * If the key has been cancelled already
169: */
170: public final boolean isWritable() {
171: return (readyOps() & OP_WRITE) == OP_WRITE;
172: }
173:
174: /**
175: * Gets the ready operation.
176: *
177: * @return the ready operation
178: * @throws CancelledKeyException
179: * If the key has been cancelled already
180: */
181: public abstract int readyOps();
182:
183: /**
184: * Gets the related selector.
185: *
186: * @return the related selector
187: */
188: public abstract Selector selector();
189: }
|