/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.harmony.nio.internal;

import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.AbstractSelectionKey;

/*
 * Default implementation of SelectionKey
 */
final class SelectionKeyImpl extends AbstractSelectionKey {

    static int stHash;

    private AbstractSelectableChannel channel;

    private int interestOps;

    private int readyOps;

    private SelectorImpl selector;

    private int index;

    private int hashCode;

    public int hashCode() {
        return hashCode;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final SelectionKeyImpl other = (SelectionKeyImpl) obj;
        return hashCode == other.hashCode;
    }

    public SelectionKeyImpl(AbstractSelectableChannel channel, int operations,
            Object attachment, SelectorImpl selector) {
        super();
        this.channel = channel;
        interestOps = operations;
        this.selector = selector;
        this.hashCode = stHash++;
        attach(attachment);
    }

    public SelectableChannel channel() {
        return channel;
    }

    public int interestOps() {
        checkValid();
        synchronized (selector.keysLock) {
            return interestOps;
        }
    }

    public SelectionKey interestOps(int operations) {
        checkValid();
        if ((operations & ~(channel().validOps())) != 0) {
            throw new IllegalArgumentException();
        }
        synchronized (selector.keysLock) {
            interestOps = operations;
            selector.modKey(this);
        }
        return this;
    }

    public int readyOps() {
        checkValid();
        return readyOps;
    }

    public Selector selector() {
        return selector;
    }

    /*
     * package private method for setting the ready operation by selector
     */
    void setReadyOps(int readyOps) {
        this.readyOps = readyOps;
    }

    int getIndex() {
        return index;
    }

    void setIndex(int index) {
        this.index = index;
    }

    private void checkValid() {
        if (!isValid()) {
            throw new CancelledKeyException();
        }
    }
}

@@ -1414,417 +1414,8 @@
 x;%0A%0A
-    private int hashCode;%0A%0A    public int hashCode() %7B%0A        return hashCode;%0A    %7D%0A%0A    public boolean equals(Object obj) %7B%0A        if (this == obj)%0A            return true;%0A        if (obj == null)%0A            return false;%0A        if (getClass() != obj.getClass())%0A            return false;%0A        final SelectionKeyImpl other = (SelectionKeyImpl) obj;%0A        return hashCode == other.hashCode;%0A    %7D%0A%0A
     
@@ -1666,42 +1666,8 @@
 or;%0A
-        this.hashCode = stHash++;%0A
     

Apply patch for HARMONY-6312: Concurrency problems in NIO