View Javadoc
1   /*
2    * Copyright 2020 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.netty.handler.codec.http3;
17  
18  import io.netty.util.collection.LongObjectHashMap;
19  import io.netty.util.collection.LongObjectMap;
20  import io.netty.util.internal.StringUtil;
21  
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  public final class DefaultHttp3SettingsFrame implements Http3SettingsFrame {
26  
27      private final LongObjectMap<Long> settings = new LongObjectHashMap<>(4);
28  
29      @Override
30      public Long get(long key) {
31          return settings.get(key);
32      }
33  
34      @Override
35      public Long put(long key, Long value) {
36          if (Http3CodecUtils.isReservedHttp2Setting(key)) {
37              throw new IllegalArgumentException("Setting is reserved for HTTP/2: " + key);
38          }
39          return settings.put(key, value);
40      }
41  
42      @Override
43      public Iterator<Map.Entry<Long, Long>> iterator() {
44          return settings.entrySet().iterator();
45      }
46  
47      @Override
48      public int hashCode() {
49          return settings.hashCode();
50      }
51  
52      @Override
53      public boolean equals(Object o) {
54          if (this == o) {
55              return true;
56          }
57          if (o == null || getClass() != o.getClass()) {
58              return false;
59          }
60          DefaultHttp3SettingsFrame that = (DefaultHttp3SettingsFrame) o;
61          return that.settings.equals(settings);
62      }
63  
64      @Override
65      public String toString() {
66          return StringUtil.simpleClassName(this) + "(settings=" + settings + ')';
67      }
68  
69      /**
70       * Creates a new {@link DefaultHttp3SettingsFrame} which is a copy of the given settings.
71       *
72       * @param settingsFrame the frame to copy.
73       * @return              the newly created copy.
74       */
75      public static DefaultHttp3SettingsFrame copyOf(Http3SettingsFrame settingsFrame) {
76          DefaultHttp3SettingsFrame copy = new DefaultHttp3SettingsFrame();
77          if (settingsFrame instanceof DefaultHttp3SettingsFrame) {
78              copy.settings.putAll(((DefaultHttp3SettingsFrame) settingsFrame).settings);
79          } else {
80              for (Map.Entry<Long, Long> entry: settingsFrame) {
81                  copy.put(entry.getKey(), entry.getValue());
82              }
83          }
84          return copy;
85      }
86  }