updates from review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2019-05-31 13:17:25 +02:00
parent 4d60260cd3
commit 39be07871b
1 changed files with 51 additions and 82 deletions

View File

@ -31,7 +31,6 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.function.Consumer;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
@ -66,7 +65,7 @@ public class HttpFields implements Iterable<HttpField>
*/
public HttpFields()
{
this(16); // TODO tune default. Currently based on small sample of Chrome requests.
this(16); // Based on small sample of Chrome requests.
}
/**
@ -86,7 +85,7 @@ public class HttpFields implements Iterable<HttpField>
*/
public HttpFields(HttpFields fields)
{
_fields=Arrays.copyOf(fields._fields,fields._fields.length+10);
_fields=Arrays.copyOf(fields._fields,fields._fields.length);
_size=fields._size;
}
@ -95,17 +94,10 @@ public class HttpFields implements Iterable<HttpField>
return _size;
}
@Override
public void forEach(Consumer<? super HttpField> action)
{
for (HttpField f : this)
action.accept(f);
}
@Override
public Iterator<HttpField> iterator()
{
return new Itr();
return new ListItr();
}
public ListIterator<HttpField> listIterator()
@ -125,10 +117,15 @@ public class HttpFields implements Iterable<HttpField>
*/
public Set<String> getFieldNamesCollection()
{
final Set<String> set = new HashSet<>(_size);
for (HttpField f : this)
Set<String> set = null;
for (int i=0;i<_size;i++)
{
HttpField f=_fields[i];
if (set==null)
set = new HashSet<>();
set.add(f.getName());
return set;
}
return set==null?Collections.emptySet():set;
}
/**
@ -1111,40 +1108,10 @@ public class HttpFields implements Iterable<HttpField>
return values.getValues();
}
private class Itr implements Iterator<HttpField>
{
int _cursor; // index of next element to return
@Override
public boolean hasNext()
{
return _cursor != _size;
}
@Override
public HttpField next()
{
if (_cursor == _size)
throw new NoSuchElementException();
return _fields[_cursor++];
}
@Override
public void remove()
{
if (_cursor==0)
throw new IllegalStateException();
System.arraycopy(_fields, _cursor, _fields,_cursor-1,_size - _cursor);
_size--;
_cursor--;
}
}
private class ListItr implements ListIterator<HttpField>
{
int _cursor; // index of next element to return
int _last=-1;
int _current =-1;
@Override
public boolean hasNext()
@ -1157,18 +1124,20 @@ public class HttpFields implements Iterable<HttpField>
{
if (_cursor == _size)
throw new NoSuchElementException();
_last = _cursor++;
return _fields[_last];
_current = _cursor++;
return _fields[_current];
}
@Override
public void remove()
{
if (_last<0)
if (_current <0)
throw new IllegalStateException();
System.arraycopy(_fields,_last+1,_fields,_last,--_size-_last);
_cursor=_last;
_last=-1;
_size--;
System.arraycopy(_fields, _current +1,_fields, _current,_size- _current);
_fields[_size]=null;
_cursor= _current;
_current =-1;
}
@Override
@ -1182,8 +1151,8 @@ public class HttpFields implements Iterable<HttpField>
{
if (_cursor == 0)
throw new NoSuchElementException();
_last = --_cursor;
return _fields[_last];
_current = --_cursor;
return _fields[_current];
}
@Override
@ -1201,9 +1170,9 @@ public class HttpFields implements Iterable<HttpField>
@Override
public void set(HttpField field)
{
if (_last<0)
if (_current <0)
throw new IllegalStateException();
_fields[_last] = field;
_fields[_current] = field;
}
@Override
@ -1212,7 +1181,7 @@ public class HttpFields implements Iterable<HttpField>
_fields = Arrays.copyOf(_fields,_fields.length+1);
System.arraycopy(_fields,_cursor,_fields,_cursor+1,_size++);
_fields[_cursor++] = field;
_last=-1;
_current =-1;
}
}
}