View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.channel;
18  
19  import io.netty.util.ReferenceCountUtil;
20  import io.netty.util.ReferenceCounted;
21  import io.netty.util.internal.StringUtil;
22  
23  import java.net.SocketAddress;
24  
25  /**
26   * The default {@link AddressedEnvelope} implementation.
27   *
28   * @param <M> the type of the wrapped message
29   * @param <A> the type of the recipient address
30   */
31  public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements AddressedEnvelope<M, A> {
32  
33      private final M message;
34      private final A sender;
35      private final A recipient;
36  
37      /**
38       * Creates a new instance with the specified {@code message}, {@code recipient} address, and
39       * {@code sender} address.
40       */
41      public DefaultAddressedEnvelope(M message, A recipient, A sender) {
42          if (message == null) {
43              throw new NullPointerException("message");
44          }
45  
46          if (recipient == null && sender == null) {
47              throw new NullPointerException("recipient and sender");
48          }
49  
50          this.message = message;
51          this.sender = sender;
52          this.recipient = recipient;
53      }
54  
55      /**
56       * Creates a new instance with the specified {@code message} and {@code recipient} address.
57       * The sender address becomes {@code null}.
58       */
59      public DefaultAddressedEnvelope(M message, A recipient) {
60          this(message, recipient, null);
61      }
62  
63      @Override
64      public M content() {
65          return message;
66      }
67  
68      @Override
69      public A sender() {
70          return sender;
71      }
72  
73      @Override
74      public A recipient() {
75          return recipient;
76      }
77  
78      @Override
79      public int refCnt() {
80          if (message instanceof ReferenceCounted) {
81              return ((ReferenceCounted) message).refCnt();
82          } else {
83              return 1;
84          }
85      }
86  
87      @Override
88      public AddressedEnvelope<M, A> retain() {
89          ReferenceCountUtil.retain(message);
90          return this;
91      }
92  
93      @Override
94      public AddressedEnvelope<M, A> retain(int increment) {
95          ReferenceCountUtil.retain(message, increment);
96          return this;
97      }
98  
99      @Override
100     public boolean release() {
101         return ReferenceCountUtil.release(message);
102     }
103 
104     @Override
105     public boolean release(int decrement) {
106         return ReferenceCountUtil.release(message, decrement);
107     }
108 
109     @Override
110     public AddressedEnvelope<M, A> touch() {
111         ReferenceCountUtil.touch(message);
112         return this;
113     }
114 
115     @Override
116     public AddressedEnvelope<M, A> touch(Object hint) {
117         ReferenceCountUtil.touch(message, hint);
118         return this;
119     }
120 
121     @Override
122     public String toString() {
123         if (sender != null) {
124             return StringUtil.simpleClassName(this) +
125                     '(' + sender + " => " + recipient + ", " + message + ')';
126         } else {
127             return StringUtil.simpleClassName(this) +
128                     "(=> " + recipient + ", " + message + ')';
129         }
130     }
131 }