text:
//////////////////////////////////////////////////////////////////////////////
//
//                    Copyright 2019, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases.openapi;

import static org.cornutum.tcases.openapi.SchemaExtensions.*;
import static org.cornutum.tcases.util.CollectionUtils.*;

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;

import org.apache.commons.lang3.text.WordUtils;
import org.cornutum.regexpgen.RegExpGen;
import org.cornutum.regexpgen.js.Parser;
import static org.cornutum.regexpgen.Bounds.bounded;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Defines methods for accessing an OpenAPI {@link Schema} object.
 */
public final class SchemaUtils
  {
  /**
   * Creates a new SchemaUtils instance.
   */
  private SchemaUtils()
    {
    // Static methods only
    }

  /**
   * Returns a concise description of the assertions made by the given schema.
   */
  public static String asserts( Schema<?> schema)
    {
    return
      Optional.ofNullable( schema)
      .map( s -> {
        String type = assertsType( s);
        return
          new StringBuilder()
          .append( WordUtils.capitalize( type))
          .append(
            assertsList(
              assertsMap( type, s).entrySet().stream()
              .map( entry -> String.format( "%s=%s", entry.getKey(), entry.getValue()))))
          .toString();
        })
      .orElse( null);
    }

  /**
   * Returns a description of the type of the given schema.
   */
  private static String assertsType( Schema<?> schema)
    {
    return
      schema.getType() == null?
      "any" :

      !"string".equals( schema.getType())?
      schema.getType() :

      "binary".equals( schema.getFormat())?
      "binary" :

      "byte".equals( schema.getFormat())?
      "byte" :

      "string";
    }

  /**
   * Returns a map of assertion descriptions for the given schema.
   */
  private static Map<String,String> assertsMap( String type, Schema<?> schema)
    {
    Map<String,String> asserts = new LinkedHashMap<String,String>();
    if( !(type.equals( "binary") || type.equals( "byte")))
      {
      Optional.ofNullable( schema.getEnum())
        .ifPresent( v -> {
          asserts.put(
            "enum",
            assertsList( v.stream().map( e -> Objects.toString( e, ""))));
          });
      Optional.ofNullable( schema.getAdditionalProperties())
        .ifPresent( v -> {
          asserts.put(
            "additionalProperties",
            Optional.ofNullable( additionalPropertiesSchema( schema))
            .map( SchemaUtils::asserts)
            .orElse( Objects.toString( v, "")));
          });
      Optional.ofNullable( schema.getExclusiveMaximum())
        .ifPresent( v -> asserts.put( "exclusiveMaximum", Objects.toString( v, "")));
      Optional.ofNullable( schema.getExclusiveMinimum())
        .ifPresent( v -> asserts.put( "exclusiveMinimum", Objects.toString( v, "")));
      Optional.ofNullable( schema.getFormat())
        .ifPresent( v -> asserts.put( "format", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMaxLength())
        .ifPresent( v -> asserts.put( "maxLength", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMaxProperties())
        .ifPresent( v -> asserts.put( "maxProperties", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMaximum())
        .ifPresent( v -> asserts.put( "maximum", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMinLength())
        .ifPresent( v -> asserts.put( "minLength", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMinProperties())
        .ifPresent( v -> asserts.put( "minProperties", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMinimum())
        .ifPresent( v -> asserts.put( "minimum", Objects.toString( v, "")));
      Optional.ofNullable( schema.getMultipleOf())
        .ifPresent( v -> asserts.put( "multipleOf", Objects.toString( v, "")));
      Optional.ofNullable( schema.getNullable())
        .ifPresent( v -> asserts.put( "nullable", Objects.toString( v, "")));
      Optional.ofNullable( schema.getProperties())
        .ifPresent( v -> {
          asserts.put(
            "properties",
            assertsList(
              v.entrySet().stream()
              .map( e -> String.format( "%s=%s", e.getKey(), asserts( e.getValue())))));
          });
      Optional.ofNullable( schema.getRequired())
        .ifPresent( v -> {
          asserts.put(
            "required",
            assertsList( v.stream().map( e -> Objects.toString( e, ""))));
          });
      Optional.ofNullable( schema.getReadOnly())
        .ifPresent( v -> asserts.put( "readOnly", Objects.toString( v, "")));
      Optional.ofNullable( schema.getWriteOnly())
        .ifPresent( v -> asserts.put( "writeOnly", Objects.toString( v, "")));

      Optional.ofNullable( schema.getExtensions())
        .ifPresent( v -> {
          v.entrySet().stream().forEach( e -> asserts.put( e.getKey(), Objects.toString( e.getValue(), "")));
          });

      Optional.ofNullable( asArraySchema( schema))
        .ifPresent( array -> {
          Optional.ofNullable( array.getItems())
            .ifPresent( v -> asserts.put( "items", asserts( v)));
          Optional.ofNullable( array.getMaxItems())
            .ifPresent( v -> asserts.put( "maxItems", Objects.toString( v, "")));
          Optional.ofNullable( array.getMinItems())
            .ifPresent( v -> asserts.put( "minItems", Objects.toString( v, "")));
          Optional.ofNullable( array.getUniqueItems())
            .ifPresent( v -> asserts.put( "uniqueItems", Objects.toString( v, "")));
          });

      Optional.ofNullable( schema.getNot())
        .ifPresent( not -> asserts.put( "not", asserts( not)));

      Optional.ofNullable( asComposedSchema( schema))
        .ifPresent( composed -> {
          Optional.ofNullable( composed.getAllOf())
            .ifPresent( v -> {
              Optional.of( v)
                .filter( schemas -> !schemas.isEmpty())
                .ifPresent( schemas -> asserts.put( "allOf", assertsList( schemas.stream().map( SchemaUtils::asserts))));
              });
          Optional.ofNullable( composed.getAnyOf())
            .ifPresent( v -> {
              Optional.of( v)
                .filter( schemas -> !schemas.isEmpty())
                .ifPresent( schemas -> asserts.put( "anyOf", assertsList( schemas.stream().map( SchemaUtils::asserts))));
              });
          Optional.ofNullable( composed.getOneOf())
            .ifPresent( v -> {
              Optional.of( v)
                .filter( schemas -> !schemas.isEmpty())
                .ifPresent( schemas -> asserts.put( "oneOf", assertsList( schemas.stream().map( SchemaUtils::asserts))));
              });
          });
      }

    return asserts;
    }

  /**
   * Returns a description of the given list of assertions.
   */
  public static String assertsList( Stream<String> assertions)
    {
    return
      new StringBuilder()
      .append( '[')
      .append( assertions.collect( joining( ", ")))
      .append( ']')
      .toString();
      }

  /**
   * If necessary, updates the type of the given schema based its properties.
   * If the type defined for this schema is valid and consistent, returns the updated schema.
   * Otherwise, throws an exception.
   */
  public static Schema<?> resolveSchemaType( Schema<?> schema)
    {
    String declaredType = schema.getType();

    List<String[]> impliedTypes =
      Stream.of(
        impliedType( "array", schema, "items", "maxItems", "minItems", "uniqueItems"),
        impliedType( "number", schema, "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "multipleOf"),
        impliedType( "object", schema, "maxProperties", "minProperties", "required", "properties", "additionalProperties"),
        impliedType( "string", schema, "maxLength", "minLength", "pattern"))
      .filter( Objects::nonNull)
      .collect( toList());

    if( impliedTypes.size() == 1)
      {
      String impliedType = impliedTypes.get(0)[0];
      if( declaredType == null)
        {
        schema.setType( impliedType);
        }
      else if( !(impliedType.equals( declaredType) || (impliedType.equals( "number") && declaredType.equals( "integer"))))
        {
        throw
          new IllegalStateException(
            String.format(
              "Schema declares type=%s but defines property=%s which implies type=%s",
              declaredType,
              impliedTypes.get(0)[1],
              impliedType));
        }
      }
    else if( impliedTypes.size() > 1)
      {
      throw
        new IllegalStateException(
          String.format(
            "Ambiguous schema type -- defines properties=%s which implies types=%s",
            impliedTypes.stream().map( typeProperty -> typeProperty[1]).collect( joining( ", ", "[", "]")),
            impliedTypes.stream().map( typeProperty -> typeProperty[0]).collect( joining( ", ", "[", "]"))));
      }

    return schema;
    }

  /**
   * If the given schema defines a value for any of the given properties, returns an array of the form [type,property].
   * Otherwise, returns null.
   */
  @SafeVarargs
  private static String[] impliedType( String type, Schema<?> schema, String... properties)
    {
    return
      Stream.of( properties)
      .filter( property -> hasProperty( schema, property))
      .findFirst()
      .map( property -> new String[]{ type, property})
      .orElse( null);
    }

  /**
   * Returns true if the given schema defines a value for the given property.
   */
  private static boolean hasProperty( Schema<?> schema, String property)
    {
    try
      {
      return
        schema.getClass()
        .getMethod( String.format( "get%s", WordUtils.capitalize( property)))
        .invoke( schema)
        != null;
      }
    catch( Exception e)
      {
      throw new RuntimeException( String.format( "Can't get property=%s for schema=%s", property, schema), e);
      }
    }

  /**
   * Returns true if the given schema type is supported by OpenAPI.
   */
  public static boolean isSchemaType( String type)
    {
    return type == null || SCHEMA_TYPES.contains( type);
    }

  /**
   * If the given schema is a ComposedSchema instance, returns the casting result.
   * Otherwise, returns null.
   */
  public static ComposedSchema asComposedSchema( Schema<?> schema)
    {
    return
      schema instanceof ComposedSchema
      ? (ComposedSchema) schema
      : null;
    }

  /**
   * If the given schema is an ArraySchema instance, returns the casting result.
   * Otherwise, returns null.
   */
  public static ArraySchema asArraySchema( Schema<?> schema)
    {
    return
      schema instanceof ArraySchema
      ? (ArraySchema) schema
      : null;
    }

  /**
   * If the given schema asserts a schema for additional properties, returns
   * the additional properties schema. Otherwise, returns null.
   */
  public static Schema<?> additionalPropertiesSchema( Schema<?> schema)
    {
    return
      schema.getAdditionalProperties() instanceof Schema
      ? (Schema<?>) schema.getAdditionalProperties()
      : null;
    }

  /**
   * Returns the given schema designated as an object property schema.
   */
  public static Schema<?> toPropertySchema( Schema<?> schema)
    {
    setPropertySchema( schema, true);
    return schema;
    }

  /**
   * Returns true if this is a basic schema without any boolean combinations of subschemas.
   */
  public static boolean isLeafSchema( Schema<?> schema)
    {
    return asComposedSchema( schema) == null && schema.getNot() == null;
    }

  /**
   * Returns true if this schema asserts any boolean combinations of subschemas.
   */
  public static boolean isNotLeafSchema( Schema<?> schema)
    {
    return !isLeafSchema( schema);
    }

  /**
   * Returns true if this schema contains no leaf assertions.
   */
  @SuppressWarnings("unchecked")
  public static boolean isLeafEmpty( Schema<?> schema)
    {
    return
      isEmpty(
        withExtensions(
          copySchema( schema)
          .type( null)
          .properties( Optional.ofNullable( schema.getProperties()).filter( p ->!p.isEmpty()).orElse( null))));
    }

  /**
   * Returns null if no example is defined for the given schema. Otherwise, returns
   * Optional.empty() if the schema example value is null.
   * Otherwise, returns the schema example value.
   */
  public static Optional<Object> schemaExample( Schema<?> schema)
    {
    return
      Optional.ofNullable( schema).map( Schema::getExampleSetFlag).orElse( false)
      ? Optional.ofNullable( schema.getExample())
      : null;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  public static Schema<?> combineSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema<?> combined;

    if( base == null)
      {
      combined = additional;
      }
    else if( additional == null)
      {
      combined = base;
      }
    else
      {
      // A null type indicates an empty schema, which can be combined with any type.
      String baseType = Optional.ofNullable( base.getType()).orElse( additional.getType());
      String additionalType = Optional.ofNullable( additional.getType()).orElse( base.getType());

      if( !Objects.equals( baseType, additionalType))
        {
        throw inconsistentAssertions( "type: %s", additionalType, baseType);
        }

      combined =
        "object".equals( baseType)?
        combineObjectSchemas( context, base, additional) :

        "string".equals( baseType)?
        combineStringSchemas( context, base, additional) :

        "integer".equals( baseType)?
        combineIntegerSchemas( context, base, additional) :

        "boolean".equals( baseType)?
        combineBooleanSchemas( context, base, additional) :

        "array".equals( baseType)?
        combineArraySchemas( context, base, additional) :

        "number".equals( baseType)?
        combineNumberSchemas( context, base, additional) :

        combineGenericSchemas( context, base, additional);
      }

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  private static Schema<?> combineObjectSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema combined = combineGenericSchemas( context, base, additional);

    // Combine additionalProperties
    Schema<?> baseExtraSchema = additionalPropertiesSchema( base);
    Schema<?> additionalExtraSchema = additionalPropertiesSchema( additional);
    if( baseExtraSchema != null)
      {
      combined.setAdditionalProperties(
        additionalExtraSchema != null?
        (Object) context.resultFor( "additionalProperties", () -> combineSchemas( context, baseExtraSchema, additionalExtraSchema)) :

        !Boolean.FALSE.equals( additional.getAdditionalProperties())?
        baseExtraSchema :

        Boolean.FALSE);
      }
    else if( additionalExtraSchema != null)
      {
      combined.setAdditionalProperties(
        !Boolean.FALSE.equals( base.getAdditionalProperties())?
        additionalExtraSchema :

        Boolean.FALSE);
      }
    else
      {
      combined.setAdditionalProperties(
        base.getAdditionalProperties() == null?
        additional.getAdditionalProperties() :

        additional.getAdditionalProperties() == null?
        base.getAdditionalProperties() :

        combineAssertions(
          "additionalProperties: %s",
          (Boolean) base.getAdditionalProperties(),
          (Boolean) additional.getAdditionalProperties()));
      }

    // Combine maxProperties
    combined.setMaxProperties(
      base.getMaxProperties() == null?
      additional.getMaxProperties() :

      additional.getMaxProperties() == null?
      base.getMaxProperties() :

      base.getMaxProperties().compareTo( additional.getMaxProperties()) < 0?
      base.getMaxProperties() :

      additional.getMaxProperties());

    // Combine minProperties
    combined.setMinProperties(
      base.getMinProperties() == null?
      additional.getMinProperties() :

      additional.getMinProperties() == null?
      base.getMinProperties() :

      base.getMinProperties().compareTo( additional.getMinProperties()) > 0?
      base.getMinProperties() :

      additional.getMinProperties());

    // Combine required
    List<String> combinedRequired =
      Stream.concat(
        Optional.ofNullable( base.getRequired()).map( required -> required.stream()).orElse( Stream.empty()),
        Optional.ofNullable( additional.getRequired()).map( required -> required.stream()).orElse( Stream.empty()))
      .collect( toOrderedSet())
      .stream().collect( toList());
    combined.setRequired( combinedRequired);

    // Combine not required
    setNotRequired( combined, getNotRequired( base));
    addNotRequired( combined, getNotRequired( additional));

    Optional.ofNullable( getNotRequired( combined))
      .flatMap( nr -> combinedRequired.stream().filter( p -> nr.contains( p)).findFirst())
      .ifPresent( p -> {
        throw inconsistentNotAssertion( "required: [%s]", p);
        });

    // Combine properties
    Map<String,Schema> basePropertyDefs = Optional.ofNullable( base.getProperties()).orElse( emptyMap());
    Map<String,Schema> additionalPropertyDefs = Optional.ofNullable( additional.getProperties()).orElse( emptyMap());
    Map<String,Schema> combinedPropertyDefs =
      context.resultFor(
        "properties",
        () ->
        Stream.concat( basePropertyDefs.keySet().stream(), additionalPropertyDefs.keySet().stream())
        .collect( toOrderedSet())
        .stream()
        .collect(
          () -> new LinkedHashMap<String,Schema>(),
          (map, p) -> map.put( p, context.resultFor( p, () -> combineSchemas( context, basePropertyDefs.get( p), additionalPropertyDefs.get( p)))),
          (map, other) -> map.putAll( other)));
    combined.setProperties( combinedPropertyDefs);

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings("rawtypes")
  private static Schema<?> combineStringSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema combined = combineGenericSchemas( context, base, additional);

    // Combine format
    if( base.getFormat() != null && additional.getFormat() != null && !base.getFormat().equals( additional.getFormat()))
      {
      throw inconsistentAssertions( "format: %s", additional.getFormat(), base.getFormat());
      }

    // Combine maxLength
    combined.setMaxLength(
      base.getMaxLength() == null?
      additional.getMaxLength() :

      additional.getMaxLength() == null?
      base.getMaxLength() :

      base.getMaxLength().compareTo( additional.getMaxLength()) < 0?
      base.getMaxLength() :

      additional.getMaxLength());

    // Combine minLength
    combined.setMinLength(
      base.getMinLength() == null?
      additional.getMinLength() :

      additional.getMinLength() == null?
      base.getMinLength() :

      base.getMinLength().compareTo( additional.getMinLength()) > 0?
      base.getMinLength() :

      additional.getMinLength());

    // Combine pattern
    setPatterns( combined, getPatterns( base));
    addPatterns( combined, getPatterns( additional));

    // Reconcile length and pattern constraints
    Optional.ofNullable( combined.getMaxLength())
      .ifPresent( maxLength -> {
        Optional.ofNullable( maxLengthPattern( combined))
          .ifPresent( generator -> {
            Integer patternMax = bounded( generator.getMaxLength()).orElse( null);
            if( patternMax != null && patternMax.compareTo( maxLength) < 0)
              {
              combined.setMaxLength( patternMax);
              }
            if( combined.getMinLength() != null && combined.getMaxLength() < combined.getMinLength())
              {
              throw inconsistentAssertions( "minLength: %s", combined.getMinLength(), "pattern: %s", generator.getOptions().getRegExp());
              }
            });
        });
    Optional.ofNullable( combined.getMinLength())
      .ifPresent( minLength -> {
        Optional.ofNullable( minLengthPattern( combined))
          .ifPresent( generator -> {
            Integer patternMin = Optional.of( generator.getMinLength()).filter( min -> min > 0).orElse( null);
            if( patternMin != null && patternMin.compareTo( combined.getMinLength()) > 0)
              {
              combined.setMinLength( patternMin);
              }
            if( combined.getMaxLength() != null && combined.getMinLength() > combined.getMaxLength())
              {
              throw inconsistentAssertions( "maxLength: %s", combined.getMaxLength(), "pattern: %s", generator.getOptions().getRegExp());
              }
            });
        });

    // Combine not patterns
    setNotPatterns( combined, getNotPatterns( base));
    addNotPatterns( combined, getNotPatterns( additional));

    Optional.ofNullable( getPatterns( combined))
      .flatMap( ps -> Optional.ofNullable( getNotPatterns( combined)).flatMap( nps -> ps.stream().filter( p -> nps.contains( p)).findFirst()))
      .ifPresent( p -> {
        throw inconsistentNotAssertion( "pattern: '%s'", p);
        });

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings("rawtypes")
  private static Schema<?> combineIntegerSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema combined = combineNumericSchemas( context, base, additional);

    // Combine format
    combined.setFormat(
      base.getFormat() == null?
      additional.getFormat() :

      additional.getFormat() == null?
      base.getFormat() :

      base.getFormat().equals( "int32")?
      base.getFormat() :

      additional.getFormat());

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  private static Schema<?> combineBooleanSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    return combineGenericSchemas( context, base, additional);
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  private static Schema<?> combineArraySchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    ArraySchema combined = combineGenericSchemas( context, new ArraySchema(), base, additional);

    // Combine maxItems
    combined.setMaxItems(
      base.getMaxItems() == null?
      additional.getMaxItems() :

      additional.getMaxItems() == null?
      base.getMaxItems() :

      base.getMaxItems().compareTo( additional.getMaxItems()) < 0?
      base.getMaxItems() :

      additional.getMaxItems());

    // Combine minItems
    combined.setMinItems(
      base.getMinItems() == null?
      additional.getMinItems() :

      additional.getMinItems() == null?
      base.getMinItems() :

      base.getMinItems().compareTo( additional.getMinItems()) > 0?
      base.getMinItems() :

      additional.getMinItems());

    // Combine uniqueItems
    combined.setUniqueItems(
      base.getUniqueItems() == null?
      additional.getUniqueItems() :

      additional.getUniqueItems() == null?
      base.getUniqueItems() :

      combineAssertions( "uniqueItems: %s", base.getUniqueItems(), additional.getUniqueItems()));

    // Combine items
    Schema<?> baseItems = Optional.ofNullable( asArraySchema( base)).map( ArraySchema::getItems).orElse( null);
    Schema<?> additionalItems = Optional.ofNullable( asArraySchema( additional)).map( ArraySchema::getItems).orElse( null);
    combined.setItems( context.resultFor( "items", () -> combineSchemas( context, baseItems, additionalItems)));

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings("rawtypes")
  private static Schema<?> combineNumberSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema combined = combineNumericSchemas( context, base, additional);

    // Combine format
    combined.setFormat(
      base.getFormat() == null?
      additional.getFormat() :

      additional.getFormat() == null?
      base.getFormat() :

      base.getFormat().equals( "float")?
      base.getFormat() :

      additional.getFormat());

    return combined;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings("rawtypes")
  private static Schema<?> combineNumericSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
    {
    Schema combined = combineGenericSchemas( context, base, additional);

    // Combine maximum
    combined.setMaximum(
      base.getMaximum() == null?
      additional.getMaximum() :

      additional.getMaximum() == null?
      base.getMaximum() :

      base.getMaximum().compareTo( additional.getMaximum()) < 0?
      base.getMaximum() :

      additional.getMaximum());

    // Combine minimum
    combined.setMinimum(
      base.getMinimum() == null?
      additional.getMinimum() :

      additional.getMinimum() == null?
      base.getMinimum() :

      base.getMinimum().compareTo( additional.getMinimum()) > 0?
      base.getMinimum() :

      additional.getMinimum());

    // Combine exclusiveMaximum
    combined.setExclusiveMaximum(
      base.getExclusiveMaximum() == null?
      additional.getExclusiveMaximum() :

      additional.getExclusiveMaximum() == null?
      base.getExclusiveMaximum() :

      combineAssertions( "exclusiveMaximum: %s", base.getExclusiveMaximum(), additional.getExclusiveMaximum()));

    // Combine exclusiveMinimum
    combined.setExclusiveMinimum(
      base.getExclusiveMinimum() == null?
      additional.getExclusiveMinimum() :

      additional.getExclusiveMinimum() == null?
      base.getExclusiveMinimum() :

      combineAssertions( "exclusiveMinimum: %s", base.getExclusiveMinimum(), additional.getExclusiveMinimum()));

    // Combine multipleOf
    combined.setMultipleOf(
      base.getMultipleOf() == null?
      additional.getMultipleOf() :

      additional.getMultipleOf() == null?
      base.getMultipleOf() :

      combineMultipleOf( base.getMultipleOf(), additional.getMultipleOf()));

    // Combine not multipleOfs
    setNotMultipleOfs( combined, getNotMultipleOfs( base));
    addNotMultipleOfs( combined, getNotMultipleOfs( additional));

    Optional.ofNullable( combined.getMultipleOf())
      .flatMap( m -> Optional.ofNullable( getNotMultipleOfs( combined)).flatMap( nms -> nms.stream().filter( nm -> isMultipleOf( m, nm)).findFirst()))
      .ifPresent( nm -> {
        throw inconsistentNotAssertion( "multipleOf: %s", combined.getMultipleOf(), nm);
        });

    return combined;
    }

  /**
   * If the given multipleOf factors are congruent, returns the maximum value.
   * Otherwise, throws an exception to report the inconsistent values.
   */
  private static BigDecimal combineMultipleOf( BigDecimal base, BigDecimal additional)
    {
    BigDecimal max = base.compareTo( additional) > 0? base : additional;
    BigDecimal min = base.compareTo( additional) < 0? base : additional;

    if( !isMultipleOf( max, min))
      {
      throw inconsistentAssertions( "multipleOf: %s", additional, base);
      }

    return max;
    }

  /**
   * Returns true if the given value is a multiple of the given factor.
   */
  public static boolean isMultipleOf( BigDecimal value, BigDecimal factor)
    {
    return
      value.compareTo( BigDecimal.ZERO) == 0
      ||
      value.remainder( factor).compareTo( BigDecimal.ZERO) == 0;
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings({ "rawtypes" })
  private static Schema combineGenericSchemas( OpenApiContext context, Schema base, Schema additional)
    {
    return combineGenericSchemas( context, emptySchema(), base, additional);
    }

  /**
   * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
   * Throws an exception if a consistent combination is not possible.
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  private static <T extends Schema> T combineGenericSchemas( OpenApiContext context, T combined, Schema base, Schema additional)
    {
    // Combine type
    String baseType = base.getType();
    String additionalType = additional.getType();

    if( baseType == null)
      {
      Optional.ofNullable( getNotTypes( base))
        .filter( notTypes -> notTypes.contains( additionalType))
        .ifPresent( notTypes -> {
          throw inconsistentNotAssertion( "type: %s", additionalType);
          });
      }
    if( additionalType == null)
      {
      Optional.ofNullable( getNotTypes( additional))
        .filter( notTypes -> notTypes.contains( baseType))
        .ifPresent( notTypes -> {
          throw inconsistentNotAssertion( "type: %s", baseType);
          });
      }

    combined.setType(
      Optional.ofNullable( baseType)
      .orElse( additionalType));

    if( combined.getType() == null)
      {
      setNotTypes( combined, getNotTypes( base));
      addNotTypes( combined, getNotTypes( additional));
      }

    // Combine default
    combined.setDefault(
      Optional.ofNullable( additional.getDefault())
      .orElse( base.getDefault()));

    // Combine format
    combined.setFormat(
      base.getFormat() == null?
      additional.getFormat() :

      additional.getFormat() == null?
      base.getFormat() :

      additional.getFormat());

    // Combine enum
    combined.setEnum(
      base.getEnum() == null?
      additional.getEnum() :

      additional.getEnum() == null?
      base.getEnum() :

      Optional.of( base.getEnum())
      .map( enums -> {
          Set<Object> baseEnums = ((List<Object>)enums).stream().collect( toOrderedSet());
          baseEnums.retainAll( additional.getEnum());
          return baseEnums;
        })
      .filter( enums -> !enums.isEmpty())
      .map( enums -> enums.stream().collect( toList()))
      .orElseThrow( () -> inconsistentAssertions( "enum: %s", additional.getEnum(), base.getEnum())));

    // Combine not enums
    setNotEnums( combined, getNotEnums( base));
    addNotEnums( combined, getNotEnums( additional));

    Optional.ofNullable( combined.getEnum())
      .flatMap( es -> Optional.ofNullable( getNotEnums( combined)).flatMap( nes -> es.stream().filter( e -> nes.contains( e)).findFirst()))
      .ifPresent( e -> {
        throw inconsistentNotAssertion( "enum: %s", e);
        });

    // Combine nullable
    combined.setNullable(
      base.getNullable() == null?
      additional.getNullable() :

      additional.getNullable() == null?
      base.getNullable() :

      combineAssertions( "nullable: %s", base.getNullable(), additional.getNullable()));

    // Combine readOnly
    setPropertySchema( combined, isPropertySchema( base) || isPropertySchema( additional));
    if( isPropertySchema( combined))
      {
      combined.setReadOnly(
        base.getReadOnly() == null?
        additional.getReadOnly() :

        additional.getReadOnly() == null?
        base.getReadOnly() :

        combineAssertions( "readOnly: %s", base.getReadOnly(), additional.getReadOnly()));

      // Combine writeOnly
      combined.setWriteOnly(
        base.getWriteOnly() == null?
        additional.getWriteOnly() :

        additional.getWriteOnly() == null?
        base.getWriteOnly() :

        combineAssertions( "writeOnly: %s", base.getWriteOnly(), additional.getWriteOnly()));

      if( Boolean.TRUE.equals( combined.getReadOnly()) && Boolean.TRUE.equals( combined.getWriteOnly()))
        {
        String baseProp = Boolean.TRUE.equals( base.getReadOnly())? "readOnly" : "writeOnly";
        String additionalProp = Boolean.TRUE.equals( additional.getReadOnly())? "readOnly" : "writeOnly";
        throw inconsistentAssertions( "%s: true", additionalProp, baseProp);
        }
      }

    return combined;
    }

  /**
   * Returns the combination of the given boolean assertions.
   */
  private static Boolean combineAssertions( String assertionFormat, boolean base, boolean additional)
    {
    if( base != additional)
      {
      throw inconsistentAssertions( assertionFormat, additional, base);
      }

    return base;
    }

  /**
   * Returns a {@link RegExpGen} the required pattern with the smallest maximum length.
   */
  private static RegExpGen maxLengthPattern( Schema<?> stringSchema)
    {
    return
      patternGenerators( stringSchema)
      .min( Comparator.comparing( RegExpGen::getMaxLength))
      .orElse( null);
    }

  /**
   * Returns a {@link RegExpGen} the required pattern with the largest minimum length.
   */
  private static RegExpGen minLengthPattern( Schema<?> stringSchema)
    {
    return
      patternGenerators( stringSchema)
      .max( Comparator.comparing( RegExpGen::getMinLength))
      .orElse( null);
    }

  /**
   * Returns a {@link RegExpGen} for each matching pattern required.
   */
  public static Stream<RegExpGen> patternGenerators( Schema<?> stringSchema)
    {
    return
      getPatterns( stringSchema).stream()
      .map( pattern -> {
        try
          {
          return Parser.parseRegExp( pattern);
          }
        catch( Exception e)
          {
          return null;
          }
        })
      .filter( Objects::nonNull);
    }

  /**
   * Returns an exception reporting inconsistent assertions.
   */
  private static <T> IllegalStateException inconsistentAssertions( String assertFormat, T value, String otherFormat, T otherValue)
    {
    return
      new IllegalStateException(
        String.format(
          "Can't combine schema requiring {" + assertFormat + "} with schema requiring {" + otherFormat + "}",
          value,
          otherValue));
    }

  /**
   * Returns an exception reporting inconsistent assertions.
   */
  private static <T> IllegalStateException inconsistentAssertions( String assertFormat, T value, T otherValue)
    {
    return inconsistentAssertions( assertFormat, value, assertFormat, otherValue);
    }

  /**
   * Returns an exception reporting inconsistent assertions.
   */
  private static <T> IllegalStateException inconsistentNotAssertion( String assertFormat, T value, T otherValue)
    {
    return inconsistentAssertions( assertFormat, value, String.format( "not: {%s}", assertFormat), otherValue);
    }

  /**
   * Returns an exception reporting inconsistent assertions.
   */
  private static <T> IllegalStateException inconsistentNotAssertion( String assertFormat, T value)
    {
    return inconsistentNotAssertion( assertFormat, value, value);
    }

  /**
   * Returns a new copy of the given schema.
   */
  public static Schema<?> copySchema( Schema<?> schema)
    {
    return combineSchemas( DEFAULT_CONTEXT, schema, EMPTY_SCHEMA);
    }

  /**
   * Returns true if the given schemas are equal, comparing values for only the specified extensions.
   */
  public static boolean equalsExtended( Schema<?> schema1, Schema<?> schema2, String... extensions)
    {
    Schema<?> compare1 = withExtensions( copySchema( schema1), extensions);
    Schema<?> compare2 = withExtensions( copySchema( schema2), extensions);
    return compare1.equals( compare2);
    }

  /**
   * Returns the given schema after removing all but the given extensions.
   */
  private static Schema<?> withExtensions( Schema<?> schema, String... extensions)
    {
    Map<String,Object> extAll = schema.getExtensions();
    if( extAll!= null)
      {
      schema.setExtensions( null);
      for( String ext : extensions)
        {
        if( extAll.containsKey( ext))
          {
          schema.addExtension( ext, extAll.get( ext));
          }
        }
      }

    return schema;
    }

  /**
   * Returns a new empty schema.
   */
  public static Schema<?> emptySchema()
    {
    return new Schema<Object>();
    }

  /**
   * Returns true if the given schema contains no assertions.
   */
  public static boolean isEmpty( Schema<?> schema)
    {
    return Optional.ofNullable( schema).map( s -> s.equals( EMPTY_SCHEMA)).orElse( true);
    }

  /**
   * Returns true if the given schema is the {@link #FALSE_SCHEMA false schema}.
   */
  public static boolean isFalse( Schema<?> schema)
    {
    return equalsExtended( schema, FALSE_SCHEMA, EXT_NOT_TYPES);
    }

  /**
   * Returns a new schema that will invalidate any instance.
   */
  private static Schema<?> falseSchema()
    {
    Schema<?> falsifier = emptySchema();
    setNotTypes( falsifier, SCHEMA_TYPES);
    falsifier.setNullable( false);

    return falsifier;
    }

  public static final Set<String> SCHEMA_TYPES =
    Arrays.asList( "array", "boolean", "integer", "number", "object", "string")
    .stream().collect( toSet());

  /**
   * A schema that contains no assertions.
   */
  public static final Schema<?> EMPTY_SCHEMA = emptySchema();

  /**
   * A schema that will invalidate any instance.
   */
  public static final Schema<?> FALSE_SCHEMA = falseSchema();

  private static final OpenApiContext DEFAULT_CONTEXT = new OpenApiContext();
  }


text:
package main

import (
	"flag"
	"time"

	log "github.com/sirupsen/logrus"

	"github.com/grid-x/backupd/pkg/backup"
	"github.com/grid-x/backupd/pkg/config"
	"github.com/grid-x/backupd/pkg/datastore"
	"github.com/grid-x/backupd/pkg/storage"
)

func main() {
	var (
		logger = log.New().WithFields(log.Fields{
			"component": "main",
		})

		configFile = flag.String("config-file", "config.yaml", "Config file to use")
	)
	flag.Parse()

	config, err := config.ReadConfigFromFile(*configFile)
	if err != nil {
		logger.Fatalf("Problem reading config file: %s", err.Error())
	}

	if config.Storage.S3 == nil {
		logger.Fatal("Not storage setting given")
	}

	s3 := storage.NewS3(config.Storage.S3.Region, config.Storage.S3.Bucket)

	if len(config.Targets) < 1 {
		logger.Warn("No target provided")
	}

	statusc := make(chan backup.JobStatus)
	var schedules []backup.Schedule

	var ds backup.DataStore
	for _, t := range config.Targets {
		logger.Infof("Processing target %s", t.Name)
		switch t.Type {
		case "etcd":
			endpoint, ok := t.Settings["endpoint"].(string)
			if !ok {
				logger.Errorf("Can't convert etcd endpoint to string for target %s", t.Name)
				continue
			}
			ds = datastore.NewEtcd(endpoint)
		case "influxdb":
			endpoint, ok := t.Settings["endpoint"].(string)
			if !ok {
				logger.Errorf("Can't convert influxdb endpoint to string for target %s", t.Name)
				continue
			}
			database, ok := t.Settings["database"].(string)
			if !ok {
				logger.Errorf("Can't convert influxdb database to string for target %s", t.Name)
				continue
			}

			days, ok := t.Settings["daysToKeep"].(int)
			var last *time.Duration
			if ok {
				last = new(time.Duration)
				*last = 24 * time.Hour * time.Duration(days)
			}
			ds = datastore.NewInflux(endpoint, database, last)
		case "mongodb":
			host, ok := t.Settings["host"].(string)
			if !ok {
				logger.Errorf("Can't convert mongodb host to string for target %s", t.Name)
				continue
			}

			port, ok := t.Settings["port"].(int)
			if !ok {
				logger.Errorf("Can't convert mongodb port to string for target %s", t.Name)
				continue
			}

			user, ok := t.Settings["user"].(string)
			if !ok {
				logger.Errorf("Can't convert mongodb user to string for target %s", t.Name)
				continue
			}
			password, ok := t.Settings["password"].(string)
			if !ok {
				logger.Errorf("Can't convert mongodb password to string for target %s", t.Name)
				continue
			}
			ds = datastore.NewMongoDB(host, port, user, password)
		case "postgres":
			url, ok := t.Settings["url"].(string)
			if !ok {
				logger.Errorf("Can't convert postgres url to string for target %s", t.Name)
				continue
			}
			ds = datastore.NewPostgres(url)
		default:
			logger.Warnf("Target type '%s' currently not supported", t.Type)
			continue
		}
		conf := backup.Config{
			Name:          t.Name,
			TempDir:       config.Settings.TmpDir,
			TempDirPrefix: "",
		}
		schedules = append(schedules, backup.Schedule{
			Spec: t.Schedule,
			Job:  backup.NewJob(ds, s3, conf, statusc),
		})
		logger.Infof("Successfully added job for target: %s", t.Name)
	}

	s, err := backup.NewScheduler(schedules)
	if err != nil {
		logger.Fatal(err)
	}

	go s.Run()
	for s := range statusc {
		if s.Error == nil {
			logger.Infof("Success: Job %s took %s to finish", s.Name, s.Duration)
		} else {
			logger.Errorf("Job %s failed after %s with error %s", s.Name, s.Duration, s.Error.Error())
		}
	}
}

redpajama_set_name: RedPajamaGithub


text:
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link com.google.cloud.spanner.Mutation}. */
@RunWith(JUnit4.class)
public class MutationTest {
  @Rule public ExpectedException expectedException = ExpectedException.none();

  @Test
  public void insertEmpty() {
    Mutation m = Mutation.newInsertBuilder("T1").build();
    assertThat(m.getTable()).isEqualTo("T1");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT);
    assertThat(m.getColumns()).isEmpty();
    assertThat(m.getValues()).isEmpty();
    assertThat(m.toString()).isEqualTo("insert(T1{})");
  }

  @Test
  public void insert() {
    Mutation m = Mutation.newInsertBuilder("T1").set("C1").to(true).set("C2").to(1234).build();
    assertThat(m.getTable()).isEqualTo("T1");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT);
    assertThat(m.getColumns()).containsExactly("C1", "C2").inOrder();
    assertThat(m.getValues()).containsExactly(Value.bool(true), Value.int64(1234)).inOrder();
    assertThat(m.toString()).isEqualTo("insert(T1{C1=true,C2=1234})");
  }

  @Test
  public void insertOrUpdateEmpty() {
    Mutation m = Mutation.newInsertOrUpdateBuilder("T2").build();
    assertThat(m.getTable()).isEqualTo("T2");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT_OR_UPDATE);
    assertThat(m.getColumns()).isEmpty();
    assertThat(m.getValues()).isEmpty();
    assertThat(m.toString()).isEqualTo("insert_or_update(T2{})");
  }

  @Test
  public void insertOrUpdate() {
    Mutation m = Mutation.newInsertOrUpdateBuilder("T1").set("C1").to(true).build();
    assertThat(m.getTable()).isEqualTo("T1");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT_OR_UPDATE);
    assertThat(m.getColumns()).containsExactly("C1");
    assertThat(m.getValues()).containsExactly(Value.bool(true));
    assertThat(m.toString()).isEqualTo("insert_or_update(T1{C1=true})");
  }

  @Test
  public void updateEmpty() {
    Mutation m = Mutation.newUpdateBuilder("T2").build();
    assertThat(m.getTable()).isEqualTo("T2");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.UPDATE);
    assertThat(m.getColumns()).isEmpty();
    assertThat(m.getValues()).isEmpty();
    assertThat(m.toString()).isEqualTo("update(T2{})");
  }

  @Test
  public void update() {
    Mutation m = Mutation.newUpdateBuilder("T1").set("C1").to(true).set("C2").to(1234).build();
    assertThat(m.getTable()).isEqualTo("T1");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.UPDATE);
    assertThat(m.getColumns()).containsExactly("C1", "C2").inOrder();
    assertThat(m.getValues()).containsExactly(Value.bool(true), Value.int64(1234)).inOrder();
    assertThat(m.toString()).isEqualTo("update(T1{C1=true,C2=1234})");
  }

  @Test
  public void replaceEmpty() {
    Mutation m = Mutation.newReplaceBuilder("T2").build();
    assertThat(m.getTable()).isEqualTo("T2");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.REPLACE);
    assertThat(m.getColumns()).isEmpty();
    assertThat(m.getValues()).isEmpty();
    assertThat(m.toString()).isEqualTo("replace(T2{})");
  }

  @Test
  public void replace() {
    Mutation m = Mutation.newReplaceBuilder("T1").set("C1").to(true).set("C2").to(1234).build();
    assertThat(m.getTable()).isEqualTo("T1");
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.REPLACE);
    assertThat(m.getColumns()).containsExactly("C1", "C2").inOrder();
    assertThat(m.getValues()).containsExactly(Value.bool(true), Value.int64(1234)).inOrder();
    assertThat(m.toString()).isEqualTo("replace(T1{C1=true,C2=1234})");
  }

  @Test
  public void duplicateColumn() {
    // The current implementation does not repeat validation performed by the server; duplicates
    // are permitted but will later cause an operation failure.
    Mutation m = Mutation.newInsertBuilder("T1").set("C1").to(true).set("C1").to(false).build();
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT);
    assertThat(m.getColumns()).containsExactly("C1", "C1").inOrder();
    assertThat(m.getValues()).containsExactly(Value.bool(true), Value.bool(false)).inOrder();
    assertThat(m.toString()).isEqualTo("insert(T1{C1=true,C1=false})");
  }

  @Test
  public void asMap() {
    Mutation m = Mutation.newInsertBuilder("T").build();
    assertThat(m.asMap()).isEqualTo(ImmutableMap.of());

    m = Mutation.newInsertBuilder("T").set("C1").to(true).set("C2").to(1234).build();
    assertThat(m.asMap())
        .isEqualTo(ImmutableMap.of("C1", Value.bool(true), "C2", Value.int64(1234)));

    m = Mutation.newInsertBuilder("T").set("C1").to(true).set("C1").to(false).build();
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Duplicate column");
    m.asMap();
  }

  @Test
  public void unfinishedBindingV1() {
    Mutation.WriteBuilder b = Mutation.newInsertBuilder("T1");
    b.set("C1");

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Incomplete binding for column C1");
    b.build();
  }

  @Test
  public void unfinishedBindingV2() {
    Mutation.WriteBuilder b = Mutation.newInsertBuilder("T1");
    b.set("C1");

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Incomplete binding for column C1");
    b.set("C2");
  }

  @Test
  public void notInBinding() {
    ValueBinder<Mutation.WriteBuilder> binder = Mutation.newInsertBuilder("T1").set("C1");
    binder.to(1234);

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("No binding currently active");
    binder.to(5678);
  }

  @Test
  public void delete() {
    KeySet keySet = KeySet.singleKey(Key.of("k1"));
    Mutation m = Mutation.delete("T1", keySet);
    assertThat(m.getOperation()).isEqualTo(Mutation.Op.DELETE);
    assertThat(m.getKeySet()).isEqualTo(keySet);
    assertThat(m.toString()).isEqualTo("delete(T1{[k1]})");
  }

  @Test
  public void equalsAndHashCode() {
    EqualsTester tester = new EqualsTester();

    // Equality, not identity.
    tester.addEqualityGroup(
        Mutation.newInsertBuilder("T1").build(), Mutation.newInsertBuilder("T1").build());

    // Operation types are distinguished.
    tester.addEqualityGroup(Mutation.newInsertOrUpdateBuilder("T1").build());
    tester.addEqualityGroup(Mutation.newUpdateBuilder("T1").build());
    tester.addEqualityGroup(Mutation.newReplaceBuilder("T1").build());

    // Table is distinguished.
    tester.addEqualityGroup(Mutation.newInsertBuilder("T2").build());

    // Columns/values are distinguished (but by equality, not identity).
    tester.addEqualityGroup(
        Mutation.newInsertBuilder("T1").set("C").to("V").build(),
        Mutation.newInsertBuilder("T1").set("C").to("V").build());

    // Deletes consider the key set.
    tester.addEqualityGroup(Mutation.delete("T1", KeySet.all()));
    tester.addEqualityGroup(
        Mutation.delete("T1", KeySet.singleKey(Key.of("k"))), Mutation.delete("T1", Key.of("k")));

    tester.testEquals();
  }

  @Test
  public void serializationBasic() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.newInsertBuilder("T").set("C").to("V").build(),
            Mutation.newUpdateBuilder("T").set("C").to("V").build(),
            Mutation.newInsertOrUpdateBuilder("T").set("C").to("V").build(),
            Mutation.newReplaceBuilder("T").set("C").to("V").build(),
            Mutation.delete("T", KeySet.singleKey(Key.of("k"))));

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();

    // Include an existing element so that we know toProto() do not clear the list.
    com.google.spanner.v1.Mutation existingProto =
        com.google.spanner.v1.Mutation.getDefaultInstance();
    proto.add(existingProto);

    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isAtLeast(1);
    assertThat(proto.get(0)).isSameAs(existingProto);
    proto.remove(0);

    assertThat(proto.size()).isEqualTo(5);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto("insert { table: 'T' columns: 'C' values { values { string_value: 'V' } } }"));
    MatcherAssert.assertThat(
        proto.get(1),
        matchesProto("update { table: 'T' columns: 'C' values { values { string_value: 'V' } } }"));
    MatcherAssert.assertThat(
        proto.get(2),
        matchesProto(
            "insert_or_update { table: 'T' columns: 'C'"
                + " values { values { string_value: 'V' } } }"));
    MatcherAssert.assertThat(
        proto.get(3),
        matchesProto(
            "replace { table: 'T' columns: 'C' values { values { string_value: 'V' } } }"));
    MatcherAssert.assertThat(
        proto.get(4),
        matchesProto("delete { table: 'T' key_set { keys { values { string_value: 'k' } } } }"));
  }

  @Test
  public void toProtoCoalescingChangeOfTable() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.newInsertBuilder("T1").set("C").to("V1").build(),
            Mutation.newInsertBuilder("T1").set("C").to("V2").build(),
            Mutation.newInsertBuilder("T1").set("C").to("V3").build(),
            Mutation.newInsertBuilder("T2").set("C").to("V4").build(),
            Mutation.newInsertBuilder("T2").set("C").to("V5").build());

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isEqualTo(2);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto(
            "insert { table: 'T1' columns: 'C' values { values { string_value: 'V1' } }"
                + " values { values { string_value: 'V2' } }"
                + " values { values { string_value: 'V3' } } }"));
    MatcherAssert.assertThat(
        proto.get(1),
        matchesProto(
            "insert { table: 'T2' columns: 'C' values { values { string_value: 'V4' } }"
                + " values { values { string_value: 'V5' } } }"));
  }

  @Test
  public void toProtoCoalescingChangeOfOperation() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.newInsertBuilder("T").set("C").to("V1").build(),
            Mutation.newInsertBuilder("T").set("C").to("V2").build(),
            Mutation.newInsertBuilder("T").set("C").to("V3").build(),
            Mutation.newUpdateBuilder("T").set("C").to("V4").build(),
            Mutation.newUpdateBuilder("T").set("C").to("V5").build());

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isEqualTo(2);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto(
            "insert { table: 'T' columns: 'C' values { values { string_value: 'V1' } }"
                + " values { values { string_value: 'V2' } }"
                + " values { values { string_value: 'V3' } } }"));
    MatcherAssert.assertThat(
        proto.get(1),
        matchesProto(
            "update { table: 'T' columns: 'C' values { values { string_value: 'V4' } }"
                + " values { values { string_value: 'V5' } } }"));
  }

  @Test
  public void toProtoCoalescingChangeOfColumn() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.newInsertBuilder("T").set("C1").to("V1").build(),
            Mutation.newInsertBuilder("T").set("C1").to("V2").build(),
            Mutation.newInsertBuilder("T").set("C1").to("V3").build(),
            Mutation.newInsertBuilder("T").set("C2").to("V4").build(),
            Mutation.newInsertBuilder("T").set("C2").to("V5").build());

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isEqualTo(2);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto(
            "insert { table: 'T' columns: 'C1' values { values { string_value: 'V1' } }"
                + " values { values { string_value: 'V2' } }"
                + " values { values { string_value: 'V3' } } }"));
    MatcherAssert.assertThat(
        proto.get(1),
        matchesProto(
            "insert { table: 'T' columns: 'C2' values { values { string_value: 'V4' } }"
                + " values { values { string_value: 'V5' } } }"));
  }

  @Test
  public void toProtoCoalescingDelete() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.delete("T", Key.of("k1")),
            Mutation.delete("T", Key.of("k2")),
            Mutation.delete("T", KeySet.range(KeyRange.closedOpen(Key.of("ka"), Key.of("kb")))),
            Mutation.delete("T", KeySet.range(KeyRange.closedClosed(Key.of("kc"), Key.of("kd")))));

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isEqualTo(1);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto(
            "delete {"
                + "  table: 'T'"
                + "  key_set {"
                + "    keys { values { string_value: 'k1' } }"
                + "    keys { values { string_value: 'k2' } }"
                + "    ranges { start_closed { values { string_value: 'ka' } } "
                + "             end_open { values { string_value: 'kb' } } }"
                + "    ranges { start_closed { values { string_value: 'kc' } } "
                + "             end_closed { values { string_value: 'kd' } } }"
                + "  }"
                + "} "));
  }

  @Test
  public void toProtoCoalescingDeleteChanges() {
    List<Mutation> mutations =
        Arrays.asList(
            Mutation.newInsertBuilder("T1").set("C").to("V1").build(),
            Mutation.delete("T1", Key.of("k1")),
            Mutation.delete("T1", Key.of("k2")),
            Mutation.delete("T2", Key.of("k3")),
            Mutation.delete("T2", Key.of("k4")),
            Mutation.newInsertBuilder("T2").set("C").to("V1").build());

    List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
    Mutation.toProto(mutations, proto);

    assertThat(proto.size()).isEqualTo(4);
    MatcherAssert.assertThat(
        proto.get(0),
        matchesProto(
            "insert { table: 'T1' columns: 'C' values { values { string_value: 'V1' } } }"));
    MatcherAssert.assertThat(
        proto.get(1),
        matchesProto(
            "delete { table: 'T1' key_set { keys { values { string_value: 'k1' } } "
                + "keys { values { string_value: 'k2' } } } }"));
    MatcherAssert.assertThat(
        proto.get(2),
        matchesProto(
            "delete { table: 'T2' key_set { keys { values { string_value: 'k3' } } "
                + "keys { values { string_value: 'k4' } } } }"));
    MatcherAssert.assertThat(
        proto.get(3),
        matchesProto(
            "insert { table: 'T2', columns: 'C', values { values { string_value: 'V1' } } }"));
  }

  static Matcher<com.google.spanner.v1.Mutation> matchesProto(String expected) {
    return SpannerMatchers.matchesProto(com.google.spanner.v1.Mutation.class, expected);
  }
}


