text:
// <copyright file="TextGraphicsOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Drawing
{
    using ImageSharp.PixelFormats;
    using SixLabors.Fonts;

    /// <summary>
    /// Options for influencing the drawing functions.
    /// </summary>
    public struct TextGraphicsOptions
    {
        /// <summary>
        /// Represents the default <see cref="TextGraphicsOptions"/>.
        /// </summary>
        public static readonly TextGraphicsOptions Default = new TextGraphicsOptions(true);

        private float? blendPercentage;

        private int? antialiasSubpixelDepth;

        private bool? antialias;

        private bool? applyKerning;

        private float? tabWidth;

        private PixelBlenderMode blenderMode;

        private bool? useImageResolution;

        private float wrapTextWidth;

        private SixLabors.Fonts.HorizontalAlignment? horizontalAlignment;
        private SixLabors.Fonts.VerticalAlignment? verticalAlignment;

        /// <summary>
        /// Initializes a new instance of the <see cref="TextGraphicsOptions" /> struct.
        /// </summary>
        /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
        public TextGraphicsOptions(bool enableAntialiasing)
        {
            this.applyKerning = true;
            this.tabWidth = 4;
            this.useImageResolution = false;
            this.wrapTextWidth = 0;
            this.horizontalAlignment = HorizontalAlignment.Left;
            this.verticalAlignment = VerticalAlignment.Top;

            this.antialiasSubpixelDepth = 16;
            this.blenderMode = PixelBlenderMode.Normal;
            this.blendPercentage = 1;
            this.antialias = enableAntialiasing;
        }

        /// <summary>
        /// Gets or sets a value indicating whether antialiasing should be applied.
        /// </summary>
        public bool Antialias { get => this.antialias ?? true; set => this.antialias = value; }

        /// <summary>
        /// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
        /// </summary>
        public int AntialiasSubpixelDepth { get => this.antialiasSubpixelDepth ?? 16; set => this.antialiasSubpixelDepth = value; }

        /// <summary>
        /// Gets or sets a value indicating the blending percentage to apply to the drawing operation
        /// </summary>
        public float BlendPercentage { get => (this.blendPercentage ?? 1).Clamp(0, 1); set => this.blendPercentage = value; }

        // In the future we could expose a PixelBlender<TPixel> directly on here
        // or some forms of PixelBlender factory for each pixel type. Will need
        // some API thought post V1.

        /// <summary>
        /// Gets or sets a value indicating the blending percentage to apply to the drawing operation
        /// </summary>
        public PixelBlenderMode BlenderMode { get => this.blenderMode; set => this.blenderMode = value; }

        /// <summary>
        /// Gets or sets a value indicating whether the text should be drawing with kerning enabled.
        /// </summary>
        public bool ApplyKerning { get => this.applyKerning ?? true; set => this.applyKerning = value; }

        /// <summary>
        /// Gets or sets a value indicating the number of space widths a tab should lock to.
        /// </summary>
        public float TabWidth { get => this.tabWidth ?? 4; set => this.tabWidth = value; }

        /// <summary>
        /// Gets or sets a value indicating whether to use the current image resultion to for point size scaling.
        /// If this is [false] the text renders at 72dpi otherwise it renders at Image resolution
        /// </summary>
        public bool UseImageResolution { get => this.useImageResolution ?? false; set => this.useImageResolution = value; }

        /// <summary>
        /// Gets or sets a value indicating if greater than zero determine the width at which text should wrap.
        /// </summary>
        public float WrapTextWidth { get => this.wrapTextWidth; set => this.wrapTextWidth = value; }

        /// <summary>
        /// Gets or sets a value indicating how to align the text relative to the rendering space.
        /// If <see cref="WrapTextWidth"/> is greater than zero it will align relative to the space
        /// defined by the location and width, if <see cref="WrapTextWidth"/> equals zero, and thus
        /// wrapping disabled, then the alignment is relative to the drawing location.
        /// </summary>
        public HorizontalAlignment HorizontalAlignment { get => this.horizontalAlignment ?? HorizontalAlignment.Left; set => this.horizontalAlignment = value; }

        /// <summary>
        /// Gets or sets a value indicating how to align the text relative to the rendering space.
        /// </summary>
        public VerticalAlignment VerticalAlignment { get => this.verticalAlignment ?? VerticalAlignment.Top; set => this.verticalAlignment = value; }

        /// <summary>
        /// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator TextGraphicsOptions(GraphicsOptions options)
        {
            return new TextGraphicsOptions(options.Antialias)
            {
                AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
                blendPercentage = options.BlendPercentage,
                blenderMode = options.BlenderMode
            };
        }

        /// <summary>
        /// Performs an explicit conversion from <see cref="TextGraphicsOptions"/> to <see cref="GraphicsOptions"/>.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static explicit operator GraphicsOptions(TextGraphicsOptions options)
        {
            return new GraphicsOptions(options.Antialias)
            {
                AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
                BlenderMode = options.BlenderMode,
                BlendPercentage = options.BlendPercentage
            };
        }
    }
}


text:
layout: doc_page
---

# Druid Quickstart

In this quickstart, we will download Druid, set up it up on a single machine, load some data, and query the data.

## Prerequisites

You will need:

  * Java 8 or higher
  * Linux, Mac OS X, or other Unix-like OS (Windows is not supported)
  * 8G of RAM
  * 2 vCPUs

On Mac OS X, you can use [Oracle's JDK
8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) to install
Java.

On Linux, your OS package manager should be able to help for Java. If your Ubuntu-
based OS does not have a recent enough version of Java, WebUpd8 offers [packages for those
OSes](http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html).

## Getting started

To install Druid, issue the following commands in your terminal:

```bash
curl -O http://static.druid.io/artifacts/releases/druid-#{DRUIDVERSION}-bin.tar.gz
tar -xzf druid-#{DRUIDVERSION}-bin.tar.gz
cd druid-#{DRUIDVERSION}
```

In the package, you should find:

* `LICENSE` - the license files.
* `bin/` - scripts useful for this quickstart.
* `conf/*` - template configurations for a clustered setup.
* `conf-quickstart/*` - configurations for this quickstart.
* `extensions/*` - all Druid extensions.
* `hadoop-dependencies/*` - Druid Hadoop dependencies.
* `lib/*` - all included software packages for core Druid.
* `quickstart/*` - files useful for this quickstart.

## Start up Zookeeper

