text:
package com.dell.doradus.common;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Holds the definition of an object table.
 */
final public class TableDefinition {
    // Milliseconds within various units:
    private static final long MILLIS_IN_HOUR = 1000 * 60 * 60;      // 3,600,000
    private static final long MILLIS_IN_DAY = MILLIS_IN_HOUR * 24;  // 86,400,000
    private static final long MILLIS_IN_WEEK = MILLIS_IN_DAY * 7;   // 604,800,000

    // Application to which we belong:
    private ApplicationDefinition m_appDef;

    // This table's logical (Doradus) application, unique to its owning application:
    private String m_tableName;

    // Map of options used by this table. Option names are stored down-cased.
    private final Map<String, String> m_optionMap =
        new HashMap<String, String>();

    // Sharding start date and granularity from parsed options:
    private GregorianCalendar   m_shardingStartDate;
    private ShardingGranularity m_shardingGranularity;

    // Map of fields that this table owns. The map is final, but the field definitions
    // can be updated.
    private final SortedMap<String, FieldDefinition> m_fieldDefMap =
        new TreeMap<String, FieldDefinition>();

    // Map of alias definitions sorted by name:
    private final SortedMap<String, AliasDefinition> m_aliasDefMap =
        new TreeMap<String, AliasDefinition>();

    // The values we support for sharding-granularity:
    public enum ShardingGranularity {
        HOUR,
        DAY,
        WEEK,
        MONTH;

        /**
         * Return the {@link ShardingGranularity} for the given granularity in string form.
         * If the value is unrecognized, null is returned. This method can be used to
         * validate granularity values.
         *
         * @param  value    Sharding granularity as a string (case-insensitive): e.g., DAY,
         *                  WEEK, or MONTH.
         * @return          {@link ShardingGranularity} for given string or null if the
         *                  value is unrecognized.
         */
        public static ShardingGranularity fromString(String value) {
            // Since we only have a few values, just search.
            for (ShardingGranularity gran : values()) {
                if (gran.toString().equalsIgnoreCase(value)) {
                    return gran;
                }
            }
            return null;
        }   // fromString

    }   // enum ShardingGranularity

    /**
     * Create a new {@link TableDefinition} with all null properties.
     */
    public TableDefinition() { }

    /**
     * Create a new TableDefinition that belongs to the given application. It will
     * not have a name or any fields until they parsed or added.
     *
     * @param appDef    Application to which table belongs.
     */
    public TableDefinition(ApplicationDefinition appDef) {
        assert appDef != null;
        m_appDef = appDef;
    }   // constructor

    /**
     * Create a new TableDefinition with the given name and belonging to the given
     * application but with all default options and no initial fields. This constructor
     * is used by implicitly-defined tables.
     *
     * @param appDef    Application to which table belongs.
     * @param tableName Unique table name.
     */
    public TableDefinition(ApplicationDefinition appDef, String tableName) {
        assert appDef != null;
        assert tableName != null && tableName.length() > 0;

        // Save parameters
        m_appDef = appDef;
        setTableName(tableName);
    }   // constructor

    /**
     * Parse a table definition rooted at the given UNode. The given node is the table
     * definition, hence its name is the table name. The node must be a MAP whose child
     * nodes are table definitions such as "options" and "fields".
     *
     * @param tableNode {@link UNode} whose name is the table's name and whose
     *                  child nodes define the tables features.
     */
    public void parse(UNode tableNode) {
        assert tableNode != null;

        // Verify table name and save it.
        setTableName(tableNode.getName());

        // Examine table node's children.
        for (String childName : tableNode.getMemberNames()) {
            UNode childNode = tableNode.getMember(childName);

            // "fields"
            if (childName.equals("fields")) {
                // Process field definitions.
                for (String fieldName : childNode.getMemberNames()) {
                    // Create a FieldDefinition and parse the node's value into it.
                    // This will throw if the definition is invalid.
                    FieldDefinition fieldDef = new FieldDefinition();
                    fieldDef.parse(childNode.getMember(fieldName));

                    // Ensure field name is unique and add it to the table's field map.
                    addFieldDefinition(fieldDef);
                }

            // "options"
            } else if (childName.equals("options")) {
                // Examine each option.
                for (String optName : childNode.getMemberNames()) {
                    // Each option must be a simple value and specified only once.
                    UNode optNode = childNode.getMember(optName);
                    Utils.require(optNode.isValue(),
                                  "'option' must be a value: " + optNode);
                    Utils.require(getOption(optName) == null,
                                  "Option '" + optName + "' can only be specified once");

                    // Add option to option map, which performs some immediate validation.
                    setOption(optName, optNode.getValue());
                }

            // "aliases"
            } else if (childName.equals("aliases")) {
                // Parse and add each AliasDefinition.
                for (String aliasName : childNode.getMemberNames()) {
                    AliasDefinition aliasDef = new AliasDefinition(m_tableName);
                    aliasDef.parse(childNode.getMember(aliasName));
                    addAliasDefinition(aliasDef);
                }

            // Unrecognized
            } else {
                Utils.require(false, "Unrecognized 'table' element: " + childName);
            }
        }
        verify();
    }   // parse

    /**
     * Indicate if the given string is a valid table name. Table names must begin with a
     * letter and consist of all letters, digits, and underscores.
     *
     * @param tableName Candidate table name.
     * @return          True if the name is not null, not empty, starts with a letter, and
     *                  consists of only letters, digits, and underscores.
     */
    public static boolean isValidTableName(String tableName) {
        return tableName != null &&
               tableName.length() > 0 &&
               Utils.isLetter(tableName.charAt(0)) &&
               Utils.allAlphaNumUnderscore(tableName);
    }   // isValidTableName

    ///// Getters

    /**
     * Compute and return the date at which the shard with the given number starts based
     * on this table's sharding options. For example, if sharding-granularity is MONTH
     * and the sharding-start is 2012-10-15, then shard 2 starts on 2012-11-01.
     *
     * @param shardNumber   Shard number (> 0).
     * @return              Date on which the given shard starts. It will >= this table's
     *                      sharding-start option.
     */
    public Date computeShardStart(int shardNumber) {
        assert isSharded();
        assert shardNumber > 0;
        assert m_shardingStartDate != null;

        // Shard #1 always starts on the sharding-start date.
        Date result = null;
        if (shardNumber == 1) {
            result = m_shardingStartDate.getTime();
        } else {
            // Clone m_shardingStartDate and adjust by shard number.
            GregorianCalendar shardDate = (GregorianCalendar)m_shardingStartDate.clone();
            switch (m_shardingGranularity) {
            case HOUR:
                // Increment start date HOUR by shard number - 1.
                shardDate.add(Calendar.HOUR_OF_DAY, shardNumber - 1);
                break;

            case DAY:
                // Increment start date DAY by shard number - 1.
                shardDate.add(Calendar.DAY_OF_MONTH, shardNumber - 1);
                break;

            case WEEK:
                // Round the sharding-start date down to the MONDAY of the same week.
                // Then increment it's DAY by (shard number - 1) * 7.
                shardDate = Utils.truncateToWeek(m_shardingStartDate);
                shardDate.add(Calendar.DAY_OF_MONTH, (shardNumber - 1) * 7);
                break;

            case MONTH:
                // Increment start date MONTH by shard number - 1, but the day is always 1.
                shardDate.add(Calendar.MONTH, shardNumber - 1);
                shardDate.set(Calendar.DAY_OF_MONTH, 1);
                break;
            }
            result = shardDate.getTime();
        }
        assert computeShardNumber(result) == shardNumber;
        return result;
    }   // computeShardStart

