Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package io.protostuff.runtime;
 
 import java.util.List;
 import java.util.Map;

Field mapping implemented on top of hash for field lookup by number. This is the less efficient than ArrayFieldMap for almost all cases. But in case when field numbers are sparse and especially when max field number is big - this mapping should be used.

Author(s):
Kostiantyn Shchepanovskyi
See also:
ArrayFieldMap
final class HashFieldMap<T> implements FieldMap<T>
    private static final FieldComparator FIELD_COMPARATOR = new FieldComparator();
    private final List<Field<T>> fields;
    private final Map<IntegerField<T>> fieldsByNumber;
    private final Map<StringField<T>> fieldsByName;
    public HashFieldMap(Collection<Field<T>> fields)
    {
         = new HashMap<>();
         = new HashMap<>();
        for (Field<T> f : fields)
        {
            if (.containsKey(f.name))
            {
                Field<T> prev = .get(f.name);
                throw new IllegalStateException(prev + " and " + f + " cannot have the same name.");
            }
            if (.containsKey(f.number))
            {
                Field<T> prev = .get(f.number);
                throw new IllegalStateException(prev + " and " + f + " cannot have the same number.");
            }
            this..put(f.numberf);
            this..put(f.namef);
        }
        List<Field<T>> fieldList = new ArrayList<>(fields.size());
        fieldList.addAll(fields);
        Collections.sort(fieldList);
        this. = Collections.unmodifiableList(fieldList);
    }
    @Override
    public Field<T> getFieldByNumber(int n)
    {
        return .get(n);
    }
    @Override
    public Field<T> getFieldByName(String fieldName)
    {
        return .get(fieldName);
    }
    @Override
    public int getFieldCount()
    {
        return .size();
    }
    @Override
    public List<Field<T>> getFields()
    {
        return ;
    }
    private static class FieldComparator implements Comparator<Field<?>>
    {
        @Override
        public int compare(Field<?> o1Field<?> o2)
        {
            return Integer.compare(o1.numbero2.number);
        }
    }
New to GrepCode? Check out our FAQ X