查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 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  
17  package io.netty.handler.codec.http;
18  
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map.Entry;
23  import java.util.Set;
24  
25  public class EmptyHttpHeaders extends HttpHeaders {
26      static final Iterator<Entry<CharSequence, CharSequence>> EMPTY_CHARS_ITERATOR =
27              Collections.<Entry<CharSequence, CharSequence>>emptyList().iterator();
28  
29      public static final EmptyHttpHeaders INSTANCE = instance();
30  
31      /**
32       * @see InstanceInitializer#EMPTY_HEADERS
33       * @deprecated Use {@link EmptyHttpHeaders#INSTANCE}
34       * <p>
35       * This is needed to break a cyclic static initialization loop between {@link HttpHeaders} and {@link
36       * EmptyHttpHeaders}.
37       */
38      @Deprecated
39      static EmptyHttpHeaders instance() {
40          return InstanceInitializer.EMPTY_HEADERS;
41      }
42  
43      protected EmptyHttpHeaders() {
44      }
45  
46      @Override
47      public String get(String name) {
48          return null;
49      }
50  
51      @Override
52      public Integer getInt(CharSequence name) {
53          return null;
54      }
55  
56      @Override
57      public int getInt(CharSequence name, int defaultValue) {
58          return defaultValue;
59      }
60  
61      @Override
62      public Short getShort(CharSequence name) {
63          return null;
64      }
65  
66      @Override
67      public short getShort(CharSequence name, short defaultValue) {
68          return defaultValue;
69      }
70  
71      @Override
72      public Long getTimeMillis(CharSequence name) {
73          return null;
74      }
75  
76      @Override
77      public long getTimeMillis(CharSequence name, long defaultValue) {
78          return defaultValue;
79      }
80  
81      @Override
82      public List<String> getAll(String name) {
83          return Collections.emptyList();
84      }
85  
86      @Override
87      public List<Entry<String, String>> entries() {
88          return Collections.emptyList();
89      }
90  
91      @Override
92      public boolean contains(String name) {
93          return false;
94      }
95  
96      @Override
97      public boolean isEmpty() {
98          return true;
99      }
100 
101     @Override
102     public int size() {
103         return 0;
104     }
105 
106     @Override
107     public Set<String> names() {
108         return Collections.emptySet();
109     }
110 
111     @Override
112     public HttpHeaders add(String name, Object value) {
113         throw new UnsupportedOperationException("read only");
114     }
115 
116     @Override
117     public HttpHeaders add(String name, Iterable<?> values) {
118         throw new UnsupportedOperationException("read only");
119     }
120 
121     @Override
122     public HttpHeaders addInt(CharSequence name, int value) {
123         throw new UnsupportedOperationException("read only");
124     }
125 
126     @Override
127     public HttpHeaders addShort(CharSequence name, short value) {
128         throw new UnsupportedOperationException("read only");
129     }
130 
131     @Override
132     public HttpHeaders set(String name, Object value) {
133         throw new UnsupportedOperationException("read only");
134     }
135 
136     @Override
137     public HttpHeaders set(String name, Iterable<?> values) {
138         throw new UnsupportedOperationException("read only");
139     }
140 
141     @Override
142     public HttpHeaders setInt(CharSequence name, int value) {
143         throw new UnsupportedOperationException("read only");
144     }
145 
146     @Override
147     public HttpHeaders setShort(CharSequence name, short value) {
148         throw new UnsupportedOperationException("read only");
149     }
150 
151     @Override
152     public HttpHeaders remove(String name) {
153         throw new UnsupportedOperationException("read only");
154     }
155 
156     @Override
157     public HttpHeaders clear() {
158         throw new UnsupportedOperationException("read only");
159     }
160 
161     @Override
162     public Iterator<Entry<String, String>> iterator() {
163         return entries().iterator();
164     }
165 
166     @Override
167     public Iterator<Entry<CharSequence, CharSequence>> iteratorCharSequence() {
168         return EMPTY_CHARS_ITERATOR;
169     }
170 
171     /**
172      * This class is needed to break a cyclic static initialization loop between {@link HttpHeaders} and
173      * {@link EmptyHttpHeaders}.
174      */
175     @Deprecated
176     private static final class InstanceInitializer {
177         /**
178          * The instance is instantiated here to break the cyclic static initialization between {@link EmptyHttpHeaders}
179          * and {@link HttpHeaders}. The issue is that if someone accesses {@link EmptyHttpHeaders#INSTANCE} before
180          * {@link HttpHeaders#EMPTY_HEADERS} then {@link HttpHeaders#EMPTY_HEADERS} will be {@code null}.
181          */
182         @Deprecated
183         private static final EmptyHttpHeaders EMPTY_HEADERS = new EmptyHttpHeaders();
184 
185         private InstanceInitializer() {
186         }
187     }
188 }