// Path: src/main/java/kafka/common/ErrorMapping.java
// public abstract class ErrorMapping {
//     public static final ByteBuffer EmptyByteBuffer = ByteBuffer.allocate(0);
// 
//     public static final short UnknownCode = -1;
//     public static final short NoError = 0;
//     public static final short OffsetOutOfRangeCode = 1;
//     public static final short InvalidMessageCode = 2;
//     public static final short UnknownTopicOrPartitionCode = 3;
//     public static final short InvalidFetchSizeCode = 4;
//     public static final short LeaderNotAvailableCode = 5;
//     public static final short NotLeaderForPartitionCode = 6;
//     public static final short RequestTimedOutCode = 7;
//     public static final short BrokerNotAvailableCode = 8;
//     public static final short ReplicaNotAvailableCode = 9;
//     public static final short MessageSizeTooLargeCode = 10;
//     public static final short StaleControllerEpochCode = 11;
//     public static final short OffsetMetadataTooLargeCode = 12;
//     public static final short StaleLeaderEpochCode = 13;
// 
//     private static BiMap<Class<? extends Throwable>, Short> exceptionToCode;
// 
//     static {
//         exceptionToCode.put(OffsetOutOfRangeException.class, OffsetOutOfRangeCode);
//         exceptionToCode.put(InvalidMessageException.class, InvalidMessageCode);
//         exceptionToCode.put(UnknownTopicOrPartitionException.class, UnknownTopicOrPartitionCode);
//         exceptionToCode.put(InvalidMessageSizeException.class, InvalidFetchSizeCode);
//         exceptionToCode.put(NotLeaderForPartitionException.class, NotLeaderForPartitionCode);
//         exceptionToCode.put(LeaderNotAvailableException.class, LeaderNotAvailableCode);
//         exceptionToCode.put(RequestTimedOutException.class, RequestTimedOutCode);
//         exceptionToCode.put(BrokerNotAvailableException.class, BrokerNotAvailableCode);
//         exceptionToCode.put(ReplicaNotAvailableException.class, ReplicaNotAvailableCode);
//         exceptionToCode.put(MessageSizeTooLargeException.class, MessageSizeTooLargeCode);
//         exceptionToCode.put(ControllerMovedException.class, StaleControllerEpochCode);
//         exceptionToCode.put(OffsetMetadataTooLargeException.class, OffsetMetadataTooLargeCode);
//     }
// 
//     public static Short codeFor(Class<? extends Throwable> exception) {
//         return Objects.firstNonNull(exceptionToCode.get(exception), UnknownCode);
//     }
// 
//     public static void maybeThrowException(Short code) {
//         Throwable throwable = exceptionFor(code);
//         if (throwable != null) {
//             throw Throwables.propagate(throwable);
//         }
//     }
// 
//     static Logger logger = LoggerFactory.getLogger(ErrorMapping.class);
// 
//     public static KafkaException exceptionFor(Short code) {
//         Class<? extends Throwable> throwable = exceptionToCode.inverse().get(code);
//         if (throwable == null) return null;
// 
//         try {
//             return (KafkaException) throwable.newInstance();
//         } catch (Exception e) {
//             logger.error("create instance of {} error", throwable, e);
//         }
//         return null;
//     }
// }


import kafka.cluster.Broker;
import kafka.common.ErrorMapping;
import kafka.utils.*;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import static kafka.api.ApiUtils.*;
package kafka.api;




public class TopicMetadata {
    public static final int NoLeaderNodeId = -1;

    public static TopicMetadata readFrom(final ByteBuffer buffer, final Map<Integer, Broker> brokers) {
        short errorCode = readShortInRange(buffer, "error code", Range.make((short) -1, Short.MAX_VALUE));
        String topic = readShortString(buffer);
        int numPartitions = readIntInRange(buffer, "number of partitions", Range.make(0, Integer.MAX_VALUE));
        List<PartitionMetadata> partitionsMetadata = Utils.flatList(0, numPartitions, new Function1<Integer, PartitionMetadata>() {
            @Override
            public PartitionMetadata apply(Integer arg) {
                return PartitionMetadata.readFrom(buffer, brokers);
            }
        });

        return new TopicMetadata(topic, partitionsMetadata, errorCode);
    }

    public String topic;
    public List<PartitionMetadata> partitionsMetadata;
    public short errorCode;

