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.internal.ObjectUtil;
19  import io.netty.util.internal.StringUtil;
20  import org.jetbrains.annotations.Nullable;
21  
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  /**
26   * Default implementation of {@link Http3SettingsFrame}.
27   *
28   */
29  public final class DefaultHttp3SettingsFrame implements Http3SettingsFrame {
30  
31      private final Http3Settings settings;
32  
33      public DefaultHttp3SettingsFrame(Http3Settings settings) {
34          this.settings = ObjectUtil.checkNotNull(settings, "settings");
35      }
36  
37      public DefaultHttp3SettingsFrame() {
38          this.settings = new Http3Settings();
39      }
40  
41      @Override
42      public Http3Settings settings() {
43          return settings;
44      }
45  
46      /**
47       * Get a setting by its key.
48       *
49       * @param key the HTTP/3 setting key
50       * @return the value, or {@code null} if not set
51       * @deprecated  use {@link #settings()}  and manipulate the {@link Http3Settings} directly
52       */
53      @Override
54      @Deprecated
55      @Nullable
56      public Long get(long key) {
57          // Legacy behavior: direct map access.
58          return settings.get(key);
59      }
60  
61      /**
62       * Set a setting value by key.
63       *
64       * @param key the HTTP/3 setting key
65       * @param value the value to set
66       * @return the previous value, or {@code null} if none
67       * @throws IllegalArgumentException if the key is reserved for HTTP/2
68       * @deprecated  use {@link #settings()}  and manipulate the {@link Http3Settings} directly
69       */
70      @Override
71      @Deprecated
72      @Nullable
73      public Long put(long key, Long value) {
74          return settings.put(key, value);
75      }
76  
77      @Override
78      public Iterator<Map.Entry<Long, Long>> iterator() {
79         return settings.iterator();
80      }
81  
82      @Override
83      public int hashCode() {
84          return settings.hashCode();
85      }
86  
87      @Override
88      public boolean equals(Object o) {
89          if (this == o) {
90              return true;
91          }
92          if (o == null || getClass() != o.getClass()) {
93              return false;
94          }
95          DefaultHttp3SettingsFrame that = (DefaultHttp3SettingsFrame) o;
96          return that.settings.equals(settings);
97      }
98  
99      @Override
100     public String toString() {
101         return StringUtil.simpleClassName(this) + "(settings=" + settings + ')';
102     }
103 
104     /**
105      * Creates a new {@link DefaultHttp3SettingsFrame} which is a copy of the given settings.
106      *
107      * @param settingsFrame the frame to copy.
108      * @return              the newly created copy.
109      */
110     public static DefaultHttp3SettingsFrame copyOf(Http3SettingsFrame settingsFrame) {
111         DefaultHttp3SettingsFrame copy = new DefaultHttp3SettingsFrame();
112         if (settingsFrame instanceof DefaultHttp3SettingsFrame) {
113             copy.settings.putAll(((DefaultHttp3SettingsFrame) settingsFrame).settings);
114         } else {
115             for (Map.Entry<Long, Long> entry: settingsFrame) {
116                 copy.put(entry.getKey(), entry.getValue());
117             }
118         }
119         return copy;
120     }
121 }