    /**
     * Compute the shard number of an object belong to this table with the given
     * sharding-field value. This method should only be called on a sharded table for
     * which the sharding-field, sharding-granularity, and sharding-start options have
     * been set. The value returned will be 0 if the given date falls before the
     * sharding-start date. Otherwise it will be >= 1, representing the shard in which
     * the object should reside.
     *
     * @param shardingFieldValue    Value of an object's sharding-field as a Date in the
     *                              UTC time zone.
     * @return                      Shard number representing the shard in which the
     *                              object should reside.
     */
    public int computeShardNumber(Date shardingFieldValue) {
        assert shardingFieldValue != null;
        assert isSharded();
        assert m_shardingStartDate != null;

        // Convert the sharding field value into a calendar object. Note that this value
        // will have non-zero time elements.
        GregorianCalendar objectDate = new GregorianCalendar(Utils.UTC_TIMEZONE);
        objectDate.setTime(shardingFieldValue);

        // Since the start date has no time elements, if the result is negative, the object's
        // date is before the start date.
        if (objectDate.getTimeInMillis() < m_shardingStartDate.getTimeInMillis()) {
            // Object date occurs before sharding start. Shard number -> 0.
            return 0;
        }

        // Determine the shard number based on the granularity.
        int shardNumber = 1;
        switch (m_shardingGranularity) {
        case HOUR:
            // Increment shard number by difference in millis-per-hours.
            shardNumber += (objectDate.getTimeInMillis() - m_shardingStartDate.getTimeInMillis()) / MILLIS_IN_HOUR;
            break;

        case DAY:
            // Since the dates are in UTC, which doesn't have DST, the difference in days
            // is simply the difference in whole number of millis-per-day.
            shardNumber += (objectDate.getTimeInMillis() - m_shardingStartDate.getTimeInMillis()) / MILLIS_IN_DAY;
            break;

        case WEEK:
            // Truncate the sharding-start date to MONDAY. The difference in weeks is then
            // the shard increment.
            GregorianCalendar shard1week = Utils.truncateToWeek(m_shardingStartDate);
            shardNumber += (objectDate.getTimeInMillis() - shard1week.getTimeInMillis()) / MILLIS_IN_WEEK;
            break;

        case MONTH:
            // Difference in months is 12 * (difference in years) + (difference in months)
            int diffInMonths = ((objectDate.get(Calendar.YEAR) - m_shardingStartDate.get(Calendar.YEAR)) * 12) +
                               (objectDate.get(Calendar.MONTH) - m_shardingStartDate.get(Calendar.MONTH));
            shardNumber += diffInMonths;
            break;

        default:
            Utils.require(false, "Unknown sharding-granularity: " + m_shardingGranularity);
        }
        return shardNumber;
    }   // computeShardNumber

    /**
     * Get the {@link ApplicationDefinition} to which this table definition applies. Note
     * that while parsing table definitions, a TableDefinition object may not yet be known
     * to the ApplicationDefinition to which it points.
     *
     * @return {@link ApplicationDefinition} to which this table definition applies.
     */
    public ApplicationDefinition getAppDef() {
        Utils.require(m_appDef != null, "Table has not been assigned to an application");
        return m_appDef;
    }   // getAppDef

    /**
     * Get the resource path for this table.
     *
     * @return  String in the form {application path}/Table:{table}
     */
    public String getPath() {
        Utils.require(m_appDef != null, "Table has not been added to an application");
        return m_appDef.getPath() + "/Table:" + m_tableName;
    }   // getPath

    /**
     * Get the name of the table represented by this table definition. This is the logical
     * or "REST name", which is unique among tables owned by the application but not
     * necessarily between applications. Also, not that the database table name will be
     * different than this name.
     *
     * @return  This table's name.
     */
    public String getTableName() {
        return m_tableName;
    }   // getTableName

    /**
     * Get the field definitions for this table as an Collection<{@link FieldDefinition}>
     * object. All outer and nested group, link, and scalar fields are returned. To expand
     * group fields without traversing the same {@link FieldDefinition} twice, call this
     * method and skip fields for which {@link FieldDefinition#isNestedField()} is true.
     * Then, for each field for which {@link FieldDefinition#isGroupField()} is true, call
     * {@link FieldDefinition#getNestedFields()} to iterate its immediate nested fields.
     *

     * NOTE: The field definitions returned by this method's iterator are not copied, so
     * be careful not to modify them!
     *
     * @return  An iterator that returns all {@link FieldDefinition}s owned by this table.
     */
    public Collection<FieldDefinition> getFieldDefinitions() {
        return m_fieldDefMap.values();
    }   // getFieldDefinitions

    /**
     * Get the {@link FieldDefinition} for the field with the given name, used by this
     * table. If there is no such field known, null is returned.
     *
     * @param   fieldName   Candidate field name.
     * @return              {@link FieldDefinition} of corresponding field if known to
     *                      this table, otherwise null.
     */
    public FieldDefinition getFieldDef(String fieldName) {
        return m_fieldDefMap.get(fieldName);
    }   // getFieldDef

    /**
     * Return the value of the option with the given name or null if there is no such
     * option stored. This method does not verify that the option name is actually
     * valid.
     *
     * @param optionName    Name of option (case-insensitive).
     * @return              Value of current value for option or null if there isn't one.
     */
    public String getOption(String optionName) {
        return m_optionMap.get(optionName.toLowerCase());
    }   // getOption

    /**
     * Get a Set of all option names currently defined for this table. For each
     * option name in the returned set, {@link #getOption(String)} can be called to get
     * the value of that option.
     *
     * @return  Set of all option names currently defined for this table. The set will be
     *          empty if no options are defined.
     */
    public Set<String> getOptionNames() {
        return m_optionMap.keySet();
    }   // getOptionNames

    /**
     * Get the {@link FieldDefinition} of the sharding-field for this table. If this table
     * is not sharded, the sharding-field option has not yet been set, or the sharding
     * field has not yet been defined, null is returned.
     *
     * @return  The {@link FieldDefinition} of the sharding-field for this table or null
     *          if the table is not sharded or the sharding-field has not yet been defined.
     */
    public FieldDefinition getShardingField() {
        if (!isSharded()) {
            return null;
        }
        String shardingFieldNme = getOption(CommonDefs.OPT_SHARDING_FIELD);
        if (shardingFieldNme == null) {
            return null;
        }
        return getFieldDef(shardingFieldNme);   // may return null
    }   // getShardingField

