查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.redis;
17  
18  import io.netty.util.AbstractReferenceCounted;
19  import io.netty.util.ReferenceCountUtil;
20  import io.netty.util.internal.ObjectUtil;
21  import io.netty.util.internal.StringUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * Arrays of <a href="https://redis.io/topics/protocol">RESP</a>.
29   */
30  @UnstableApi
31  public class ArrayRedisMessage extends AbstractReferenceCounted implements RedisMessage {
32  
33      private final List<RedisMessage> children;
34  
35      private ArrayRedisMessage() {
36          children = Collections.emptyList();
37      }
38  
39      /**
40       * Creates a {@link ArrayRedisMessage} for the given {@code content}.
41       *
42       * @param children the children.
43       */
44      public ArrayRedisMessage(List<RedisMessage> children) {
45          // do not retain here. children are already retained when created.
46          this.children = ObjectUtil.checkNotNull(children, "children");
47      }
48  
49      /**
50       * Get children of this Arrays. It can be null or empty.
51       *
52       * @return list of {@link RedisMessage}s.
53       */
54      public final List<RedisMessage> children() {
55          return children;
56      }
57  
58      /**
59       * Returns whether the content of this message is {@code null}.
60       *
61       * @return indicates whether the content of this message is {@code null}.
62       */
63      public boolean isNull() {
64          return false;
65      }
66  
67      @Override
68      protected void deallocate() {
69          for (RedisMessage msg : children) {
70              ReferenceCountUtil.release(msg);
71          }
72      }
73  
74      @Override
75      public ArrayRedisMessage touch(Object hint) {
76          for (RedisMessage msg : children) {
77              ReferenceCountUtil.touch(msg);
78          }
79          return this;
80      }
81  
82      @Override
83      public String toString() {
84          return new StringBuilder(StringUtil.simpleClassName(this))
85                  .append('[')
86                  .append("children=")
87                  .append(children.size())
88                  .append(']').toString();
89      }
90  
91      /**
92       * A predefined null array instance for {@link ArrayRedisMessage}.
93       */
94      public static final ArrayRedisMessage NULL_INSTANCE = new ArrayRedisMessage() {
95          @Override
96          public boolean isNull() {
97              return true;
98          }
99  
100         @Override
101         public ArrayRedisMessage retain() {
102             return this;
103         }
104 
105         @Override
106         public ArrayRedisMessage retain(int increment) {
107             return this;
108         }
109 
110         @Override
111         public ArrayRedisMessage touch() {
112             return this;
113         }
114 
115         @Override
116         public ArrayRedisMessage touch(Object hint) {
117             return this;
118         }
119 
120         @Override
121         public boolean release() {
122             return false;
123         }
124 
125         @Override
126         public boolean release(int decrement) {
127             return false;
128         }
129 
130         @Override
131         public String toString() {
132             return "NullArrayRedisMessage";
133         }
134     };
135 
136     /**
137      * A predefined empty array instance for {@link ArrayRedisMessage}.
138      */
139     public static final ArrayRedisMessage EMPTY_INSTANCE = new ArrayRedisMessage() {
140 
141         @Override
142         public ArrayRedisMessage retain() {
143             return this;
144         }
145 
146         @Override
147         public ArrayRedisMessage retain(int increment) {
148             return this;
149         }
150 
151         @Override
152         public ArrayRedisMessage touch() {
153             return this;
154         }
155 
156         @Override
157         public ArrayRedisMessage touch(Object hint) {
158             return this;
159         }
160 
161         @Override
162         public boolean release() {
163             return false;
164         }
165 
166         @Override
167         public boolean release(int decrement) {
168             return false;
169         }
170 
171         @Override
172         public String toString() {
173             return "EmptyArrayRedisMessage";
174         }
175     };
176 
177 }