text:
#import <Foundation/Foundation.h>

#import "MXJSONModel.h"
#import "MXBaseKeyBackupAuthData.h"

NS_ASSUME_NONNULL_BEGIN

/**
 Data model for MXKeyBackupVersion.authData in case of kMXCryptoAes256KeyBackupAlgorithm.
 */
@interface MXAes256BackupAuthData : MXJSONModel <MXBaseKeyBackupAuthData>

/**
 The identity vector used to encrypt the backups.
 */
@property (nonatomic, nullable) NSString *iv;

/**
 The mac used to encrypt the backups.
 */
@property (nonatomic, nullable) NSString *mac;

@end

NS_ASSUME_NONNULL_END


text:
using System;
using System.Linq.Expressions;

namespace HeptaSoft.SmartEntity.Environment.Providers
{
    internal interface IRepositoryFilterExecutorProvider
    {
        /// <summary>
        /// Gets the filter executor.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns>The lambda expression.</returns>
        LambdaExpression GetFilterExecutor(Type entityType);
    }
}


text:
package it.unibz.inf.ontop.docker.mysql;

import it.unibz.inf.ontop.docker.AbstractVirtualModeTest;
import it.unibz.inf.ontop.owlapi.OntopOWLEngine;
import it.unibz.inf.ontop.owlapi.connection.OntopOWLConnection;
import it.unibz.inf.ontop.owlapi.connection.OntopOWLStatement;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

