Source:
<code>public int offer(BBContainer data, long startId, long endId) throws IOException { synchronized (PersistentBinaryDeque.this) { if (m_closed) throw new IOException("GapWriter is closed"); assert(m_requiresId && m_gapHeader != null); assert(startId != PBDSegment.INVALID_ID && endId != PBDSegment.INVALID_ID && endId >= startId); Map.Entry<Long, PBDSegment<M>> entry = m_segments.floorEntry(startId); PBDSegment<M> prev = findValidSegmentFrom((entry == null ? null : entry.getValue()), false, PBDSegment.INVALID_ID, true); if (m_activeSegment!= null && m_activeSegment.m_id != m_segments.lastKey() && (prev == null || prev.getStartId() != m_activeSegment.getStartId())) { <START> finishWrite(m_activeSegment); <END> } if (prev == null && peekFirstSegment() != null) { verifyRangeNotInSegment(peekFirstSegment(), startId, endId); } else if (prev != null) { verifyRangeNotInSegment(prev, startId, endId); Map.Entry<Long, PBDSegment<M>> nextEntry = m_segments.higherEntry(entry.getKey()); PBDSegment<M> next = findValidSegmentFrom((nextEntry == null ? null : nextEntry.getValue()), true, PBDSegment.INVALID_ID, true); if (next != null) { verifyRangeNotInSegment(next, startId, endId); } } Pair<Integer, PBDSegment<M>> result = offerToSegment(prev, data, startId, endId, m_gapHeader, false); m_activeSegment = result.getSecond(); return result.getFirst(); } }</code><technical_language>want set activeSegment null here. worth adding a utility method callsFinishwrite current segment sets null operations done together</technical_language>

Target:
public int offer(BBContainer data, long startId, long endId) throws IOException { synchronized (PersistentBinaryDeque.this) { if (m_closed) throw new IOException("updateGapHeader call on closed PBD " + m_nonce); assert(m_gapHeader != null); assert(startId != PBDSegment.INVALID_ID && endId >= startId); PBDSegment<M> prev = m_activeSegment; if (m_activeSegment == null || startId != m_activeSegment.getEndId()+1) { Map.Entry<Long, PBDSegment<M>> entry = m_segments.floorEntry(startId); prev = findValidSegmentFrom((entry == null ? null : entry.getValue()), false, true); if (m_activeSegment!= null && m_activeSegment.m_id != m_segments.lastKey()) { finishActiveSegmentWrite(); } } PBDSegment<M> next = null; if (prev == null) { next = findValidSegmentFrom(peekFirstSegment(), true, true); } else { Map.Entry<Long, PBDSegment<M>> nextEntry = m_segments.higherEntry(prev.m_id); next = findValidSegmentFrom((nextEntry == null ? null : nextEntry.getValue()), true, true); } if (prev!=null && startId <= prev.getEndId()) { throw new IllegalArgumentException("PBD segment with range [" + prev.getStartId() + "-" + prev.getEndId() + "] already contains some entries offered in the range ]" + startId + "-" + endId + "]"); } if (next!=null && endId >= next.getStartId()) { throw new IllegalArgumentException("PBD segment with range [" + next.getStartId() + "-" + next.getEndId() + "] already contains some entries offered in the range ]" + startId + "-" + endId + "]"); } if (prev != null && startId != prev.getEndId()+1) { prev = null; } m_filledGap = true; Pair<Integer, PBDSegment<M>> result = offerToSegment(prev, data, startId, endId, m_gapHeader, false); m_activeSegment = result.getSecond(); return result.getFirst(); } }