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.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.memcache.AbstractMemcacheObject;
20  
21  /**
22   * Default implementation of a {@link BinaryMemcacheMessage}.
23   */
24  public abstract class AbstractBinaryMemcacheMessage
25      extends AbstractMemcacheObject
26      implements BinaryMemcacheMessage {
27  
28      /**
29       * Contains the optional key.
30       */
31      private String key;
32  
33      /**
34       * Contains the optional extras.
35       */
36      private ByteBuf extras;
37  
38      private byte magic;
39      private byte opcode;
40      private short keyLength;
41      private byte extrasLength;
42      private byte dataType;
43      private int totalBodyLength;
44      private int opaque;
45      private long cas;
46  
47      /**
48       * Create a new instance with all properties set.
49       *
50       * @param key    the message key.
51       * @param extras the message extras.
52       */
53      protected AbstractBinaryMemcacheMessage(String key, ByteBuf extras) {
54          this.key = key;
55          this.extras = extras;
56      }
57  
58      @Override
59      public String key() {
60          return key;
61      }
62  
63      @Override
64      public ByteBuf extras() {
65          return extras;
66      }
67  
68      @Override
69      public BinaryMemcacheMessage setKey(String key) {
70          this.key = key;
71          return this;
72      }
73  
74      @Override
75      public BinaryMemcacheMessage setExtras(ByteBuf extras) {
76          this.extras = extras;
77          return this;
78      }
79  
80      @Override
81      public byte magic() {
82          return magic;
83      }
84  
85      @Override
86      public BinaryMemcacheMessage setMagic(byte magic) {
87          this.magic = magic;
88          return this;
89      }
90  
91      @Override
92      public long cas() {
93          return cas;
94      }
95  
96      @Override
97      public BinaryMemcacheMessage setCas(long cas) {
98          this.cas = cas;
99          return this;
100     }
101 
102     @Override
103     public int opaque() {
104         return opaque;
105     }
106 
107     @Override
108     public BinaryMemcacheMessage setOpaque(int opaque) {
109         this.opaque = opaque;
110         return this;
111     }
112 
113     @Override
114     public int totalBodyLength() {
115         return totalBodyLength;
116     }
117 
118     @Override
119     public BinaryMemcacheMessage setTotalBodyLength(int totalBodyLength) {
120         this.totalBodyLength = totalBodyLength;
121         return this;
122     }
123 
124     @Override
125     public byte dataType() {
126         return dataType;
127     }
128 
129     @Override
130     public BinaryMemcacheMessage setDataType(byte dataType) {
131         this.dataType = dataType;
132         return this;
133     }
134 
135     @Override
136     public byte extrasLength() {
137         return extrasLength;
138     }
139 
140     @Override
141     public BinaryMemcacheMessage setExtrasLength(byte extrasLength) {
142         this.extrasLength = extrasLength;
143         return this;
144     }
145 
146     @Override
147     public short keyLength() {
148         return keyLength;
149     }
150 
151     @Override
152     public BinaryMemcacheMessage setKeyLength(short keyLength) {
153         this.keyLength = keyLength;
154         return this;
155     }
156 
157     @Override
158     public byte opcode() {
159         return opcode;
160     }
161 
162     @Override
163     public BinaryMemcacheMessage setOpcode(byte opcode) {
164         this.opcode = opcode;
165         return this;
166     }
167 
168     @Override
169     public int refCnt() {
170         if (extras != null) {
171             return extras.refCnt();
172         }
173         return 1;
174     }
175 
176     @Override
177     public BinaryMemcacheMessage retain() {
178         if (extras != null) {
179             extras.retain();
180         }
181         return this;
182     }
183 
184     @Override
185     public BinaryMemcacheMessage retain(int increment) {
186         if (extras != null) {
187             extras.retain(increment);
188         }
189         return this;
190     }
191 
192     @Override
193     public boolean release() {
194         if (extras != null) {
195             return extras.release();
196         }
197         return false;
198     }
199 
200     @Override
201     public boolean release(int decrement) {
202         if (extras != null) {
203             return extras.release(decrement);
204         }
205         return false;
206     }
207 
208     @Override
209     public BinaryMemcacheMessage touch() {
210         return touch(null);
211     }
212 
213     @Override
214     public BinaryMemcacheMessage touch(Object hint) {
215         if (extras != null) {
216             extras.touch(hint);
217         }
218         return this;
219     }
220 }