/**
 * Test to check if the sql parser supports regex correctly when written with mysql syntax.
 * Translated in a datalog function and provides the correct results
 */
public class RegexMySQLTest extends AbstractVirtualModeTest {

	static final String owlfile = "/mysql/regex/stockBolzanoAddress.owl";
	static final String obdafile = "/mysql/regex/stockexchangeRegexMySQL.obda";
	static final String propertiesfile = "/mysql/regex/stockexchangeRegexMySQL.properties";

	private static OntopOWLEngine REASONER;
	private static OntopOWLConnection CONNECTION;

	@BeforeClass
	public static void before() throws OWLOntologyCreationException {
		REASONER = createReasoner(owlfile, obdafile, propertiesfile);
		CONNECTION = REASONER.getConnection();
	}

	@Override
	protected OntopOWLStatement createStatement() throws OWLException {
		return CONNECTION.createStatement();
	}

	@AfterClass
	public static void after() throws Exception {
		CONNECTION.close();
		REASONER.close();
	}

	/**
	 * Test use of regex in MySQL
	 * select id, street, number, city, state, country from address where city regexp 'b.+z'
	 * @throws Exception
	 */
	@Test
	public void testRegexLike() throws Exception {
		String query = "PREFIX : <http://www.owl-ontologies.com/Ontology1207768242.owl#> SELECT ?x WHERE {?x a :BolzanoAddress}";
		countResults(2, query);
	}

