View Javadoc
1   /*
2    * Copyright 2017 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  package io.netty5.handler.codec.http2;
17  
18  import io.netty5.channel.ChannelId;
19  
20  /**
21   * ChannelId implementation which is used by our {@link Http2StreamChannel} implementation.
22   */
23  final class Http2StreamChannelId implements ChannelId {
24      private static final long serialVersionUID = -6642338822166867585L;
25  
26      private final int id;
27      private final ChannelId parentId;
28  
29      Http2StreamChannelId(ChannelId parentId, int id) {
30          this.parentId = parentId;
31          this.id = id;
32      }
33  
34      @Override
35      public String asShortText() {
36          return parentId.asShortText() + '/' + id;
37      }
38  
39      @Override
40      public String asLongText() {
41          return parentId.asLongText() + '/' + id;
42      }
43  
44      @Override
45      public int compareTo(ChannelId o) {
46          if (o instanceof Http2StreamChannelId) {
47              Http2StreamChannelId otherId = (Http2StreamChannelId) o;
48              int res = parentId.compareTo(otherId.parentId);
49              if (res == 0) {
50                  return id - otherId.id;
51              } else {
52                  return res;
53              }
54          }
55          return parentId.compareTo(o);
56      }
57  
58      @Override
59      public int hashCode() {
60          return id * 31 + parentId.hashCode();
61      }
62  
63      @Override
64      public boolean equals(Object obj) {
65          if (!(obj instanceof Http2StreamChannelId)) {
66              return false;
67          }
68          Http2StreamChannelId otherId = (Http2StreamChannelId) obj;
69          return id == otherId.id && parentId.equals(otherId.parentId);
70      }
71  
72      @Override
73      public String toString() {
74          return asShortText();
75      }
76  }