Druid currently has a dependency on [Apache ZooKeeper](http://zookeeper.apache.org/) for distributed coordination. You'll
need to download and run Zookeeper.

```bash
curl http://www.gtlib.gatech.edu/pub/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz -o zookeeper-3.4.10.tar.gz
tar -xzf zookeeper-3.4.10.tar.gz
cd zookeeper-3.4.10
cp conf/zoo_sample.cfg conf/zoo.cfg
./bin/zkServer.sh start
```

## Start up Druid services

With Zookeeper running, return to the druid-#{DRUIDVERSION} directory. In that directory, issue the command:

```bash
bin/init
```

This will setup up some directories for you. Next, you can start up the Druid processes in different terminal windows.
This tutorial runs every Druid process on the same system. In a large distributed production cluster,
many of these Druid processes can still be co-located together.

```bash
java `cat conf-quickstart/druid/historical/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/historical:lib/*" io.druid.cli.Main server historical
java `cat conf-quickstart/druid/broker/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/broker:lib/*" io.druid.cli.Main server broker
java `cat conf-quickstart/druid/coordinator/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/coordinator:lib/*" io.druid.cli.Main server coordinator
java `cat conf-quickstart/druid/overlord/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/overlord:lib/*" io.druid.cli.Main server overlord
java `cat conf-quickstart/druid/middleManager/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/middleManager:lib/*" io.druid.cli.Main server middleManager
```

You should see a log message printed out for each service that starts up.

Later on, if you'd like to stop the services, CTRL-C to exit from the running java processes. If you
want a clean start after stopping the services, delete the `var` directory and run the `init` script again.

Once every service has started, you are now ready to load data.

## Load batch data

We've included a sample of Wikipedia edits from September 12, 2015 to get you started.

This section shows you how to load data in batches, but you can skip ahead to learn how to load
streams in real-time. Druid's streaming ingestion can load data
with virtually no delay between events occurring and being available for queries.

The [dimensions](https://en.wikipedia.org/wiki/Dimension_%28data_warehouse%29) (attributes you can
filter and split on) in the Wikipedia dataset, other than time, are:

  * channel
  * cityName
  * comment
  * countryIsoCode
  * countryName
  * isAnonymous
  * isMinor
  * isNew
  * isRobot
  * isUnpatrolled
  * metroCode
  * namespace
  * page
  * regionIsoCode
  * regionName
  * user

The [measures](https://en.wikipedia.org/wiki/Measure_%28data_warehouse%29), or *metrics* as they are known in Druid (values you can aggregate)
in the Wikipedia dataset are:

  * count
  * added
  * deleted
  * delta
  * user_unique

To load this data into Druid, you can submit an *ingestion task* pointing to the file. We've included
a task that loads the `wikiticker-2015-09-12-sampled.json` file included in the archive. To submit
this task, POST it to Druid in a new terminal window from the druid-#{DRUIDVERSION} directory:

```bash
curl -X 'POST' -H 'Content-Type:application/json' -d @quickstart/wikiticker-index.json localhost:8090/druid/indexer/v1/task
```

Which will print the ID of the task if the submission was successful:

```bash
{"task":"index_hadoop_wikipedia_2013-10-09T21:30:32.802Z"}
```

To view the status of your ingestion task, go to your overlord console:
[http://localhost:8090/console.html](http://localhost:8090/console.html). You can refresh the console periodically, and after
the task is successful, you should see a "SUCCESS" status for the task.

After your ingestion task finishes, the data will be loaded by historical nodes and available for
querying within a minute or two. You can monitor the progress of loading your data in the
coordinator console, by checking whether there is a datasource "wikiticker" with a blue circle
indicating "fully available": [http://localhost:8081/#/](http://localhost:8081/#/).

Once the data is fully available, you can immediately query it— to see how, skip to the [Query
data](#query-data) section below. Or, continue to the [Load your own data](#load-your-own-data)
section if you'd like to load a different dataset.

## Load streaming data

To load streaming data, we are going to push events into Druid
over a simple HTTP API. To do this we will use [Tranquility], a high level data producer
library for Druid.

To download Tranquility, issue the following commands in your terminal:

```bash
curl -O http://static.druid.io/tranquility/releases/tranquility-distribution-0.8.0.tgz
tar -xzf tranquility-distribution-0.8.0.tgz
cd tranquility-distribution-0.8.0
```

We've included a configuration file in `conf-quickstart/tranquility/server.json` as part of the Druid distribution
for a *metrics* datasource. We're going to start the Tranquility server process, which can be used to push events
directly to Druid.

``` bash
bin/tranquility server -configFile <path_to_druid_distro>/conf-quickstart/tranquility/server.json
```

This section shows you how to load data using Tranquility Server, but Druid also supports a wide
variety of other streaming ingestion options, including from
popular streaming systems like Kafka, Storm, Samza, and Spark Streaming.

The [dimensions](https://en.wikipedia.org/wiki/Dimension_%28data_warehouse%29) (attributes you can
filter and split on) for this datasource are flexible. It's configured for *schemaless dimensions*,
meaning it will accept any field in your JSON input as a dimension.

The metrics (also called
[measures](https://en.wikipedia.org/wiki/Measure_%28data_warehouse%29); values
you can aggregate) in this datasource are:

  * count
  * value_sum (derived from `value` in the input)
  * value_min (derived from `value` in the input)
  * value_max (derived from `value` in the input)

We've included a script that can generate some random sample metrics to load into this datasource.
To use it, simply run in your Druid distribution repository:

```bash
bin/generate-example-metrics | curl -XPOST -H'Content-Type: application/json' --data-binary @- http://localhost:8200/v1/post/metrics
```

Which will print something like:

```
{"result":{"received":25,"sent":25}}
```

This indicates that the HTTP server received 25 events from you, and sent 25 to Druid. Note that
this may take a few seconds to finish the first time you run it, as Druid resources must be
allocated to the ingestion task. Subsequent POSTs should complete quickly.

Once the data is sent to Druid, you can immediately [query it](#query-data).

## Query data

### Direct Druid queries

Druid supports a rich [family of JSON-based
queries](../querying/querying.html). We've included an example topN query
in `quickstart/wikiticker-top-pages.json` that will find the most-edited articles in this dataset:

```bash
curl -L -H'Content-Type: application/json' -XPOST --data-binary @quickstart/wikiticker-top-pages.json http://localhost:8082/druid/v2/?pretty
```

## Visualizing data

Druid is ideal for power user-facing analytic applications. There are a number of different open source applications to
visualize and explore data in Druid. We recommend trying [Pivot](https://github.com/implydata/pivot),
[Superset](https://github.com/airbnb/superset), or [Metabase](https://github.com/metabase/metabase) to start
visualizing the data you just ingested.

If you installed Pivot for example, you should be able to view your data in your browser at [localhost:9090](http://localhost:9090/).

### SQL and other query libraries

There are many more query tools for Druid than we've included here, including SQL
engines, and libraries for various languages like Python and Ruby. Please see [the list of
libraries](../development/libraries.html) for more information.

## Clustered setup

This quickstart sets you up with all services running on a single machine. The next step is to [load
your own data](ingestion.html). Or, you can skip ahead to [running a distributed cluster](cluster.html).

redpajama_set_name: RedPajamaGithub


text:
#include <string.h>
#include <jim.h>

/* Provides the [pack] and [unpack] commands to pack and unpack
 * a binary string to/from arbitrary width integers and strings.
 *
 * This may be used to implement the [binary] command.
 */

/**
 * Big endian bit test.
 *
 * Considers 'bitvect' as a big endian bit stream and returns
 * bit 'b' as zero or non-zero.
 */
static int JimTestBitBigEndian(const unsigned char *bitvec, int b)
{
    div_t pos = div(b, 8);
    return bitvec[pos.quot] & (1 << (7 - pos.rem));
}

/**
 * Little endian bit test.
 *
 * Considers 'bitvect' as a little endian bit stream and returns
 * bit 'b' as zero or non-zero.
 */
static int JimTestBitLittleEndian(const unsigned char *bitvec, int b)
{
    div_t pos = div(b, 8);
    return bitvec[pos.quot] & (1 << pos.rem);
}

/**
 * Sign extends the given value, 'n' of width 'width' bits.
 *
 * For example, sign extending 0x80 with a width of 8, produces -128
 */
static jim_wide JimSignExtend(jim_wide n, int width)
{
    if (width == sizeof(jim_wide) * 8) {
        /* Can't sign extend the maximum size integer */
        return n;
    }
    if (n & ((jim_wide)1 << (width - 1))) {
        /* Need to extend */
        n -= ((jim_wide)1 << width);
    }

    return n;
}

/**
 * Big endian integer extraction.
 *
 * Considers 'bitvect' as a big endian bit stream.
 * Returns an integer of the given width (in bits)
 * starting at the given position (in bits).
 *
 * The pos/width must represent bits inside bitvec,
 * and the width be no more than the width of jim_wide.
 */
static jim_wide JimBitIntBigEndian(const unsigned char *bitvec, int pos, int width)
{
    jim_wide result = 0;
    int i;

    /* Aligned, byte extraction */
    if (pos % 8 == 0 && width % 8 == 0) {
        for (i = 0; i < width; i += 8) {
            result = (result << 8) + bitvec[(pos + i) / 8];
        }
        return result;
    }

    /* Unaligned */
    for (i = 0; i < width; i++) {
        if (JimTestBitBigEndian(bitvec, pos + width - i - 1)) {
            result |= ((jim_wide)1 << i);
        }
    }

    return result;
}

/**
 * Little endian integer extraction.
 *
 * Like JimBitIntBigEndian() but considers 'bitvect' as a little endian bit stream.
 */
static jim_wide JimBitIntLittleEndian(const unsigned char *bitvec, int pos, int width)
{
    jim_wide result = 0;
    int i;

    /* Aligned, byte extraction */
    if (pos % 8 == 0 && width % 8 == 0) {
        for (i = 0; i < width; i += 8) {
            result += (jim_wide)bitvec[(pos + i) / 8] << i;
        }
        return result;
    }

    /* Unaligned */
    for (i = 0; i < width; i++) {
        if (JimTestBitLittleEndian(bitvec, pos + i)) {
            result |= ((jim_wide)1 << i);
        }
    }

    return result;
}

/**
 * Big endian bit set.
 *
 * Considers 'bitvect' as a big endian bit stream and sets
 * bit 'b' to 'bit'
 */
static void JimSetBitBigEndian(unsigned char *bitvec, int b, int bit)
{
    div_t pos = div(b, 8);
    if (bit) {
        bitvec[pos.quot] |= (1 << (7 - pos.rem));
    }
    else {
        bitvec[pos.quot] &= ~(1 << (7 - pos.rem));
    }
}

/**
 * Little endian bit set.
 *
 * Considers 'bitvect' as a little endian bit stream and sets
 * bit 'b' to 'bit'
 */
static void JimSetBitLittleEndian(unsigned char *bitvec, int b, int bit)
{
    div_t pos = div(b, 8);
    if (bit) {
        bitvec[pos.quot] |= (1 << pos.rem);
    }
    else {
        bitvec[pos.quot] &= ~(1 << pos.rem);
    }
}

/**
 * Big endian integer packing.
 *
 * Considers 'bitvect' as a big endian bit stream.
 * Packs integer 'value' of the given width (in bits)
 * starting at the given position (in bits).
 *
 * The pos/width must represent bits inside bitvec,
 * and the width be no more than the width of jim_wide.
 */
static void JimSetBitsIntBigEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
    int i;

    /* Common fast option */
    if (pos % 8 == 0 && width == 8) {
        bitvec[pos / 8] = value;
        return;
    }

    for (i = 0; i < width; i++) {
        int bit = !!(value & ((jim_wide)1 << i));
        JimSetBitBigEndian(bitvec, pos + width - i - 1, bit);
    }
}

/**
 * Little endian version of JimSetBitsIntBigEndian()
 */
static void JimSetBitsIntLittleEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
    int i;

    /* Common fast option */
    if (pos % 8 == 0 && width == 8) {
        bitvec[pos / 8] = value;
        return;
    }

    for (i = 0; i < width; i++) {
        int bit = !!(value & ((jim_wide)1 << i));
        JimSetBitLittleEndian(bitvec, pos + i, bit);
    }
}

/**
 * Binary conversion of jim_wide integer to float
 *
 * Considers the least significant bits of
 * jim_wide 'value' as a IEEE float.
 *
 * Should work for both little- and big-endian platforms.
 */
static float JimIntToFloat(jim_wide value)
{
    int offs;
    float val;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;

    memcpy(&val, (unsigned char *) &value + offs, sizeof(float));
    return val;
}

/**
 * Binary conversion of jim_wide integer to double
 *
 * Double precision version of JimIntToFloat
 */
static double JimIntToDouble(jim_wide value)
{
    int offs;
    double val;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;

    memcpy(&val, (unsigned char *) &value + offs, sizeof(double));
    return val;
}

/**
 * Binary conversion of float to jim_wide integer
 *
 * Considers the bits of IEEE float 'value' as integer.
 * The integer is zero-extended to jim_wide.
 *
 * Should work for both little- and big-endian platforms.
 */
static jim_wide JimFloatToInt(float value)
{
    int offs;
    jim_wide val = 0;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;

    memcpy((unsigned char *) &val + offs, &value, sizeof(float));
    return val;
}

/**
 * Binary conversion of double to jim_wide integer
 *
 * Double precision version of JimFloatToInt
 */
static jim_wide JimDoubleToInt(double value)
{
    int offs;
    jim_wide val = 0;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;

    memcpy((unsigned char *) &val + offs, &value, sizeof(double));
    return val;
}

/**
 * [unpack]
 *
 * Usage: unpack binvalue -intbe|-intle|-uintbe|-uintle|-floatbe|-floatle|-str bitpos bitwidth
 *
 * Unpacks bits from $binvalue at bit position $bitpos and with $bitwidth.
 * Interprets the value according to the type and returns it.
 */
static int Jim_UnpackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    int option;
    static const char * const options[] = { "-intbe", "-intle", "-uintbe", "-uintle",
        "-floatbe", "-floatle", "-str", NULL };
    enum { OPT_INTBE, OPT_INTLE, OPT_UINTBE, OPT_UINTLE, OPT_FLOATBE, OPT_FLOATLE, OPT_STR, };
    jim_wide pos;
    jim_wide width;

    if (argc != 5) {
        Jim_WrongNumArgs(interp, 1, argv,
                "binvalue -intbe|-intle|-uintbe|-uintle|-floatbe|-floatle|-str bitpos bitwidth");
        return JIM_ERR;
    }
    if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
        return JIM_ERR;
    }

    if (Jim_GetWide(interp, argv[3], &pos) != JIM_OK) {
        return JIM_ERR;
    }
    if (Jim_GetWide(interp, argv[4], &width) != JIM_OK) {
        return JIM_ERR;
    }

    if (option == OPT_STR) {
        int len;
        const char *str = Jim_GetString(argv[1], &len);

        if (width % 8 || pos % 8) {
            Jim_SetResultString(interp, "string field is not on a byte boundary", -1);
            return JIM_ERR;
        }

        if (pos >= 0 && width > 0 && pos < len * 8) {
            if (pos + width > len * 8) {
                width = len * 8 - pos;
            }
            Jim_SetResultString(interp, str + pos / 8, width / 8);
        }
        return JIM_OK;
    }
    else {
        int len;
        const unsigned char *str = (const unsigned char *)Jim_GetString(argv[1], &len);
        jim_wide result = 0;

        if (width > sizeof(jim_wide) * 8) {
            Jim_SetResultFormatted(interp, "int field is too wide: %#s", argv[4]);
            return JIM_ERR;
        }

        if (pos >= 0 && width > 0 && pos < len * 8) {
            if (pos + width > len * 8) {
                width = len * 8 - pos;
            }
            if (option == OPT_INTBE || option == OPT_UINTBE || option == OPT_FLOATBE) {
                result = JimBitIntBigEndian(str, pos, width);
            }
            else {
                result = JimBitIntLittleEndian(str, pos, width);
            }
            if (option == OPT_INTBE || option == OPT_INTLE) {
                result = JimSignExtend(result, width);
            }

        }

        if (option == OPT_FLOATBE || option == OPT_FLOATLE) {
            double fresult;
            if (width == 32) {
                fresult = (double) JimIntToFloat(result);
            } else if (width == 64) {
                fresult = JimIntToDouble(result);
            } else {
                Jim_SetResultFormatted(interp, "float field has bad bitwidth: %#s", argv[4]);
                return JIM_ERR;
            }
            Jim_SetResult(interp, Jim_NewDoubleObj(interp, fresult));
        } else {
            Jim_SetResultInt(interp, result);
        }
        return JIM_OK;
    }
}

