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 org.apache.harmony.nio.internal;
018:
019: import java.nio.channels.CancelledKeyException;
020: import java.nio.channels.SelectableChannel;
021: import java.nio.channels.SelectionKey;
022: import java.nio.channels.Selector;
023: import java.nio.channels.spi.AbstractSelectableChannel;
024: import java.nio.channels.spi.AbstractSelectionKey;
025:
026: /*
027: * Default implementation of SelectionKey
028: */
029: final class SelectionKeyImpl extends AbstractSelectionKey {
030:
031: static int stHash;
032:
033: private AbstractSelectableChannel channel;
034:
035: private int interestOps;
036:
037: private int readyOps;
038:
039: private SelectorImpl selector;
040:
041: private int index;
042:
043: private int hashCode;
044:
045: public int hashCode() {
046: return hashCode;
047: }
048:
049: public boolean equals(Object obj) {
050: if (this == obj)
051: return true;
052: if (obj == null)
053: return false;
054: if (getClass() != obj.getClass())
055: return false;
056: final SelectionKeyImpl other = (SelectionKeyImpl) obj;
057: return hashCode == other.hashCode;
058: }
059:
060: public SelectionKeyImpl(AbstractSelectableChannel channel,
061: int operations, Object attachment, SelectorImpl selector) {
062: super ();
063: this .channel = channel;
064: interestOps = operations;
065: this .selector = selector;
066: this .hashCode = stHash++;
067: attach(attachment);
068: }
069:
070: public SelectableChannel channel() {
071: return channel;
072: }
073:
074: public int interestOps() {
075: checkValid();
076: synchronized (selector.keysLock) {
077: return interestOps;
078: }
079: }
080:
081: public SelectionKey interestOps(int operations) {
082: checkValid();
083: if ((operations & ~(channel().validOps())) != 0) {
084: throw new IllegalArgumentException();
085: }
086: synchronized (selector.keysLock) {
087: interestOps = operations;
088: selector.modKey(this );
089: }
090: return this ;
091: }
092:
093: public int readyOps() {
094: checkValid();
095: return readyOps;
096: }
097:
098: public Selector selector() {
099: return selector;
100: }
101:
102: /*
103: * package private method for setting the ready operation by selector
104: */
105: void setReadyOps(int readyOps) {
106: this .readyOps = readyOps;
107: }
108:
109: int getIndex() {
110: return index;
111: }
112:
113: void setIndex(int index) {
114: this .index = index;
115: }
116:
117: private void checkValid() {
118: if (!isValid()) {
119: throw new CancelledKeyException();
120: }
121: }
122: }
|