1. /*
  2. * @(#)AbstractSelectionKey.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 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.11, 03/12/19
  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. void invalidate() { // package-private
  31. valid = false;
  32. }
  33. /**
  34. * Cancels this key.
  35. *
  36. * <p> If this key has not yet been cancelled then it is added to its
  37. * selector's cancelled-key set while synchronized on that set. </p>
  38. */
  39. public final void cancel() {
  40. if (valid) {
  41. valid = false;
  42. ((AbstractSelector)selector()).cancel(this);
  43. }
  44. }
  45. }