<commit_before>//////////////////////////////////////////////////////////////////////////////
// 
//                    Copyright 2019, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases;

import static org.cornutum.tcases.util.CollectionUtils.toStream;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;

/**
 * Base class for building {@link Annotated} instances.
 */
public abstract class AnnotatedBuilder<T extends AnnotatedBuilder<T>>
  {
  /**
   * Returns the {@link Annotated} instance for this builder.
   */
  protected abstract Annotated getAnnotated();

  /**
   * Adds an annotation.
   */
  @SuppressWarnings("unchecked")
  public T has( String name, Object value)
    {
    getAnnotated().setAnnotation( name, Objects.toString( value, null));
    return (T) this;
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Object... values)
    {
    return has( name, Arrays.stream( values));
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Iterable<?> values)
    {
    return has( name, toStream( values));
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Stream<?> values)
    {
    return has( name, values.map( value -> String.valueOf( value)).collect( joining( ",")));
    }

  /**
   * Adds an annotation if the given value is non-null
   */
  public T hasIf( String name, Object value)
    {
    return hasIf( name, Optional.ofNullable( value));
    }

  /**
   * Adds an annotation if the given value is defined
   */
  @SuppressWarnings("unchecked")
  public T hasIf( String name, Optional<?> value)<commit_msg>AnnotatedBuilder.hasIf: No annotation defined if value stream is empty<commit_after>//////////////////////////////////////////////////////////////////////////////
// 
//                    Copyright 2019, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases;

import static org.cornutum.tcases.util.CollectionUtils.toStream;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Base class for building {@link Annotated} instances.
 */
public abstract class AnnotatedBuilder<T extends AnnotatedBuilder<T>>
  {
  /**
   * Returns the {@link Annotated} instance for this builder.
   */
  protected abstract Annotated getAnnotated();

  /**
   * Adds an annotation.
   */
  @SuppressWarnings("unchecked")
  public T has( String name, Object value)
    {
    getAnnotated().setAnnotation( name, Objects.toString( value, null));
    return (T) this;
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Object... values)
    {
    return has( name, Arrays.stream( values));
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Iterable<?> values)
    {
    return has( name, toStream( values));
    }

  /**
   * Adds an annotation using a comma-separated list of the string representations of the given values.
   */
  public T has( String name, Stream<?> values)
    {
    return
      hasIf(
        name,

        values
        .map( value -> String.valueOf( value))
        .reduce( (list,value) -> list + "," + value));
    }

  /**
   * Adds an annotation if the given value is non-null
   */
  public T hasIf( String name, Object value)
    {
    return hasIf( name, Optional.ofNullable( value));
    }

  /**
   * Adds an annotation if the given value is defined
   */
  @SuppressWarnings("unchecked")
  public T hasIf( String name, Optional<?> value)
{'fraction_non_alphanumeric': 0.1774614472123369, 'fraction_numerical': 0.0018979833926453143, 'mean_word_length': 2.113737075332349, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1694160', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1131, 'n_words': 384}