1 20 package org.apache.mina.common; 21 22 import java.nio.channels.SelectionKey ; 23 24 31 public class TrafficMask { 32 35 public static final TrafficMask NONE = new TrafficMask(0, "none"); 36 37 40 public static final TrafficMask READ = new TrafficMask( 41 SelectionKey.OP_READ, "read"); 42 43 46 public static final TrafficMask WRITE = new TrafficMask( 47 SelectionKey.OP_WRITE, "write"); 48 49 52 public static final TrafficMask ALL = new TrafficMask(SelectionKey.OP_READ 53 | SelectionKey.OP_WRITE, "all"); 54 55 60 public static TrafficMask getInstance(int interestOps) { 61 boolean read = (interestOps & SelectionKey.OP_READ) != 0; 62 boolean write = (interestOps & SelectionKey.OP_WRITE) != 0; 63 if (read) { 64 if (write) { 65 return ALL; 66 } else { 67 return READ; 68 } 69 } else if (write) { 70 return WRITE; 71 } else { 72 return NONE; 73 } 74 } 75 76 private final int interestOps; 77 78 private final String name; 79 80 private TrafficMask(int interestOps, String name) { 81 this.interestOps = interestOps; 82 this.name = name; 83 } 84 85 88 public String getName() { 89 return name; 90 } 91 92 95 public boolean isReadable() { 96 return (interestOps & SelectionKey.OP_READ) != 0; 97 } 98 99 102 public boolean isWritable() { 103 return (interestOps & SelectionKey.OP_WRITE) != 0; 104 } 105 106 109 public int getInterestOps() { 110 return interestOps; 111 } 112 113 117 public TrafficMask and(TrafficMask mask) { 118 return getInstance(interestOps & mask.interestOps); 119 } 120 121 125 public TrafficMask or(TrafficMask mask) { 126 return getInstance(interestOps | mask.interestOps); 127 } 128 129 132 public TrafficMask not() { 133 return getInstance(~interestOps); 134 } 135 136 140 public TrafficMask xor(TrafficMask mask) { 141 return getInstance(interestOps ^ mask.interestOps); 142 } 143 144 public String toString() { 145 return name; 146 } 147 } 148 | Popular Tags |