class BoundedOffsetQueue implements OffsetQueue<T> {

    @Override
    public OffsetList addOffset(TermAndZone termAndZone, T offset) {
        String key = termAndZone.getToken();
        queue.remove(key);
        
        List<T> offsets = offsetsMap.remove(key);
        if (null == offsets) {
            offsets = new ArrayList<>();
        }
        offsets.add(offset);
        
        offsetsMap.put(key, offsets);
        queue.add(key);
        
        numOffsets++;
        if (numOffsets > maxNumOffsets) {
            OffsetList<T> list = new OffsetList<>();
            String token = queue.remove();
            list.offsets = offsetsMap.remove(token);
            numOffsets -= list.offsets.size();
            list.termAndZone = new TermAndZone(token);
            return list;
        } else {
            return null;
        }
    }

    public  BoundedOffsetQueue(int maxNumOffsets);

}