<commit_before>package net.ontopia.presto.spi.impl.couchdb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.ontopia.presto.spi.PrestoField;
import net.ontopia.presto.spi.PrestoTopic;
import net.ontopia.presto.spi.PrestoType;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

public abstract class CouchTopic implements PrestoTopic {

  private final CouchDataProvider dataProvider;  
  private final ObjectNode data;

  protected CouchTopic(CouchDataProvider dataProvider, ObjectNode data) {
    this.dataProvider = dataProvider;
    this.data = data;    
  }

  protected CouchDataProvider getDataProvider() {
    return dataProvider;
  }
  
  public boolean equals(Object o) {
    if (o instanceof CouchTopic) {
      CouchTopic other = (CouchTopic)o;
      return other.getId().equals(getId());
    }
    return false;
  }

  public int hashCode() {
    return getId().hashCode();
  }

  protected static ObjectNode newInstanceObjectNode(CouchDataProvider dataProvider, PrestoType type) {
    ObjectNode data = dataProvider.getObjectMapper().createObjectNode();
    data.put(":type", type.getId());
    return data;
  }

  ObjectNode getData() {
    return data;
  }

  public String getId() {
    return data.get("_id").getTextValue();
  }

  public String getName() {
    JsonNode name = data.get(":name");
    return name == null ? null : name.getTextValue();
  }

  public String getTypeId() {
    return data.get(":type").getTextValue();
  }

  // json data access strategy

  protected abstract ArrayNode getFieldValue(PrestoField field);

  protected abstract void putFieldValue(PrestoField field, ArrayNode value);

  protected abstract void removeFieldValue(PrestoField field);

  // methods for retrieving the state of a couchdb document

  public Collection<Object> getValues(PrestoField field) {
    boolean isReferenceField = field.isReferenceField();
    List<Object> values = new ArrayList<Object>();
    ArrayNode fieldNode = getFieldValue(field);
    if (fieldNode != null) { 
      if (isReferenceField) {
        List<String> topicIds = new ArrayList<String>(fieldNode.size());
        for (JsonNode value : fieldNode) {
          topicIds.add(value.getTextValue());
        }
        values.addAll(dataProvider.getTopicsByIds(topicIds));
      } else {
        for (JsonNode value : fieldNode) {
          String textValue = value.getTextValue();
          values.add(textValue);          
        }
      }
    }
    return values;
  }

  // methods for updating the state of a couchdb document

  private String getValue(Object value) {
    if (value instanceof CouchTopic) {
      CouchTopic valueTopic = (CouchTopic)value;
      return valueTopic.getId();
    } else {
      return(String)value;
    }
  }

  void setValue(PrestoField field, Collection<? extends Object> values) {
    if (values.isEmpty()) {
      removeFieldValue(field);
    } else {
      ArrayNode arrayNode = dataProvider.getObjectMapper().createArrayNode();
      for (Object value : values) {
        arrayNode.add(getValue(value));
      }
      putFieldValue(field, arrayNode);<commit_msg>Non-string values will be returned using the .toString() method, which in practical terms means a json string representation.<commit_after>package net.ontopia.presto.spi.impl.couchdb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.ontopia.presto.spi.PrestoField;
import net.ontopia.presto.spi.PrestoTopic;
import net.ontopia.presto.spi.PrestoType;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

public abstract class CouchTopic implements PrestoTopic {

  private final CouchDataProvider dataProvider;  
  private final ObjectNode data;

  protected CouchTopic(CouchDataProvider dataProvider, ObjectNode data) {
    this.dataProvider = dataProvider;
    this.data = data;    
  }

  protected CouchDataProvider getDataProvider() {
    return dataProvider;
  }
  
  public boolean equals(Object o) {
    if (o instanceof CouchTopic) {
      CouchTopic other = (CouchTopic)o;
      return other.getId().equals(getId());
    }
    return false;
  }

  public int hashCode() {
    return getId().hashCode();
  }

  protected static ObjectNode newInstanceObjectNode(CouchDataProvider dataProvider, PrestoType type) {
    ObjectNode data = dataProvider.getObjectMapper().createObjectNode();
    data.put(":type", type.getId());
    return data;
  }

  ObjectNode getData() {
    return data;
  }

  public String getId() {
    return data.get("_id").getTextValue();
  }

  public String getName() {
    JsonNode name = data.get(":name");
    return name == null ? null : name.getTextValue();
  }

  public String getTypeId() {
    return data.get(":type").getTextValue();
  }

  // json data access strategy

  protected abstract ArrayNode getFieldValue(PrestoField field);

  protected abstract void putFieldValue(PrestoField field, ArrayNode value);

  protected abstract void removeFieldValue(PrestoField field);

  // methods for retrieving the state of a couchdb document

  public Collection<Object> getValues(PrestoField field) {
    boolean isReferenceField = field.isReferenceField();
    List<Object> values = new ArrayList<Object>();
    ArrayNode fieldNode = getFieldValue(field);
    if (fieldNode != null) { 
      if (isReferenceField) {
        List<String> topicIds = new ArrayList<String>(fieldNode.size());
        for (JsonNode value : fieldNode) {
          topicIds.add(value.getTextValue());
        }
        values.addAll(dataProvider.getTopicsByIds(topicIds));
      } else {
        for (JsonNode value : fieldNode) {
          if (value.isTextual()) {
              values.add(value.getTextValue());
          } else {
              values.add(value.toString());
          }
        }
      }
    }
    return values;
  }

  // methods for updating the state of a couchdb document

  private String getValue(Object value) {
    if (value instanceof CouchTopic) {
      CouchTopic valueTopic = (CouchTopic)value;
      return valueTopic.getId();
    } else {
      return(String)value;
    }
  }

  void setValue(PrestoField field, Collection<? extends Object> values) {
    if (values.isEmpty()) {
      removeFieldValue(field);
    } else {
      ArrayNode arrayNode = dataProvider.getObjectMapper().createArrayNode();
      for (Object value : values) {
        arrayNode.add(getValue(value));
      }
      putFieldValue(field, arrayNode);
{'fraction_non_alphanumeric': 0.10401891252955082, 'fraction_numerical': 0.0, 'mean_word_length': 3.4069010416666665, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7153936', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1949, 'n_words': 473}