    /**
     * Determine the shard number of the given object for this table. If this table is
     * not sharded or the object has no value for its sharding field
     * (see {@link #getShardingField()}), the shard number is 0. Otherwise, the
     * sharding-field value is used to determine the shard number based on the table's
     * sharding start and granularity.
     *

     * Note: The caller must ensure that if a value exists for the sharding-field, it is
     * loaded into the given DBObject. Otherwise, this method will incorrectly assume the
     * sharding value has not been set and therefore imply shard 0.
     *
     * @param dbObj     {@link DBObject} to determine shard number.
     * @return          0 if the object's owning table is not sharded, the object has no
     *                  sharding-field value, or the object's sharding field value places
     *                  it before sharding was started. Otherwise, the value is > 0.
     */
    public int getShardNumber(DBObject dbObj) {
        if (!isSharded()) {
            return 0;
        }

        String value = dbObj.getFieldValue(getShardingField().getName());
        if (value == null) {
            return 0;
        }
        Date shardingFieldValue = Utils.dateFromString(value);
        return computeShardNumber(shardingFieldValue);
    }   // getShardNumber

    /**
     * Get the alias definitions owned by this table as an Iterable object. NOTE: The
     * definitions are not copied, hence the caller must be careful not to modify it!
     *
     * @return AliasDefinitions owne by this table as an Iterable.
     */
    public Iterable<AliasDefinition> getAliasDefinitions() {
        return m_aliasDefMap.values();
    }   // getAliasDefinitions

    /**
     * Get the {@link AliasDefinition} belonging to this table with the given name, or
     * null if this table does not own such an alias.
     *
     * @param aliasName Name of alias owned by this table.
     * @return          {@link AliasDefinition} of alias or null if unknown.
     */
    public AliasDefinition getAliasDef(String aliasName) {
        return m_aliasDefMap.get(aliasName);
    }   // getAliasDef

    /**
     * Return true if the given field name is an MV scalar field. Since scalar fields
     * must be declared as MV, only predefined fields can be MV.
     *
     * @param   fieldName   Candidate field name.
     * @return              True if the name is a known MV scalar field, otherwise false.
     */
    public boolean isCollection(String fieldName) {
        FieldDefinition fieldDef = m_fieldDefMap.get(fieldName);
        return fieldDef != null && fieldDef.isScalarField() && fieldDef.isCollection();
    }   // isCollection

    /**
     * Return true if this table definition possesses a FieldDefinition with the given name
     * that defines a Link field.
     *
     * @param   fieldName   Candidate field name.
     * @return              True if the name is a known Link field, otherwise false.
     */
    public boolean isLinkField(String fieldName) {
        FieldDefinition fieldDef = m_fieldDefMap.get(fieldName);
        return fieldDef != null && fieldDef.isLinkField();
    }   // isLinkField

    /**
     * Return true if this table definition possesses a FieldDefinition with the given name
     * that defines a scalar field or if the field is undefined and therefore considered a
     * text field.
     *
     * @param   fieldName   Candidate field name.
     * @return              True if the name is a known scalar field, otherwise false.
     */
    public boolean isScalarField(String fieldName) {
        FieldDefinition fieldDef = m_fieldDefMap.get(fieldName);
        return fieldDef == null || fieldDef.isScalarField();
    }   // isScalarField

    /**
     * Return true if this table is sharded, meaning the sharding-field option has been
     * defined. Note, however, that we don't check to see if the sharding field itself
     * has been defined and is of the appropriate type.
     *
     * @return  True if this table is sharded.
     */
    public boolean isSharded() {
        return getOption(CommonDefs.OPT_SHARDING_FIELD) != null;
    }   // isSharded

    /**
     * Serialize this table definition into a {@link UNode} tree and return the root node.
     *
     * @return  The root node of a {@link UNode} tree for this table definition.
     */
    public UNode toDoc() {
        // The root node is a MAP, but we set its tag name to "table" for XML.
        UNode tableNode = UNode.createMapNode(m_tableName, "table");

        // Add options, if any.
        if (getOptionNames().size() > 0) {
            // Options node is a MAP.
            UNode optsNode = tableNode.addMapNode("options");
            for (String optName : getOptionNames()) {
                // Set the tag name of each option to "option" for XML.
                optsNode.addValueNode(optName, getOption(optName), "option");
            }
        }

        // Add fields, if any.
        if (m_fieldDefMap.size() > 0) {
            // Fields node is a MAP.
            UNode fieldsNode = tableNode.addMapNode("fields");

            // Add definitions of outer (non-nested) fields only. Each group field will
            // recurse to its nested fields.
            for (FieldDefinition fieldDef : m_fieldDefMap.values()) {
                if (!fieldDef.isNestedField()) {
                    fieldsNode.addChildNode(fieldDef.toDoc());
                }
            }
        }

        // Add "aliases", if any.
        if (m_aliasDefMap.size() > 0) {
            // Aliases node is a MAP.
            UNode aliasesNode = tableNode.addMapNode("aliases");
            for (AliasDefinition aliasDef : m_aliasDefMap.values()) {
                aliasesNode.addChildNode(aliasDef.toDoc());
            }
        }

        return tableNode;
    }   // toDoc

    // Return "Table 'foo'"
    @Override
    public String toString() {
        return "Table '" + m_tableName + "'";
    }   // toString

    ///// Setters

    /**
     * Transfer this table definition to the given {@link ApplicationDefinition} by
     * re-assigning its ApplicationDefinition pointer to the given one.
     *
     * @param appDef    New {@link ApplicationDefinition} owner of this table.
     */
    public void setApplication(ApplicationDefinition appDef) {
        assert appDef != null;
        m_appDef = appDef;
    }   // setApplication

    /**
     * Set this table's name to the given value. If the name is not a valid table name,
     * an exception is thrown. This method should only be used when building a new
     * TableDefinition -- it does not change the name of an existing table.
     *
     * @param tableName New name of this table.
     */
    public void setTableName(String tableName) {
        if (!isValidTableName(tableName)) {
            throw new IllegalArgumentException("Invalid table name: " + tableName);
        }
        m_tableName = tableName;
    }   // setTableName

    /**
     * Set the option with the given name to the given value. This method does not
     * validate that the given option name and value are valid since options are
     * storage service-specific.
     *
     * @param optionName    Option name. This is down-cased when stored.
     * @param optionValue   Option value. Cannot be null.
     */
    public void setOption(String optionName, String optionValue) {
        // Ensure option value is not empty and trim excess whitespace.
        Utils.require(optionName != null, "optionName");
        Utils.require(optionValue != null && optionValue.trim().length() > 0,
                      "Value for option '" + optionName + "' can not be empty");
        optionValue = optionValue.trim();
        m_optionMap.put(optionName.toLowerCase(), optionValue);

        // sharding-granularity and sharding-start are validated here since we must set
        // local members when the table's definition is parsed.
        if (optionName.equalsIgnoreCase(CommonDefs.OPT_SHARDING_GRANULARITY)) {
            m_shardingGranularity = ShardingGranularity.fromString(optionValue);
            Utils.require(m_shardingGranularity != null,
                          "Unrecognized 'sharding-granularity' value: " + optionValue);
        } else if (optionName.equalsIgnoreCase(CommonDefs.OPT_SHARDING_START)) {
            Utils.require(isValidShardDate(optionValue),
                          "'sharding-start' must be YYYY-MM-DD: " + optionValue);
            m_shardingStartDate = new GregorianCalendar(Utils.UTC_TIMEZONE);
            m_shardingStartDate.setTime(Utils.dateFromString(optionValue));
        }
    }   // setOption