	/**
	 * Test use of regex in MySQL
	 * select "id", "name", "lastname", "dateofbirth", "ssn" from "broker" where "name" regexp binary 'J.+a'
	 * @throws Exception
	 */
	@Test
	public void testRegexLikeUppercase() throws Exception {
		String query = "PREFIX : <http://www.owl-ontologies.com/Ontology1207768242.owl#> SELECT ?x WHERE {?x a :PhysicalPerson}";
		countResults(1, query);
	}

}


text:
typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX;

namespace base {
namespace win {

// Windows memory pressure monitor. Because there is no OS provided signal this
// polls at a low frequency (once per second), and applies internal hysteresis.
class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
 public:
  // Constants governing the polling and hysteresis behaviour of the observer.

  // The polling interval, in milliseconds. While under critical pressure, this
  // is also the timer to repeat cleanup attempts.
  static const int kPollingIntervalMs;
  // The time which should pass between 2 successive moderate memory pressure
  // signals, in milliseconds.
  static const int kModeratePressureCooldownMs;
  // The number of cycles that should pass between 2 successive moderate memory
  // pressure signals.
  static const int kModeratePressureCooldownCycles;

  // Constants governing the memory pressure level detection.

  // The amount of total system memory beyond which a system is considered to be
  // a large-memory system.
  static const int kLargeMemoryThresholdMb;
  // Default minimum free memory thresholds for small-memory systems, in MB.
  static const int kSmallMemoryDefaultModerateThresholdMb;
  static const int kSmallMemoryDefaultCriticalThresholdMb;
  // Default minimum free memory thresholds for large-memory systems, in MB.
  static const int kLargeMemoryDefaultModerateThresholdMb;
  static const int kLargeMemoryDefaultCriticalThresholdMb;

