1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.spdy.api;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 public class Settings implements Iterable<Settings.Setting>
27 {
28 private final Map<ID, Settings.Setting> settings;
29
30 public Settings()
31 {
32 settings = new HashMap<>();
33 }
34
35 public Settings(Settings original, boolean immutable)
36 {
37 Map<ID, Settings.Setting> copy = new HashMap<>(original.size());
38 copy.putAll(original.settings);
39 settings = immutable ? Collections.unmodifiableMap(copy) : copy;
40 }
41
42 public Setting get(ID id)
43 {
44 return settings.get(id);
45 }
46
47 public void put(Setting setting)
48 {
49 settings.put(setting.id(), setting);
50 }
51
52 public Setting remove(ID id)
53 {
54 return settings.remove(id);
55 }
56
57 public int size()
58 {
59 return settings.size();
60 }
61
62 public void clear()
63 {
64 settings.clear();
65 }
66
67 @Override
68 public boolean equals(Object obj)
69 {
70 if (this == obj)
71 return true;
72 if (obj == null || getClass() != obj.getClass())
73 return false;
74 Settings that = (Settings)obj;
75 return settings.equals(that.settings);
76 }
77
78 @Override
79 public int hashCode()
80 {
81 return settings.hashCode();
82 }
83
84 @Override
85 public Iterator<Setting> iterator()
86 {
87 return settings.values().iterator();
88 }
89
90 @Override
91 public String toString()
92 {
93 return settings.toString();
94 }
95
96 public static final class ID
97 {
98 public static final ID UPLOAD_BANDWIDTH = new ID(1);
99 public static final ID DOWNLOAD_BANDWIDTH = new ID(2);
100 public static final ID ROUND_TRIP_TIME = new ID(3);
101 public static final ID MAX_CONCURRENT_STREAMS = new ID(4);
102 public static final ID CURRENT_CONGESTION_WINDOW = new ID(5);
103 public static final ID DOWNLOAD_RETRANSMISSION_RATE = new ID(6);
104 public static final ID INITIAL_WINDOW_SIZE = new ID(7);
105
106 public synchronized static ID from(int code)
107 {
108 ID id = Codes.codes.get(code);
109 if (id == null)
110 id = new ID(code);
111 return id;
112 }
113
114 private final int code;
115
116 private ID(int code)
117 {
118 this.code = code;
119 Codes.codes.put(code, this);
120 }
121
122 public int code()
123 {
124 return code;
125 }
126
127 @Override
128 public String toString()
129 {
130 return String.valueOf(code);
131 }
132
133 private static class Codes
134 {
135 private static final Map<Integer, ID> codes = new HashMap<>();
136 }
137 }
138
139 public static enum Flag
140 {
141 NONE((byte)0),
142 PERSIST((byte)1),
143 PERSISTED((byte)2);
144
145 public static Flag from(byte code)
146 {
147 return Codes.codes.get(code);
148 }
149
150 private final byte code;
151
152 private Flag(byte code)
153 {
154 this.code = code;
155 Codes.codes.put(code, this);
156 }
157
158 public byte code()
159 {
160 return code;
161 }
162
163 private static class Codes
164 {
165 private static final Map<Byte, Flag> codes = new HashMap<>();
166 }
167 }
168
169 public static class Setting
170 {
171 private final ID id;
172 private final Flag flag;
173 private final int value;
174
175 public Setting(ID id, int value)
176 {
177 this(id, Flag.NONE, value);
178 }
179
180 public Setting(ID id, Flag flag, int value)
181 {
182 this.id = id;
183 this.flag = flag;
184 this.value = value;
185 }
186
187 public ID id()
188 {
189 return id;
190 }
191
192 public Flag flag()
193 {
194 return flag;
195 }
196
197 public int value()
198 {
199 return value;
200 }
201
202 @Override
203 public boolean equals(Object obj)
204 {
205 if (this == obj)
206 return true;
207 if (obj == null || getClass() != obj.getClass())
208 return false;
209 Setting that = (Setting)obj;
210 return value == that.value && flag == that.flag && id == that.id;
211 }
212
213 @Override
214 public int hashCode()
215 {
216 int result = id.hashCode();
217 result = 31 * result + flag.hashCode();
218 result = 31 * result + value;
219 return result;
220 }
221
222 @Override
223 public String toString()
224 {
225 return String.format("[id=%s,flags=%s:value=%d]", id(), flag(), value());
226 }
227 }
228 }