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.core.session; 21 22 import java.nio.channels.SelectionKey; 23 24 25 /** 26 * A type-safe mask that is used to control the traffic of {@link IoSession} 27 * with {@link IoSession#setTrafficMask(TrafficMask)}. 28 * 29 * @author The Apache MINA Project (dev@mina.apache.org) 30 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 31 */ 32 public class TrafficMask { 33 /** 34 * This mask suspends both reads and writes. 35 */ 36 public static final TrafficMask NONE = new TrafficMask(0, "none"); 37 38 /** 39 * This mask suspends writes, and resumes reads if reads were suspended. 40 */ 41 public static final TrafficMask READ = new TrafficMask( 42 SelectionKey.OP_READ, "read"); 43 44 /** 45 * This mask suspends reads, and resumes writes if writes were suspended. 46 */ 47 public static final TrafficMask WRITE = new TrafficMask( 48 SelectionKey.OP_WRITE, "write"); 49 50 /** 51 * This mask resumes both reads and writes if any of them were suspended. 52 */ 53 public static final TrafficMask ALL = new TrafficMask(SelectionKey.OP_READ 54 | SelectionKey.OP_WRITE, "all"); 55 56 /** 57 * Returns an appropriate {@link TrafficMask} instance from the 58 * specified <tt>interestOps</tt>. 59 * @see SelectionKey 60 */ 61 public static TrafficMask getInstance(int interestOps) { 62 boolean read = (interestOps & SelectionKey.OP_READ) != 0; 63 boolean write = (interestOps & SelectionKey.OP_WRITE) != 0; 64 if (read) { 65 if (write) { 66 return ALL; 67 } else { 68 return READ; 69 } 70 } else if (write) { 71 return WRITE; 72 } else { 73 return NONE; 74 } 75 } 76 77 private final int interestOps; 78 79 private final String name; 80 81 private TrafficMask(int interestOps, String name) { 82 this.interestOps = interestOps; 83 this.name = name; 84 } 85 86 /** 87 * Returns the name of this mask. 88 */ 89 public String getName() { 90 return name; 91 } 92 93 /** 94 * Returns <tt>true</tt> if this mask allows a read operation. 95 */ 96 public boolean isReadable() { 97 return (interestOps & SelectionKey.OP_READ) != 0; 98 } 99 100 /** 101 * Returns <tt>true</tt> if this mask allows a write operation. 102 */ 103 public boolean isWritable() { 104 return (interestOps & SelectionKey.OP_WRITE) != 0; 105 } 106 107 /** 108 * Returns an interestOps of {@link SelectionKey} for this mask. 109 */ 110 public int getInterestOps() { 111 return interestOps; 112 } 113 114 /** 115 * Peforms an <tt>AND</tt> operation on this mask with the specified 116 * <tt>mask</tt> and returns the result. 117 */ 118 public TrafficMask and(TrafficMask mask) { 119 return getInstance(interestOps & mask.interestOps); 120 } 121 122 /** 123 * Peforms an <tt>OR</tt> operation on this mask with the specified 124 * <tt>mask</tt> and returns the result. 125 */ 126 public TrafficMask or(TrafficMask mask) { 127 return getInstance(interestOps | mask.interestOps); 128 } 129 130 /** 131 * Returns a negated mask of this one. 132 */ 133 public TrafficMask not() { 134 return getInstance(~interestOps); 135 } 136 137 /** 138 * Peforms an <tt>XOR</tt> operation on this mask with the specified 139 * <tt>mask</tt> and returns the result. 140 */ 141 public TrafficMask xor(TrafficMask mask) { 142 return getInstance(interestOps ^ mask.interestOps); 143 } 144 145 @Override 146 public String toString() { 147 return name; 148 } 149 }