/*
 * Copyright DataStax, Inc.
 *
 * Licensed 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 com.datastax.oss.driver.internal.core.adminrequest;

import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import com.datastax.oss.protocol.internal.response.result.ColumnSpec;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import net.jcip.annotations.Immutable;

@Immutable
public class AdminRow {

  @VisibleForTesting
  static final TypeCodec<List<String>> LIST_OF_TEXT = TypeCodecs.listOf(TypeCodecs.TEXT);

  private static final TypeCodec<Set<String>> SET_OF_TEXT = TypeCodecs.setOf(TypeCodecs.TEXT);
  private static final TypeCodec<Map<String, String>> MAP_OF_STRING_TO_STRING =
      TypeCodecs.mapOf(TypeCodecs.TEXT, TypeCodecs.TEXT);

  private final Map<String, ColumnSpec> columnSpecs;
  private final List<ByteBuffer> data;
  private final ProtocolVersion protocolVersion;

  public AdminRow(
      Map<String, ColumnSpec> columnSpecs, List<ByteBuffer> data, ProtocolVersion protocolVersion) {
    this.columnSpecs = columnSpecs;
    this.data = data;
    this.protocolVersion = protocolVersion;
  }

  public Boolean getBoolean(String columnName) {
    return get(columnName, TypeCodecs.BOOLEAN);
  }

  public Integer getInteger(String columnName) {
    return get(columnName, TypeCodecs.INT);
  }

  public boolean isString(String columnName) {
    return columnSpecs.get(columnName).type.id == ProtocolConstants.DataType.VARCHAR;
  }

  public String getString(String columnName) {
    return get(columnName, TypeCodecs.TEXT);
  }

  public UUID getUuid(String columnName) {
    return get(columnName, TypeCodecs.UUID);
  }

  public ByteBuffer getByteBuffer(String columnName) {
    return get(columnName, TypeCodecs.BLOB);
  }

  public InetAddress getInetAddress(String columnName) {
    return get(columnName, TypeCodecs.INET);
  }

  public List<String> getListOfString(String columnName) {
    return get(columnName, LIST_OF_TEXT);
  }

  public Set<String> getSetOfString(String columnName) {
    return get(columnName, SET_OF_TEXT);
  }

  public Map<String, String> getMapOfStringToString(String columnName) {
    return get(columnName, MAP_OF_STRING_TO_STRING);
  }

  public boolean contains(String columnName) {
    return columnSpecs.containsKey(columnName);
  }

  public <T> T get(String columnName, TypeCodec<T> codec) {
    // Minimal checks here: this is for internal use, so the caller should know what they're
    // doing
    if (!contains(columnName)) {
      return null;
    } else {
      int index = columnSpecs.get(columnName).index;
      return codec.decode(data.get(index), protocolVersion);
    }
  }
}

@@ -1034,24 +1034,73 @@
 ColumnSpec;%0A
+import edu.umd.cs.findbugs.annotations.Nullable;%0A
 import java.
@@ -1116,16 +1116,16 @@
 ddress;%0A
-
 import j
@@ -2021,24 +2021,36 @@
 rsion;%0A  %7D%0A%0A
+  @Nullable%0A
   public Boo
@@ -2131,32 +2131,44 @@
 .BOOLEAN);%0A  %7D%0A%0A
+  @Nullable%0A
   public Integer
@@ -2383,24 +2383,36 @@
 RCHAR;%0A  %7D%0A%0A
+  @Nullable%0A
   public Str
@@ -2488,32 +2488,44 @@
 ecs.TEXT);%0A  %7D%0A%0A
+  @Nullable%0A
   public UUID ge
@@ -2593,32 +2593,44 @@
 ecs.UUID);%0A  %7D%0A%0A
+  @Nullable%0A
   public ByteBuf
@@ -2710,32 +2710,44 @@
 ecs.BLOB);%0A  %7D%0A%0A
+  @Nullable%0A
   public InetAdd
@@ -2829,32 +2829,44 @@
 ecs.INET);%0A  %7D%0A%0A
+  @Nullable%0A
   public List%3CSt
@@ -2947,32 +2947,44 @@
 _OF_TEXT);%0A  %7D%0A%0A
+  @Nullable%0A
   public Set%3CStr
@@ -3062,32 +3062,44 @@
 _OF_TEXT);%0A  %7D%0A%0A
+  @Nullable%0A
   public Map%3CStr
@@ -3305,32 +3305,44 @@
 lumnName);%0A  %7D%0A%0A
+  @Nullable%0A
   public %3CT%3E T g

Annotate AdminRow with nullability annotations