  // Default constructor. Will choose thresholds automatically basd on the
  // actual amount of system memory.
  MemoryPressureMonitor();

  // Constructor with explicit memory thresholds. These represent the amount of
  // free memory below which the applicable memory pressure state engages.
  MemoryPressureMonitor(int moderate_threshold_mb, int critical_threshold_mb);

  ~MemoryPressureMonitor() override;

  // Schedules a memory pressure check to run soon. This must be called on the
  // same thread where the monitor was instantiated.
  void CheckMemoryPressureSoon();

  // Get the current memory pressure level. This can be called from any thread.
  MemoryPressureLevel GetCurrentPressureLevel() const override;

  // Returns the moderate pressure level free memory threshold, in MB.
  int moderate_threshold_mb() const { return moderate_threshold_mb_; }

  // Returns the critical pressure level free memory threshold, in MB.
  int critical_threshold_mb() const { return critical_threshold_mb_; }

 protected:
  // Internals are exposed for unittests.

  // Automatically infers threshold values based on system memory. This invokes
  // GetMemoryStatus so it can be mocked in unittests.
  void InferThresholds();

  // Starts observing the memory fill level. Calls to StartObserving should
  // always be matched with calls to StopObserving.
  void StartObserving();

  // Stop observing the memory fill level. May be safely called if
  // StartObserving has not been called. Must be called from the same thread on
  // which the monitor was instantiated.
  void StopObserving();