    /**
     * Add the given {@link FieldDefinition} object to this table's list of fields. This
     * method recursively validates and adds the given field and its nested fields, if
     * any, to this table.
     *
     * @param fieldDef  New {@link FieldDefinition} to add to this table.
     */
    public void addFieldDefinition(FieldDefinition fieldDef) {
        // Assure field name is unique and add the definition to the map.
        Utils.require(!Utils.isEmpty(fieldDef.getName()), "Field name is required");
        Utils.require(!m_fieldDefMap.containsKey(fieldDef.getName()),
                      "Field names must be unique: " + fieldDef.getName());
        m_fieldDefMap.put(fieldDef.getName(), fieldDef);
        fieldDef.setTableDefinition(this);

        // Recurse to nested fields, if any.
        if (fieldDef.isGroupField()) {
            for (FieldDefinition nestedFieldDef : fieldDef.getNestedFields()) {
                addFieldDefinition(nestedFieldDef);
            }
        }
    }   // addFieldDefinition

    /**
     * Get the TableDefinition of the "extent" table for the given link field.
     *
     * @param  linkDef  {@link FieldDefinition} for a link field.
     * @return          {@link TableDefinition} for link's extent (target) table.
     */
    public TableDefinition getLinkExtentTableDef(FieldDefinition linkDef) {
        assert linkDef != null;
        assert linkDef.isLinkType();
        assert m_appDef != null;

        TableDefinition tableDef = m_appDef.getTableDef(linkDef.getLinkExtent());
        assert tableDef != null;
        return tableDef;
    }   // getLinkExtentTableDef

    /**
     * Examine the given column name and, if it represents an MV link value, add it to the
     * given MV link value map. If a link value is successfully extracted, true is returned.
     * If the column name is not in the format used for MV link values, false is returned.
     *
     * @param colName        Column name from an object record belonging to this table (in
     *                       string form).
     * @param mvLinkValueMap Link value map to be updated if the column represents a valid
     *                       link value.
     * @return               True if a link value is extracted and added to the map; false
     *                       means the column name was not a link value.
     */
    public boolean extractLinkValue(String colName, Map<String, Set<String>> mvLinkValueMap) {
        // Link column names always begin with '~'.
        if (colName.length() == 0 || colName.charAt(0) != '~') {
            return false;
        }

        // A '/' should separate the field name and object value.
        int slashInx = colName.indexOf('/');
        if (slashInx < 0) {
            return false;
        }

        // Extract the field name and ensure we know about this field.
        String fieldName = colName.substring(1, slashInx);

        // Extract the link value's target object ID and add it to the value set for the field.
        String linkValue = colName.substring(slashInx + 1);
        Set<String> valueSet = mvLinkValueMap.get(fieldName);
        if (valueSet == null) {
            // First value for this field.
            valueSet = new HashSet<String>();
            mvLinkValueMap.put(fieldName, valueSet);
        }
        valueSet.add(linkValue);
        return true;
    }   // extractLinkValue

    ///// Builder interface

    /**
     * Create a new {@link Builder} object. This is a convenience method, which calls
     * {@code new Builder()}.
     *
     * @return  A new {@link Builder} object for building a {@link TableDefinition}
     *          using the builder pattern.
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Helper class for building {@link TableDefinition} objects using the builder
     * pattern.
     */
    public static class Builder {
        private final TableDefinition m_tableDef = new TableDefinition();

        /**
         * Create a new {@link Builder} object to begin construction of a new
         * {@link TableDefinition}.
         */
        public Builder() {};

        /**
         * Verify and return the completed {@link TableDefinition} object.
         *
         * @return  The completed {@link TableDefinition} object.
         */
        public TableDefinition build() {
            m_tableDef.verify();
            return m_tableDef;
        }

        /**
         * Set the table's name.
         *
         * @param tableName Table name.
         * @return          This {@link Builder}.
         */
        public Builder withName(String tableName) {
            m_tableDef.setTableName(tableName);
            return this;
        }

        /**
         * Set the given table option. If the given option is already set, its value is
         * replaced with the new value or unset if the given value is null.
         *
         * @param optName   Table option name.
         * @param optValue  New option value or null to unset the option.
         * @return          This {@link Builder}.
         */
        public Builder withOption(String optName, String optValue) {
            m_tableDef.setOption(optName, optValue);
            return this;
        }

        /**
         * Add the given {@link FieldDefinition} to the table.
         *
         * @param fieldDef  {@link FieldDefinition} of a field to add to this table.
         * @return          This {@link Builder}.
         */
        public Builder withField(FieldDefinition fieldDef) {
            m_tableDef.addFieldDefinition(fieldDef);
            return this;
        }

    }   // class Builder

    ///// Private methods

    // Add the given alias definition to this table definition.
    private void addAliasDefinition(AliasDefinition aliasDef) {
        // Prerequisites:
        assert aliasDef != null;
        assert !m_aliasDefMap.containsKey(aliasDef.getName());
        assert aliasDef.getTableName().equals(this.getTableName());

        m_aliasDefMap.put(aliasDef.getName(), aliasDef);
    }   // addAliasDefinition

    // Verify that the given shard-starting date is in the format YYYY-MM-DD. If the format
    // is bad, just return false.
    private boolean isValidShardDate(String shardDate) {
        try {
            // If the format is invalid, a ParseException is thrown.
            Utils.dateFromString(shardDate);
            return true;
        } catch (IllegalArgumentException ex) {
            return false;
        }
    }   // isValidShardDate

    // Verify that the table is complete. Currently, only xlinks require checking.
    private void verify() {
        for (FieldDefinition fieldDef : m_fieldDefMap.values()) {
            if (fieldDef.isXLinkField()) {
                verifyJunctionField(fieldDef);
            }
        }
    }

    // Verify that the given xlink's junction field is either _ID or an SV text field.
    private void verifyJunctionField(FieldDefinition xlinkDef) {
        String juncField = xlinkDef.getXLinkJunction();
        if (!"_ID".equals(juncField)) {
            FieldDefinition juncFieldDef = m_fieldDefMap.get(juncField);
            Utils.require(juncFieldDef != null,
                          String.format("Junction field for xlink '%s' has not been defined: %s",
                                        xlinkDef.getName(), xlinkDef.getXLinkJunction()));
            Utils.require(juncFieldDef.getType() == FieldType.TEXT,
                          String.format("Junction field for xlink '%s' must be a text field: ",
                                        xlinkDef.getName(), xlinkDef.getXLinkJunction()));
        }
    }   // verifyJunctionField

    /**
     * Replaces of all occurences of aliases defined with this table, by their expressions.
     * Now a simple string.replace is used.
     *
     * @param str string to replace
     * @return string with replaced aliases. If there were no aliases, the string is unchanged.
     */
	public String replaceAliaces(String str) {
		return getAppDef().replaceAliaces(str);
	}

}   // class TableDefinition


text:
"""autogenerated by genpy from foo_msgs/Observed_angles.msg. Do not edit."""
import sys
python3 = True if sys.hexversion > 0x03000000 else False
import genpy
import struct

import foo_msgs.msg
import std_msgs.msg

