EnumMap
did essentially exactly this in its entrySet()
iterator, which causes confusing, crazy, depressing bugs to this day.
If I were you, I just wouldn't use an Iterator
-- I'd write a different API (possibly quite dissimilar from Iterator, even) and implement that. For example, you might write a new API that takes as input the ByteBuffer
to write the message into, so users of the API could control whether or not the buffer gets reused. That seems reasonably intuitive (the user can write code that obviously and cleanly reuses the ByteBuffer
), without creating unnecessarily cluttered code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…