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          this.message = message;
47          this.sender = sender;
48          this.recipient = recipient;
49      }
50  
51      /**
52       * Creates a new instance with the specified {@code message} and {@code recipient} address.
53       * The sender address becomes {@code null}.
54       */
55      public DefaultAddressedEnvelope(M message, A recipient) {
56          this(message, recipient, null);
57      }
58  
59      @Override
60      public M content() {
61          return message;
62      }
63  
64      @Override
65      public A sender() {
66          return sender;
67      }
68  
69      @Override
70      public A recipient() {
71          return recipient;
72      }
73  
74      @Override
75      public int refCnt() {
76          if (message instanceof ReferenceCounted) {
77              return ((ReferenceCounted) message).refCnt();
78          } else {
79              return 1;
80          }
81      }
82  
83      @Override
84      public AddressedEnvelope<M, A> retain() {
85          ReferenceCountUtil.retain(message);
86          return this;
87      }
88  
89      @Override
90      public AddressedEnvelope<M, A> retain(int increment) {
91          ReferenceCountUtil.retain(message, increment);
92          return this;
93      }
94  
95      @Override
96      public boolean release() {
97          return ReferenceCountUtil.release(message);
98      }
99  
100     @Override
101     public boolean release(int decrement) {
102         return ReferenceCountUtil.release(message, decrement);
103     }
104 
105     @Override
106     public AddressedEnvelope<M, A> touch() {
107         ReferenceCountUtil.touch(message);
108         return this;
109     }
110 
111     @Override
112     public AddressedEnvelope<M, A> touch(Object hint) {
113         ReferenceCountUtil.touch(message, hint);
114         return this;
115     }
116 
117     @Override
118     public String toString() {
119         if (sender != null) {
120             return StringUtil.simpleClassName(this) +
121                     '(' + sender + " => " + recipient + ", " + message + ')';
122         } else {
123             return StringUtil.simpleClassName(this) +
124                     "(=> " + recipient + ", " + message + ')';
125         }
126     }
127 }