/**
 * [pack]
 *
 * Usage: pack varname value -intbe|-intle|-floatle|-floatbe|-str width ?bitoffset?
 *
 * Packs the binary representation of 'value' into the variable of the given name.
 * The value is packed according to the given type, width and bitoffset.
 * The variable is created if necessary (like [append])
 * The variable is expanded if necessary
 */
static int Jim_PackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    int option;
    static const char * const options[] = { "-intle", "-intbe", "-floatle", "-floatbe",
        "-str", NULL };
    enum { OPT_LE, OPT_BE, OPT_FLOATLE, OPT_FLOATBE, OPT_STR };
    jim_wide pos = 0;
    jim_wide width;
    jim_wide value;
    double fvalue;
    Jim_Obj *stringObjPtr;
    int len;
    int freeobj = 0;

    if (argc != 5 && argc != 6) {
        Jim_WrongNumArgs(interp, 1, argv,
                "varName value -intle|-intbe|-floatle|-floatbe|-str bitwidth ?bitoffset?");
        return JIM_ERR;
    }
    if (Jim_GetEnum(interp, argv[3], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
        return JIM_ERR;
    }
    if ((option == OPT_LE || option == OPT_BE) &&
            Jim_GetWide(interp, argv[2], &value) != JIM_OK) {
        return JIM_ERR;
    }
    if ((option == OPT_FLOATLE || option == OPT_FLOATBE) &&
            Jim_GetDouble(interp, argv[2], &fvalue) != JIM_OK) {
        return JIM_ERR;
    }
    if (Jim_GetWide(interp, argv[4], &width) != JIM_OK) {
        return JIM_ERR;
    }
    if (width <= 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) ||
       ((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) {
        Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]);
        return JIM_ERR;
    }
    if (argc == 6) {
        if (Jim_GetWide(interp, argv[5], &pos) != JIM_OK) {
            return JIM_ERR;
        }
        if (pos < 0 || (option == OPT_STR && pos % 8)) {
            Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[5]);
            return JIM_ERR;
        }
    }

    stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
    if (!stringObjPtr) {
        /* Create the string if it doesn't exist */
        stringObjPtr = Jim_NewEmptyStringObj(interp);
        freeobj = 1;
    }
    else if (Jim_IsShared(stringObjPtr)) {
        freeobj = 1;
        stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr);
    }

    len = Jim_Length(stringObjPtr) * 8;

    /* Extend the string as necessary first */
    while (len < pos + width) {
        Jim_AppendString(interp, stringObjPtr, "", 1);
        len += 8;
    }

    Jim_SetResultInt(interp, pos + width);

    /* Now set the bits. Note that the the string *must* have no non-string rep
     * since we are writing the bytes directly.
     */
    Jim_AppendString(interp, stringObjPtr, "", 0);

    /* Convert floating point to integer if necessary */
    if (option == OPT_FLOATLE || option == OPT_FLOATBE) {
        /* Note that the following is slightly incompatible with Tcl behaviour.
         * In Tcl floating overflow gives FLT_MAX (cf. test binary-13.13).
         * In Jim Tcl it gives Infinity. This behavior may change.
         */
        value = (width == 32) ? JimFloatToInt((float)fvalue) : JimDoubleToInt(fvalue);
    }

    if (option == OPT_BE || option == OPT_FLOATBE) {
        JimSetBitsIntBigEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
    }
    else if (option == OPT_LE || option == OPT_FLOATLE) {
        JimSetBitsIntLittleEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
    }
    else {
        pos /= 8;
        width /= 8;

        if (width > Jim_Length(argv[2])) {
            width = Jim_Length(argv[2]);
        }
        memcpy(stringObjPtr->bytes + pos, Jim_String(argv[2]), width);
        /* No padding is needed since the string is already extended */
    }

    if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) {
        if (freeobj) {
            Jim_FreeNewObj(interp, stringObjPtr);
            return JIM_ERR;
        }
    }
    return JIM_OK;
}

int Jim_packInit(Jim_Interp *interp)
{
    if (Jim_PackageProvide(interp, "pack", "1.0", JIM_ERRMSG)) {
        return JIM_ERR;
    }

    Jim_CreateCommand(interp, "unpack", Jim_UnpackCmd, NULL, NULL);
    Jim_CreateCommand(interp, "pack", Jim_PackCmd, NULL, NULL);
    return JIM_OK;
}


text:
package planner.manager;

import java.io.*;
import java.text.*;
import java.util.*;

import planner.db.StudentCourseData;
import planner.model.Index;
import planner.model.Student;
import planner.model.StudentCourse;

public class StudentCourseMgr {
	/**
	 * Create a new course with the necessary information
	 * @throws ParseException
	 * @throws IOException
	 */
	public static void registerCourse(Student s, int indexNumber) throws IOException, ParseException {
		ArrayList<Index> indexList = DataListMgr.getIndexes();
		for (Index i : indexList){
			if (i.getIndexNumber() == indexNumber){
				int vacancy = i.getVacancy();
				int waitingList = i.getWaitingList();
				String registerStatus = "On Waiting List";
				String courseCode = i.getCourseCode();

				if (i.getVacancy() <= 0){
					waitingList++;
				}
				else if (i.getVacancy() > 0){
					vacancy--;
					registerStatus = "Registered";
				}

				// Adding
				StudentCourse newStudentCourse = new StudentCourse(s.getUserName(), courseCode, indexNumber, registerStatus);
				DataListMgr.writeObject(newStudentCourse);

				// Update new vacancy & waiting list
				indexList.remove(i);
			    Index newIndex = new Index(i.getCourseCode(), indexNumber, i.getTutorialGroup(), vacancy, waitingList);
			    DataListMgr.writeObject(newIndex);

				System.out.println();
				if (registerStatus.equals("On Waiting List")){
					System.out.println("Due to lack of vacancy, your Index " + indexNumber + " (" + courseCode + ") will be put into waiting list.");
				}
				else if (registerStatus.equals("Registered")){
					System.out.println("Index " + indexNumber + " (" + courseCode + ") has been successfully added!");
				}

				return;
			}
		}
	}

	public static void removeCourse(Student s, int indexNumber) throws IOException, ParseException{
		ArrayList<StudentCourse> studentCourseList = DataListMgr.getStudentCourses();
		ArrayList<Index> indexList = DataListMgr.getIndexes();

		for(StudentCourse course : studentCourseList){
			if (course.getIndexNumber() == indexNumber && course.getUserName().equals(s.getUserName())){
				studentCourseList.remove(course);
				StudentCourseData.saveStudentCourses(studentCourseList);

				System.out.println("Index " + course.getIndexNumber() + " (" + course.getCourseCode() + ") has been removed!");

				for (Index i : indexList){
					int vacancy = i.getVacancy();
					int waitingList = i.getWaitingList();

					if (course.getRegisterStatus().equals("Registered")){
						vacancy++;
					}
					else if (course.getRegisterStatus().equals("On Waiting List")){
						waitingList--;
					}

					if (i.getIndexNumber() == indexNumber){
						// Update new vacancy & waiting list
						indexList.remove(i);
					    Index newIndex = new Index(i.getCourseCode(), indexNumber, i.getTutorialGroup(), vacancy, waitingList);
					    DataListMgr.writeObject(newIndex);

						return;
					}
				}
			}
		}
	}
}


text:
title: Comparison to Other Systems
kind: misc
weight: 16
---

Often the easiest way to understand a new language is by comparing
it to languages you already know.  Here we show how policies from
several existing policy systems can be implemented with the Open
Policy Agent.

## Role-based access control (RBAC)

Role-based access control (RBAC) is pervasive today for authorization.
To use RBAC for authorization, you write down two different kinds of
information.

* Which users have which roles
* Which roles have which permissions

Once you provide RBAC with both those assignments, RBAC tells you
how to make an authorization decision.  A user is authorized for
all those permissions assigned to any of the roles she is assigned to.

For example, we might have the following user/role assignments:

| User | Role |
| --- | --- |
| ``alice`` | ``engineering`` |
| ``alice`` | ``webdev`` |
| ``bob`` | ``hr`` |

And the following role/permission assignments:

| Role | Permission | Resource |
| --- | --- | --- |
| ``engineering`` | ``read`` | ``server123`` |
| ``webdev`` | ``write`` | ``server123`` |
| ``webdev`` | ``read`` | ``server123`` |
| ``hr`` | ``write`` | ``database456`` |

In this example, RBAC makes the following authorization decisions:

| User | Operation | Resource | Decision |
| --- | --- | --- | --- |
| ``alice`` | ``read`` | ``server123`` | ``allow`` because ``alice`` is in ``engineering`` |
| ``alice`` | ``write`` | ``server123`` | ``allow`` because ``alice`` is in ``webdev`` |
| ``bob`` | ``read`` | ``database456`` | ``allow`` because ``bob`` is in ``hr`` |
| ``bob`` | ``read`` | ``server123`` | ``deny`` because ``bob`` is not in ``engineering`` or ``webdev`` |

With OPA, you can write the following snippets to implement the
example RBAC policy shown above.

```live:rbac:module:openable
package rbac.authz

# user-role assignments
user_roles := {
    "alice": ["engineering", "webdev"],
    "bob": ["hr"]
}

# role-permissions assignments
role_permissions := {
    "engineering": [{"action": "read",  "object": "server123"}],
    "webdev":      [{"action": "read",  "object": "server123"},
                    {"action": "write", "object": "server123"}],
    "hr":          [{"action": "read",  "object": "database456"}]
}

# logic that implements RBAC.
default allow = false
allow {
    # lookup the list of roles for the user
    roles := user_roles[input.user]
    # for each role in that list
    r := roles[_]
    # lookup the permissions list for role r
    permissions := role_permissions[r]
    # for each permission
    p := permissions[_]
    # check if the permission granted to r matches the user's request
    p == {"action": input.action, "object": input.object}
}
```

```live:rbac:query:hidden
allow
```

As you can see, querying the `allow` rule with the following input

```live:rbac:input
{
  "user": "bob",
  "action": "read",
  "object": "server123"
}
```

Results in the response you'd expect.

```live:rbac:output
```

### RBAC Separation of duty (SOD)

Separation of duty (SOD) refers to the idea that there are certain
combinations of permissions that no one should have at the same time.
For example, no one should be able to both create payments and approve payments.

In RBAC, that means there are some pairs of roles that no one should be
assigned simultaneously.  For example, any user assigned both of the roles
in each pair below would violate SOD.

* create-payment and approve-payment
* create-vendor and pay-vendor

OPA's API does not yet let you enforce SOD by rejecting improper role-assignments,
but it does let you express SOD constraints and ask for all SOD violations,
as shown below.  (Here we assume the statements below are added to the RBAC
statements above.)

```live:rbac/sod:module:openable
# Pairs of roles that no user can be assigned to simultaneously
sod_roles := [
    ["create-payment", "approve-payment"],
    ["create-vendor", "pay-vendor"],
]

# Find all users violating SOD
sod_violation[user] {
    some user
    # grab one role for a user
    role1 := user_roles[user][_]
    # grab another role for that same user
    role2 := user_roles[user][_]
    # check if those roles are forbidden by SOD
    sod_roles[_] == [role1, role2]
}
```

(For those familiar with SOD, this is the static version since SOD violations
happen whenever a user is assigned two conflicting roles.  The dynamic version of SOD allows
a single user to be assigned two conflicting roles but requires that the same user not
utilize those roles on the same transaction, which is out of scope for this document.)

## Attribute-based access control (ABAC)

With attribute-based access control, you make policy decisions using the
attributes of the users, objects, and actions involved in the request.
It has three main components:

