Repository: apache/kafka
File: clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java (Java)

Code before:
            return "Represents a double-precision 64-bit format IEEE 754 value. " +
                    "The values are encoded using eight bytes in network byte order (big-endian).";
        }
    };

    public static final DocumentedType STRING = new StringType();

    public static final DocumentedType COMPACT_STRING = new CompactString();

    public static final DocumentedType NULLABLE_STRING = new NullableString();

    public static final DocumentedType COMPACT_NULLABLE_STRING = new CompactNullableString();

    public static final DocumentedType BYTES = new Bytes();

    public static final DocumentedType COMPACT_BYTES = new CompactBytes();

    public static final DocumentedType NULLABLE_BYTES = new NullableBytes();

    public static final DocumentedType COMPACT_NULLABLE_BYTES = new CompactNullableBytes();

    public static final DocumentedType RECORDS = new Records();

    public static final DocumentedType COMPACT_RECORDS = new CompactRecords();

    public static final DocumentedType NULLABLE_RECORDS = new NullableRecords();

    public static final DocumentedType COMPACT_NULLABLE_RECORDS = new CompactNullableRecords();

    public static final DocumentedType VARINT = new DocumentedType() {
        @Override
        public void write(ByteBuffer buffer, Object o) {
            ByteUtils.writeVarint((Integer) o, buffer);
        }

        @Override
        public Integer read(ByteBuffer buffer) {
            return ByteUtils.readVarint(buffer);
        }

        @Override
        public Integer validate(Object item) {
            if (item instanceof Integer)
                return (Integer) item;
            throw new SchemaException(item + " is not an integer");
        }

        public String typeName() {
            return "VARINT";
        }


Review comment (suggestion):
For those types created as anonymous class, it's probably better to just keep them as they are. To avoid code duplication, we could redirect the calls in the nullable version to the non-nullable one. For example, in NULLABLE_RECORDS.write(), we can call RECORDS.write(buffer, o).

Code after:
            return 8;
        }

        @Override
        public String typeName() {
            return "FLOAT64";
        }

        @Override
        public Double validate(Object item) {
            if (item instanceof Double)
                return (Double) item;
            else
                throw new SchemaException(item + " is not a Double.");
        }

        @Override
        public String documentation() {
            return "Represents a double-precision 64-bit format IEEE 754 value. " +
                    "The values are encoded using eight bytes in network byte order (big-endian).";
        }
    };

    public static final DocumentedType STRING = new DocumentedType() {
        @Override
        public void write(ByteBuffer buffer, Object o) {
            byte[] bytes = Utils.utf8((String) o);
            if (bytes.length > Short.MAX_VALUE)
                throw new SchemaException("String length " + bytes.length + " is larger than the maximum string length.");
            buffer.putShort((short) bytes.length);
            buffer.put(bytes);
        }

        @Override
        public String read(ByteBuffer buffer) {
            short length = buffer.getShort();
            if (length < 0)
                throw new SchemaException("String length " + length + " cannot be negative");
            return stringRead(buffer, length);
        }

        @Override
        public int sizeOf(Object o) {
            return 2 + Utils.utf8Length((String) o);
        }

        @Override
        public String typeName() {
            return "STRING";
        }