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    *   https://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.ObjectUtil;
22  import io.netty.util.internal.StringUtil;
23  
24  import java.net.SocketAddress;
25  
26  /**
27   * The default {@link AddressedEnvelope} implementation.
28   *
29   * @param <M> the type of the wrapped message
30   * @param <A> the type of the recipient address
31   */
32  public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements AddressedEnvelope<M, A> {
33  
34      private final M message;
35      private final A sender;
36      private final A recipient;
37  
38      /**
39       * Creates a new instance with the specified {@code message}, {@code recipient} address, and
40       * {@code sender} address.
41       */
42      public DefaultAddressedEnvelope(M message, A recipient, A sender) {
43          ObjectUtil.checkNotNull(message, "message");
44          if (recipient == null && sender == null) {
45              throw new NullPointerException("recipient and sender");
46          }
47  
48          this.message = message;
49          this.sender = sender;
50          this.recipient = recipient;
51      }
52  
53      /**
54       * Creates a new instance with the specified {@code message} and {@code recipient} address.
55       * The sender address becomes {@code null}.
56       */
57      public DefaultAddressedEnvelope(M message, A recipient) {
58          this(message, recipient, null);
59      }
60  
61      @Override
62      public M content() {
63          return message;
64      }
65  
66      @Override
67      public A sender() {
68          return sender;
69      }
70  
71      @Override
72      public A recipient() {
73          return recipient;
74      }
75  
76      @Override
77      public int refCnt() {
78          if (message instanceof ReferenceCounted) {
79              return ((ReferenceCounted) message).refCnt();
80          } else {
81              return 1;
82          }
83      }
84  
85      @Override
86      public AddressedEnvelope<M, A> retain() {
87          ReferenceCountUtil.retain(message);
88          return this;
89      }
90  
91      @Override
92      public AddressedEnvelope<M, A> retain(int increment) {
93          ReferenceCountUtil.retain(message, increment);
94          return this;
95      }
96  
97      @Override
98      public boolean release() {
99          return ReferenceCountUtil.release(message);
100     }
101 
102     @Override
103     public boolean release(int decrement) {
104         return ReferenceCountUtil.release(message, decrement);
105     }
106 
107     @Override
108     public AddressedEnvelope<M, A> touch() {
109         ReferenceCountUtil.touch(message);
110         return this;
111     }
112 
113     @Override
114     public AddressedEnvelope<M, A> touch(Object hint) {
115         ReferenceCountUtil.touch(message, hint);
116         return this;
117     }
118 
119     @Override
120     public String toString() {
121         if (sender != null) {
122             return StringUtil.simpleClassName(this) +
123                     '(' + sender + " => " + recipient + ", " + message + ')';
124         } else {
125             return StringUtil.simpleClassName(this) +
126                     "(=> " + recipient + ", " + message + ')';
127         }
128     }
129 }