Skip to main content
Rollback to Revision 2
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177
public class PojoDecodeIteratorStringDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        publicprivate PojoDecodeIteratorStringDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(getNextOffsetoffset(), this.buffer);
        }

        private synchronized int getNextOffsetoffset() {
            if (!(this.count >=< this.size)) {
                throw new NoSuchElementException("No more elements"element");
            }
            this.count++;
            int resultOffseto = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + Integer.BYTES;4;
            return resultOffset;o;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int, byte[] buffer); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

public class PojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        public PojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(getNextOffset(), this.buffer);
        }

        private synchronized int getNextOffset() {
            if (this.count >= this.size) {
                throw new NoSuchElementException("No more elements");
            }
            this.count++;
            int resultOffset = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + Integer.BYTES;
            return resultOffset;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int, byte[] buffer); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

public class StringDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        private StringDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offset(), this.buffer);
        }

        private synchronized int offset() {
            if (!(this.count < this.size)) {
                throw new NoSuchElementException("No more element");
            }
            this.count++;
            int o = this.offset;
            this.offset += Utils.getInt(o) + 4;
            return o;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

added 47 characters in body
Source Link
William
  • 141
  • 2
public class PojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        public PojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offsetgetNextOffset(), this.buffer);
        }

        private synchronized int offsetgetNextOffset() {
            if (!(this.count <>= this.size)) {
                throw new NoSuchElementException("No more element"elements");
            }
            this.count++;
            int oresultOffset = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + 4;Integer.BYTES;
            return o;resultOffset;
        }

    }
public class PojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        public PojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offset(), this.buffer);
        }

        private synchronized int offset() {
            if (!(this.count < this.size)) {
                throw new NoSuchElementException("No more element");
            }
            this.count++;
            int o = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + 4;
            return o;
        }

    }
public class PojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        public PojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(getNextOffset(), this.buffer);
        }

        private synchronized int getNextOffset() {
            if (this.count >= this.size) {
                throw new NoSuchElementException("No more elements");
            }
            this.count++;
            int resultOffset = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + Integer.BYTES;
            return resultOffset;
        }

    }
added 23 characters in body
Source Link
William
  • 141
  • 2
public class StringDecodeIteratorPojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        privatepublic StringDecodeIteratorPojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offset(), this.buffer);
        }

        private synchronized int offset() {
            if (!(this.count < this.size)) {
                throw new NoSuchElementException("No more element");
            }
            this.count++;
            int o = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + 4;
            return o;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int, byte[] buffer); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

public class StringDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        private StringDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offset(), this.buffer);
        }

        private synchronized int offset() {
            if (!(this.count < this.size)) {
                throw new NoSuchElementException("No more element");
            }
            this.count++;
            int o = this.offset;
            this.offset += Utils.getInt(o) + 4;
            return o;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

public class PojoDecodeIterator implements Iterator<Pojo> {

        private final int size;
        private final PojoDecoder decoder;
        private final byte[] buffer;
        private int offset = 0;
        private int count = 0;

        public PojoDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
            this.size = size;
            this.decoder = decoder;
            this.buffer = buffer;
        }

        @Override
        public synchronized boolean hasNext() {
            return this.count < this.size;
        }

        @Override
        public Pojo next() {
            return this.decoder.decode(offset(), this.buffer);
        }

        private synchronized int offset() {
            if (!(this.count < this.size)) {
                throw new NoSuchElementException("No more element");
            }
            this.count++;
            int o = this.offset;
            this.offset += Utils.getInt(o, this.buffer) + 4;
            return o;
        }

    }
  1. size is the number of element to decode : It is a known value; we know how many Pojos are encoded.

  2. PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.

    interface PojoDecoder {
        Pojo decode(int offset, byte[] buffer);
    }
    
  3. Utils#getInt(int, byte[] buffer); just converts 4 bytes to an int from the given position.

  4. PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.

Spelling and grammar
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
Became Hot Network Question
Source Link
William
  • 141
  • 2
Loading