class Observed_angles(genpy.Message):
  _md5sum = "8b371119782f9b738e9c6225d8efba1c"
  _type = "foo_msgs/Observed_angles"
  _has_header = True #flag to mark the presence of a Header object
  _full_text = """Header header
Angles[] Obj

================================================================================
MSG: std_msgs/Header
# Standard metadata for higher-level stamped data types.
# This is generally used to communicate timestamped data
# in a particular coordinate frame.
#
# sequence ID: consecutively increasing ID
uint32 seq
#Two-integer timestamp that is expressed as:
# * stamp.secs: seconds (stamp_secs) since epoch
# * stamp.nsecs: nanoseconds since stamp_secs
# time-handling sugar is provided by the client library
time stamp
#Frame this data is associated with
# 0: no frame
# 1: global frame
string frame_id

================================================================================
MSG: foo_msgs/Angles
string name
Angle[] landmarks
Angle[] quads

================================================================================
MSG: foo_msgs/Angle
string name
bool visible
float64 azim
float64 elev

"""
  __slots__ = ['header','Obj']
  _slot_types = ['std_msgs/Header','foo_msgs/Angles[]']

  def __init__(self, *args, **kwds):
    """
    Constructor. Any message fields that are implicitly/explicitly
    set to None will be assigned a default value. The recommend
    use is keyword arguments as this is more robust to future message
    changes.  You cannot mix in-order arguments and keyword arguments.

    The available fields are:
       header,Obj

    :param args: complete set of field values, in .msg order
    :param kwds: use keyword arguments corresponding to message field names
    to set specific fields.
    """
    if args or kwds:
      super(Observed_angles, self).__init__(*args, **kwds)
      #message fields cannot be None, assign default values for those that are
      if self.header is None:
        self.header = std_msgs.msg.Header()
      if self.Obj is None:
        self.Obj = []
    else:
      self.header = std_msgs.msg.Header()
      self.Obj = []

  def _get_types(self):
    """
    internal API method
    """
    return self._slot_types

  def serialize(self, buff):
    """
    serialize message into buffer
    :param buff: buffer, ``StringIO``
    """
    try:
      _x = self
      buff.write(_struct_3I.pack(_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs))
      _x = self.header.frame_id
      length = len(_x)
      if python3 or type(_x) == unicode:
        _x = _x.encode('utf-8')
        length = len(_x)
      buff.write(struct.pack('<I%ss'%length, length, _x))
      length = len(self.Obj)
      buff.write(_struct_I.pack(length))
      for val1 in self.Obj:
        _x = val1.name
        length = len(_x)
        if python3 or type(_x) == unicode:
          _x = _x.encode('utf-8')
          length = len(_x)
        buff.write(struct.pack('<I%ss'%length, length, _x))
        length = len(val1.landmarks)
        buff.write(_struct_I.pack(length))
        for val2 in val1.landmarks:
          _x = val2.name
          length = len(_x)
          if python3 or type(_x) == unicode:
            _x = _x.encode('utf-8')
            length = len(_x)
          buff.write(struct.pack('<I%ss'%length, length, _x))
          _x = val2
          buff.write(_struct_B2d.pack(_x.visible, _x.azim, _x.elev))
        length = len(val1.quads)
        buff.write(_struct_I.pack(length))
        for val2 in val1.quads:
          _x = val2.name
          length = len(_x)
          if python3 or type(_x) == unicode:
            _x = _x.encode('utf-8')
            length = len(_x)
          buff.write(struct.pack('<I%ss'%length, length, _x))
          _x = val2
          buff.write(_struct_B2d.pack(_x.visible, _x.azim, _x.elev))
    except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(_x))))
    except TypeError as te: self._check_types(ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(_x))))

  def deserialize(self, str):
    """
    unpack serialized message in str into this message instance
    :param str: byte array of serialized message, ``str``
    """
    try:
      if self.header is None:
        self.header = std_msgs.msg.Header()
      if self.Obj is None:
        self.Obj = None
      end = 0
      _x = self
      start = end
      end += 12
      (_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs,) = _struct_3I.unpack(str[start:end])
      start = end
      end += 4
      (length,) = _struct_I.unpack(str[start:end])
      start = end
      end += length
      if python3:
        self.header.frame_id = str[start:end].decode('utf-8')
      else:
        self.header.frame_id = str[start:end]
      start = end
      end += 4
      (length,) = _struct_I.unpack(str[start:end])
      self.Obj = []
      for i in range(0, length):
        val1 = foo_msgs.msg.Angles()
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        start = end
        end += length
        if python3:
          val1.name = str[start:end].decode('utf-8')
        else:
          val1.name = str[start:end]
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        val1.landmarks = []
        for i in range(0, length):
          val2 = foo_msgs.msg.Angle()
          start = end
          end += 4
          (length,) = _struct_I.unpack(str[start:end])
          start = end
          end += length
          if python3:
            val2.name = str[start:end].decode('utf-8')
          else:
            val2.name = str[start:end]
          _x = val2
          start = end
          end += 17
          (_x.visible, _x.azim, _x.elev,) = _struct_B2d.unpack(str[start:end])
          val2.visible = bool(val2.visible)
          val1.landmarks.append(val2)
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        val1.quads = []
        for i in range(0, length):
          val2 = foo_msgs.msg.Angle()
          start = end
          end += 4
          (length,) = _struct_I.unpack(str[start:end])
          start = end
          end += length
          if python3:
            val2.name = str[start:end].decode('utf-8')
          else:
            val2.name = str[start:end]
          _x = val2
          start = end
          end += 17
          (_x.visible, _x.azim, _x.elev,) = _struct_B2d.unpack(str[start:end])
          val2.visible = bool(val2.visible)
          val1.quads.append(val2)
        self.Obj.append(val1)
      return self
    except struct.error as e:
      raise genpy.DeserializationError(e) #most likely buffer underfill

  def serialize_numpy(self, buff, numpy):
    """
    serialize message with numpy array types into buffer
    :param buff: buffer, ``StringIO``
    :param numpy: numpy python module
    """
    try:
      _x = self
      buff.write(_struct_3I.pack(_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs))
      _x = self.header.frame_id
      length = len(_x)
      if python3 or type(_x) == unicode:
        _x = _x.encode('utf-8')
        length = len(_x)
      buff.write(struct.pack('<I%ss'%length, length, _x))
      length = len(self.Obj)
      buff.write(_struct_I.pack(length))
      for val1 in self.Obj:
        _x = val1.name
        length = len(_x)
        if python3 or type(_x) == unicode:
          _x = _x.encode('utf-8')
          length = len(_x)
        buff.write(struct.pack('<I%ss'%length, length, _x))
        length = len(val1.landmarks)
        buff.write(_struct_I.pack(length))
        for val2 in val1.landmarks:
          _x = val2.name
          length = len(_x)
          if python3 or type(_x) == unicode:
            _x = _x.encode('utf-8')
            length = len(_x)
          buff.write(struct.pack('<I%ss'%length, length, _x))
          _x = val2
          buff.write(_struct_B2d.pack(_x.visible, _x.azim, _x.elev))
        length = len(val1.quads)
        buff.write(_struct_I.pack(length))
        for val2 in val1.quads:
          _x = val2.name
          length = len(_x)
          if python3 or type(_x) == unicode:
            _x = _x.encode('utf-8')
            length = len(_x)
          buff.write(struct.pack('<I%ss'%length, length, _x))
          _x = val2
          buff.write(_struct_B2d.pack(_x.visible, _x.azim, _x.elev))
    except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(_x))))
    except TypeError as te: self._check_types(ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(_x))))

  def deserialize_numpy(self, str, numpy):
    """
    unpack serialized message in str into this message instance using numpy for array types
    :param str: byte array of serialized message, ``str``
    :param numpy: numpy python module
    """
    try:
      if self.header is None:
        self.header = std_msgs.msg.Header()
      if self.Obj is None:
        self.Obj = None
      end = 0
      _x = self
      start = end
      end += 12
      (_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs,) = _struct_3I.unpack(str[start:end])
      start = end
      end += 4
      (length,) = _struct_I.unpack(str[start:end])
      start = end
      end += length
      if python3:
        self.header.frame_id = str[start:end].decode('utf-8')
      else:
        self.header.frame_id = str[start:end]
      start = end
      end += 4
      (length,) = _struct_I.unpack(str[start:end])
      self.Obj = []
      for i in range(0, length):
        val1 = foo_msgs.msg.Angles()
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        start = end
        end += length
        if python3:
          val1.name = str[start:end].decode('utf-8')
        else:
          val1.name = str[start:end]
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        val1.landmarks = []
        for i in range(0, length):
          val2 = foo_msgs.msg.Angle()
          start = end
          end += 4
          (length,) = _struct_I.unpack(str[start:end])
          start = end
          end += length
          if python3:
            val2.name = str[start:end].decode('utf-8')
          else:
            val2.name = str[start:end]
          _x = val2
          start = end
          end += 17
          (_x.visible, _x.azim, _x.elev,) = _struct_B2d.unpack(str[start:end])
          val2.visible = bool(val2.visible)
          val1.landmarks.append(val2)
        start = end
        end += 4
        (length,) = _struct_I.unpack(str[start:end])
        val1.quads = []
        for i in range(0, length):
          val2 = foo_msgs.msg.Angle()
          start = end
          end += 4
          (length,) = _struct_I.unpack(str[start:end])
          start = end
          end += length
          if python3:
            val2.name = str[start:end].decode('utf-8')
          else:
            val2.name = str[start:end]
          _x = val2
          start = end
          end += 17
          (_x.visible, _x.azim, _x.elev,) = _struct_B2d.unpack(str[start:end])
          val2.visible = bool(val2.visible)
          val1.quads.append(val2)
        self.Obj.append(val1)
      return self
    except struct.error as e:
      raise genpy.DeserializationError(e) #most likely buffer underfill

