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 EpollSelectionKeyImpl extends AbstractSelectionKey {
030:
031: private AbstractSelectableChannel channel;
032:
033: private int interestOps;
034:
035: private int readyOps;
036:
037: private EpollSelectorImpl selector;
038:
039: private int index;
040:
041: static int stHash;
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 EpollSelectionKeyImpl other = (EpollSelectionKeyImpl) obj;
057: if (hashCode != other.hashCode)
058: return false;
059: return true;
060: }
061:
062: public EpollSelectionKeyImpl(AbstractSelectableChannel channel,
063: int operations, Object attachment,
064: EpollSelectorImpl selector) {
065: super ();
066: this .channel = channel;
067: interestOps = operations;
068: this .selector = selector;
069: this .hashCode = stHash++;
070: attach(attachment);
071: }
072:
073: public SelectableChannel channel() {
074: return channel;
075: }
076:
077: public int interestOps() {
078: checkValid();
079: synchronized (selector.keysLock) {
080: return interestOps;
081: }
082: }
083:
084: public SelectionKey interestOps(int operations) {
085: checkValid();
086: if ((operations & ~(channel().validOps())) != 0) {
087: throw new IllegalArgumentException();
088: }
089: synchronized (selector.keysLock) {
090: interestOps = operations;
091: selector.modKey(this );
092: }
093: return this ;
094: }
095:
096: public int readyOps() {
097: checkValid();
098: return readyOps;
099: }
100:
101: public Selector selector() {
102: return selector;
103: }
104:
105: /*
106: * package private method for setting the ready operation by selector
107: */
108: void setReadyOps(int readyOps) {
109: this .readyOps = readyOps;
110: }
111:
112: int getIndex() {
113: return index;
114: }
115:
116: void setIndex(int index) {
117: this .index = index;
118: }
119:
120: private void checkValid() {
121: if (!isValid()) {
122: throw new CancelledKeyException();
123: }
124: }
125:
126: }
|