  // Checks memory pressure, storing the current level, applying any hysteresis
  // and emitting memory pressure level change signals as necessary. This
  // function is called periodically while the monitor is observing memory
  // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so
  // that it may be called by CheckMemoryPressureSoon and not invoke UMA
  // logging. Must be called from the same thread on which the monitor was
  // instantiated.
  void CheckMemoryPressure();

  // Wrapper to CheckMemoryPressure that also records the observed memory
  // pressure level via an UMA enumeration. This is the function that is called
  // periodically by the timer. Must be called from the same thread on which the
  // monitor was instantiated.
  void CheckMemoryPressureAndRecordStatistics();

  // Calculates the current instantaneous memory pressure level. This does not
  // use any hysteresis and simply returns the result at the current moment. Can
  // be called on any thread.
  MemoryPressureLevel CalculateCurrentPressureLevel();

  // Gets system memory status. This is virtual as a unittesting hook. Returns
  // true if the system call succeeds, false otherwise. Can be called on any
  // thread.
  virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status);

 private:
  // Threshold amounts of available memory that trigger pressure levels. See
  // memory_pressure_monitor.cc for a discussion of reasonable values for these.
  int moderate_threshold_mb_;
  int critical_threshold_mb_;

  // A periodic timer to check for memory pressure changes.
  base::RepeatingTimer<MemoryPressureMonitor> timer_;

  // The current memory pressure.
  MemoryPressureLevel current_memory_pressure_level_;

  // To slow down the amount of moderate pressure event calls, this gets used to
  // count the number of events since the last event occured. This is used by
  // |CheckMemoryPressure| to apply hysteresis on the raw results of
  // |CalculateCurrentPressureLevel|.
  int moderate_pressure_repeat_count_;

  // Ensures that this object is used from a single thread.
  base::ThreadChecker thread_checker_;

  // Weak pointer factory to ourself used for scheduling calls to
  // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
  base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
};

}  // namespace win
}  // namespace base

#endif  // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_


text:
package com.intellij.util.xml.converters;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.psi.*;
import com.intellij.psi.search.ProjectScope;
import com.intellij.psi.util.PropertyMemberType;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.util.xml.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Gregory.Shrago
 */
public abstract class AbstractMemberResolveConverter extends ResolvingConverter<PsiMember> {
  @Nullable
  protected abstract PsiClass getTargetClass(final ConvertContext context);

  @NotNull
  protected abstract PropertyMemberType[] getMemberTypes(final ConvertContext context);

  @NotNull
  protected PsiType getPsiType(final ConvertContext context) {
    return PsiType.getJavaLangObject(context.getPsiManager(), ProjectScope.getAllScope(context.getPsiManager().getProject()));
  }

  protected boolean isLookDeep() {
    return true;
  }

  protected boolean isPropertyNameUsed() {
    return true;
  }

  public PsiMember fromString(final String s, final ConvertContext context) {
    if (s == null) return null;
    final PsiClass psiClass = getTargetClass(context);
    if (psiClass == null) return null;
    final String propertyName = isPropertyNameUsed() ? s : PropertyUtil.getPropertyName(s);
    for (PropertyMemberType type : getMemberTypes(context)) {
      switch (type) {
        case FIELD:
          final PsiField field = psiClass.findFieldByName(s, isLookDeep());
          if (field != null) return field;
          break;
        case GETTER:
          final PsiMethod getter = PropertyUtil.findPropertyGetter(psiClass, propertyName, false, isLookDeep());
          if (getter != null) return getter;
          break;
        case SETTER:
          final PsiMethod setter = PropertyUtil.findPropertySetter(psiClass, propertyName, false, isLookDeep());
          if (setter != null) return setter;
          break;
      }
    }
    return null;
  }

  public String toString(final PsiMember t, final ConvertContext context) {
    return t == null? null : isPropertyNameUsed()? PropertyUtil.getPropertyName(t) : t.getName();
  }

  public String getErrorMessage(final String s, final ConvertContext context) {
    final DomElement parent = context.getInvocationElement().getParent();
    assert parent != null;
    return CodeInsightBundle.message("error.cannot.resolve.0.1", TypeNameManager.getTypeName(parent.getClass()), s);
  }

  @NotNull
  public Collection<? extends PsiMember> getVariants(final ConvertContext context) {
    final PsiClass psiClass = getTargetClass(context);
    if (psiClass == null) return Collections.emptyList();

    final ArrayList<PsiMember> list = new ArrayList<PsiMember>();
    for (PsiField psiField : isLookDeep()? psiClass.getAllFields() : psiClass.getFields()) {
      if (fieldSuits(psiField)) {
        list.add(psiField);
      }
    }
    for (PsiMethod psiMethod : isLookDeep()? psiClass.getAllMethods() : psiClass.getMethods()) {
      if (methodSuits(psiMethod)) {
        list.add(psiMethod);
      }
    }
    return list;
  }

  protected boolean methodSuits(final PsiMethod psiMethod) {
    return !psiMethod.isConstructor() && !psiMethod.hasModifierProperty(PsiModifier.STATIC) && PropertyUtil.getPropertyName(psiMethod) != null;
  }

  protected boolean fieldSuits(final PsiField psiField) {
    return !psiField.hasModifierProperty(PsiModifier.STATIC);
  }

  public LocalQuickFix[] getQuickFixes(final ConvertContext context) {
    final String targetName = ((GenericValue)context.getInvocationElement()).getStringValue();
    if (targetName == null) return super.getQuickFixes(context);
    final PsiClass targetClass = getTargetClass(context);
    if (targetClass == null) return super.getQuickFixes(context);
    final PropertyMemberType memberType = getMemberTypes(context)[0];

    final PsiType psiType = getPsiType(context);
    final IntentionAction fix = QuickFixFactory.getInstance().createCreateFieldOrPropertyFix(targetClass, targetName, psiType, memberType);
    return fix instanceof LocalQuickFix? new LocalQuickFix[] {(LocalQuickFix)fix} : super.getQuickFixes(context);
  }

  public void handleElementRename(final GenericDomValue<PsiMember> genericValue, final ConvertContext context, final String newElementName) {
    super.handleElementRename(genericValue, context, isPropertyNameUsed()? PropertyUtil.getPropertyName(newElementName) : newElementName);
  }

  public void bindReference(final GenericDomValue<PsiMember> genericValue, final ConvertContext context, final PsiElement newTarget) {
    if (newTarget instanceof PsiMember) {
      final String elementName = ((PsiMember)newTarget).getName();
      genericValue.setStringValue(isPropertyNameUsed() ? PropertyUtil.getPropertyName(elementName) : elementName);
    }
  }
}


text:
	#include "lws_config.h"
#endif

#if defined(_LWS_WIN32)
	#include "win32_platform.h"
#elif
#endif

namespace ILWS
{
	#ifndef _LWS_STRUCTS_MACROS_DEFINED_
		#error "Undefined Managment of OS"
	#endif

	struct Window
	{
		LWS::System::Window* mainObject;

		_ILWS_WINDOW_STATE_;
	};

	struct Library
	{
		_ILWS_LIBRARY_WINDOW_STATE_;
		_ILWS_LIBRARY_TIME_STATE_;
	};

	extern Libary ILWS;
}

#endif