_struct_I = genpy.struct_I
_struct_3I = struct.Struct("<3I")
_struct_B2d = struct.Struct("<B2d")

redpajama_set_name: RedPajamaGithub


text:
package controllers

import (
	"github.com/miyurusagarage/memeeconomy/models"

)

type LeaderboardController struct {
	BaseController
}

func (c *LeaderboardController) Get() {

	users,_ := models.GetLeaders()
	x:=models.User{}
	n:=[]models.User{x}
	vc := append(n ,*users...)
	c.Data["data"] = vc
	c.TplName = "leaderboard.tpl"
}


text:
import sys
import nose
import os

SDK_PATH = os.environ.get("APPENGINE_SDK", "/usr/local/google_appengine/")

def main():
    #logging.basicConfig(level=logging.DEBUG)
    sys.path.insert(0, SDK_PATH)
    sys.path.insert(0, "stashboard")
    sys.path.insert(0, "stashboard/contrib")
    import dev_appserver
    dev_appserver.fix_sys_path()
    nose.main()

if __name__ == '__main__':
    main()


text:
package com.sun.tools.xjc.reader.gbind;

import java.util.Collections;
import java.util.Iterator;

/**
 * A set over a list of {@link Element}.
 *
 * @author Kohsuke Kawaguchi
 */
public interface ElementSet extends Iterable<Element> {
    /**
     * For each element in this set, adds an edge to the given element.
     */
    void addNext(Element element);

    ElementSet EMPTY_SET = new ElementSet() {
        @Override
        public void addNext(Element element) {
            // noop
        }

        @Override
        public boolean contains(ElementSet element) {
            return this==element;
        }

        @Override
        public Iterator<Element> iterator() {
            return Collections.emptyIterator();
        }
    };

    /**
     * Doesn't have to be strict (it's OK for this method to return false
     * when it's actually true) since this is used just for optimization.
     *
     * (Erring on the other side is NG)
     */
    boolean contains(ElementSet rhs);
}


text:
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

/* eslint-env browser */
import {BehaviorSubject, Observable, Subject, Subscription} from 'rxjs';
import DeviceTransport from './device_transport';
import type {SerialPort, Serial, SerialOptions, Navigator, SerialPortFilter} from "pigweedjs/types/serial"

const DEFAULT_SERIAL_OPTIONS: SerialOptions & {baudRate: number} = {
  // Some versions of chrome use `baudrate` (linux)
  baudrate: 115200,
  // Some versions use `baudRate` (chromebook)
  baudRate: 115200,
  databits: 8,
  parity: 'none',
  stopbits: 1,
};

interface PortReadConnection {
  chunks: Observable<Uint8Array>;
  errors: Observable<Error>;
}

interface PortConnection extends PortReadConnection {
  sendChunk: (chunk: Uint8Array) => Promise<void>;
}

export class DeviceLostError extends Error {
  message = 'The device has been lost';
}

export class DeviceLockedError extends Error {
  message =
    "The device's port is locked. Try unplugging it" +
    ' and plugging it back in.';
}

/**
 * WebSerialTransport sends and receives UInt8Arrays to and
 * from a serial device connected over USB.
 */
export class WebSerialTransport implements DeviceTransport {
  chunks = new Subject<Uint8Array>();
  errors = new Subject<Error>();
  connected = new BehaviorSubject<boolean>(false);
  private portConnections: Map<SerialPort, PortConnection> = new Map();
  private activePortConnectionConnection: PortConnection | undefined;
  private rxSubscriptions: Subscription[] = [];

  constructor(
    private serial: Serial = (navigator as unknown as Navigator).serial,
    private filters: SerialPortFilter[] = [],
    private serialOptions = DEFAULT_SERIAL_OPTIONS
  ) { }

  /**
   * Send a UInt8Array chunk of data to the connected device.
   * @param {Uint8Array} chunk The chunk to send
   */
  async sendChunk(chunk: Uint8Array): Promise<void> {
    if (this.activePortConnectionConnection) {
      return this.activePortConnectionConnection.sendChunk(chunk);
    }
    throw new Error('Device not connected');
  }

  /**
   * Attempt to open a connection to a device. This includes
   * asking the user to select a serial port and should only
   * be called in response to user interaction.
   */
  async connect(): Promise<void> {
    const port = await this.serial.requestPort({filters: this.filters});
    await this.connectPort(port);
  }

  private disconnect() {
    for (const subscription of this.rxSubscriptions) {
      subscription.unsubscribe();
    }
    this.rxSubscriptions = [];

    this.activePortConnectionConnection = undefined;
    this.connected.next(false);
  }