* Attributes for users
* Attributes for objects
* Logic dictating which attribute combinations are authorized

For example, we might know the following attributes for our users

* alice
  * joined the company 15 years ago
  * is a trader
* bob
  * joined the company 5 years ago
  * is an analyst

We would also have attributes for the objects, in this case stock ticker symbols.

* MSFT
  * is sold on NASDAQ
  * sells at $59.20 per share
* AMZN
  * is sold on NASDAQ
  * sells at $813.64 per share

An example ABAC policy in english might be:

* Traders may purchase NASDAQ stocks for under $2M
* Traders with 10+ years experience may purchase NASDAQ stocks for under $5M

OPA supports ABAC policies as shown below.

```live:abac:module:openable
package abac

# User attributes
user_attributes := {
    "alice": {"tenure": 15, "title": "trader"},
    "bob": {"tenure": 5, "title": "analyst"}
}

# Stock attributes
ticker_attributes := {
    "MSFT": {"exchange": "NASDAQ", "price": 59.20},
    "AMZN": {"exchange": "NASDAQ", "price": 813.64}
}

default allow = false

# all traders may buy NASDAQ under $2M
allow {
    # lookup the user's attributes
    user := user_attributes[input.user]
    # check that the user is a trader
    user.title == "trader"
    # check that the stock being purchased is sold on the NASDAQ
    ticker_attributes[input.ticker].exchange == "NASDAQ"
    # check that the purchase amount is under $2M
    input.amount <= 2000000
}

# traders with 10+ years experience may buy NASDAQ under $5M
allow {
    # lookup the user's attributes
    user := user_attributes[input.user]
    # check that the user is a trader
    user.title == "trader"
    # check that the stock being purchased is sold on the NASDAQ
    ticker_attributes[input.ticker].exchange == "NASDAQ"
    # check that the user has at least 10 years of experience
    user.tenure > 10
    # check that the purchase amount is under $5M
    input.amount <= 5000000
}
```

```live:abac:query:hidden
allow
```

```live:abac:input
{
  "user": "alice",
  "ticker": "MSFT",
  "action": "buy",
  "amount": 1000000
}
```

Querying the `allow` rule with the input above returns the following answer:

```live:abac:output
```

In OPA, there's nothing special about users and objects.  You can attach
attributes to anything.  And the attributes can themselves be structured JSON objects
and have attributes on attributes on attributes, etc.  Because OPA was designed to work
with arbitrarily nested JSON data, it supports incredibly rich ABAC policies.

## Amazon Web Services IAM

Amazon Web Services (AWS) lets you create policies that can be attached to users, roles, groups,
and selected resources. You write `allow` and `deny` statements to enforce which users/roles can/can't
execute which API calls on which resources under certain conditions.
By default all API access requests are implicitly denied (i.e., not allowed). Policy statements
can explicitly allow or deny API requests. If a request is both allowed and denied, it is always denied.
Let's assume that the following [customer managed policy](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#customer-managed-policies) is defined in AWS:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "FirstStatement",
      "Effect": "Allow",
      "Action": ["iam:ChangePassword"],
      "Resource": "*"
    },
    {
      "Sid": "SecondStatement",
      "Effect": "Allow",
      "Action": "s3:ListAllMyBuckets",
      "Resource": "*"
    },
    {
      "Sid": "ThirdStatement",
      "Effect": "Allow",
      "Action": [
        "s3:List*",
        "s3:Get*"
      ],
      "Resource": [
        "arn:aws:s3:::confidential-data",
        "arn:aws:s3:::confidential-data/*"
      ]
    }
  ]
}
```

And the above policy is attached to principal alice in AWS using
[attach-user-policy](https://docs.aws.amazon.com/cli/latest/reference/iam/attach-user-policy.html) API.
In OPA, you write each of the AWS `allow` statements as a separate statement, and you
expect the input to have `principal`, `action`, and `resource` fields.

```live:iam:module:openable
package aws

default allow = false

# FirstStatement
allow {
    principals_match
    input.action == "iam:ChangePassword"
}

# SecondStatement
allow {
    principals_match
    input.action == "s3:ListAllMyBuckets"
}

# ThirdStatement
#  Use helpers to handle implicit OR in the AWS policy.
#  Below all of the 'principals_match', 'actions_match' and 'resources_match' must be true.
allow {
    principals_match
    actions_match
    resources_match
}

# principals_match is true if input.principal matches
principals_match {
    input.principal == "alice"
}

# actions_match is true if input.action matches one in the list
actions_match {
    # iterate over the actions in the list
    actions := ["s3:List.*","s3:Get.*"]
    action := actions[_]
    # check if input.action matches an action
    regex.globs_match(input.action, action)
}

# resources_match is true if input.resource matches one in the list
resources_match {
    # iterate over the resources in the list
    resources := ["arn:aws:s3:::confidential-data","arn:aws:s3:::confidential-data/.*"]
    resource := resources[_]
    # check if input.resource matches a resource
    regex.globs_match(input.resource, resource)
}
```

```live:iam:input
{
  "principal": "alice",
  "action": "ec2:StartInstance",
  "resource": "arn:aws:ec2:::instance/i78999879"
}
```

Querying `allow` with the input above returns the following answer:

```live:iam:query:hidden
allow
```

```live:iam:output
```

## XACML

eXtensible Access Control Markup Language (XACML) was designed to express security policies: allow/deny decisions  using attributes of users, resources, actions, and the environment.
The following policy says that users from the organization Curtiss or Packard who are US or GreatBritain nationals and who work on DetailedDesign or Simulation are permitted access to documents about NavigationSystems.

```xml
<Policy PolicyId="urn:curtiss:ba:taa:taa-1.1" RuleCombiningAlgId="urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:deny-overrides">
  <Description>Policy for Business Authorization category TAA-1.1</Description>
  <Target>
    <AnyOf>
      <AllOf>
        <Match
         MatchId="urn:oasis:names:tc:xacml:1.0:function:string-equal">
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">NavigationSystem</AttributeValue>
          <AttributeDesignator
           MustBePresent="true"
           Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource"
           AttributeId="urn:curtiss:names:tc:xacml:1.0:resource:Topics"
           DataType="http://www.w3.org/2001/XMLSchema#string"/>
        </Match>
      </AllOf>
    </AnyOf>
  </Target>
  <Rule Effect="Permit">
    <Description />
    <Target>
      <Actions>
        <Action>
          <ActionMatch MatchId="urn:oasis:names:tc:xacml:1.0:function:string-equal">
            <ActionAttributeDesignator
              AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id"
              DataType="http://www.w3.org/2001/XMLSchema#string" />
            <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Any</AttributeValue>
          </ActionMatch>
        </Action>
      </Actions>
    </Target>
    <Condition FunctionId="urn:oasis:names:tc:xacml:1.0:function:and">
      <Apply xsi:type="AtLeastMemberOf" functionId="urn:oasis:names:tc:xacml:1.0:function:string-at-least-one-member-of">
        <Apply functionId="urn:oasis:names:tc:xacml:1.0:function:string-bag">
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Curtiss</AttributeValue>
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Packard</AttributeValue>
        </Apply>
        <AttributeDesignator AttributeId="http://schemas.tscp.org/2012-03/claims/OrganizationID" DataType="http://www.w3.org/2001/XMLSchema#string" />
      </Apply>
      <Apply xsi:type="AtLeastMemberOf" functionId="urn:oasis:names:tc:xacml:1.0:function:string-at-least-one-member-of">
        <Apply functionId="urn:oasis:names:tc:xacml:1.0:function:string-bag">
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">US</AttributeValue>
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">GB</AttributeValue>
        </Apply>
        <AttributeDesignator AttributeId="http://schemas.tscp.org/2012-03/claims/Nationality" DataType="http://www.w3.org/2001/XMLSchema#string" />
      </Apply>
      <Apply xsi:type="AtLeastMemberOf" functionId="urn:oasis:names:tc:xacml:1.0:function:string-at-least-one-member-of">
        <Apply functionId="urn:oasis:names:tc:xacml:1.0:function:string-bag">
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">DetailedDesign</AttributeValue>
          <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Simulation</AttributeValue>
        </Apply>
        <AttributeDesignator AttributeId="http://schemas.tscp.org/2012-03/claims/Work-Effort" DataType="http://www.w3.org/2001/XMLSchema#string" />
      </Apply>
      <Apply xsi:type="AndFunction" functionId="urn:oasis:names:tc:xacml:1.0:function:and" />
    </Condition>
  </Rule>
</Policy>
```

The same statement is shown below in OPA.  Here the inputs are assumed to be
roughly the same as for XACML: attributes of users, actions, and resources.

```live:xacml:module:openable
package xacml

