View Javadoc
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    *   http://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          Integer key = Integer.valueOf(id);
40          return settingsMap.containsKey(key);
41      }
42  
43      @Override
44      public int getValue(int id) {
45          Integer key = Integer.valueOf(id);
46          if (settingsMap.containsKey(key)) {
47              return settingsMap.get(key).getValue();
48          } else {
49              return -1;
50          }
51      }
52  
53      @Override
54      public SpdySettingsFrame setValue(int id, int value) {
55          return setValue(id, value, false, false);
56      }
57  
58      @Override
59      public SpdySettingsFrame setValue(int id, int value, boolean persistValue, boolean persisted) {
60          if (id < 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
61              throw new IllegalArgumentException("Setting ID is not valid: " + id);
62          }
63          Integer key = Integer.valueOf(id);
64          if (settingsMap.containsKey(key)) {
65              Setting setting = settingsMap.get(key);
66              setting.setValue(value);
67              setting.setPersist(persistValue);
68              setting.setPersisted(persisted);
69          } else {
70              settingsMap.put(key, new Setting(value, persistValue, persisted));
71          }
72          return this;
73      }
74  
75      @Override
76      public SpdySettingsFrame removeValue(int id) {
77          Integer key = Integer.valueOf(id);
78          if (settingsMap.containsKey(key)) {
79              settingsMap.remove(key);
80          }
81          return this;
82      }
83  
84      @Override
85      public boolean isPersistValue(int id) {
86          Integer key = Integer.valueOf(id);
87          if (settingsMap.containsKey(key)) {
88              return settingsMap.get(key).isPersist();
89          } else {
90              return false;
91          }
92      }
93  
94      @Override
95      public SpdySettingsFrame setPersistValue(int id, boolean persistValue) {
96          Integer key = Integer.valueOf(id);
97          if (settingsMap.containsKey(key)) {
98              settingsMap.get(key).setPersist(persistValue);
99          }
100         return this;
101     }
102 
103     @Override
104     public boolean isPersisted(int id) {
105         Integer key = Integer.valueOf(id);
106         if (settingsMap.containsKey(key)) {
107             return settingsMap.get(key).isPersisted();
108         } else {
109             return false;
110         }
111     }
112 
113     @Override
114     public SpdySettingsFrame setPersisted(int id, boolean persisted) {
115         Integer key = Integer.valueOf(id);
116         if (settingsMap.containsKey(key)) {
117             settingsMap.get(key).setPersisted(persisted);
118         }
119         return this;
120     }
121 
122     @Override
123     public boolean clearPreviouslyPersistedSettings() {
124         return clear;
125     }
126 
127     @Override
128     public SpdySettingsFrame setClearPreviouslyPersistedSettings(boolean clear) {
129         this.clear = clear;
130         return this;
131     }
132 
133     private Set<Map.Entry<Integer, Setting>> getSettings() {
134         return settingsMap.entrySet();
135     }
136 
137     private void appendSettings(StringBuilder buf) {
138         for (Map.Entry<Integer, Setting> e: getSettings()) {
139             Setting setting = e.getValue();
140             buf.append("--> ");
141             buf.append(e.getKey());
142             buf.append(':');
143             buf.append(setting.getValue());
144             buf.append(" (persist value: ");
145             buf.append(setting.isPersist());
146             buf.append("; persisted: ");
147             buf.append(setting.isPersisted());
148             buf.append(')');
149             buf.append(StringUtil.NEWLINE);
150         }
151     }
152 
153     @Override
154     public String toString() {
155         StringBuilder buf = new StringBuilder()
156             .append(StringUtil.simpleClassName(this))
157             .append(StringUtil.NEWLINE);
158         appendSettings(buf);
159 
160         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
161         return buf.toString();
162     }
163 
164     private static final class Setting {
165 
166         private int value;
167         private boolean persist;
168         private boolean persisted;
169 
170         Setting(int value, boolean persist, boolean persisted) {
171             this.value = value;
172             this.persist = persist;
173             this.persisted = persisted;
174         }
175 
176         int getValue() {
177             return value;
178         }
179 
180         void setValue(int value) {
181             this.value = value;
182         }
183 
184         boolean isPersist() {
185             return persist;
186         }
187 
188         void setPersist(boolean persist) {
189             this.persist = persist;
190         }
191 
192         boolean isPersisted() {
193             return persisted;
194         }
195 
196         void setPersisted(boolean persisted) {
197             this.persisted = persisted;
198         }
199     }
200 }