text:
<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:orx="http://openrosa.org/jr/xforms" xmlns="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
	<h:head>
		<h:title>New Form</h:title>
		<model>
			<instance>
				<data xmlns:jrm="http://dev.commcarehq.org/jr/xforms" xmlns="http://openrosa.org/formdesigner/FAD4A0A8-4DB4-429B-B656-42A394D6108C" uiVersion="1" version="44" name="New Form">
					<one/>
					<two/>
					<three/>
					<four/>
					<five/>
					<six/>
				<orx:meta xmlns:cc="http://commcarehq.org/xforms"><orx:deviceID/><orx:timeStart/><orx:timeEnd/><orx:username/><orx:userID/><orx:instanceID/><cc:appVersion/></orx:meta></data>
			</instance>
			<instance src="jr://instance/session" id="commcaresession"/><bind nodeset="/data/one"/>
			<bind nodeset="/data/two"/>
			<bind nodeset="/data/three"/>
			<bind nodeset="/data/four"/>
			<bind nodeset="/data/five"/>
			<bind nodeset="/data/six"/>
			<itext>
				<translation lang="en" default="">
					<text id="one-label">
						<value>Welcome to the multimedia test form. This form will test your phone's media capabilities</value>
						<value form="image">jr://file/commcare/image/data/one.png</value>
					</text>
					<text id="two-label">
						<value>This question has attached audio data</value>
						<value form="audio">jr://file/commcare/audio/data/two.mp3</value>
						<value form="image">jr://file/commcare/image/data/two.png</value>
					</text>
					<text id="three-label">
						<value>Each of these items should have an image attached, and sound</value>
						<value form="image">jr://file/commcare/image/data/three.png</value>
					</text>
					<text id="three-three_one-label">
						<value>One</value>
						<value form="image">jr://file/commcare/image/data/three-three_one.png</value>
						<value form="audio">jr://file/commcare/audio/data/three-three_one.mp3</value>
					</text>
					<text id="three-three_two-label">
						<value>Two</value>
						<value form="image">jr://file/commcare/image/data/three-three_two.png</value>
						<value form="audio">jr://file/commcare/audio/data/three-three_two.mp3</value>
					</text>
					<text id="three-three_three-label">
						<value>Three</value>
						<value form="image">jr://file/commcare/image/data/three-three_three.png</value>
						<value form="audio">jr://file/commcare/audio/data/three-three_three.mp3</value>
					</text>
					<text id="four-label">
						<value>This question and each of its items should have an image attached, and sound.</value>
						<value form="image">jr://file/commcare/image/data/four.png</value>
						<value form="audio">jr://file/commcare/audio/data/four.mp3</value>
					</text>
					<text id="four-four_one-label">
						<value>One</value>
						<value form="image">jr://file/commcare/image/data/four-four_one.png</value>
						<value form="audio">jr://file/commcare/audio/data/four-four_one.mp3</value>
					</text>
					<text id="four-four_two-label">
						<value>Two</value>
						<value form="image">jr://file/commcare/image/data/four-four_two.png</value>
						<value form="audio">jr://file/commcare/audio/data/four-four_two.mp3</value>
					</text>
					<text id="four-four_three-label">
						<value>Three</value>
						<value form="image">jr://file/commcare/image/data/four-four_three.png</value>
						<value form="audio">jr://file/commcare/audio/data/four-four_three.mp3</value>
					</text>
					<text id="five-label">
						<value>This question and each of its items should have an image attached, and sound</value>
						<value form="image">jr://file/commcare/image/data/five.png</value>
						<value form="audio">jr://file/commcare/audio/data/five.mp3</value>
					</text>
					<text id="five-five_one-label">
						<value>One</value>
						<value form="image">jr://file/commcare/image/data/five-five_one.png</value>
						<value form="audio">jr://file/commcare/audio/data/five-five_one.mp3</value>
					</text>
					<text id="five-five_two-label">
						<value>Two</value>
						<value form="image">jr://file/commcare/image/data/five-five_two.png</value>
						<value form="audio">jr://file/commcare/audio/data/five-five_two.mp3</value>
					</text>
					<text id="five-five_three-label">
						<value>Three</value>
						<value form="image">jr://file/commcare/image/data/five-five_three.png</value>
						<value form="audio">jr://file/commcare/audio/data/five-five_three.mp3</value>
					</text>
					<text id="six-label">
						<value>That's it, thanks!</value>
					</text>
				</translation>
			</itext>
		<setvalue ref="/data/meta/deviceID" event="xforms-ready" value="instance('commcaresession')/session/context/deviceid"/><setvalue ref="/data/meta/timeStart" event="xforms-ready" value="now()"/><bind nodeset="/data/meta/timeStart" type="xsd:dateTime"/><setvalue ref="/data/meta/timeEnd" event="xforms-revalidate" value="now()"/><bind nodeset="/data/meta/timeEnd" type="xsd:dateTime"/><setvalue ref="/data/meta/username" event="xforms-ready" value="instance('commcaresession')/session/context/username"/><setvalue ref="/data/meta/userID" event="xforms-ready" value="instance('commcaresession')/session/context/userid"/><setvalue ref="/data/meta/instanceID" event="xforms-ready" value="uuid()"/><setvalue ref="/data/meta/appVersion" event="xforms-ready" value="instance('commcaresession')/session/context/appversion"/></model>
	</h:head>
	<h:body>
		<trigger ref="/data/one">
			<label ref="jr:itext('one-label')"/>
		</trigger>
		<trigger ref="/data/two">
			<label ref="jr:itext('two-label')"/>
		</trigger>
		<select1 ref="/data/three">
			<label ref="jr:itext('three-label')"/>
			<item>
				<label ref="jr:itext('three-three_one-label')"/>
				<value>three_one</value>
			</item>
			<item>
				<label ref="jr:itext('three-three_two-label')"/>
				<value>three_two</value>
			</item>
			<item>
				<label ref="jr:itext('three-three_three-label')"/>
				<value>three_three</value>
			</item>
		</select1>
		<select1 ref="/data/four">
			<label ref="jr:itext('four-label')"/>
			<item>
				<label ref="jr:itext('four-four_one-label')"/>
				<value>four_one</value>
			</item>
			<item>
				<label ref="jr:itext('four-four_two-label')"/>
				<value>four_two</value>
			</item>
			<item>
				<label ref="jr:itext('four-four_three-label')"/>
				<value>four_three</value>
			</item>
		</select1>
		<select ref="/data/five">
			<label ref="jr:itext('five-label')"/>
			<item>
				<label ref="jr:itext('five-five_one-label')"/>
				<value>five_one</value>
			</item>
			<item>
				<label ref="jr:itext('five-five_two-label')"/>
				<value>five_two</value>
			</item>
			<item>
				<label ref="jr:itext('five-five_three-label')"/>
				<value>five_three</value>
			</item>
		</select>
		<trigger ref="/data/six">
			<label ref="jr:itext('six-label')"/>
		</trigger>
	</h:body>
</h:html>


text:
// requires local modules: keysym, keysymdef, keyboard

var assert = chai.assert;
var expect = chai.expect;

describe('Helpers', function() {
    "use strict";
    describe('keysymFromKeyCode', function() {
        it('should map known keycodes to keysyms', function() {
            expect(kbdUtil.keysymFromKeyCode(0x41, false), 'a').to.be.equal(0x61);
            expect(kbdUtil.keysymFromKeyCode(0x41, true), 'A').to.be.equal(0x41);
            expect(kbdUtil.keysymFromKeyCode(0xd, false), 'enter').to.be.equal(0xFF0D);
            expect(kbdUtil.keysymFromKeyCode(0x11, false), 'ctrl').to.be.equal(0xFFE3);
            expect(kbdUtil.keysymFromKeyCode(0x12, false), 'alt').to.be.equal(0xFFE9);
            expect(kbdUtil.keysymFromKeyCode(0xe1, false), 'altgr').to.be.equal(0xFE03);
            expect(kbdUtil.keysymFromKeyCode(0x1b, false), 'esc').to.be.equal(0xFF1B);
            expect(kbdUtil.keysymFromKeyCode(0x26, false), 'up').to.be.equal(0xFF52);
        });
        it('should return null for unknown keycodes', function() {
            expect(kbdUtil.keysymFromKeyCode(0xc0, false), 'DK æ').to.be.null;
            expect(kbdUtil.keysymFromKeyCode(0xde, false), 'DK ø').to.be.null;
        });
    });

    describe('keysyms.fromUnicode', function() {
        it('should map ASCII characters to keysyms', function() {
            expect(keysyms.fromUnicode('a'.charCodeAt())).to.have.property('keysym', 0x61);
            expect(keysyms.fromUnicode('A'.charCodeAt())).to.have.property('keysym', 0x41);
            });
        it('should map Latin-1 characters to keysyms', function() {
            expect(keysyms.fromUnicode('ø'.charCodeAt())).to.have.property('keysym', 0xf8);

            expect(keysyms.fromUnicode('é'.charCodeAt())).to.have.property('keysym', 0xe9);
        });
        it('should map characters that are in Windows-1252 but not in Latin-1 to keysyms', function() {
            expect(keysyms.fromUnicode('Š'.charCodeAt())).to.have.property('keysym', 0x01a9);
        });
        it('should map characters which aren\'t in Latin1 *or* Windows-1252 to keysyms', function() {
            expect(keysyms.fromUnicode('ŵ'.charCodeAt())).to.have.property('keysym', 0x1000175);
        });
        it('should return undefined for unknown codepoints', function() {
            expect(keysyms.fromUnicode('\n'.charCodeAt())).to.be.undefined;
            expect(keysyms.fromUnicode('\u1F686'.charCodeAt())).to.be.undefined;
        });
    });

    describe('substituteCodepoint', function() {
        it('should replace characters which don\'t have a keysym', function() {
            expect(kbdUtil.substituteCodepoint('Ș'.charCodeAt())).to.equal('Ş'.charCodeAt());
            expect(kbdUtil.substituteCodepoint('ș'.charCodeAt())).to.equal('ş'.charCodeAt());
            expect(kbdUtil.substituteCodepoint('Ț'.charCodeAt())).to.equal('Ţ'.charCodeAt());
            expect(kbdUtil.substituteCodepoint('ț'.charCodeAt())).to.equal('ţ'.charCodeAt());
        });
        it('should pass other characters through unchanged', function() {
            expect(kbdUtil.substituteCodepoint('T'.charCodeAt())).to.equal('T'.charCodeAt());
        });
    });

    describe('nonCharacterKey', function() {
        it('should  recognize the right keys', function() {
            expect(kbdUtil.nonCharacterKey({keyCode: 0xd}), 'enter').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0x08}), 'backspace').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0x09}), 'tab').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0x10}), 'shift').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0x11}), 'ctrl').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0x12}), 'alt').to.be.defined;
            expect(kbdUtil.nonCharacterKey({keyCode: 0xe0}), 'meta').to.be.defined;
        });
        it('should  not recognize character keys', function() {
            expect(kbdUtil.nonCharacterKey({keyCode: 'A'}), 'A').to.be.null;
            expect(kbdUtil.nonCharacterKey({keyCode: '1'}), '1').to.be.null;
            expect(kbdUtil.nonCharacterKey({keyCode: '.'}), '.').to.be.null;
            expect(kbdUtil.nonCharacterKey({keyCode: ' '}), 'space').to.be.null;
        });
    });

    describe('getKeysym', function() {
        it('should prefer char', function() {
            expect(kbdUtil.getKeysym({char : 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.have.property('keysym', 0x61);
        });
        it('should use charCode if no char', function() {
            expect(kbdUtil.getKeysym({char : '', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.have.property('keysym', 0x01a9);
            expect(kbdUtil.getKeysym({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.have.property('keysym', 0x01a9);
            expect(kbdUtil.getKeysym({char : 'hello', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.have.property('keysym', 0x01a9);
        });
        it('should use keyCode if no charCode', function() {
            expect(kbdUtil.getKeysym({keyCode: 0x42, which: 0x43, shiftKey: false})).to.have.property('keysym', 0x62);
            expect(kbdUtil.getKeysym({keyCode: 0x42, which: 0x43, shiftKey: true})).to.have.property('keysym', 0x42);
        });
        it('should use which if no keyCode', function() {
            expect(kbdUtil.getKeysym({which: 0x43, shiftKey: false})).to.have.property('keysym', 0x63);
            expect(kbdUtil.getKeysym({which: 0x43, shiftKey: true})).to.have.property('keysym', 0x43);
        });
        it('should substitute where applicable', function() {
            expect(kbdUtil.getKeysym({char : 'Ș'})).to.have.property('keysym', 0x1aa);
        });
    });

    describe('Modifier Sync', function() { // return a list of fake events necessary to fix modifier state
        describe('Toggle all modifiers', function() {
            var sync = kbdUtil.ModifierSync();
            it ('should do nothing if all modifiers are up as expected', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: false,
                    altKey: false,
                    altGraphKey: false,
                    shiftKey: false,
                    metaKey: false})
                    ).to.have.lengthOf(0);
            });
            it ('should synthesize events if all keys are unexpectedly down', function() {
                var result = sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: true,
                    altKey: true,
                    altGraphKey: true,
                    shiftKey: true,
                    metaKey: true
                });
                expect(result).to.have.lengthOf(5);
                var keysyms = {};
                for (var i = 0; i < result.length; ++i) {
                    keysyms[result[i].keysym] = (result[i].type == 'keydown');
                }
                expect(keysyms[0xffe3]);
                expect(keysyms[0xffe9]);
                expect(keysyms[0xfe03]);
                expect(keysyms[0xffe1]);
                expect(keysyms[0xffe7]);
            });
            it ('should do nothing if all modifiers are down as expected', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: true,
                    altKey: true,
                    altGraphKey: true,
                    shiftKey: true,
                    metaKey: true
                    })).to.have.lengthOf(0);
            });
        });
        describe('Toggle Ctrl', function() {
            var sync = kbdUtil.ModifierSync();
            it('should sync if modifier is suddenly down', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: true,
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe3), type: 'keydown'}]);
            });
            it('should sync if modifier is suddenly up', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: false
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe3), type: 'keyup'}]);
            });
        });
        describe('Toggle Alt', function() {
            var sync = kbdUtil.ModifierSync();
            it('should sync if modifier is suddenly down', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    altKey: true,
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe9), type: 'keydown'}]);
            });
            it('should sync if modifier is suddenly up', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    altKey: false
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe9), type: 'keyup'}]);
            });
        });
        describe('Toggle AltGr', function() {
            var sync = kbdUtil.ModifierSync();
            it('should sync if modifier is suddenly down', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    altGraphKey: true,
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xfe03), type: 'keydown'}]);
            });
            it('should sync if modifier is suddenly up', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    altGraphKey: false
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xfe03), type: 'keyup'}]);
            });
        });
        describe('Toggle Shift', function() {
            var sync = kbdUtil.ModifierSync();
            it('should sync if modifier is suddenly down', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    shiftKey: true,
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe1), type: 'keydown'}]);
            });
            it('should sync if modifier is suddenly up', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    shiftKey: false
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe1), type: 'keyup'}]);
            });
        });
        describe('Toggle Meta', function() {
            var sync = kbdUtil.ModifierSync();
            it('should sync if modifier is suddenly down', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    metaKey: true,
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe7), type: 'keydown'}]);
            });
            it('should sync if modifier is suddenly up', function() {
                expect(sync.keydown({
                    keyCode: 0x41,
                    metaKey: false
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe7), type: 'keyup'}]);
            });
        });
        describe('Modifier keyevents', function() {
            it('should not sync a modifier on its own events', function() {
                expect(kbdUtil.ModifierSync().keydown({
                    keyCode: 0x11,
                    ctrlKey: false
                })).to.be.deep.equal([]);
                expect(kbdUtil.ModifierSync().keydown({
                    keyCode: 0x11,
                    ctrlKey: true
                }), 'B').to.be.deep.equal([]);
            })
            it('should update state on modifier keyevents', function() {
                var sync = kbdUtil.ModifierSync();
                sync.keydown({
                    keyCode: 0x11,
                });
                expect(sync.keydown({
                    keyCode: 0x41,
                    ctrlKey: true,
                })).to.be.deep.equal([]);
            });
            it('should sync other modifiers on ctrl events', function() {
                expect(kbdUtil.ModifierSync().keydown({
                    keyCode: 0x11,
                    altKey: true
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe9), type: 'keydown'}]);
            })
        });
        describe('sync modifiers on non-key events', function() {
            it('should generate sync events when receiving non-keyboard events', function() {
                expect(kbdUtil.ModifierSync().syncAny({
                    altKey: true
                })).to.be.deep.equal([{keysym: keysyms.lookup(0xffe9), type: 'keydown'}]);
            });
        });
        describe('do not treat shift as a modifier key', function() {
            it('should not treat shift as a shortcut modifier', function() {
                expect(kbdUtil.hasShortcutModifier([], {0xffe1 : true})).to.be.false;
            });
            it('should not treat shift as a char modifier', function() {
                expect(kbdUtil.hasCharModifier([], {0xffe1 : true})).to.be.false;
            });
        });
    });
});


