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  package io.netty.handler.codec.memcache;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.util.internal.StringUtil;
20  import io.netty.util.internal.UnstableApi;
21  
22  /**
23   * The default {@link MemcacheContent} implementation.
24   */
25  @UnstableApi
26  public class DefaultMemcacheContent extends AbstractMemcacheObject implements MemcacheContent {
27  
28      private final ByteBuf content;
29  
30      /**
31       * Creates a new instance with the specified content.
32       */
33      public DefaultMemcacheContent(ByteBuf content) {
34          if (content == null) {
35              throw new NullPointerException("Content cannot be null.");
36          }
37          this.content = content;
38      }
39  
40      @Override
41      public ByteBuf content() {
42          return content;
43      }
44  
45      @Override
46      public MemcacheContent copy() {
47          return replace(content.copy());
48      }
49  
50      @Override
51      public MemcacheContent duplicate() {
52          return replace(content.duplicate());
53      }
54  
55      @Override
56      public MemcacheContent retainedDuplicate() {
57          return replace(content.retainedDuplicate());
58      }
59  
60      @Override
61      public MemcacheContent replace(ByteBuf content) {
62          return new DefaultMemcacheContent(content);
63      }
64  
65      @Override
66      public MemcacheContent retain() {
67          super.retain();
68          return this;
69      }
70  
71      @Override
72      public MemcacheContent retain(int increment) {
73          super.retain(increment);
74          return this;
75      }
76  
77      @Override
78      public MemcacheContent touch() {
79          super.touch();
80          return this;
81      }
82  
83      @Override
84      public MemcacheContent touch(Object hint) {
85          content.touch(hint);
86          return this;
87      }
88  
89      @Override
90      protected void deallocate() {
91          content.release();
92      }
93  
94      @Override
95      public String toString() {
96          return StringUtil.simpleClassName(this) +
97                 "(data: " + content() + ", decoderResult: " + decoderResult() + ')';
98      }
99  }