  /**
   * Connect to a given SerialPort. This involves no user interaction.
   * and can be called whenever a port is available.
   */
  async connectPort(port: SerialPort): Promise<void> {
    this.disconnect();

    this.activePortConnectionConnection =
      this.portConnections.get(port) ?? (await this.conectNewPort(port));

    this.connected.next(true);

    this.rxSubscriptions.push(
      this.activePortConnectionConnection.chunks.subscribe(
        (chunk: any) => {
          this.chunks.next(chunk);
        },
        (err: any) => {
          throw new Error(`Chunks observable had an unexpected error ${err}`);
        },
        () => {
          this.connected.next(false);
          this.portConnections.delete(port);
          // Don't complete the chunks observable because then it would not
          // be able to forward any future chunks.
        }
      )
    );

    this.rxSubscriptions.push(
      this.activePortConnectionConnection.errors.subscribe((error: any) => {
        this.errors.next(error);
        if (error instanceof DeviceLostError) {
          // The device has been lost
          this.connected.next(false);
        }
      })
    );
  }

  private async conectNewPort(port: SerialPort): Promise<PortConnection> {
    await port.open(this.serialOptions);
    const writer = port.writable.getWriter();

    async function sendChunk(chunk: Uint8Array) {
      await writer.ready;
      await writer.write(chunk);
    }

    const {chunks, errors} = this.getChunks(port);

    const connection: PortConnection = {sendChunk, chunks, errors};
    this.portConnections.set(port, connection);
    return connection;
  }

  private getChunks(port: SerialPort): PortReadConnection {
    const chunks = new Subject<Uint8Array>();
    const errors = new Subject<Error>();

    async function read() {
      if (!port.readable) {
        throw new DeviceLostError();
      }
      if (port.readable.locked) {
        throw new DeviceLockedError();
      }
      await port.readable.pipeTo(
        new WritableStream({
          write: chunk => {
            chunks.next(chunk);
          },
          close: () => {
            chunks.complete();
            errors.complete();
          },
          abort: () => {
            // Reconnect to the port.
            connect();
          },
        })
      );
    }

    function connect() {
      read().catch(err => {
        // Don't error the chunks observable since that stops it from
        // reading any more packets, and we often want to continue
        // despite an error. Instead, push errors to the 'errors'
        // observable.
        errors.next(err);
      });
    }

    connect();

    return {chunks, errors};
  }
}


text: App for testing CoreLocation framework


text:
package io.lacuna.bifurcan.utils;

import io.lacuna.bifurcan.IDurableEncoding;
import io.lacuna.bifurcan.IEntry;
import io.lacuna.bifurcan.IList;
import io.lacuna.bifurcan.LinearList;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @author ztellman
 */
public class Iterators {

  private static final Object NONE = new Object();

  public static final Iterator EMPTY = new Iterator() {
    @Override
    public boolean hasNext() {
      return false;
    }

    @Override
    public Object next() {
      throw new NoSuchElementException();
    }
  };

  public static <V> Iterator<V> mergeSort(IList<Iterator<V>> iterators, Comparator<V> comparator) {

    if (iterators.size() == 1) {
      return iterators.first();
    }

    PriorityQueue<IEntry<V, Iterator<V>>> heap = new PriorityQueue<>(Comparator.comparing(IEntry::key, comparator));
    for (Iterator<V> it : iterators) {
      if (it.hasNext()) {
        heap.add(IEntry.of(it.next(), it));
      }
    }

    return from(
        () -> heap.size() > 0,
        () -> {
          IEntry<V, Iterator<V>> e = heap.poll();
          if (e.value().hasNext()) {
            heap.add(IEntry.of(e.value().next(), e.value()));
          }
          return e.key();
        }
    );
  }

  /**
   * A utility class for dynamically appending and prepending iterators to a collection, which itself can be iterated
   * over.
   */
  public static class IteratorStack<V> implements Iterator<V> {

    LinearList<Iterator<V>> iterators = new LinearList<>();

    public IteratorStack() {
    }

    public IteratorStack(Iterator<V>... its) {
      for (Iterator<V> it : its) {
        iterators.addFirst(it);
      }
    }

    private void primeIterator() {
      while (iterators.size() > 0 && !iterators.first().hasNext()) {
        iterators.removeFirst();
      }
    }

    @Override
    public boolean hasNext() {
      primeIterator();
      return iterators.size() > 0 && iterators.first().hasNext();
    }

    @Override
    public V next() {
      primeIterator();
      if (iterators.size() == 0) {
        throw new NoSuchElementException();
      }
      return iterators.first().next();
    }

    public void addFirst(Iterator<V> it) {
      iterators.addFirst(it);
    }

    public void addLast(Iterator<V> it) {
      iterators.addLast(it);
    }
  }

  public static <V> boolean equals(Iterator<V> a, Iterator<V> b, BiPredicate<V, V> equals) {
    while (a.hasNext()) {
      if (!equals.test(a.next(), b.next())) {
        return false;
      }
    }
    return true;
  }

  public static <V> Iterator<V> from(BooleanSupplier hasNext, Supplier<V> next) {
    return new Iterator<V>() {
      @Override
      public boolean hasNext() {
        return hasNext.getAsBoolean();
      }

      @Override
      public V next() {
        return next.get();
      }
    };
  }

  public static <V> Iterator<V> onExhaustion(Iterator<V> it, Runnable f) {
    return new Iterator<V>() {
      @Override
      public boolean hasNext() {
        return it.hasNext();
      }

      @Override
      public V next() {
        V result = it.next();
        if (!it.hasNext()) {
          f.run();
        }
        return result;
      }
    };
  }

  public static <V> Iterator<V> singleton(V val) {
    return new Iterator<V>() {
      boolean consumed = false;

      @Override
      public boolean hasNext() {
        return !consumed;
      }

      @Override
      public V next() {
        if (!consumed) {
          consumed = true;
          return val;
        } else {
          throw new NoSuchElementException();
        }
      }
    };
  }

  /**
   * @param it an iterator
   * @param f  a predicate
   * @return an iterator which only yields values that satisfy the predicate
   */
  public static <V> Iterator<V> filter(Iterator<V> it, Predicate<V> f) {
    return new Iterator<V>() {

      private Object next = NONE;
      private boolean done = false;

      private void prime() {
        if (next == NONE && !done) {
          while (it.hasNext()) {
            next = it.next();
            if (f.test((V) next)) {
              return;
            }
          }
          done = true;
        }
      }

      @Override
      public boolean hasNext() {
        prime();
        return !done;
      }

      @Override
      public V next() {
        prime();
        if (next == NONE) {
          throw new NoSuchElementException();
        }

        V val = (V) next;
        next = NONE;
        return val;
      }
    };
  }

  /**
   * @param it an iterator
   * @param f  a function which transforms values
   * @return an iterator which yields the transformed values
   */
  public static <U, V> Iterator<V> map(Iterator<U> it, Function<U, V> f) {
    return from(it::hasNext, () -> f.apply(it.next()));
  }

