InMemorySorter implements Sorter { public boolean addIfRoom(KV<byte[], byte[]> record) { checkState(!sortCalled, "Records can only be added before sort()"); long recordBytes = estimateRecordBytes(record); if (roomInBuffer(numBytes + recordBytes, records.size() + 1L)) { records.add(record); numBytes += recordBytes; return true; } else { return false; } } private  InMemorySorter(Options options); static InMemorySorter create(Options options); @Override void add(KV<byte[], byte[]> record); boolean addIfRoom(KV<byte[], byte[]> record); @Override Iterable<KV<byte[], byte[]>> sort();  }
@Test public void testAddIfRoom() { InMemorySorter.Options options = new InMemorySorter.Options(); options.setMemoryMB(1); InMemorySorter sorter = InMemorySorter.create(options); KV<byte[], byte[]> bigRecord = KV.of(new byte[1024 * 500], new byte[1024 * 500]); Assert.assertTrue(sorter.addIfRoom(bigRecord)); Assert.assertFalse(sorter.addIfRoom(bigRecord)); }