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

Code before:
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.common.protocol.types;

import org.apache.kafka.common.protocol.types.Type.DocumentedType;

import java.nio.ByteBuffer;
import java.util.Optional;

/**
 * Represents a type for an array of a particular type
 */
public class ArrayOf extends DocumentedType {
    private static final String ARRAY_TYPE_NAME = "ARRAY";

    private final Type type;

    public ArrayOf(Type type) {
        this.type = type;
    }

    @Override
    public boolean isNullable() {
        return false;
    }

    @Override
    public void write(ByteBuffer buffer, Object o) {
        Object[] objs = (Object[]) o;
        int size = objs.length;
        buffer.putInt(size);

        for (Object obj : objs)
            type.write(buffer, obj);
    }

    @Override
    public Object read(ByteBuffer buffer) {
        int size = buffer.getInt();

Review comment (suggestion):
Since we kept the BYTES implementation, we already have different patterns for implementing nullable types. So, instead of unifying the pattern, the goal should probably be to avoid code duplication. Since there is no code duplication in the existing ArrayOf, we can just keep it as it is. Ditto for CompactArraryOf.

Code after:
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.common.protocol.types;

import org.apache.kafka.common.protocol.types.Type.DocumentedType;

import java.nio.ByteBuffer;
import java.util.Optional;

/**
 * Represents a type for an array of a particular type
 */
public class ArrayOf extends DocumentedType {

    private static final String ARRAY_TYPE_NAME = "ARRAY";

    private static final String NULLABLE_ARRAY_TYPE_NAME = "NULLABLE_ARRAY";

    private final Type type;
    private final boolean nullable;

    public ArrayOf(Type type) {
        this(type, false);
    }

    public static ArrayOf nullable(Type type) {
        return new ArrayOf(type, true);
    }

    private ArrayOf(Type type, boolean nullable) {
        this.type = type;
        this.nullable = nullable;
    }

    @Override
    public boolean isNullable() {
        return nullable;
    }