  /**
   * @param it an iterator
   * @param f  a function which transforms values into iterators
   * @return an iterator which yields the concatenation of the iterators
   */
  public static <U, V> Iterator<V> flatMap(Iterator<U> it, Function<U, Iterator<V>> f) {
    return new Iterator<V>() {

      Iterator<V> curr = EMPTY;

      private void prime() {
        while (!curr.hasNext() && it.hasNext()) {
          curr = f.apply(it.next());
        }
      }

      @Override
      public boolean hasNext() {
        prime();
        return curr.hasNext();
      }

      @Override
      public V next() {
        prime();
        return curr.next();
      }
    };
  }

  /**
   * @param min an inclusive start of the range
   * @param max an exclusive end of the range
   * @param f   a function which transforms a number in the range into a value
   * @return an iterator which yields the values returned by {@code f}
   */
  public static <V> Iterator<V> range(long min, long max, LongFunction<V> f) {
    return new Iterator<V>() {

      long i = min;

      @Override
      public boolean hasNext() {
        return i < max;
      }

      @Override
      public V next() {
        if (hasNext()) {
          return f.apply(i++);
        } else {
          throw new NoSuchElementException();
        }
      }
    };
  }

  /**
   * Represents a range implicitly starting at 0.
   *
   * @param max an exclusive end of the range.
   * @param f   a function which transforms a number in the range into a value.
   * @return an iterator which yields the values returned by {@code f}
   */
  public static <V> Iterator<V> range(long max, LongFunction<V> f) {
    return range(0, max, f);
  }

  /**
   * @param iterators a list of iterators
   * @return a concatenation of all iterators, in the order provided
   */
  public static <V> Iterator<V> concat(Iterator<V>... iterators) {
    if (iterators.length == 1) {
      return iterators[0];
    } else {
      IteratorStack<V> stack = new IteratorStack<V>();
      for (Iterator<V> it : iterators) {
        stack.addLast(it);
      }
      return stack;
    }
  }

  /**
   * @param it an iterator
   * @param n  the number of elements to drop, which may be larger than the number of values in the iterator
   * @return an iterator with the first {@code n} values dropped
   */
  public static <V> Iterator<V> drop(Iterator<V> it, long n) {
    for (long i = 0; i < n && it.hasNext(); i++) {
      it.next();
    }
    return it;
  }

  public static <V> Stream<V> toStream(Iterator<V> it) {
    return toStream(it, 0);
  }

  public static <V> Stream<V> toStream(Iterator<V> it, long estimatedSize) {
    return StreamSupport.stream(Spliterators.spliterator(it, estimatedSize, Spliterator.ORDERED), false);
  }

  public static <V> IDurableEncoding.SkippableIterator skippable(Iterator<V> it) {
    return new IDurableEncoding.SkippableIterator() {
      @Override
      public void skip() {
        it.next();
      }

      @Override
      public boolean hasNext() {
        return it.hasNext();
      }

      @Override
      public Object next() {
        return it.next();
      }
    };
  }

  public static class Indexed<T> {
    public final long index;
    public final T value;

    public Indexed(long index, T value) {
      this.index = index;
      this.value = value;
    }

    @Override
    public String toString() {
      return index + ": " + value;
    }
  }

  public static <V> Iterator<Indexed<V>> indexed(Iterator<V> it) {
    return indexed(it, 0);
  }

  public static <V> Iterator<Indexed<V>> indexed(Iterator<V> it, long offset) {
    AtomicLong counter = new AtomicLong(offset);
    return map(it, v -> new Indexed<>(counter.getAndIncrement(), v));
  }

}


text:
using Humanizer;

namespace Geco.Common
{
    public class HumanizerInflector : IInflector
    {
        public string Pluralise(string word)
        {
            return word.Pluralize(false);
        }

        public string Singularise(string word)
        {
            return word.Singularize(false);
        }

        public string Titleise(string word)
        {
            return word.Titleize();
        }

        public string Humanise(string lowercaseAndUnderscoredWord)
        {
            return lowercaseAndUnderscoredWord.Humanize();
        }

        public string Pascalise(string lowercaseAndUnderscoredWord)
        {
            return lowercaseAndUnderscoredWord.Pascalize().Replace(" ", "");
        }

        public string Camelise(string lowercaseAndUnderscoredWord)
        {
            return lowercaseAndUnderscoredWord.Camelize().Replace(" ", "");
        }

        public string Underscore(string pascalCasedWord)
        {
           return pascalCasedWord.Underscore().Replace(" ", "");
        }

        public string Capitalise(string word)
        {
            return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
        }

        public string Uncapitalise(string word)
        {
            return word.Substring(0, 1).ToLower() + word.Substring(1);
        }

        public string Ordinalise(string number)
        {
            return number.Ordinalize();
        }

        public string Ordinalise(int number)
        {
            return number.Ordinalize();
        }

        public string Dasherise(string underscoredWord)
        {
            return underscoredWord.Dasherize();
        }
    }
}


text:
using System;
using Com.Aspose.Cells.Api;
using Com.Aspose.Cells.Model;
using Com.Aspose.Storage.Api;

namespace Cells
{
    class SetValueOfCell
    {
        public static void Run()
        {
            // ExStart:1
            CellsApi cellsApi = new CellsApi(Common.APP_KEY, Common.APP_SID, Common.BASEPATH);
            StorageApi storageApi = new StorageApi(Common.APP_KEY, Common.APP_SID, Common.BASEPATH);

            String fileName = "Sample_Test_Book.xls";
            String sheetName = "Sheet2";
            String cellName = "a1";
            String value = "99";
            String type = "int";
            String formula = "";
            String storage = "";
            String folder = "";

            try
            {
                // Upload source file to aspose cloud storage
                storageApi.PutCreate(fileName, "", "", System.IO.File.ReadAllBytes(Common.GetDataDir() + fileName));

                // Invoke Aspose.Cells Cloud SDK API to set cell value of worksheet
                CellResponse apiResponse = cellsApi.PostWorksheetCellSetValue(fileName, sheetName, cellName, value, type, formula, storage, folder);

                if (apiResponse != null && apiResponse.Status.Equals("OK"))
                {
                    Console.WriteLine("Set Value of a Cell in a Worksheet, Done!");
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
            }
            // ExEnd:1
        }
    }
}


text:
var testHelper = require('./test_helper');
var should = testHelper.should;
var waterlock = testHelper.waterlock_local;
var proxyquire = testHelper.proxyquire;

describe('utils',function(){
  var utils = proxyquire('../lib/utils',
    {
      './waterlock-local-auth': waterlock,
      'path': {
        normalize: function(str) {
          return __dirname+"/email.test.jade";
        }
      }
    });

  describe('getHtmlEmail', function(){
    it('should exist', function(done){
      utils.should.have.property('getHtmlEmail');
      done();
    });

    it('should return html', function(done){
      utils.getHtmlEmail({owner: "test", resetToken: "token"}).should.be.String
      done()
    });

    it('should error', function(done) {
      utils = proxyquire('../lib/utils', {'./waterlock-local-auth': {}});
      (function(){ utils.getHtmlEmail({owner: "test", resetToken: "token"})}).should.throwError('No config file defined, try running [waterlock install config]')
      done()
    });
  });

  describe('mailCallback', function(){
    it('should exist', function(done){
      utils.should.have.property('mailCallback');
      done();
    });
  });
});