text:
Website Scraper

You can pass three arguments to this URL.

URL: the URL you want to parse
Selector: the CSS3 selector you want to use (optional)
Callback: the JavaScript callback you want it wrapped in (optional)

Usage: /?url=http://www.cnn.com&selector=a&callback=myCallback


text:
package alluxio.stress.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import alluxio.stress.BaseParameters;
import alluxio.stress.GraphGenerator;
import alluxio.stress.TaskResult;

import org.junit.Test;

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

public class GeneralBenchSummaryTest {
  // Add a test implementation to test the abstract parent
  private class TestTaskResult implements TaskResult {
    private BaseParameters mBaseParameters;
    private List<String> mErrors;

    TestTaskResult() {
      mBaseParameters = new BaseParameters();
      mErrors = new ArrayList<>();
    }

    @Override
    public Aggregator aggregator() {
      return null;
    }

    @Override
    public BaseParameters getBaseParameters() {
      return mBaseParameters;
    }

    @Override
    public List<String> getErrors() {
      return mErrors;
    }

    public void addErrors(String s) {
      mErrors.add(s);
    }
  }

  private class TestMultipleNodeSummary extends GeneralBenchSummary<TestTaskResult> {
    @Override
    public GraphGenerator graphGenerator() {
      return null;
    }

    public void addTaskResultWithErrors(int n) {
      int index = mNodeResults.size();
      for (int i = 0; i < n; i++) {
        TestTaskResult result = new TestTaskResult();
        String taskID = "task" + index;
        result.getBaseParameters().mId = taskID;
        result.addErrors("error" + index);
        mNodeResults.put(taskID, result);
        index++;
      }
    }

    public void addTaskResultWithoutErrors(int n) {
      int index = mNodeResults.size();
      for (int i = 0; i < n; i++) {
        TestTaskResult result = new TestTaskResult();
        String taskID = "task" + index;
        result.getBaseParameters().mId = taskID;
        mNodeResults.put(taskID, result);
        index++;
      }
    }
  }

  @Test
  public void collectErrorFromAllNodesWithEmptyResults() {
    // test summary with empty nodes
    TestMultipleNodeSummary summary = new TestMultipleNodeSummary();
    List<String> emptyList = summary.collectErrorsFromAllNodes();
    assertTrue(emptyList.isEmpty());
  }

  @Test
  public void collectErrorFromAllNodesWithNoErrors() {
    // test summary with node and no error
    TestMultipleNodeSummary summary = new TestMultipleNodeSummary();
    summary.addTaskResultWithoutErrors(6);
    List<String> emptyList = summary.collectErrorsFromAllNodes();
    assertTrue(emptyList.isEmpty());
  }

  @Test
  public void collectErrorFromAllNodesWithErrors() {
    // test summary with errors
    TestMultipleNodeSummary summary = new TestMultipleNodeSummary();

    summary.addTaskResultWithoutErrors(4);
    summary.addTaskResultWithErrors(3);

    List<String> list = summary.collectErrorsFromAllNodes();
    assertEquals(list.size(), 3);
    Set<String> set = new HashSet<>(list);
    for (int i = 4; i < 6; i++) {
      String message = String.format("task%s :error%s", i, i);
      assertTrue(set.contains(message));
    }
  }
}


text:
SpringWind - 项目管理
#end
#override("css")
<link href="#springUrl('/static/css/plugins/bootstrap-table/bootstrap-table.min.css')" rel="stylesheet">
#end
#override("breadcrumb")

农电商数据报表

类别数据

分类体系

#end
#override("content")
<button type="button" class="btn btn-primary pull-right" onClick="OutExcel()">导出Excel</button>

	<form id="formExcel">
		<input type="hidden" id="currentIndex" name="_index">
		<input type="hidden" id="currentSize" name="_size">
		<input type="hidden" id="platName" name="platName" value="数据分析所使用的三级分类表">
		<select id="ExcelValue" name="ExcelValue" class="form-control">
				<option value="1">当前页</option>
				<option value="2">全部</option>
		</select>
	</form>

数据分析所使用的三级分类表

#end
#override("js")

#end
#extends("/common/framework.html")


text:
layout: doc_page
---
# Time Boundary Queries
Time boundary queries return the earliest and latest data points of a data set. The grammar is:

```json
{
    "queryType" : "timeBoundary",
    "dataSource": "sample_datasource",
    "bound"     : < "maxTime" | "minTime" > # optional, defaults to returning both timestamps if not set
}
```

There are 3 main parts to a time boundary query:

|property|description|required?|
|--------|-----------|---------|
|queryType|This String should always be "timeBoundary"; this is the first thing Druid looks at to figure out how to interpret the query|yes|
|dataSource|A String or Object defining the data source to query, very similar to a table in a relational database. See [DataSource](../querying/datasource.html) for more information.|yes|
|bound   | Optional, set to `maxTime` or `minTime` to return only the latest or earliest timestamp. Default to returning both if not set| no |
|context|See [Context](../querying/query-context.html)|no|

The format of the result is:

```json
[ {
  "timestamp" : "2013-05-09T18:24:00.000Z",
  "result" : {
    "minTime" : "2013-05-09T18:24:00.000Z",
    "maxTime" : "2013-05-09T18:37:00.000Z"
  }
} ]
```


text:
from .parser import CrawledTree  # noqa
from .nodes import HostNode, URLNode, HarTreeNode  # noqa
from .har2tree import Har2Tree, HarFile  # noqa
from .helper import Har2TreeError, Har2TreeLogAdapter  # noqa

import logging
logging.getLogger(__name__).addHandler(logging.NullHandler())


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Concurrency;
using System.Disposables;

namespace RxAs.Rx2.ProofTests.Mock
{
    public class ManualScheduler : IScheduler
    {
        private Queue<Action> actions = new Queue<Action>();

        public DateTimeOffset Now
        {
            get;
            set;
        }

        public IDisposable Schedule(Action action, TimeSpan dueTime)
        {
            actions.Enqueue(action);

            return Disposable.Create(() => { });
        }

        public IDisposable Schedule(Action action)
        {
            actions.Enqueue(action);

            return Disposable.Create(() => {  });
        }

        public void RunAll()
        {
            while (actions.Count > 0)
            {
                actions.Dequeue()();
            }
        }

        public void RunNext()
        {
            if (actions.Count > 0)
            {
                actions.Dequeue()();
            }
        }

        public int QueueSize
        {
            get { return actions.Count; }
        }
    }
}