1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.mina.common; 21 22 import java.nio.channels.SelectionKey; 23 24 /** 25 * A type-safe mask that is used to control the traffic of {@link IoSession} 26 * with {@link IoSession#setTrafficMask(TrafficMask)}. 27 * 28 * @author The Apache Directory Project (mina-dev@directory.apache.org) 29 * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ 30 */ 31 public class TrafficMask { 32 /** 33 * This mask suspends both reads and writes. 34 */ 35 public static final TrafficMask NONE = new TrafficMask(0, "none"); 36 37 /** 38 * This mask suspends writes, and resumes reads if reads were suspended. 39 */ 40 public static final TrafficMask READ = new TrafficMask( 41 SelectionKey.OP_READ, "read"); 42 43 /** 44 * This mask suspends reads, and resumes writes if writes were suspended. 45 */ 46 public static final TrafficMask WRITE = new TrafficMask( 47 SelectionKey.OP_WRITE, "write"); 48 49 /** 50 * This mask resumes both reads and writes if any of them were suspended. 51 */ 52 public static final TrafficMask ALL = new TrafficMask(SelectionKey.OP_READ 53 | SelectionKey.OP_WRITE, "all"); 54 55 /** 56 * Returns an appropriate {@link TrafficMask} instance from the 57 * specified <tt>interestOps</tt>. 58 * @see SelectionKey 59 */ 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 /** 86 * Returns the name of this mask. 87 */ 88 public String getName() { 89 return name; 90 } 91 92 /** 93 * Returns <tt>true</tt> if this mask allows a read operation. 94 */ 95 public boolean isReadable() { 96 return (interestOps & SelectionKey.OP_READ) != 0; 97 } 98 99 /** 100 * Returns <tt>true</tt> if this mask allows a write operation. 101 */ 102 public boolean isWritable() { 103 return (interestOps & SelectionKey.OP_WRITE) != 0; 104 } 105 106 /** 107 * Returns an interestOps of {@link SelectionKey} for this mask. 108 */ 109 public int getInterestOps() { 110 return interestOps; 111 } 112 113 /** 114 * Peforms an <tt>AND</tt> operation on this mask with the specified 115 * <tt>mask</tt> and returns the result. 116 */ 117 public TrafficMask and(TrafficMask mask) { 118 return getInstance(interestOps & mask.interestOps); 119 } 120 121 /** 122 * Peforms an <tt>OR</tt> operation on this mask with the specified 123 * <tt>mask</tt> and returns the result. 124 */ 125 public TrafficMask or(TrafficMask mask) { 126 return getInstance(interestOps | mask.interestOps); 127 } 128 129 /** 130 * Returns a negated mask of this one. 131 */ 132 public TrafficMask not() { 133 return getInstance(~interestOps); 134 } 135 136 /** 137 * Peforms an <tt>XOR</tt> operation on this mask with the specified 138 * <tt>mask</tt> and returns the result. 139 */ 140 public TrafficMask xor(TrafficMask mask) { 141 return getInstance(interestOps ^ mask.interestOps); 142 } 143 144 public String toString() { 145 return name; 146 } 147 }