<commit_before>/*
 * Copyright (C) 2014 University of Washington
 *
 * 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 org.opendatakit.common.android.utilities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.database.Cursor;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class ODKCursorUtils {

  private static final String t = "ODKCursorUtils";

  /**
   * values that can be returned from getTableHealth()
   */
  public static final int TABLE_HEALTH_IS_CLEAN = 0;
  public static final int TABLE_HEALTH_HAS_CONFLICTS = 1;
  public static final int TABLE_HEALTH_HAS_CHECKPOINTS = 2;
  public static final int TABLE_HEALTH_HAS_CHECKPOINTS_AND_CONFLICTS = 3;

  public static final String DEFAULT_LOCALE = "default";
  public static final String DEFAULT_CREATOR = "anonymous";

  /**
   * Return the data stored in the cursor at the given index and given position
   * (ie the given row which the cursor is currently on) as null OR a String.
   * <p>
   * NB: Currently only checks for Strings, long, int, and double.
   *
   * @param c
   * @param i
   * @return
   */
  @SuppressLint("NewApi")
  public static String getIndexAsString(Cursor c, int i) {
    // If you add additional return types here be sure to modify the javadoc.
    if (i == -1)
      return null;
    if (c.isNull(i)) {
      return null;
    }
    switch (c.getType(i)) {
    case Cursor.FIELD_TYPE_STRING:
      return c.getString(i);
    case Cursor.FIELD_TYPE_FLOAT:
      return Double.toString(c.getDouble(i));
    case Cursor.FIELD_TYPE_INTEGER:
      return Long.toString(c.getLong(i));
    case Cursor.FIELD_TYPE_NULL:
      return c.getString(i);
    default:
    case Cursor.FIELD_TYPE_BLOB:
      throw new IllegalStateException("Unexpected data type in SQLite table");
    }
  }

  /**
   * Retrieve the data type of the [i] field in the Cursor.
   * 
   * @param c
   * @param i
   * @return<commit_msg>Attempt to fix SEGV in long-to-string conversion<commit_after>/*
 * Copyright (C) 2014 University of Washington
 *
 * 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 org.opendatakit.common.android.utilities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.database.Cursor;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class ODKCursorUtils {

  private static final String t = "ODKCursorUtils";

  /**
   * values that can be returned from getTableHealth()
   */
  public static final int TABLE_HEALTH_IS_CLEAN = 0;
  public static final int TABLE_HEALTH_HAS_CONFLICTS = 1;
  public static final int TABLE_HEALTH_HAS_CHECKPOINTS = 2;
  public static final int TABLE_HEALTH_HAS_CHECKPOINTS_AND_CONFLICTS = 3;

  public static final String DEFAULT_LOCALE = "default";
  public static final String DEFAULT_CREATOR = "anonymous";

  /**
   * Return the data stored in the cursor at the given index and given position
   * (ie the given row which the cursor is currently on) as null OR a String.
   * <p>
   * NB: Currently only checks for Strings, long, int, and double.
   *
   * @param c
   * @param i
   * @return
   */
  @SuppressLint("NewApi")
  public static String getIndexAsString(Cursor c, int i) {
    // If you add additional return types here be sure to modify the javadoc.
    if (i == -1)
      return null;
    if (c.isNull(i)) {
      return null;
    }
    switch (c.getType(i)) {
    case Cursor.FIELD_TYPE_STRING:
      return c.getString(i);
    case Cursor.FIELD_TYPE_FLOAT: {
       // the static version of this seems to have problems...
       Double d = c.getDouble(i);
       String v = d.toString();
       return v;
    }
    case Cursor.FIELD_TYPE_INTEGER: {
       // the static version of this seems to have problems...
       Long l = c.getLong(i);
       String v = l.toString();
       return v;
    }
    case Cursor.FIELD_TYPE_NULL:
      return c.getString(i);
    default:
    case Cursor.FIELD_TYPE_BLOB:
      throw new IllegalStateException("Unexpected data type in SQLite table");
    }
  }

  /**
   * Retrieve the data type of the [i] field in the Cursor.
   * 
   * @param c
   * @param i
   * @return
{'fraction_non_alphanumeric': 0.0849249026515854, 'fraction_numerical': 0.0048210643426664194, 'mean_word_length': 3.3117505995203835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2375766', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1581, 'n_words': 581}