permit {
    # Check that resource has a "NavigationSystem" entry
    input.resource["NavigationSystem"]

    # Check that organization is one of the options (underscore implements "any")
    org_options := ["Packard", "Curtiss"]
    input.user.organization == org_options[_]

    # Check that nationality is one of the options (underscore implements "any")
    nationality_options := ["GB", "US"]
    input.user.nationality == nationality_options[_]

    # Check that work_effort is one of the options (underscore implements "any")
    work_options := ["DetailedDesign", "Simulation"]
    input.user.work_effort == work_options[_]
}
```

```live:xacml:input
{
  "user": {
    "name": "alice",
    "organization": "Packard",
    "nationality": "GB",
    "work_effort": "DetailedDesign"
  },
  "resource": {
    "NavigationSystem": true
  },
  "action": {
    "name": "read"
  }
}
```

Querying `permit` with the input above returns the following answer:

```live:xacml:query:hidden
permit
```

```live:xacml:output
```


text:
<?php

namespace Novosga\Entity;

/**
 * AbstractAtendimentoCodificado
 * atendimento codificado (servico realizado).
 *
 * @author Rogerio Lino <rogeriolino@gmail.com>
 */
abstract class AbstractAtendimentoCodificado
{
    /**
     * @var Servico
     */
    protected $servico;

    /**
     * @var int
     */
    protected $peso;

    abstract public function getAtendimento(): AbstractAtendimento;

    abstract public function setAtendimento(AbstractAtendimento $atendimento): self;

    public function getServico()
    {
        return $this->servico;
    }

    public function setServico($servico): self
    {
        $this->servico = $servico;

        return $this;
    }

    public function getPeso()
    {
        return $this->peso;
    }

    public function setPeso($peso): self
    {
        $this->peso = $peso;

        return $this;
    }
}


text:
namespace SqlNado
{
    public enum SQLiteObjectAction
    {
        Loading,
        Loaded,
        Saving,
        Saved,
    }
}


text:
@interface CALayer (EKExtension)

#pragma mark - Frame Accessor

@property (nonatomic, assign) CGPoint ek_origin;
@property (nonatomic, assign) CGSize ek_size;
@property (nonatomic, assign) CGFloat ek_minX;
@property (nonatomic, assign) CGFloat ek_midX;
@property (nonatomic, assign) CGFloat ek_maxX;
@property (nonatomic, assign) CGFloat ek_minY;
@property (nonatomic, assign) CGFloat ek_midY;
@property (nonatomic, assign) CGFloat ek_maxY;
@property (nonatomic, assign) CGFloat ek_width;
@property (nonatomic, assign) CGFloat ek_height;

@property (nonatomic, assign) CGFloat ek_left;
@property (nonatomic, assign) CGFloat ek_right;
@property (nonatomic, assign) CGFloat ek_top;
@property (nonatomic, assign) CGFloat ek_bottom;

@property (nonatomic, assign) CGFloat ek_centerX;
@property (nonatomic, assign) CGFloat ek_centerY;
@property (nonatomic, assign) CGPoint ek_center;

#pragma mark -

- (nullable UIImage *)snapshotImage;
- (nullable NSData *)snapshotPDF;
- (void)setLayerShadow:(nonnull UIColor *)color offset:(CGSize)offset radius:(CGFloat)radius;
- (void)removeAllSublayers;

@end


text:
#include "tensorflow/lite/micro/testing/micro_test.h"
#include "tensorflow/lite/micro/testing/test_utils.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(ArgumentsExecutedOnlyOnce) {
  float count = 0.;
  // Make sure either argument is executed once after macro expansion.
  TF_LITE_MICRO_EXPECT_NEAR(0, count++, 0.1);
  TF_LITE_MICRO_EXPECT_NEAR(1, count++, 0.1);
  TF_LITE_MICRO_EXPECT_NEAR(count++, 2, 0.1);
  TF_LITE_MICRO_EXPECT_NEAR(count++, 3, 0.1);
}

TF_LITE_MICRO_TESTS_END


text:
The new `version-4` comes with a whole new code base and a lot of breaking changes. We will try to document all the things that have changed to guide you the best through the upgrade process but please be kind if we missed something.

## Upgrade Guide

### Configuration File

We have changed the way how translations are working to get close to the laravel way. To achieve this we have changed the old `translations` where all translations are stored to a simple `translation.enabled` to enable/disable the translations itself and `translation.domain` to tell us how your translation file is called.

### Notifable Trait

Here we have some of the most changes first we have moved the Trait from `Fenos\Notifynder\Notifable` to `Fenos\Notifynder\Traits\Notifable`. Second we have dropped some methods in the trait:

**kept**
* `notifications()` relationship
* `getNotifications($limit, $order)` but removed `$paginate` and `$filterScope`
* `readAllNotifications()` read all notifications received by this model

**added**
* `notifynder($category)` get a Notifynder Builder instance with a given category
* `sendNotificationFrom($category)` get a Notifynder Builder instance with a given category and the model as sender
* `sendNotificationTo($category)` get a Notifynder Builder instance with a given category and the model as receiver
* `readNotification($notification)` read a single notification
* `unreadNotification($notification)` unread a single notification

**renamed**
* `countNotificationsNotRead($filterScope)` to `countUnreadNotifications()` and dropped the `$filterScope`

**dropped**
* `getNotificationsNotRead($limit, $paginate, $order, $filterScope)`
* `getLastNotification($category, $filterScope)`
* `readLimitNotifications($numbers, $order)`
* `deleteLimitNotifications($numbers, $order)`
* `deleteAllNotifications()`

### Notifynder Facade

The new facade is reduced to the total basics, we have dropped or moved about 40 methods to remove duplicate code and make it more understandable where is what done.
Cause we have changed this much and not everything that is removed is really away we wont give you a full list of what is different. Please take a look in the examples of the new version to see how the new facade is used.

### Notification Builder

The Builder, the class that creates the notifications, has also got some reworks but hasn't changed this much. Most important change is that we now create a real object `Fenos\Notifynder\Builder\Notification` and no more arrays. This will help you if you want to create a custom sender or something like this.

### Sender Manager

This is something really new, not the name but the behaviour. The new `Fenos\Notifynder\Managers\SenderManager` is the core of the sending process. It holds all senders that are registered, also the default ones. This way you can simply override the default senders and we don't have the problem that we register the default senders in a different way.

### Migrations

We have made the `from_id` nullable to allow anonymous notifications, a new method on the Builder is also added.
The `expire_time` column on the `notifications` table is renamed to `expires_at` to get closer to laravel standards.

### CategoryGroups

This feature has been dropped - if you need it we recommend you to rebuild it in the laravel way with [events and listeners](https://laravel.com/docs/5.2/events).


text:
class Gws::UserCsv::Exporter
  include ActiveModel::Model

  attr_accessor :site, :form, :criteria, :encoding, :webmail_support

  PREFIX = 'A:'.freeze

  class << self
    def csv_basic_headers(opts = {})
      headers = %w(
        id name kana uid organization_uid email password tel tel_ext title_ids occupation_ids type
        account_start_date account_expiration_date initial_password_warning session_lifetime
        organization_id groups gws_main_group_ids switch_user_id remark
        ldap_dn gws_roles sys_roles
      )
      headers += %w(webmail_roles) if opts[:webmail_support]
      headers.map! { |k| Gws::User.t(k) }
    end

    def csv_headers(opts = {})
      new(opts).csv_headers
    end

    def enum_csv(criteria, opts = {})
      opts = opts.dup
      opts[:criteria] = criteria
      new(opts).enum_csv
    end

    def to_csv(criteria, opts = {})
      enum_csv(criteria, opts).to_a.to_csv
    end
  end

  def csv_extend_headers
    return [] if !site

    cur_form = form || Gws::UserForm.find_for_site(site)
    return [] if !cur_form
    return [] if cur_form.state_closed?

    cur_form.columns.order_by(order: 1, created: 1).map do |column|
      "#{PREFIX}#{column.name}"
    end
  end

  def csv_headers
    self.class.csv_basic_headers(webmail_support: @webmail_support) + csv_extend_headers
  end

  def enum_csv
    form ||= begin
      if site
        Gws::UserForm.find_for_site(site)
      end
    end

    Enumerator.new do |y|
      csv_headers.to_csv.tap do |csv|
        case encoding
        when "Shift_JIS"
          y << encode_sjis(csv)
        when "UTF-8"
          y << SS::Csv::UTF8_BOM + csv
        end
      end
      @criteria.each do |item|
        item_to_csv(item).to_csv.tap do |csv|
          case encoding
          when "Shift_JIS"
            y << encode_sjis(csv)
          when "UTF-8"
            y << csv
          end
        end
      end
    end
  end

  private

  def item_to_csv(item)
    main_group = item.gws_main_group_ids.present? ? item.gws_main_group(site) : nil
    switch_user = item.switch_user

    terms = []
    terms << item.id
    terms << item.name
    terms << item.kana
    terms << item.uid
    terms << item.organization_uid
    terms << item.email
    terms << nil
    terms << item.tel
    terms << item.tel_ext
    terms << item.title(site).try(:code)
    terms << item.occupation(site).try(:code)
    terms << item.label(:type)
    terms << (item.account_start_date.present? ? I18n.l(item.account_start_date) : nil)
    terms << (item.account_expiration_date.present? ? I18n.l(item.account_expiration_date) : nil)
    terms << I18n.t("ss.options.state.#{item.initial_password_warning.present? ? 'enabled' : 'disabled'}")
    terms << item.session_lifetime
    terms << (item.organization ? item.organization.name : nil)
    terms << item.groups.where(name: /\A#{Regexp.escape(root_group_name)}/).pluck(:name).join("\n")
    terms << main_group.try(:name)
    terms << (switch_user ? "#{switch_user.id},#{switch_user.name}" : nil)
    terms << item.remark
    terms << item.ldap_dn
    terms << item_roles(item).map(&:name).join("\n")
    terms << item.sys_roles.and_general.map(&:name).join("\n")
    terms << item.webmail_roles.map(&:name).join("\n") if @webmail_support

    terms += item_column_values(item)

    terms
  end

  def item_roles(item)
    roles = item.gws_roles
    roles = roles.site(site) if site
    roles
  end

  def item_column_values(item)
    terms = []

    return terms if !form || form.state_closed?

    form_data = Gws::UserFormData.site(site).user(item).form(form).order_by(id: 1, created: 1).first
    form.columns.order_by(order: 1, created: 1).each do |column|
      column_value = form_data.column_values.where(column_id: column.id).first rescue nil
      terms << column_value.try(:value)
    end

    terms
  end

  def encode_sjis(str)
    str.encode("SJIS", invalid: :replace, undef: :replace)
  end

  def root_group_name
    @root_group_name ||= site.root.name
  end
end


text:
namespace Google.Cloud.VMMigration.V1.Snippets
{
    // [START vmmigration_v1_generated_VmMigration_CreateCloneJob_sync]
    using Google.Cloud.VMMigration.V1;
    using Google.LongRunning;

    public sealed partial class GeneratedVmMigrationClientSnippets
    {
        /// <summary>Snippet for CreateCloneJob</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public void CreateCloneJobRequestObject()
        {
            // Create client
            VmMigrationClient vmMigrationClient = VmMigrationClient.Create();
            // Initialize request argument(s)
            CreateCloneJobRequest request = new CreateCloneJobRequest
            {
                ParentAsMigratingVmName = MigratingVmName.FromProjectLocationSourceMigratingVm("[PROJECT]", "[LOCATION]", "[SOURCE]", "[MIGRATING_VM]"),
                CloneJobId = "",
                CloneJob = new CloneJob(),
                RequestId = "",
            };
            // Make the request
            Operation<CloneJob, OperationMetadata> response = vmMigrationClient.CreateCloneJob(request);

            // Poll until the returned long-running operation is complete
            Operation<CloneJob, OperationMetadata> completedResponse = response.PollUntilCompleted();
            // Retrieve the operation result
            CloneJob result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation<CloneJob, OperationMetadata> retrievedResponse = vmMigrationClient.PollOnceCreateCloneJob(operationName);
            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                CloneJob retrievedResult = retrievedResponse.Result;
            }
        }
    }
    // [END vmmigration_v1_generated_VmMigration_CreateCloneJob_sync]
}


text:
namespace boost { namespace geometry
{

namespace strategy { namespace buffer
{

template<typename PointOut>
struct join_round2
{
    typedef PointOut vector_type;

    template <typename Vector, typename Point1, typename Point2>
    static inline Vector create_vector(Point1 const& p1, Point2 const& p2)
    {
        Vector v;
        geometry::convert(p1, v);
        subtract_point(v, p2);
        return v;
    }

    inline join_round2(int max_level = 4)
        : m_max_level(max_level)
    {}

    typedef typename coordinate_type<PointOut>::type coordinate_type;
    int m_max_level;

    template <typename OutputIterator, typename Point, typename PointP, typename Point1, typename Point2>
    inline void mid_points(Point const& vertex, PointP const& perpendicular,
                Point1 const& p1, Point2 const& p2,
                coordinate_type const& buffer_distance,
                coordinate_type const& max_distance,
                OutputIterator out,
                int level = 1) const
    {
        // Generate 'vectors'
        coordinate_type vp1_x = get<0>(p1) - get<0>(vertex);
        coordinate_type vp1_y = get<1>(p1) - get<1>(vertex);

        coordinate_type vp2_x = (get<0>(p2) - get<0>(vertex));
        coordinate_type vp2_y = (get<1>(p2) - get<1>(vertex));

        // Average them to generate vector in between
        coordinate_type two = 2;
        coordinate_type v_x = (vp1_x + vp2_x) / two;
        coordinate_type v_y = (vp1_y + vp2_y) / two;

        coordinate_type between_length = sqrt(v_x * v_x + v_y * v_y);

        coordinate_type const positive_buffer_distance = geometry::math::abs(buffer_distance);
        coordinate_type prop = positive_buffer_distance / between_length;

        PointOut mid_point;
        set<0>(mid_point, get<0>(vertex) + v_x * prop);
        set<1>(mid_point, get<1>(vertex) + v_y * prop);

        if (buffer_distance > max_distance)
        {
            // Calculate point projected on original perpendicular segment,
            // using vector maths
            vector_type v = create_vector<vector_type>(perpendicular, vertex);
            vector_type w = create_vector<vector_type>(mid_point, vertex);

            coordinate_type c1 = dot_product(w, v);
            if (c1 > 0)
            {
                coordinate_type c2 = dot_product(v, v);
                if (c2 > c1)
                {
                    coordinate_type b = c1 / c2;

                    PointOut projected_point;

                    multiply_value(v, b);
                    geometry::convert(vertex, projected_point);
                    add_point(projected_point, v);

                    coordinate_type projected_distance = geometry::distance(projected_point, mid_point);

                    if (projected_distance > max_distance)
                    {
                        // Do not generate from here on.
                        return;
                    }
                }
            }
        }

        if (level < m_max_level)
        {
            mid_points(vertex, perpendicular, p1, mid_point, positive_buffer_distance, max_distance, out, level + 1);
        }
        *out++ = mid_point;
        if (level < m_max_level)
        {
            mid_points(vertex, perpendicular, mid_point, p2, positive_buffer_distance, max_distance, out, level + 1);
        }
    }

    template <typename OutputIterator, typename Point, typename Point2>
    inline OutputIterator apply(Point const& vertex,
                Point2 const& perpendicular,
                Point2 const& p1, Point2 const& p2,
                coordinate_type const& buffer_distance,
                coordinate_type const& max_distance,
                OutputIterator out) const
    {
        mid_points(vertex, perpendicular, p1, p2, buffer_distance, max_distance, out);
        return out;
    }
};

template<typename PointOut>
struct join_none
{
    template <typename OutputIterator, typename Point, typename Point2,
        typename DistanceType>
    inline OutputIterator apply(Point const& ,
                Point2 const& ,
                Point2 const& , Point2 const& ,
                DistanceType const& ,
                DistanceType const& ,
                OutputIterator out) const
    {
        return out;
    }
};

}} // namespace strategy::buffer

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_STRATEGIES_BUFFER_JOIN_ROUND_HPP


text:
angular
        .module('sekcApp')
        .controller('CrudController', ['$scope', '$window', '$aside', 'PlaceholderTextService', function($scope, $window, $aside, PlaceholderTextService){

  // settings
  $scope.settings = {
    singular: 'Item',
    plural: 'Items',
    cmd: 'Add'
  };

  // adding demo data
  var data = [];
  for (var i = 1; i <= 90; i++){
    data.push({
      icon: PlaceholderTextService.createIcon(true),
      firstname: PlaceholderTextService.createFirstname(),
      lastname: PlaceholderTextService.createLastname(),
      paragraph: PlaceholderTextService.createSentence()
    });
  }
  $scope.data = data;

  // defining template
  var formTpl = $aside({
    scope: $scope,
    templateUrl: 'content/tpl/apps/crud-form.html',
    show: false,
    placement: 'left',
    backdrop: false,
    animation: 'am-slide-left'
  });

  // methods
  $scope.checkAll = function () {
    angular.forEach($scope.data, function (item) {
      item.selected = !item.selected;
    });
  };

  $scope.editItem = function(item){
    if(item){
      item.editing = true;
      $scope.item = item;
      $scope.settings.cmd = 'Edit';
      showForm();
    }
  };

  $scope.viewItem = function(item){
    if(item){
      item.editing = false;
      $scope.item = item;
      $scope.settings.cmd = 'View';
      showForm();
    }
  };

  $scope.createItem = function(){
    var item = {
      icon: PlaceholderTextService.createIcon(true),
      editing: true
    };
    $scope.item = item;
    $scope.settings.cmd = 'New';
    showForm();
  };

  $scope.saveItem = function(){
    if($scope.settings.cmd == 'New'){
      $scope.data.push($scope.item);
    }
    hideForm();
  };

  $scope.remove = function(item){
    if(confirm('Are you sure?')){
      if(item){
        $scope.data.splice($scope.data.indexOf(item), 1);
      } else {
        $scope.data = $scope.data.filter(
          function(item) {
            return !item.selected;
          }
        );
        $scope.selectAll = false;
      }
    }
  };

  showForm = function(){
    angular.element('.tooltip').remove();
    formTpl.show();
  };

  hideForm = function(){
    formTpl.hide();
  };

  $scope.$on('$destroy', function() {
    hideForm();
  });

}]);


text:
'use strict';

//var eagle_ui = require('../index');

// To know the usage of `assert`, see: http://nodejs.org/api/assert.html
var assert = require('assert');

describe("description", function(){
  it("should do blah blah blah", function(){

  });
});


text:
package org.apache.commons.collections4;

import java.util.Map;

/**
 * The "write" subset of the {@link Map} interface.
 * <p>
 * NOTE: in the original {@link Map} interface, {@link Map#put(Object, Object)} is known
 * to have the same return type as {@link Map#get(Object)}, namely {@code V}. {@link Put}
 * makes no assumptions in this regard (there is no association with, nor even knowledge
 * of, a "reading" interface) and thus defines {@link #put(Object, Object)} as returning
 * {@link Object}.
 *
 * @since 4.0
 * @version $Id$
 *
 * @see Get
 */
public interface Put<K, V> {

    /**
     * @see Map#clear()
     */
    void clear();

    /**
     * Note that the return type is Object, rather than V as in the Map interface.
     * See the class Javadoc for further info.
     *
     * @see Map#put(Object, Object)
     */
    Object put(K key, V value);

    /**
     * @see Map#putAll(Map)
     */
    void putAll(Map<? extends K, ? extends V> t);

}


text:
package com.amazonaws.services.datapipeline.model.transform;

import java.util.Map;
import java.util.Map.Entry;

import com.amazonaws.services.datapipeline.model.*;
import com.amazonaws.transform.SimpleTypeJsonUnmarshallers.*;
import com.amazonaws.transform.*;

import com.fasterxml.jackson.core.JsonToken;
import static com.fasterxml.jackson.core.JsonToken.*;

/**
 * PipelineIdName JSON Unmarshaller
 */
public class PipelineIdNameJsonUnmarshaller implements
        Unmarshaller<PipelineIdName, JsonUnmarshallerContext> {

    public PipelineIdName unmarshall(JsonUnmarshallerContext context)
            throws Exception {
        PipelineIdName pipelineIdName = new PipelineIdName();

        int originalDepth = context.getCurrentDepth();
        String currentParentElement = context.getCurrentParentElement();
        int targetDepth = originalDepth + 1;

        JsonToken token = context.getCurrentToken();
        if (token == null)
            token = context.nextToken();
        if (token == VALUE_NULL)
            return null;

        while (true) {
            if (token == null)
                break;

            if (token == FIELD_NAME || token == START_OBJECT) {
                if (context.testExpression("id", targetDepth)) {
                    context.nextToken();
                    pipelineIdName.setId(StringJsonUnmarshaller.getInstance()
                            .unmarshall(context));
                }
                if (context.testExpression("name", targetDepth)) {
                    context.nextToken();
                    pipelineIdName.setName(StringJsonUnmarshaller.getInstance()
                            .unmarshall(context));
                }
            } else if (token == END_ARRAY || token == END_OBJECT) {
                if (context.getLastParsedParentElement() == null
                        || context.getLastParsedParentElement().equals(
                                currentParentElement)) {
                    if (context.getCurrentDepth() <= originalDepth)
                        break;
                }
            }
            token = context.nextToken();
        }

        return pipelineIdName;
    }

    private static PipelineIdNameJsonUnmarshaller instance;

    public static PipelineIdNameJsonUnmarshaller getInstance() {
        if (instance == null)
            instance = new PipelineIdNameJsonUnmarshaller();
        return instance;
    }
}


text:
/**
 * table td element style
 */
/* td default align to left, the following class align to center*/
td.theYear,td.theMonth,td.theDate,td.actions,td.operations,td.view,td.createTime,td.updateTime,td.submitDate,td.auditDate,td.flag,td.tags,td.userName,td.taskDefineUserName,td.sendUserName,td.nickname,td.realName,td.enabled,td.email,td.officePhone,td.mobilePhone,td.groupName,td.smallClass,td.seqCode,td.status,td.fileNo,td.deptName,td.orgName
	{
	text-align: center;
}
/* no wrap td */
td.rowType,td.flag,td.flag,td.orgName,td.deptName,td.view,td.actions,td.operations,td.flag,td.fileno,td.dept,td.jobPosition,td.JobTitle,td.inCharge,td.remark,td.orgName,td.flag,td.realName,td.operate,td.div.operate
	{
	white-space: nowrap
}
/* align to left td*/
td.taskName,td.deptName,td.positionName,td.SchemeName,td.comment,td.description,td.digest,td.title,td.readme,td.validResult,td.schemeName,td.content,td.sendTo
	{
	text-align: left;
}


text:
import {AoiLayer} from 'app/home/map/aoiLayer'
import {LabelsLayer} from 'app/home/map/labelsLayer'
import {LegendLayer} from './legendLayer'
import {PaletteLayer} from './paletteLayer'
import {SceneAreasLayer} from 'app/home/body/process/recipe/opticalMosaic/sceneAreasLayer'
import {ValuesLayer} from './valuesLayer'
import {compose} from 'compose'
import {withLayers} from 'app/home/body/process/withLayers'
import {withRecipe} from 'app/home/body/process/recipeContext'
import PropTypes from 'prop-types'
import React from 'react'
import ReferenceDataLayer from '../body/process/recipe/classification/referenceDataLayer'

const _FeatureLayers = ({featureLayerSources, featureLayers, map}) =>
    map
        ? featureLayers
            .filter(({disabled}) => disabled !== true)
            .map((layer, i) => {
                const source = featureLayerSources.find(({id}) => id === layer.sourceId)
                return (
                    source
                        ? (
                            <FeatureLayer
                                key={i}
                                id={source.type}
                                source={source}
                                layerConfig={layer.layerConfig}
                                layerIndex={i + 1}
                                map={map}
                            />
                        )
                        : null
                )
            })
        : null

export const FeatureLayers = compose(
    _FeatureLayers,
    withLayers()
)

FeatureLayers.propTypes = {
    featureLayers: PropTypes.any,
    map: PropTypes.any,
}

class _FeatureLayer extends React.Component {
    render() {
        const {source, map, recipe, layerConfig, layerIndex} = this.props
        const id = source.type
        switch (source.type) {
        case 'Labels':return <LabelsLayer id={id} layerIndex={layerIndex} map={map}/>
        case 'Legend': return <LegendLayer/>
        case 'Palette': return <PaletteLayer/>
        case 'Values': return <ValuesLayer/>
        case 'Aoi': return (
            <AoiLayer
                id={source.type}
                layerConfig={layerConfig}
                layerIndex={layerIndex}
                recipe={recipe}
                map={map}
            />
        )
        case 'SceneAreas':
            return <SceneAreasLayer map={map}/>
        case 'ReferenceData':
            return <ReferenceDataLayer map={map}/>
        default:
            throw Error(`Unsupported feature layer type: ${source.type}`)
        }
    }

    renderFeature(component) {
        // TODO: Feature layer source should define the image layer types it supports
        //   Legend, Palette, Values only support RecipeImageLayer and AssetImageLayer
        //   Use both for populate the featureLayerSources buttons in the menu, and rendering here
        return component
    }
}

export const FeatureLayer = compose(
    _FeatureLayer,
    withRecipe(recipe => ({recipe})),
    withLayers()
)


text:
extern const cPrefab::sDef g_SandFlatRoofVillagePrefabs[];
extern const cPrefab::sDef g_SandFlatRoofVillageStartingPrefabs[];
extern const size_t g_SandFlatRoofVillagePrefabsCount;
extern const size_t g_SandFlatRoofVillageStartingPrefabsCount;


text:
<?php

/** StripQueryParams.php */
namespace Auth\Filter;

use Laminas\Filter\FilterInterface;

class StripQueryParams implements FilterInterface
{
    protected $stripParams = array(
        'logout',
    );

    public function setStripParams(array $params)
    {
        $this->stripParams = $params;
        return $this;
    }

    public function getStripParams()
    {
        return $this->stripParams;
    }

    public function filter($value, array $stripParams = array())
    {
        if (false === strpos($value, '?')) {
            return $value;
        }

        list($uri, $query) = explode('?', $value, 2);

        if (empty($stripParams)) {
            $stripParams = $this->getStripParams();
        }

        parse_str($query, $queryParams);
        $queryParams = array_diff_key($queryParams, array_flip($stripParams));
        return $uri . (empty($queryParams) ? '' : '?' . http_build_query($queryParams));
    }
}


text:
<?hh

/*
 * (c) Kévin Dunglas <dunglas@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PropertyInfo\Tests\NativeExtractors\DataProviders;

use PropertyInfo\Extractors\PropertyExtractor;
use PropertyInfo\Extractors\SetterExtractor;
use PropertyInfo\Extractors\GetterExtractor;
use PropertyInfo\Tests\NativeExtractors\Data\HackData;

/**
 * @author Mihai Stancu <stancu.t.mihai@gmail.com>
 */
class HackDataProvider
{
    /**
     * @return array
     */
    public function extractorsDataProvider()
    {
        $properties = [
            ['name' => 'bool', 'type' => 'bool', 'class' => null],
            ['name' => 'int', 'type' => 'int', 'class' => null],
            ['name' => 'float', 'type' => 'float', 'class' => null],
            ['name' => 'string', 'type' => 'string', 'class' => null],
            ['name' => 'array', 'type' => 'array', 'class' => null],
            ['name' => 'object', 'type' => 'object', 'class' => 'stdClass'],
        ];

        $extra = $properties;
        foreach ($properties as $property) {
            $property['name'] = $property['name'] . 'Nullable';
            $extra[] = $property;
        }
        foreach ($properties as $property) {
            $property['name'] = $property['name'] . 'Array';
            $extra[] = $property;
        }
        foreach ($properties as $property) {
            $property['name'] = $property['name'] . 'ArrayInt';
            $extra[] = $property;
        }
        foreach ($properties as $property) {
            $property['name'] = $property['name'] . 'Vector';
            $extra[] = $property;
        }
        $properties = $extra;

        $cases = [
            [HackData::class, PropertyExtractor::class, $properties],
            [HackData::class, GetterExtractor::class, $properties],
            [HackData::class, SetterExtractor::class, $properties],
        ];

        return $cases;
    }
}


text:
"use strict";

(function() {

  function init()
  {
  }

  Espruino.Core.Notifications = {
      init : init,
      success: function(msg, setStatus)
      {
        toastr.success(msg);
      },
      error: function(msg, setStatus)
      {
        toastr.error(msg);
      },
      warning: function(msg, setStatus)
      {
        Espruino.callProcessor("notification",{type:"warning",msg:msg},function(){});
      },
      info: function(msg, setStatus)
      {
        Espruino.callProcessor("notification",{type:"info",msg:msg},function(){});
      }
  };

})();


text:
NSString * const dialHasOscillatorValueTransformer = @"ELDialHasOscillatorValueTransformer";

static NSImage *onImage;

@implementation ELDialHasOscillatorValueTransformer

+ (void)initialize {
  if( !onImage ) {
    onImage = [NSImage imageNamed:@"red_dot.tif"];
  }
}

+ (Class)transformedValueClass {
  return [NSImage class];
}

+ (BOOL)allowsReverseTransformation {
  return NO;
}

- (id)transformedValue:(id)value {
  if( value ) {
    return onImage;
  } else {
    return nil;
  }
}

@end


text:
package com.intellij.util.io;

import com.intellij.openapi.util.LowMemoryWatcher;
import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CompressionUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.containers.SLRUMap;
import gnu.trove.TLongArrayList;
import org.iq80.snappy.CorruptionException;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.Arrays;

/**
 * Random read append only file, that internally consists of sequence of (compressed) chunks with the same length (buffer size) +
 * tail that is smaller that buffer size. Main file contains compressed data chunks, there are also chunk length table (.s) and incomplete
 * chunk file (.at).
 * (Decompressed) chunks are cached.
 */
public class CompressedAppendableFile {
  private final File myBaseFile;

  // force will clear the buffer and reset the position
  private byte[] myNextChunkBuffer;
  private int myBufferPosition;
  private boolean myDirty;

  private short[] myChunkLengthTable;
  private int myChunkTableLength;
  private static final int FACTOR = 32;
  private long [] myChunkOffsetTable; // one long offset per FACTOR compressed chunks
  private static final boolean doDebug = SystemProperties.getBooleanProperty("idea.compressed.file.self.check", true);
  private TLongArrayList myCompressedChunksFileOffsets = doDebug ? new TLongArrayList() : null;
  private static final int MAX_PAGE_LENGTH = 0xFFFF;

  private long myFileLength;
  private long myUncompressedFileLength = -1;

  protected final int myAppendBufferLength;
  private static final int myMinAppendBufferLength = 1024;

  public static final String INCOMPLETE_CHUNK_LENGTH_FILE_EXTENSION = ".s";
  @SuppressWarnings("unused") private final LowMemoryWatcher myLowMemoryWatcher;

  public CompressedAppendableFile(File file) {
    this(file, PersistentBTreeEnumerator.PAGE_SIZE);
  }

  private CompressedAppendableFile(File file, int bufferSize) {
    myBaseFile = file;
    myAppendBufferLength = bufferSize;
    assert bufferSize <= MAX_PAGE_LENGTH; // length of compressed buffer size should be in short range
    myLowMemoryWatcher = LowMemoryWatcher.register(new Runnable() {
      @Override
      public void run() {
        synchronized (CompressedAppendableFile.this) {
          force();
          myChunkLengthTable = null;
          myChunkTableLength = 0;
          myChunkOffsetTable = null;
          myNextChunkBuffer = null;
          myBufferPosition = 0;
          if (doDebug) myCompressedChunksFileOffsets.clear();
        }
      }
    });
  }

  public synchronized <Data> Data read(final long addr, KeyDescriptor<Data> descriptor) throws IOException {
    DataInputStream stream = getStream(addr);
    try {
      return descriptor.read(stream);
    } finally {
      stream.close();
    }
  }

  @NotNull
  public synchronized DataInputStream getStream(final long addr) throws IOException {
    initChunkLengthTable();
    loadAppendBuffer();
    return new DataInputStream(
      new SegmentedChunkInputStream(addr, myChunkTableLength, myNextChunkBuffer, myBufferPosition)
    );
  }

  protected File getChunkLengthFile() {
    return new File(myBaseFile.getPath() + INCOMPLETE_CHUNK_LENGTH_FILE_EXTENSION);
  }

  private synchronized void initChunkLengthTable() throws IOException {
    if (myChunkLengthTable != null) return;
    File chunkLengthFile = getChunkLengthFile();

    if (chunkLengthFile.exists()) {
      final DataInputStream chunkLengthStream = new DataInputStream(new BufferedInputStream(
        new LimitedInputStream(new FileInputStream(chunkLengthFile), (int)chunkLengthFile.length()) {
          @Override
          public int available() throws IOException {
            return remainingLimit();
          }
        }, 32768));
      try{
        short[] chunkLengthTable = new short[(int)(chunkLengthFile.length() / 2)];
        int chunkLengthTableLength = 0;

        long o = 0;
        while (chunkLengthStream.available() != 0) {
          int chunkLength = DataInputOutputUtil.readINT(chunkLengthStream);
          o += chunkLength;
          if (chunkLengthTableLength == chunkLengthTable.length) {
            chunkLengthTable = reallocShortTable(chunkLengthTable);
          }
          chunkLengthTable[chunkLengthTableLength++] = (short)chunkLength;
          if (doDebug) myCompressedChunksFileOffsets.add(o);
        }
        myChunkLengthTable = chunkLengthTable;
        myChunkTableLength = chunkLengthTableLength;

        if (myChunkTableLength >= FACTOR) {
          long[] chunkOffsetTable = new long[myChunkTableLength / FACTOR];
          long offset = 0;
          for(int i = 0; i < chunkOffsetTable.length; ++i) {
            int start = i * FACTOR;
            for(int j = 0; j < FACTOR; ++j) {
              offset += (chunkLengthTable[start + j] & MAX_PAGE_LENGTH);
            }
            chunkOffsetTable[i] = offset;
          }
          myChunkOffsetTable = chunkOffsetTable;
          if (doDebug) { // check all offsets
            for(int i = 0; i < chunkLengthTableLength; ++i) {
              calcOffsetOfPage(i);
            }
          }
        } else {
          myChunkOffsetTable = ArrayUtil.EMPTY_LONG_ARRAY;
        }

        myFileLength = calcOffsetOfPage(myChunkTableLength - 1);
      } finally {
        try {
          chunkLengthStream.close();
        } catch (IOException ignore) {}
      }
    }
    else {
      myChunkLengthTable = ArrayUtil.EMPTY_SHORT_ARRAY;
      myChunkTableLength = 0;
      myChunkOffsetTable = ArrayUtil.EMPTY_LONG_ARRAY;
      myFileLength = 0;
    }

    if (myUncompressedFileLength == -1) {
      long tempFileLength = getIncompleteChunkFile().length();
      myUncompressedFileLength = ((long)myChunkTableLength * myAppendBufferLength) + tempFileLength;
      if (myUncompressedFileLength != myFileLength + tempFileLength) {
        if (CompressionUtil.DUMP_COMPRESSION_STATS) {
          System.out.println(myUncompressedFileLength + "->" + (myFileLength + tempFileLength) + " for " + myBaseFile);
        }
      }
    }
  }

  private static final FileChunkReadCache ourDecompressedCache = new FileChunkReadCache();

  private synchronized byte[] loadChunk(int chunkNumber) throws IOException {
    try {
      if (myChunkLengthTable == null) initChunkLengthTable();
      assert chunkNumber < myChunkTableLength;

      final DataInputStream keysStream = getChunkStream(getChunksFile(), chunkNumber);
      try {
        if (keysStream.available() > 0) {
          byte[] decompressedBytes = decompress(keysStream);
          if (decompressedBytes.length != myAppendBufferLength) {
            assert false;
          }
          return decompressedBytes;
        }
      } finally {
        try {
          keysStream.close();
        } catch (IOException ignore) {}
      }

      assert false;
      return ArrayUtil.EMPTY_BYTE_ARRAY;
    }
    catch(CorruptionException e) {
      throw new IOException(e);
    }
  }

  @NotNull
  private DataInputStream getChunkStream(final File appendFile, int pageNumber) throws IOException {
    assert myFileLength != 0;
    int limit;
    long pageStartOffset;
    final long pageEndOffset = pageNumber < myChunkTableLength ? calcOffsetOfPage(pageNumber) : myFileLength;

    if (pageNumber > 0) {
      pageStartOffset = calcOffsetOfPage(pageNumber - 1);
      limit = (int)(pageEndOffset - pageStartOffset);
    } else {
      pageStartOffset = 0;
      limit = (int)pageEndOffset;
    }

    return new DataInputStream(getChunkInputStream(appendFile, pageStartOffset, limit));
  }

  private long calcOffsetOfPage(int pageNumber) {
    final int calculatedOffset = ((pageNumber + 1) / FACTOR);
    long offset = calculatedOffset > 0 ? myChunkOffsetTable[calculatedOffset - 1]:0;
    final int baseOffset = calculatedOffset * FACTOR;
    for(int index = 0, len = (pageNumber + 1) % FACTOR; index < len; ++index) {
      offset += (myChunkLengthTable[baseOffset + index] & MAX_PAGE_LENGTH);
    }
    if (doDebug) {
      assert myCompressedChunksFileOffsets.get(pageNumber) == offset;
    }
    return offset;
  }

  @NotNull
  protected InputStream getChunkInputStream(File appendFile, long offset, int pageSize) throws IOException {
    FileInputStream in = new FileInputStream(appendFile);
    if (offset > 0) {
      in.skip(offset);
    }
    return new BufferedInputStream(new LimitedInputStream(in, pageSize) {
      @Override
      public int available() throws IOException {
        return remainingLimit();
      }
    }, 32768);
  }

  public synchronized <Data> void append(Data value, KeyDescriptor<Data> descriptor) throws IOException {
    final BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream();
    DataOutput out = new DataOutputStream(bos);
    descriptor.save(out, value);
    final int size = bos.size();
    final byte[] buffer = bos.getInternalBuffer();
    append(buffer, size);
  }

  public synchronized void append(byte[] buffer, int size) throws IOException {
    if (size == 0) return;

    if (myNextChunkBuffer == null) loadAppendBuffer();
    int newBufferSize = calcBufferSize(myBufferPosition + size);
    if (newBufferSize != myNextChunkBuffer.length) {
      myNextChunkBuffer = Arrays.copyOf(myNextChunkBuffer, newBufferSize);
    }

    int bufferPosition = 0;
    int sizeToWrite = size;

    while (sizeToWrite > 0) {
      int bytesToWriteInTheBuffer = Math.min(myNextChunkBuffer.length - myBufferPosition, sizeToWrite);
      System.arraycopy(buffer, bufferPosition, myNextChunkBuffer, myBufferPosition, bytesToWriteInTheBuffer);
      myBufferPosition += bytesToWriteInTheBuffer;
      bufferPosition += bytesToWriteInTheBuffer;
      sizeToWrite -= bytesToWriteInTheBuffer;
      saveNextChunkIfNeeded();
    }

    myUncompressedFileLength += size;
    myDirty = true;
  }

  private synchronized void loadAppendBuffer() throws IOException {
    if (myNextChunkBuffer != null) return;
    myNextChunkBuffer = new byte[myAppendBufferLength];
    File tempAppendFile = getIncompleteChunkFile();
    if (tempAppendFile.exists()) {
      myBufferPosition = (int)tempAppendFile.length();
      myNextChunkBuffer = new byte[calcBufferSize(myBufferPosition)];

      FileInputStream stream = new FileInputStream(tempAppendFile);
      try {
        stream.read(myNextChunkBuffer, 0, myBufferPosition);
      }
      finally {
        try {
          stream.close();
        }
        catch (IOException ignore) {
        }
      }
    }
    else {
      myBufferPosition = 0;
      myNextChunkBuffer = new byte[myMinAppendBufferLength];
    }
  }

  private int calcBufferSize(int position) {
    return Math.min(
      myAppendBufferLength,
      Integer.highestOneBit(Math.max(myMinAppendBufferLength - 1, position)) << 1
    );
  }

  private void saveNextChunkIfNeeded() throws IOException {
    if (myBufferPosition == myNextChunkBuffer.length) {
      BufferExposingByteArrayOutputStream compressedOut = new BufferExposingByteArrayOutputStream();
      DataOutputStream compressedDataOut = new DataOutputStream(compressedOut);
      compress(compressedDataOut, myNextChunkBuffer);
      compressedDataOut.close();

      assert compressedDataOut.size() <= MAX_PAGE_LENGTH; // we need to be in short range for chunk length table
      saveChunk(compressedOut, myFileLength);

      myBufferPosition = 0;
      initChunkLengthTable();

      myFileLength += compressedOut.size();
      if (doDebug) myCompressedChunksFileOffsets.add(myFileLength);

      if (myChunkLengthTable.length == myChunkTableLength) {
        myChunkLengthTable = reallocShortTable(myChunkLengthTable);
      }

      myChunkLengthTable[myChunkTableLength++] = (short)compressedOut.size();
      if (myChunkTableLength / FACTOR > myChunkOffsetTable.length) {
        long[] newChunkOffsetTable = new long[myChunkOffsetTable.length + 1];
        System.arraycopy(myChunkOffsetTable, 0, newChunkOffsetTable, 0, myChunkOffsetTable.length);
        newChunkOffsetTable[myChunkOffsetTable.length] = myFileLength;
        myChunkOffsetTable = newChunkOffsetTable;
      }

      byte[] bytes = new byte[myAppendBufferLength];
      System.arraycopy(myNextChunkBuffer, 0, bytes, 0, myAppendBufferLength);
      ourDecompressedCache.put(this, myChunkTableLength - 1, bytes);
    }
  }

  private static short[] reallocShortTable(short[] table) {
    short[] newTable = new short[Math.max(table.length * 8 / 5, table.length + 1)];
    System.arraycopy(table, 0, newTable, 0, table.length);
    return newTable;
  }

  protected int compress(DataOutputStream compressedDataOut, byte[] buffer) throws IOException {
    return CompressionUtil.writeCompressedWithoutOriginalBufferLength(compressedDataOut, buffer, myAppendBufferLength);
  }

  protected byte[] decompress(DataInputStream keysStream) throws IOException {
    return CompressionUtil.readCompressedWithoutOriginalBufferLength(keysStream);
  }

  protected void saveChunk(BufferExposingByteArrayOutputStream compressedChunk, long endOfFileOffset) throws IOException {
    DataOutputStream stream =
      new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunksFile(), true)));
    try {
      stream.write(compressedChunk.getInternalBuffer(), 0, compressedChunk.size());
    } finally {
      try {
        stream.close();
      } catch (IOException ignore) {}
    }

    DataOutputStream chunkLengthStream =
      new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunkLengthFile(), true)));
    try {
      DataInputOutputUtil.writeINT(chunkLengthStream, compressedChunk.size());
    } finally {
      try {
        chunkLengthStream.close();
      } catch (IOException ignore) {}
    }
  }

  @NotNull
  protected File getChunksFile() {
    return new File(myBaseFile.getPath() + ".a");
  }

  private void saveIncompleteChunk() {
    if (myNextChunkBuffer != null && myBufferPosition != 0 && myDirty) {

      try {
        saveNextChunkIfNeeded();
        if (myBufferPosition != 0) {
          BufferedOutputStream stream =
            new BufferedOutputStream(new FileOutputStream(getIncompleteChunkFile()));
          try {
            stream.write(myNextChunkBuffer, 0, myBufferPosition);
          }
          finally {
            try {
              stream.close();
            }
            catch (IOException ignore) {
            }
          }
        }
      }
      catch (IOException ex) {
        throw new RuntimeException(ex);
      }
      myDirty = false;
    }
  }

  @NotNull
  private File getIncompleteChunkFile() {
    return new File(myBaseFile.getPath() + ".at");
  }

  public synchronized void force() {
    saveIncompleteChunk();
  }

  public synchronized void dispose() {
    force();
  }

  public synchronized long length() {
    if (myUncompressedFileLength == -1) {
      if (myChunkLengthTable == null) {
        try {
          initChunkLengthTable();
        }
        catch (IOException ex) {
          throw new RuntimeException(ex);
        }
      }
    }
    return myUncompressedFileLength;
  }

  public synchronized boolean isDirty() {
    return myDirty;
  }

  private static class FileChunkReadCache extends SLRUMap<FileChunkKey<CompressedAppendableFile>, byte[]> {
    @SuppressWarnings("unused") private final LowMemoryWatcher myLowMemoryWatcher = LowMemoryWatcher.register(new Runnable() {
      @Override
      public void run() {
        synchronized (FileChunkReadCache.this) {
          clear();
        }
      }
    });
    private final FileChunkKey<CompressedAppendableFile> myKey = new FileChunkKey<CompressedAppendableFile>(null, 0);

    public FileChunkReadCache() {
      super(64, 64);
    }

    @NotNull
    public byte[] get(CompressedAppendableFile file, int page) throws IOException {
      byte[] bytes;
      synchronized (this) {
        myKey.setup(file, page);
        bytes = get(myKey);
        if (bytes != null) return bytes;
      }

      bytes = file.loadChunk(page);   // out of lock
      synchronized (this) {
        put(file, page, bytes);
      }
      return bytes;
    }

    public void put(CompressedAppendableFile file, long page, byte[] bytes) {
      synchronized (this) {
        myKey.setup(file, page);
        put(myKey, bytes);
      }
    }
  }

  private class SegmentedChunkInputStream extends InputStream {
    private final long myAddr;
    private final int myChunkLengthTableSnapshotLength;
    private final byte[] myNextChunkBufferSnapshot;
    private final int myBufferPositionSnapshot;

    private InputStream bytesFromCompressedBlock;
    private InputStream bytesFromTempAppendBlock;

    private int myCurrentPageNumber;
    private int myPageOffset;

    public SegmentedChunkInputStream(long addr, int chunkLengthTableSnapshotLength, byte[] tableRef, int position) {
      myAddr = addr;
      myChunkLengthTableSnapshotLength = chunkLengthTableSnapshotLength;
      myNextChunkBufferSnapshot = tableRef;
      myBufferPositionSnapshot = position;
      myCurrentPageNumber = (int)(myAddr / myAppendBufferLength);
      myPageOffset = (int)(myAddr % myAppendBufferLength);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      if (bytesFromCompressedBlock == null) {
        byte[] decompressedBytes = myCurrentPageNumber < myChunkLengthTableSnapshotLength ?
                                   ourDecompressedCache.get(CompressedAppendableFile.this, myCurrentPageNumber) : ArrayUtil.EMPTY_BYTE_ARRAY;
        bytesFromCompressedBlock = new ByteArrayInputStream(decompressedBytes, myPageOffset, decompressedBytes.length);
      }
      int readBytesCount = 0;

      if (bytesFromCompressedBlock.available() > 0) {
        readBytesCount = bytesFromCompressedBlock.read(b, off, len);
        myPageOffset += readBytesCount;
        if (myPageOffset == myAppendBufferLength) {
          ++myCurrentPageNumber;
          myPageOffset = 0;
        }

        if (readBytesCount == len) return readBytesCount;
      }

      while (myCurrentPageNumber < myChunkLengthTableSnapshotLength) {
        byte[] decompressedBytes = ourDecompressedCache.get(CompressedAppendableFile.this, myCurrentPageNumber);
        bytesFromCompressedBlock = new ByteArrayInputStream(decompressedBytes, 0, decompressedBytes.length);
        int read = bytesFromCompressedBlock.read(b, off + readBytesCount, len - readBytesCount);
        myPageOffset += read;
        if (myPageOffset == myAppendBufferLength) {
          ++myCurrentPageNumber;
          myPageOffset = 0;
        }
        readBytesCount += read;
        if (readBytesCount == len) return readBytesCount;
      }

      if (bytesFromTempAppendBlock == null) {
        bytesFromTempAppendBlock = new ByteArrayInputStream(myNextChunkBufferSnapshot, myPageOffset, myBufferPositionSnapshot);
      }
      return readBytesCount + bytesFromTempAppendBlock.read(b, off + readBytesCount, len - readBytesCount);
    }

    @Override
    public int read() throws IOException {
      byte[] buf = {0};
      int read = read(buf);
      if (read == -1) return -1;
      return buf[0] & 0xFF;
    }
  }
}