1. /*
  2. * @(#)AbstractSelectionKey.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels.spi;
  8. import java.nio.channels.*;
  9. /**
  10. * Base implementation class for selection keys.
  11. *
  12. * <p> This class tracks the validity of the key and implements cancellation.
  13. *
  14. * @author Mark Reinhold
  15. * @author JSR-51 Expert Group
  16. * @version 1.9, 03/01/23
  17. * @since 1.4
  18. */
  19. public abstract class AbstractSelectionKey
  20. extends SelectionKey
  21. {
  22. /**
  23. * Initializes a new instance of this class. </p>
  24. */
  25. protected AbstractSelectionKey() { }
  26. private volatile boolean valid = true;
  27. public final boolean isValid() {
  28. return valid;
  29. }
  30. /**
  31. * Cancels this key.
  32. *
  33. * <p> If this key has not yet been cancelled then it is added to its
  34. * selector's cancelled-key set while synchronized on that set. </p>
  35. */
  36. public final void cancel() {
  37. if (valid) {
  38. valid = false;
  39. ((AbstractSelector)selector()).cancel(this);
  40. }
  41. }
  42. }