    public TopicMetadata(String topic, List<PartitionMetadata> partitionsMetadata) {
// Path: src/main/java/kafka/common/ErrorMapping.java
// public abstract class ErrorMapping {
//     public static final ByteBuffer EmptyByteBuffer = ByteBuffer.allocate(0);
// 
//     public static final short UnknownCode = -1;
//     public static final short NoError = 0;
//     public static final short OffsetOutOfRangeCode = 1;
//     public static final short InvalidMessageCode = 2;
//     public static final short UnknownTopicOrPartitionCode = 3;
//     public static final short InvalidFetchSizeCode = 4;
//     public static final short LeaderNotAvailableCode = 5;
//     public static final short NotLeaderForPartitionCode = 6;
//     public static final short RequestTimedOutCode = 7;
//     public static final short BrokerNotAvailableCode = 8;
//     public static final short ReplicaNotAvailableCode = 9;
//     public static final short MessageSizeTooLargeCode = 10;
//     public static final short StaleControllerEpochCode = 11;
//     public static final short OffsetMetadataTooLargeCode = 12;
//     public static final short StaleLeaderEpochCode = 13;
// 
//     private static BiMap<Class<? extends Throwable>, Short> exceptionToCode;
// 
//     static {
//         exceptionToCode.put(OffsetOutOfRangeException.class, OffsetOutOfRangeCode);
//         exceptionToCode.put(InvalidMessageException.class, InvalidMessageCode);
//         exceptionToCode.put(UnknownTopicOrPartitionException.class, UnknownTopicOrPartitionCode);
//         exceptionToCode.put(InvalidMessageSizeException.class, InvalidFetchSizeCode);
//         exceptionToCode.put(NotLeaderForPartitionException.class, NotLeaderForPartitionCode);
//         exceptionToCode.put(LeaderNotAvailableException.class, LeaderNotAvailableCode);
//         exceptionToCode.put(RequestTimedOutException.class, RequestTimedOutCode);
//         exceptionToCode.put(BrokerNotAvailableException.class, BrokerNotAvailableCode);
//         exceptionToCode.put(ReplicaNotAvailableException.class, ReplicaNotAvailableCode);
//         exceptionToCode.put(MessageSizeTooLargeException.class, MessageSizeTooLargeCode);
//         exceptionToCode.put(ControllerMovedException.class, StaleControllerEpochCode);
//         exceptionToCode.put(OffsetMetadataTooLargeException.class, OffsetMetadataTooLargeCode);
//     }
// 
//     public static Short codeFor(Class<? extends Throwable> exception) {
//         return Objects.firstNonNull(exceptionToCode.get(exception), UnknownCode);
//     }
// 
//     public static void maybeThrowException(Short code) {
//         Throwable throwable = exceptionFor(code);
//         if (throwable != null) {
//             throw Throwables.propagate(throwable);
//         }
//     }
// 
//     static Logger logger = LoggerFactory.getLogger(ErrorMapping.class);
// 
//     public static KafkaException exceptionFor(Short code) {
//         Class<? extends Throwable> throwable = exceptionToCode.inverse().get(code);
//         if (throwable == null) return null;
// 
//         try {
//             return (KafkaException) throwable.newInstance();
//         } catch (Exception e) {
//             logger.error("create instance of {} error", throwable, e);
//         }
//         return null;
//     }
// }


// Path: src/main/java/kafka/api/TopicMetadata.java
import kafka.cluster.Broker;
import kafka.common.ErrorMapping;
import kafka.utils.*;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import static kafka.api.ApiUtils.*;

package kafka.api;




public class TopicMetadata {
    public static final int NoLeaderNodeId = -1;

    public static TopicMetadata readFrom(final ByteBuffer buffer, final Map<Integer, Broker> brokers) {
        short errorCode = readShortInRange(buffer, "error code", Range.make((short) -1, Short.MAX_VALUE));
        String topic = readShortString(buffer);
        int numPartitions = readIntInRange(buffer, "number of partitions", Range.make(0, Integer.MAX_VALUE));
        List<PartitionMetadata> partitionsMetadata = Utils.flatList(0, numPartitions, new Function1<Integer, PartitionMetadata>() {
            @Override
            public PartitionMetadata apply(Integer arg) {
                return PartitionMetadata.readFrom(buffer, brokers);
            }
        });

        return new TopicMetadata(topic, partitionsMetadata, errorCode);
    }

    public String topic;
    public List<PartitionMetadata> partitionsMetadata;
    public short errorCode;

    public TopicMetadata(String topic, List<PartitionMetadata> partitionsMetadata) {
        this(topic, partitionsMetadata, ErrorMapping.NoError);