1 /*
2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.nio.ch;
27 
28 import java.nio.channels.*;
29 import java.nio.channels.spi.*;
30 
31 
32 /**
33  * An implementation of SelectionKey for Solaris.
34  */
35 
36 public class SelectionKeyImpl
37     extends AbstractSelectionKey
38 {
39 
40     final SelChImpl channel;                            // package-private
41     public final SelectorImpl selector;
42 
43     // Index for a pollfd array in Selector that this key is registered with
44     private int index;
45 
46     private volatile int interestOps;
47     private int readyOps;
48 
SelectionKeyImpl(SelChImpl ch, SelectorImpl sel)49     SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) {
50         channel = ch;
51         selector = sel;
52     }
53 
channel()54     public SelectableChannel channel() {
55         return (SelectableChannel)channel;
56     }
57 
selector()58     public Selector selector() {
59         return selector;
60     }
61 
getIndex()62     int getIndex() {                                    // package-private
63         return index;
64     }
65 
setIndex(int i)66     void setIndex(int i) {                              // package-private
67         index = i;
68     }
69 
ensureValid()70     private void ensureValid() {
71         if (!isValid())
72             throw new CancelledKeyException();
73     }
74 
interestOps()75     public int interestOps() {
76         ensureValid();
77         return interestOps;
78     }
79 
interestOps(int ops)80     public SelectionKey interestOps(int ops) {
81         ensureValid();
82         return nioInterestOps(ops);
83     }
84 
readyOps()85     public int readyOps() {
86         ensureValid();
87         return readyOps;
88     }
89 
90     // The nio versions of these operations do not care if a key
91     // has been invalidated. They are for internal use by nio code.
92 
nioReadyOps(int ops)93     public void nioReadyOps(int ops) {
94         readyOps = ops;
95     }
96 
nioReadyOps()97     public int nioReadyOps() {
98         return readyOps;
99     }
100 
nioInterestOps(int ops)101     public SelectionKey nioInterestOps(int ops) {
102         if ((ops & ~channel().validOps()) != 0)
103             throw new IllegalArgumentException();
104         channel.translateAndSetInterestOps(ops, this);
105         interestOps = ops;
106         return this;
107     }
108 
nioInterestOps()109     public int nioInterestOps() {
110         return interestOps;
111     }
112 
113 }
114