查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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.spdy;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.TreeMap;
23  
24  /**
25   * The default {@link SpdySettingsFrame} implementation.
26   */
27  public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
28  
29      private boolean clear;
30      private final Map<Integer, Setting> settingsMap = new TreeMap<Integer, Setting>();
31  
32      @Override
33      public Set<Integer> ids() {
34          return settingsMap.keySet();
35      }
36  
37      @Override
38      public boolean isSet(int id) {
39          return settingsMap.containsKey(id);
40      }
41  
42      @Override
43      public int getValue(int id) {
44          final Setting setting = settingsMap.get(id);
45          return setting != null ? setting.getValue() : -1;
46      }
47  
48      @Override
49      public SpdySettingsFrame setValue(int id, int value) {
50          return setValue(id, value, false, false);
51      }
52  
53      @Override
54      public SpdySettingsFrame setValue(int id, int value, boolean persistValue, boolean persisted) {
55          if (id < 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
56              throw new IllegalArgumentException("Setting ID is not valid: " + id);
57          }
58          final Integer key = Integer.valueOf(id);
59          final Setting setting = settingsMap.get(key);
60          if (setting != null) {
61              setting.setValue(value);
62              setting.setPersist(persistValue);
63              setting.setPersisted(persisted);
64          } else {
65              settingsMap.put(key, new Setting(value, persistValue, persisted));
66          }
67          return this;
68      }
69  
70      @Override
71      public SpdySettingsFrame removeValue(int id) {
72          settingsMap.remove(id);
73          return this;
74      }
75  
76      @Override
77      public boolean isPersistValue(int id) {
78          final Setting setting = settingsMap.get(id);
79          return setting != null && setting.isPersist();
80      }
81  
82      @Override
83      public SpdySettingsFrame setPersistValue(int id, boolean persistValue) {
84          final Setting setting = settingsMap.get(id);
85          if (setting != null) {
86              setting.setPersist(persistValue);
87          }
88          return this;
89      }
90  
91      @Override
92      public boolean isPersisted(int id) {
93          final Setting setting = settingsMap.get(id);
94          return setting != null && setting.isPersisted();
95      }
96  
97      @Override
98      public SpdySettingsFrame setPersisted(int id, boolean persisted) {
99          final Setting setting = settingsMap.get(id);
100         if (setting != null) {
101             setting.setPersisted(persisted);
102         }
103         return this;
104     }
105 
106     @Override
107     public boolean clearPreviouslyPersistedSettings() {
108         return clear;
109     }
110 
111     @Override
112     public SpdySettingsFrame setClearPreviouslyPersistedSettings(boolean clear) {
113         this.clear = clear;
114         return this;
115     }
116 
117     private Set<Map.Entry<Integer, Setting>> getSettings() {
118         return settingsMap.entrySet();
119     }
120 
121     private void appendSettings(StringBuilder buf) {
122         for (Map.Entry<Integer, Setting> e: getSettings()) {
123             Setting setting = e.getValue();
124             buf.append("--> ");
125             buf.append(e.getKey());
126             buf.append(':');
127             buf.append(setting.getValue());
128             buf.append(" (persist value: ");
129             buf.append(setting.isPersist());
130             buf.append("; persisted: ");
131             buf.append(setting.isPersisted());
132             buf.append(')');
133             buf.append(StringUtil.NEWLINE);
134         }
135     }
136 
137     @Override
138     public String toString() {
139         StringBuilder buf = new StringBuilder()
140             .append(StringUtil.simpleClassName(this))
141             .append(StringUtil.NEWLINE);
142         appendSettings(buf);
143 
144         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
145         return buf.toString();
146     }
147 
148     private static final class Setting {
149 
150         private int value;
151         private boolean persist;
152         private boolean persisted;
153 
154         Setting(int value, boolean persist, boolean persisted) {
155             this.value = value;
156             this.persist = persist;
157             this.persisted = persisted;
158         }
159 
160         int getValue() {
161             return value;
162         }
163 
164         void setValue(int value) {
165             this.value = value;
166         }
167 
168         boolean isPersist() {
169             return persist;
170         }
171 
172         void setPersist(boolean persist) {
173             this.persist = persist;
174         }
175 
176         boolean isPersisted() {
177             return persisted;
178         }
179 
180         void setPersisted(boolean persisted) {
181             this.persisted = persisted;
182         }
183     }
184 }