text:
package org.apache.kafka.streams.integration;

import kafka.tools.ConsoleConsumer;
import kafka.utils.MockTime;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.LongDeserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster;
import org.apache.kafka.streams.integration.utils.IntegrationTestUtils;
import org.apache.kafka.streams.kstream.Aggregator;
import org.apache.kafka.streams.kstream.ForeachAction;
import org.apache.kafka.streams.kstream.Initializer;
import org.apache.kafka.streams.kstream.KGroupedStream;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KeyValueMapper;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.kstream.Reducer;
import org.apache.kafka.streams.kstream.Serialized;
import org.apache.kafka.streams.kstream.SessionWindowedDeserializer;
import org.apache.kafka.streams.kstream.SessionWindows;
import org.apache.kafka.streams.kstream.TimeWindowedDeserializer;
import org.apache.kafka.streams.kstream.TimeWindows;
import org.apache.kafka.streams.kstream.Windowed;
import org.apache.kafka.streams.kstream.WindowedSerdes;
import org.apache.kafka.streams.kstream.internals.SessionWindow;
import org.apache.kafka.streams.kstream.internals.TimeWindow;
import org.apache.kafka.streams.state.KeyValueIterator;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.QueryableStoreTypes;
import org.apache.kafka.streams.state.ReadOnlySessionStore;
import org.apache.kafka.streams.state.SessionStore;
import org.apache.kafka.streams.state.WindowStore;
import org.apache.kafka.test.IntegrationTest;
import org.apache.kafka.test.MockMapper;
import org.apache.kafka.test.TestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@Category({IntegrationTest.class})
public class KStreamAggregationIntegrationTest {
    private static final int NUM_BROKERS = 1;

    @ClassRule
    public static final EmbeddedKafkaCluster CLUSTER =
        new EmbeddedKafkaCluster(NUM_BROKERS);

    private static volatile int testNo = 0;
    private final MockTime mockTime = CLUSTER.time;
    private StreamsBuilder builder;
    private Properties streamsConfiguration;
    private KafkaStreams kafkaStreams;
    private String streamOneInput;
    private String userSessionsStream = "user-sessions";
    private String outputTopic;
    private KGroupedStream<String, String> groupedStream;
    private Reducer<String> reducer;
    private Initializer<Integer> initializer;
    private Aggregator<String, String, Integer> aggregator;
    private KStream<Integer, String> stream;

    @Before
    public void before() throws InterruptedException {
        testNo++;
        builder = new StreamsBuilder();
        createTopics();
        streamsConfiguration = new Properties();
        final String applicationId = "kgrouped-stream-test-" + testNo;
        streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
        streamsConfiguration
            .put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
        streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
        streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
        streamsConfiguration.put(IntegrationTestUtils.INTERNAL_LEAVE_GROUP_ON_CLOSE, true);
        streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 100);
        streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.Integer().getClass());

        final KeyValueMapper<Integer, String, String> mapper = MockMapper.selectValueMapper();
        stream = builder.stream(streamOneInput, Consumed.with(Serdes.Integer(), Serdes.String()));
        groupedStream = stream
            .groupBy(
                mapper,
                Serialized.with(Serdes.String(), Serdes.String()));

        reducer = new Reducer<String>() {
            @Override
            public String apply(final String value1, final String value2) {
                return value1 + ":" + value2;
            }
        };
        initializer = new Initializer<Integer>() {
            @Override
            public Integer apply() {
                return 0;
            }
        };
        aggregator = new Aggregator<String, String, Integer>() {
            @Override
            public Integer apply(final String aggKey, final String value, final Integer aggregate) {
                return aggregate + value.length();
            }
        };
    }

    @After
    public void whenShuttingDown() throws IOException {
        if (kafkaStreams != null) {
            kafkaStreams.close();
        }
        IntegrationTestUtils.purgeLocalStreamsState(streamsConfiguration);
    }

    @Test
    public void shouldReduce() throws Exception {
        produceMessages(mockTime.milliseconds());
        groupedStream
            .reduce(reducer, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("reduce-by-key"))
            .toStream()
            .to(outputTopic, Produced.with(Serdes.String(), Serdes.String()));

        startStreams();

        produceMessages(mockTime.milliseconds());

        final List<KeyValue<String, String>> results = receiveMessages(
            new StringDeserializer(),
            new StringDeserializer(),
            10);

        Collections.sort(results, new Comparator<KeyValue<String, String>>() {
            @Override
            public int compare(final KeyValue<String, String> o1, final KeyValue<String, String> o2) {
                return KStreamAggregationIntegrationTest.compare(o1, o2);
            }
        });

        assertThat(results, is(Arrays.asList(KeyValue.pair("A", "A"),
            KeyValue.pair("A", "A:A"),
            KeyValue.pair("B", "B"),
            KeyValue.pair("B", "B:B"),
            KeyValue.pair("C", "C"),
            KeyValue.pair("C", "C:C"),
            KeyValue.pair("D", "D"),
            KeyValue.pair("D", "D:D"),
            KeyValue.pair("E", "E"),
            KeyValue.pair("E", "E:E"))));
    }

    private static <K extends Comparable, V extends Comparable> int compare(final KeyValue<K, V> o1,
                                                                            final KeyValue<K, V> o2) {
        final int keyComparison = o1.key.compareTo(o2.key);
        if (keyComparison == 0) {
            return o1.value.compareTo(o2.value);
        }
        return keyComparison;
    }

    @Test
    public void shouldReduceWindowed() throws Exception {
        final long firstBatchTimestamp = mockTime.milliseconds();
        mockTime.sleep(1000);
        produceMessages(firstBatchTimestamp);
        final long secondBatchTimestamp = mockTime.milliseconds();
        produceMessages(secondBatchTimestamp);
        produceMessages(secondBatchTimestamp);

        Serde<Windowed<String>> windowedSerde = WindowedSerdes.timeWindowedSerdeFrom(String.class);
        groupedStream
                .windowedBy(TimeWindows.of(500L))
                .reduce(reducer)
                .toStream()
                .to(outputTopic, Produced.with(windowedSerde, Serdes.String()));

        startStreams();

        final List<KeyValue<Windowed<String>, String>> windowedOutput = receiveMessages(
            new TimeWindowedDeserializer<String>(),
            new StringDeserializer(),
            String.class,
            15);

        // read from ConsoleConsumer
        String resultFromConsoleConsumer = readWindowedKeyedMessagesViaConsoleConsumer(
                new TimeWindowedDeserializer<String>(),
                new StringDeserializer(),
                String.class,
                15);

        final Comparator<KeyValue<Windowed<String>, String>>
            comparator =
            new Comparator<KeyValue<Windowed<String>, String>>() {
                @Override
                public int compare(final KeyValue<Windowed<String>, String> o1,
                                   final KeyValue<Windowed<String>, String> o2) {
                    final int keyComparison = o1.key.key().compareTo(o2.key.key());
                    return keyComparison == 0 ? o1.value.compareTo(o2.value) : keyComparison;
                }
            };

        Collections.sort(windowedOutput, comparator);
        final long firstBatchWindow = firstBatchTimestamp / 500 * 500;
        final long secondBatchWindow = secondBatchTimestamp / 500 * 500;

        List<KeyValue<Windowed<String>, String>> expectResult = Arrays.asList(
                new KeyValue<>(new Windowed<>("A", new TimeWindow(firstBatchWindow, Long.MAX_VALUE)), "A"),
                new KeyValue<>(new Windowed<>("A", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "A"),
                new KeyValue<>(new Windowed<>("A", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "A:A"),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(firstBatchWindow, Long.MAX_VALUE)), "B"),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "B"),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "B:B"),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(firstBatchWindow, Long.MAX_VALUE)), "C"),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "C"),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "C:C"),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(firstBatchWindow, Long.MAX_VALUE)), "D"),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "D"),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "D:D"),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(firstBatchWindow, Long.MAX_VALUE)), "E"),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "E"),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(secondBatchWindow, Long.MAX_VALUE)), "E:E")
        );
        assertThat(windowedOutput, is(expectResult));

        Set<String> expectResultString = new HashSet<>(expectResult.size());
        for (KeyValue<Windowed<String>, String> eachRecord: expectResult) {
            expectResultString.add(eachRecord.toString());
        }

        // check every message is contained in the expect result
        String[] allRecords = resultFromConsoleConsumer.split("\n");
        for (String record: allRecords) {
            record = "KeyValue(" + record + ")";
            assertTrue(expectResultString.contains(record));
        }
    }

    @Test
    public void shouldAggregate() throws Exception {
        produceMessages(mockTime.milliseconds());
        groupedStream.aggregate(
            initializer,
            aggregator,
            Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("aggregate-by-selected-key"))
            .toStream()
            .to(outputTopic, Produced.with(Serdes.String(), Serdes.Integer()));

        startStreams();

        produceMessages(mockTime.milliseconds());

        final List<KeyValue<String, Integer>> results = receiveMessages(
            new StringDeserializer(),
            new IntegerDeserializer(),
            10);

        Collections.sort(results, new Comparator<KeyValue<String, Integer>>() {
            @Override
            public int compare(final KeyValue<String, Integer> o1, final KeyValue<String, Integer> o2) {
                return KStreamAggregationIntegrationTest.compare(o1, o2);
            }
        });

        assertThat(results, is(Arrays.asList(
            KeyValue.pair("A", 1),
            KeyValue.pair("A", 2),
            KeyValue.pair("B", 1),
            KeyValue.pair("B", 2),
            KeyValue.pair("C", 1),
            KeyValue.pair("C", 2),
            KeyValue.pair("D", 1),
            KeyValue.pair("D", 2),
            KeyValue.pair("E", 1),
            KeyValue.pair("E", 2)
        )));
    }

    @Test
    public void shouldAggregateWindowed() throws Exception {
        final long firstTimestamp = mockTime.milliseconds();
        mockTime.sleep(1000);
        produceMessages(firstTimestamp);
        final long secondTimestamp = mockTime.milliseconds();
        produceMessages(secondTimestamp);
        produceMessages(secondTimestamp);

        Serde<Windowed<String>> windowedSerde = WindowedSerdes.timeWindowedSerdeFrom(String.class);
        groupedStream.windowedBy(TimeWindows.of(500L))
                .aggregate(
                        initializer,
                        aggregator,
                        Materialized.<String, Integer, WindowStore<Bytes, byte[]>>with(null, Serdes.Integer())
                )
                .toStream()
                .to(outputTopic, Produced.with(windowedSerde, Serdes.Integer()));

        startStreams();

        final List<KeyValue<Windowed<String>, Integer>> windowedMessages = receiveMessages(
            new TimeWindowedDeserializer<String>(),
            new IntegerDeserializer(),
            String.class,
            15);

        // read from ConsoleConsumer
        String resultFromConsoleConsumer = readWindowedKeyedMessagesViaConsoleConsumer(
                new TimeWindowedDeserializer<String>(),
                new IntegerDeserializer(),
                String.class,
                15);

        final Comparator<KeyValue<Windowed<String>, Integer>>
            comparator =
            new Comparator<KeyValue<Windowed<String>, Integer>>() {
                @Override
                public int compare(final KeyValue<Windowed<String>, Integer> o1,
                                   final KeyValue<Windowed<String>, Integer> o2) {
                    final int keyComparison = o1.key.key().compareTo(o2.key.key());
                    return keyComparison == 0 ? o1.value.compareTo(o2.value) : keyComparison;
                }
            };

        Collections.sort(windowedMessages, comparator);

        final long firstWindow = firstTimestamp / 500 * 500;
        final long secondWindow = secondTimestamp / 500 * 500;

        List<KeyValue<Windowed<String>, Integer>> expectResult = Arrays.asList(
                new KeyValue<>(new Windowed<>("A", new TimeWindow(firstWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("A", new TimeWindow(secondWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("A", new TimeWindow(secondWindow, Long.MAX_VALUE)), 2),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(firstWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(secondWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("B", new TimeWindow(secondWindow, Long.MAX_VALUE)), 2),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(firstWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(secondWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("C", new TimeWindow(secondWindow, Long.MAX_VALUE)), 2),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(firstWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(secondWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("D", new TimeWindow(secondWindow, Long.MAX_VALUE)), 2),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(firstWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(secondWindow, Long.MAX_VALUE)), 1),
                new KeyValue<>(new Windowed<>("E", new TimeWindow(secondWindow, Long.MAX_VALUE)), 2));

        assertThat(windowedMessages, is(expectResult));

        Set<String> expectResultString = new HashSet<>(expectResult.size());
        for (KeyValue<Windowed<String>, Integer> eachRecord: expectResult) {
            expectResultString.add(eachRecord.toString());
        }

        // check every message is contained in the expect result
        String[] allRecords = resultFromConsoleConsumer.split("\n");
        for (String record: allRecords) {
            record = "KeyValue(" + record + ")";
            assertTrue(expectResultString.contains(record));
        }

    }

    private void shouldCountHelper() throws Exception {
        startStreams();

        produceMessages(mockTime.milliseconds());

        final List<KeyValue<String, Long>> results = receiveMessages(
            new StringDeserializer(),
            new LongDeserializer(),
            10);
        Collections.sort(results, new Comparator<KeyValue<String, Long>>() {
            @Override
            public int compare(final KeyValue<String, Long> o1, final KeyValue<String, Long> o2) {
                return KStreamAggregationIntegrationTest.compare(o1, o2);
            }
        });

        assertThat(results, is(Arrays.asList(
            KeyValue.pair("A", 1L),
            KeyValue.pair("A", 2L),
            KeyValue.pair("B", 1L),
            KeyValue.pair("B", 2L),
            KeyValue.pair("C", 1L),
            KeyValue.pair("C", 2L),
            KeyValue.pair("D", 1L),
            KeyValue.pair("D", 2L),
            KeyValue.pair("E", 1L),
            KeyValue.pair("E", 2L)
        )));
    }

    @Test
    public void shouldCount() throws Exception {
        produceMessages(mockTime.milliseconds());

        groupedStream.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("count-by-key"))
                .toStream()
                .to(outputTopic, Produced.with(Serdes.String(), Serdes.Long()));

        shouldCountHelper();
    }

    @Test
    public void shouldCountWithInternalStore() throws Exception {
        produceMessages(mockTime.milliseconds());

        groupedStream.count()
                .toStream()
                .to(outputTopic, Produced.with(Serdes.String(), Serdes.Long()));

        shouldCountHelper();
    }

    @Test
    public void shouldGroupByKey() throws Exception {
        final long timestamp = mockTime.milliseconds();
        produceMessages(timestamp);
        produceMessages(timestamp);

        stream.groupByKey(Serialized.with(Serdes.Integer(), Serdes.String()))
                .windowedBy(TimeWindows.of(500L))
                .count()
                .toStream(new KeyValueMapper<Windowed<Integer>, Long, String>() {
                    @Override
                    public String apply(final Windowed<Integer> windowedKey, final Long value) {
                        return windowedKey.key() + "@" + windowedKey.window().start();
                    }
                }).to(outputTopic, Produced.with(Serdes.String(), Serdes.Long()));

        startStreams();

        final List<KeyValue<String, Long>> results = receiveMessages(
            new StringDeserializer(),
            new LongDeserializer(),
            10);
        Collections.sort(results, new Comparator<KeyValue<String, Long>>() {
            @Override
            public int compare(final KeyValue<String, Long> o1, final KeyValue<String, Long> o2) {
                return KStreamAggregationIntegrationTest.compare(o1, o2);
            }
        });

        final long window = timestamp / 500 * 500;
        assertThat(results, is(Arrays.asList(
            KeyValue.pair("1@" + window, 1L),
            KeyValue.pair("1@" + window, 2L),
            KeyValue.pair("2@" + window, 1L),
            KeyValue.pair("2@" + window, 2L),
            KeyValue.pair("3@" + window, 1L),
            KeyValue.pair("3@" + window, 2L),
            KeyValue.pair("4@" + window, 1L),
            KeyValue.pair("4@" + window, 2L),
            KeyValue.pair("5@" + window, 1L),
            KeyValue.pair("5@" + window, 2L)
        )));

    }

    @Test
    public void shouldCountSessionWindows() throws Exception {
        final long sessionGap = 5 * 60 * 1000L;
        final long maintainMillis = sessionGap * 3;

        final long t1 = mockTime.milliseconds() - TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
        final List<KeyValue<String, String>> t1Messages = Arrays.asList(new KeyValue<>("bob", "start"),
                                                                        new KeyValue<>("penny", "start"),
                                                                        new KeyValue<>("jo", "pause"),
                                                                        new KeyValue<>("emily", "pause"));

        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                t1Messages,
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t1);

        final long t2 = t1 + (sessionGap / 2);
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Collections.singletonList(
                        new KeyValue<>("emily", "resume")
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t2);
        final long t3 = t1 + sessionGap + 1;
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Arrays.asList(
                        new KeyValue<>("bob", "pause"),
                        new KeyValue<>("penny", "stop")
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t3);

        final long t4 = t3 + (sessionGap / 2);
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Arrays.asList(
                        new KeyValue<>("bob", "resume"), // bobs session continues
                        new KeyValue<>("jo", "resume")   // jo's starts new session
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t4);

        final Map<Windowed<String>, Long> results = new HashMap<>();
        final CountDownLatch latch = new CountDownLatch(11);

        builder.stream(userSessionsStream, Consumed.with(Serdes.String(), Serdes.String()))
                .groupByKey(Serialized.with(Serdes.String(), Serdes.String()))
                .windowedBy(SessionWindows.with(sessionGap).until(maintainMillis))
                .count()
                .toStream()
                .foreach(new ForeachAction<Windowed<String>, Long>() {
                    @Override
                    public void apply(final Windowed<String> key, final Long value) {
                        results.put(key, value);
                        latch.countDown();
                    }
                });

        startStreams();
        latch.await(30, TimeUnit.SECONDS);
        assertThat(results.get(new Windowed<>("bob", new SessionWindow(t1, t1))), equalTo(1L));
        assertThat(results.get(new Windowed<>("penny", new SessionWindow(t1, t1))), equalTo(1L));
        assertThat(results.get(new Windowed<>("jo", new SessionWindow(t1, t1))), equalTo(1L));
        assertThat(results.get(new Windowed<>("jo", new SessionWindow(t4, t4))), equalTo(1L));
        assertThat(results.get(new Windowed<>("emily", new SessionWindow(t1, t2))), equalTo(2L));
        assertThat(results.get(new Windowed<>("bob", new SessionWindow(t3, t4))), equalTo(2L));
        assertThat(results.get(new Windowed<>("penny", new SessionWindow(t3, t3))), equalTo(1L));
    }

    @Test
    public void shouldReduceSessionWindows() throws Exception {
        final long sessionGap = 1000L; // something to do with time
        final long maintainMillis = sessionGap * 3;

        final long t1 = mockTime.milliseconds();
        final List<KeyValue<String, String>> t1Messages = Arrays.asList(new KeyValue<>("bob", "start"),
                                                                        new KeyValue<>("penny", "start"),
                                                                        new KeyValue<>("jo", "pause"),
                                                                        new KeyValue<>("emily", "pause"));

        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                t1Messages,
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t1);

        final long t2 = t1 + (sessionGap / 2);
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Collections.singletonList(
                        new KeyValue<>("emily", "resume")
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t2);
        final long t3 = t1 + sessionGap + 1;
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Arrays.asList(
                        new KeyValue<>("bob", "pause"),
                        new KeyValue<>("penny", "stop")
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t3);

        final long t4 = t3 + (sessionGap / 2);
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
                userSessionsStream,
                Arrays.asList(
                        new KeyValue<>("bob", "resume"), // bobs session continues
                        new KeyValue<>("jo", "resume")   // jo's starts new session
                ),
                TestUtils.producerConfig(
                        CLUSTER.bootstrapServers(),
                        StringSerializer.class,
                        StringSerializer.class,
                        new Properties()),
                t4);

        final Map<Windowed<String>, String> results = new HashMap<>();
        final CountDownLatch latch = new CountDownLatch(11);
        final String userSessionsStore = "UserSessionsStore";
        builder.stream(userSessionsStream, Consumed.with(Serdes.String(), Serdes.String()))
                .groupByKey(Serialized.with(Serdes.String(), Serdes.String()))
                .windowedBy(SessionWindows.with(sessionGap).until(maintainMillis))
                .reduce(new Reducer<String>() {
                    @Override
                    public String apply(final String value1, final String value2) {
                        return value1 + ":" + value2;
                    }
                }, Materialized.<String, String, SessionStore<Bytes, byte[]>>as(userSessionsStore))
                .toStream()
                .foreach(new ForeachAction<Windowed<String>, String>() {
                    @Override
                    public void apply(final Windowed<String> key, final String value) {
                        results.put(key, value);
                        latch.countDown();
                    }
                });

        startStreams();
        latch.await(30, TimeUnit.SECONDS);
        final ReadOnlySessionStore<String, String> sessionStore
                = kafkaStreams.store(userSessionsStore, QueryableStoreTypes.<String, String>sessionStore());

        // verify correct data received
        assertThat(results.get(new Windowed<>("bob", new SessionWindow(t1, t1))), equalTo("start"));
        assertThat(results.get(new Windowed<>("penny", new SessionWindow(t1, t1))), equalTo("start"));
        assertThat(results.get(new Windowed<>("jo", new SessionWindow(t1, t1))), equalTo("pause"));
        assertThat(results.get(new Windowed<>("jo", new SessionWindow(t4, t4))), equalTo("resume"));
        assertThat(results.get(new Windowed<>("emily", new SessionWindow(t1, t2))), equalTo("pause:resume"));
        assertThat(results.get(new Windowed<>("bob", new SessionWindow(t3, t4))), equalTo("pause:resume"));
        assertThat(results.get(new Windowed<>("penny", new SessionWindow(t3, t3))), equalTo("stop"));

        // verify can query data via IQ
        final KeyValueIterator<Windowed<String>, String> bob = sessionStore.fetch("bob");
        assertThat(bob.next(), equalTo(KeyValue.pair(new Windowed<>("bob", new SessionWindow(t1, t1)), "start")));
        assertThat(bob.next(), equalTo(KeyValue.pair(new Windowed<>("bob", new SessionWindow(t3, t4)), "pause:resume")));
        assertFalse(bob.hasNext());

    }

    private void produceMessages(final long timestamp) throws Exception {
        IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(
            streamOneInput,
            Arrays.asList(
                new KeyValue<>(1, "A"),
                new KeyValue<>(2, "B"),
                new KeyValue<>(3, "C"),
                new KeyValue<>(4, "D"),
                new KeyValue<>(5, "E")),
            TestUtils.producerConfig(
                CLUSTER.bootstrapServers(),
                IntegerSerializer.class,
                StringSerializer.class,
                new Properties()),
            timestamp);
    }

    private void createTopics() throws InterruptedException {
        streamOneInput = "stream-one-" + testNo;
        outputTopic = "output-" + testNo;
        userSessionsStream = userSessionsStream + "-" + testNo;
        CLUSTER.createTopic(streamOneInput, 3, 1);
        CLUSTER.createTopics(userSessionsStream, outputTopic);
    }

    private void startStreams() {
        kafkaStreams = new KafkaStreams(builder.build(), streamsConfiguration);
        kafkaStreams.start();
    }

    private <K, V> List<KeyValue<K, V>> receiveMessages(final Deserializer<K> keyDeserializer,
                                                        final Deserializer<V> valueDeserializer,
                                                        final int numMessages)
        throws InterruptedException {
        return receiveMessages(keyDeserializer, valueDeserializer, null, numMessages);
    }

    private <K, V> List<KeyValue<K, V>> receiveMessages(final Deserializer<K>
                                                                keyDeserializer,
                                                        final Deserializer<V>
                                                                valueDeserializer,
                                                        final Class innerClass,
                                                        final int numMessages) throws InterruptedException {
        final Properties consumerProperties = new Properties();
        consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
        consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "kgroupedstream-test-" + testNo);
        consumerProperties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializer.getClass().getName());
        consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer.getClass().getName());
        if (keyDeserializer instanceof TimeWindowedDeserializer || keyDeserializer instanceof SessionWindowedDeserializer) {
            consumerProperties.setProperty(StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS,
                    Serdes.serdeFrom(innerClass).getClass().getName());
        }
        return IntegrationTestUtils.waitUntilMinKeyValueRecordsReceived(
                consumerProperties,
                outputTopic,
                numMessages,
                60 * 1000);
    }

    private <K, V> String readWindowedKeyedMessagesViaConsoleConsumer(final Deserializer<K> keyDeserializer,
                                                                      final Deserializer<V> valueDeserializer,
                                                                      final Class innerClass,
                                                                      final int numMessages) {
        ByteArrayOutputStream newConsole = new ByteArrayOutputStream();
        PrintStream originalStream = System.out;
        try (PrintStream newStream = new PrintStream(newConsole)) {
            System.setOut(newStream);

            String keySeparator = ", ";
            // manually construct the console consumer argument array
            String[] args = new String[] {
                "--bootstrap-server", CLUSTER.bootstrapServers(),
                "--from-beginning",
                "--property", "print.key=true",
                "--topic", outputTopic,
                "--max-messages", String.valueOf(numMessages),
                "--property", "key.deserializer=" + keyDeserializer.getClass().getName(),
                "--property", "value.deserializer=" + valueDeserializer.getClass().getName(),
                "--property", "key.separator=" + keySeparator,
                "--property", "key.deserializer." + StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS + "=" + Serdes.serdeFrom(innerClass).getClass().getName()
            };

            ConsoleConsumer.messageCount_$eq(0); //reset the message count
            ConsoleConsumer.run(new ConsoleConsumer.ConsumerConfig(args));
            newStream.flush();
            System.setOut(originalStream);
            return newConsole.toString();
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <!--
            Define the database name of your application.
            By default each database name should be end with .db.
            If you didn't name your database end with .db,
            LitePal would plus the suffix automaticly for you.
            For example:
            <dbname value="demo" ></dbname>
        -->
    <dbname value="collection"></dbname>

    <!--
        Define the version of your database. Each time you want
        to upgrade your database, the version tag would helps.
        Modify the models you defined in the mapping tag, and just
        make the version value plus one, the upgrade of database
        will be processed automaticly without concern.
            For example:
        <version value="1" ></version>
    -->
    <version value="2"></version>

    <!--
        Define your models in the list with mapping tag, LitePal will
        create tables for each mapping class. The supported fields
        defined in models will be mapped into columns.
        For example:
        <list>
            <mapping class="com.test.model.Reader"></mapping>
            <mapping class="com.test.model.Magazine"></mapping>
        </list>
    -->
    <list>
        <mapping class="com.stupidman.admin.collectionandroiddemo.litepal.bean.Users"></mapping>
        <mapping class="com.stupidman.admin.collectionandroiddemo.litepal.bean.News"></mapping>
        <mapping class="com.stupidman.admin.collectionandroiddemo.litepal.bean.Comments"></mapping>
        <mapping class="com.stupidman.admin.collectionandroiddemo.litepal.bean.Category"></mapping>
    </list>
</litepal>

redpajama_set_name: RedPajamaGithub


text:
@interface ProgressTableViewCell : UITableViewCell{
    FBProgressView *progressView_;
}
@property (strong, readonly) FBProgressView *progressView;

- (id)initWithSubmitter: (id<ENGPhotoSubmitterProtocol>) submitter andSize:(CGSize)size;
- (void)showText:(NSString *)text;
@end


text:
class Chapter

  include Mongoid::Document

  field :_id, overwrite: true, type: String

  field :n,   type: Integer

  belongs_to :translation
  belongs_to :book

  embeds_many :verses

  def abbr
    "#{book.abbr} #{n}"
  end

  def book_title
    book.title
  end

end


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00000000"
    android:id="@+id/rl_regisration">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv_registration"
            android:layout_width="match_parent"
            android:layout_height="@dimen/y280"
            android:layout_marginLeft="@dimen/x30"
            android:layout_marginRight="@dimen/x30"
            android:background="@drawable/registration_btn_case"></ListView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="match_parent"
                android:layout_height="@dimen/y110"
                android:layout_marginBottom="@dimen/y20"
                android:layout_marginLeft="@dimen/x30"
                android:layout_marginRight="@dimen/x30"
                android:layout_marginTop="@dimen/y16"
                android:background="@drawable/registration_btn_case"
                android:text="取消"
                android:textColor="@color/backgroundblue"
                android:textSize="@dimen/y38" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>


text:
<?php
namespace FatPanda\Illuminate\WordPress\Models;

use FatPanda\Illuminate\WordPress\Concerns\CanBeSavedToProfile;

class SimpleProfileSection {

	use CanBeSavedToProfile;

	function __construct($type)
	{
		$this->type = $type;
	}

	function getMetaType($data) {
		return 'profile_section_'.strtolower(str_replace('\\', '_', $this->type));
	}

}


text:
/**
 * This file is part of the CernVM File System.
 */

#ifndef CVMFS_PACK_H_
#define CVMFS_PACK_H_

#include <inttypes.h>
#include <pthread.h>

#include <cstdio>
#include <set>
#include <string>
#include <vector>

#include "gtest/gtest_prod.h"
#include "hash.h"
#include "util/single_copy.h"
#include "util_concurrency.h"

/**
 * Multiple content-addressable objects in a single BLOB.  A (serialized)
 * ObjectPack has a header, an index containing all the objects and their
 * offsets followed by the concatenated objects.  The secure hash of the index
 * is in the header.
 *
 * This allows to verify the hash of the index and the hash of all objects
 * individually.  Thus, objects can be read and written in parallel to and from
 * the ObjectPack.
 *
 * Objects are used by "tentacles" to send change sets to the "octopus server"
 * as well as by the stratum 0 to transfer object bulks to stratum 1s during
 * replication.
 */
class ObjectPack : SingleCopy {
  FRIEND_TEST(T_Pack, Bucket);
  FRIEND_TEST(T_Pack, ObjectPack);
  FRIEND_TEST(T_Pack, ObjectPackTransfer);

 private:
  struct Bucket;

 public:
  typedef Bucket *BucketHandle;

  /**
   * This is used to identify the content type of different buckets. Initially,
   * the contents of a bucket are identified as kEmpty. When committing a
   * bucket, this is set to either kNamed - if the bucket holds the contents of
   * a named file - or kCas - if the bucket holds the contents of a content
   * addressable buffer.
   */
  enum BucketContentType { kEmpty, kNamed, kCas };

  static const uint64_t kDefaultLimit = 200 * 1024 * 1024;  // 200MB

  /**
   * Limit the maximum number of objects to avoid very large headers.  Assuming
   * Sha256 (71 bytes hex) + 9 bytes for the file sizes, a header with 100,000
   * files should fit in 10M.
   */
  static const uint64_t kMaxObjects = 100000;

  explicit ObjectPack(const uint64_t limit = kDefaultLimit);
  ~ObjectPack();

  static void AddToBucket(const void *buf, const uint64_t size,
                          const BucketHandle handle);

  BucketHandle NewBucket();

  bool CommitBucket(const BucketContentType type, const shash::Any &id,
                    const BucketHandle handle, const std::string &name = "");

  void DiscardBucket(const BucketHandle handle);
  void TransferBucket(const BucketHandle handle, ObjectPack *other);

  unsigned char *BucketContent(size_t idx) const;
  uint64_t BucketSize(size_t idx) const;
  const shash::Any &BucketId(size_t idx) const;

  uint64_t size() const { return size_; }

  // This returns the number of objects in the pack (equal to the number of
  // committed buckets)
  size_t GetNoObjects() const { return buckets_.size(); }

 private:
  /**
   * Wrapper around memory to which data can be added.  The memory should
   * represent a piece of content-addressable storage.
   */
  struct Bucket : SingleCopy {
    static const unsigned kInitialSize = 128;

    Bucket();
    ~Bucket();
    void Add(const void *buf, const uint64_t buf_size);

    unsigned char *content;
    uint64_t size;
    uint64_t capacity;
    shash::Any id;
    BucketContentType content_type;
    std::string name;
  };

  void InitLock();

  /**
   * Protects open_buckets_ and buckets_ collections.
   */
  pthread_mutex_t *lock_;

  /**
   * Maximum size of this object pack.
   */
  uint64_t limit_;
  /**
   * Accumulated size of all committed buckets.
   */
  uint64_t size_;
  /**
   * Buckets that were requested but that are not yet committed
   */
  std::set<BucketHandle> open_buckets_;
  /**
   * Buckets that are committed to the object pack.
   */
  std::vector<BucketHandle> buckets_;
};

/**
 * Data structures required for the ObjectPack serialization.  Event is a
 * template parameter for the Observable base class of ObjectPack and hence
 * moved into this base class.
 */
namespace ObjectPackBuild {
struct Event {
  Event(const shash::Any &id, uint64_t size, unsigned buf_size, const void *buf,
        ObjectPack::BucketContentType type, const std::string &name)
      : id(id),
        size(size),
        buf_size(buf_size),
        buf(buf),
        object_type(type),
        object_name(name) {}

  shash::Any id;
  uint64_t size;
  unsigned buf_size;
  const void *buf;
  ObjectPack::BucketContentType object_type;
  std::string object_name;
};

enum State {
  kStateContinue = 0,
  kStateDone,
  kStateCorrupt,
  kStateBadFormat,
  kStateHeaderTooBig,
  kStateTrailingBytes,
};
}  // namespace ObjectPackBuild

/**
 * Serializes ObjectPacks.  It can also serialize a single large file as an
 * "object pack", which otherwise would need special treatment.
 *
 * The serialized format has a global, human readable header which has lines of
 * character keys and string values (like the cvmfs manifest) follwed by a "--"
 * separator line followed by the index of objects. The index contains one line
 * for each item in the pack. Each line contains the following space-separated
 * tokens:
 * 1. object type identifier ('N' for named files, 'C' for CAS blobs)
 * 2. hash digest (hex)
 * 3. object size (decimal)
 * 4. object name - base64 encoding of the object name (optional - only if the
 *                  object type is 'N')
 */
class ObjectPackProducer {
 public:
  explicit ObjectPackProducer(ObjectPack *pack);
  ObjectPackProducer(const shash::Any &id, FILE *big_file,
                     const std::string &file_name = "");
  unsigned ProduceNext(const unsigned buf_size, unsigned char *buf);
  void GetDigest(shash::Any *hash);
  unsigned GetHeaderSize() { return header_.size(); }

 private:
  /**
   * Unused if big_file_ is used.
   */
  ObjectPack *pack_;

  /**
   * Unused if object pack is used.  Rewind before giving to ObjectPackProducer.
   */
  FILE *big_file_;

  /**
   * Keeps track of how many bytes have been produced.
   */
  uint64_t pos_;

  /**
   * Keeps track of the current index in pack_->buckets_
   */
  size_t idx_;

  /**
   * Keeps track of the current position in pack_->buckets_[idx_]
   */
  size_t pos_in_bucket_;

  /**
   * The header is created in the constructor.
   */
  std::string header_;
};

/**
 * Deserializes an ObjectPack created by ObjectPackProducer.  For every object
 * it calls all listeners with a Event parameter at least once for every
 * object.  For large objects, it calls the listeners multiple times.  It won't
 * verify the incoming data, this is up to the listeners handling the data.
 * The ObjectPackConsumer will verify the header digest, however.
 */
class ObjectPackConsumer : public Observable<ObjectPackBuild::Event> {
 public:
  explicit ObjectPackConsumer(const shash::Any &expected_digest,
                              const unsigned expected_header_size);
  ObjectPackBuild::State ConsumeNext(const unsigned buf_size,
                                     const unsigned char *buf);

 private:
  /**
   * For large objects, notify listeners in chunks of 128kB.
   */
  static const unsigned kAccuSize = 128 * 1024;

  struct IndexEntry {
    IndexEntry() : id(), size(), entry_type(), entry_name() {}
    IndexEntry(const shash::Any &id, const uint64_t size,
               ObjectPack::BucketContentType type, const std::string &name)
        : id(id), size(size), entry_type(type), entry_name(name) {}
    shash::Any id;
    uint64_t size;
    ObjectPack::BucketContentType entry_type;
    std::string entry_name;
  };

  bool ParseHeader();
  bool ParseItem(const std::string &line, IndexEntry *entry,
                 uint64_t *sum_size);

  ObjectPackBuild::State ConsumePayload(const unsigned buf_size,
                                        const unsigned char *buf);

  shash::Any expected_digest_;
  unsigned expected_header_size_;

  /**
   * Keeps track of how many bytes have been consumed from the payload.
   */
  uint64_t pos_;

  /**
   * Keeps track of the current index in the array of objects (index_)
   */
  unsigned idx_;

  /**
   * Keeps track of how many bytes have been processed from the current object.
   */
  unsigned pos_in_object_;

  /**
   * Collects data for large objects so that the number of callbacks to the
   * listeners is reduced.
   */
  unsigned char accumulator_[kAccuSize];

  /**
   * Keeps track of how many live bytes are stored in the accumulator_.
   */
  unsigned pos_in_accu_;

  /**
   * The state starts in kStateContinue and makes exactly one transition into
   * one of the other states as more bytes are consumed.
   */
  ObjectPackBuild::State state_;

  /**
   * Temporary store for the incomplete header.  Once completely consumed, the
   * header is interpreted into global_header_ and object_index_.
   */
  std::string raw_header_;

  /**
   * Total size of all the objects in the pack (header not included).
   */
  uint64_t size_;

  /**
   * Hash id and size of the individual objects in order.
   */
  std::vector<IndexEntry> index_;
};

#endif  // CVMFS_PACK_H_


text:
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
	"regexp"
	"strings"

	"opentelemetry-trace-codelab-go/server/shakesapp"

	"cloud.google.com/go/storage"
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"go.opentelemetry.io/otel"
	stdout "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
	"go.opentelemetry.io/otel/propagation"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
	"google.golang.org/grpc"
	healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

const (
	listenPort = "5050"

	bucketName   = "dataflow-samples"
	bucketPrefix = "shakespeare/"
)

type serverService struct {
	shakesapp.UnimplementedShakespeareServiceServer
	healthpb.UnimplementedHealthServer
}

func NewServerService() *serverService {
	return &serverService{}
}

// step2. add OpenTelemetry initialization function
func initTracer() (*sdktrace.TracerProvider, error) {
	// create a stdout exporter to show collected spans out to stdout.
	exporter, err := stdout.New(stdout.WithPrettyPrint())
	if err != nil {
		return nil, err
	}
	// for the demonstration, we use AlwaysSmaple sampler to take all spans.
	// do not use this option in production.
	tp := sdktrace.NewTracerProvider(
		sdktrace.WithSampler(sdktrace.AlwaysSample()),
		sdktrace.WithBatcher(exporter),
	)
	otel.SetTracerProvider(tp)
	otel.SetTextMapPropagator(propagation.TraceContext{})
	return tp, nil
}

// step2: end OpenTelemetry initialization function

// TODO: instrument the application with Cloud Profiler agent
func main() {
	port := listenPort
	if os.Getenv("PORT") != "" {
		port = os.Getenv("PORT")
	}

	lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
	if err != nil {
		log.Fatalf("error %v; error listening port %v", err, port)
	}

	// step2. setup OpenTelemetry
	tp, err := initTracer()
	if err != nil {
		log.Fatalf("failed to initialize TracerProvider: %v", err)
	}
	defer func() {
		if err := tp.Shutdown(context.Background()); err != nil {
			log.Fatalf("error shutting down TracerProvider: %v", err)
		}
	}()
	// step2. end setup

	svc := NewServerService()
	// step2: add interceptor
	interceptorOpt := otelgrpc.WithTracerProvider(otel.GetTracerProvider())
	srv := grpc.NewServer(
		grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(interceptorOpt)),
		grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(interceptorOpt)),
	)
	// step2: end adding interceptor
	shakesapp.RegisterShakespeareServiceServer(srv, svc)
	healthpb.RegisterHealthServer(srv, svc)
	if err := srv.Serve(lis); err != nil {
		log.Fatalf("error serving server: %v", err)
	}
}

// GetMatchCount implements a server for ShakespeareService.
//
// TODO: instrument the application to take the latency of the request to Cloud Storage
func (s *serverService) GetMatchCount(ctx context.Context, req *shakesapp.ShakespeareRequest) (*shakesapp.ShakespeareResponse, error) {
	resp := &shakesapp.ShakespeareResponse{}
	texts, err := readFiles(ctx, bucketName, bucketPrefix)
	if err != nil {
		return resp, fmt.Errorf("fails to read files: %s", err)
	}
	for _, text := range texts {
		for _, line := range strings.Split(text, "\n") {
			line, query := strings.ToLower(line), strings.ToLower(req.Query)
			// TODO: Compiling and matching a regular expression on every request
			// might be too expensive? Consider optimizing.
			isMatch, err := regexp.MatchString(query, line)
			if err != nil {
				return resp, err
			}
			if isMatch {
				resp.MatchCount++
			}
		}
	}
	return resp, nil
}

// readFiles reads the content of files within the specified bucket with the
// specified prefix path in parallel and returns their content. It fails if
// operations to find or read any of the files fails.
func readFiles(ctx context.Context, bucketName, prefix string) ([]string, error) {
	type resp struct {
		s   string
		err error
	}

	client, err := storage.NewClient(ctx, option.WithoutAuthentication())
	if err != nil {
		return []string{}, fmt.Errorf("failed to create storage client: %s", err)
	}
	defer client.Close()

	bucket := client.Bucket(bucketName)

	var paths []string
	it := bucket.Objects(ctx, &storage.Query{Prefix: bucketPrefix})
	for {
		attrs, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return []string{}, fmt.Errorf("failed to iterate over files in %s starting with %s: %v", bucketName, prefix, err)
		}
		if attrs.Name != "" {
			paths = append(paths, attrs.Name)
		}
	}

	resps := make(chan resp)
	for _, path := range paths {
		go func(path string) {
			obj := bucket.Object(path)
			r, err := obj.NewReader(ctx)
			if err != nil {
				resps <- resp{"", err}
			}
			defer r.Close()
			data, err := ioutil.ReadAll(r)
			resps <- resp{string(data), err}
		}(path)
	}
	ret := make([]string, len(paths))
	for i := 0; i < len(paths); i++ {
		r := <-resps
		if r.err != nil {
			err = r.err
		}
		ret[i] = r.s
	}
	return ret, err
}

// Check is for health checking.
func (s *serverService) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
	return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil
}

// Watch is for health checking.
func (s *serverService) Watch(req *healthpb.HealthCheckRequest, server healthpb.Health_WatchServer) error {
	return nil
}


text:
<?php

use Blocktrail\SDK\BlocktrailSDK;
use Blocktrail\SDK\Connection\Exceptions\ObjectNotFound;
use Blocktrail\SDK\Wallet;
use Blocktrail\SDK\WalletInterface;

require_once __DIR__ . "/../vendor/autoload.php";

$client = new BlocktrailSDK("MY_APIKEY", "MY_APISECRET", "BTC", true /* testnet */, 'v1');
// $client->setVerboseErrors();
// $client->setCurlDebugging();

/**
 * @var $wallet             \Blocktrail\SDK\WalletInterface
 * @var $backupMnemonic     string
 */
try {
    /** @var Wallet $wallet */
    $wallet = $client->initWallet([
        "identifier" => "example-wallet",
        "readonly" => true
    ]);
} catch (ObjectNotFound $e) {
    list($wallet, $primaryMnemonic, $backupMnemonic, $blocktrailPublicKeys) = $client->createNewWallet([
        "identifier" => "example-wallet",
        "passphrase" => "example-strong-password",
        "key_index" => 9999
    ]);
    $wallet->doDiscovery();
    $wallet->lock();
}

// var_dump($wallet->deleteWebhook());
// var_dump($wallet->setupWebhook("http://www.example.com/wallet/webhook/example-wallet"));

// print some new addresses
var_dump($wallet->getAddressByPath("M/9999'/0/0"), $wallet->getAddressByPath("M/9999'/0/1"));

// print the balance
list($confirmed, $unconfirmed) = $wallet->getBalance();
var_dump($confirmed, BlocktrailSDK::toBTC($confirmed));
var_dump($unconfirmed, BlocktrailSDK::toBTC($unconfirmed));

// send a payment (will fail unless you've send some BTC to an address part of this wallet)
$addr = $wallet->getNewAddress();

// unlock with 2nd argument a callable, wallet will only be unlocked within that callable
$wallet->unlock(["passphrase" => "example-strong-password"], function(Wallet $wallet) use($addr) {
    var_dump($wallet->pay([
        $addr => BlocktrailSDK::toSatoshi(0.001),
    ]));
});

// wallet is locked again after callable is done
var_dump($wallet->isLocked());

// unlock without callable, won't be locked until ->lock is called
$wallet->unlock(["passphrase" => "example-strong-password"]);

var_dump($wallet->pay([
    $addr => BlocktrailSDK::toSatoshi(0.001),
]));

// wallet still unlocked!
var_dump($wallet->isLocked());


text:
[![Build Status](https://api.travis-ci.org/TiarkRompf/virtualization-lms-core.png)](https://travis-ci.org/TiarkRompf/virtualization-lms-core)
[![Maven Central](https://img.shields.io/maven-central/v/org.scala-lang.lms/lms-core_2.11.svg)](https://maven-badges.herokuapp.com/maven-central/org.scala-lang.lms/lms-core_2.11)

Lightweight Modular Staging (LMS) is a runtime code generation approach.
This framework, LMS-Core, provides a library of core components for building
high performance code generators and embedded compilers in Scala.

Closely related projects are [Delite](https://github.com/stanford-ppl/Delite/),
a framework for heterogeneous parallel domain specific languages (DSLs),
and [Scala-Virtualized](https://github.com/tiarkrompf/scala-virtualized/),
a set of minimal extensions to the Scala compiler to make embedding DSLs
more seamless.

### Background:

- [LMS website](http://scala-lms.github.io)

- [LMS paper](http://infoscience.epfl.ch/record/150347/files/gpce63-rompf.pdf)

- [Delite website](http://stanford-ppl.github.com/Delite/)

- [Scala-Virtualized wiki](https://github.com/TiarkRompf/scala-virtualized/wiki)

### How to build:

1. Install the [SBT](http://www.scala-sbt.org/) build tool.

2. Run `sbt test` to run the test suite.

3. Run `sbt publish-local` to install LMS-Core for use in other projects.

### License:

Copyright 2010-2016, EPFL and collaborators. Licensed under the revised BSD License.


text:
[![Build Status](https://secure.travis-ci.org/isamu/swipejs.png?branch=master)](http://travis-ci.org/isamu/swipejs)

# Swipe Javascript Engine

Yet another swipe engine.

Original Swipe engine is here.

https://github.com/swipe-org/swipe

## What is Swipe?

Swipe is a **domain-specific, declarative language** for non-developers (such as designers, animators, illustrators, musicians, videographers and comic writers) to create **media-rich/animated documents** that contain photos, videos, images, vector graphics, animations, voices, musics and sound effects, which will be consumed on touch-enabled devices such as smartphones, tablets and touch-enabled set-top-boxes (such as **iPhone and Apple TV**).

## What is Swipe Engine?

Swipe Engine is the **viewer** of documents described in Swipe. Since it supports hyperlinking and allows you to browse from one Swipe document to another, you could even call it a **domain-specific browser**. At this moment (October 2015), Swipe Engine is available for **iOS and tvOS**, but will be ported to other platforms such as Android, Windows and even HTML5 (and we are looking for volunteers to do so).

## Why do we need Swipe?

Because existing platforms (such as HTML5, ePub, Flash and iBooks) require some form of programming to create media-rich/animated documents, which is expensive and error-prone.

* HTML used be a simple declarative language, but became a full-blown programming environment with DOM and JavaScript.
* Flash used be a great animation tool for designers, but became a full-blown programming environment with the introduction of ActionScript.
* ePub does not directly support animations, and the author needs to embed some JavaScript code to enable animations, which may or may not work depending on the eBook platform.

That's why I came to the conclusion that it's time to design a new platform, which is powerful enough to describe media-rich documents that allow us to take full advantage of modern devices, but is also **strictly declarative** (no API, no script), easy to read, easy to write and easy to auto-generate.

## Why is it open?

Because I want to make it an industry standard so that everybody can benefit from it. I was lucky enough to work on several successful products, such as Windows 95, which were used by hundreds of millions of people. I'd like to see such a success again.

## Is it really free?

Yes, I still maintain the copyright, but it's **absolutely free**. You can use it for non-commercial and commercial applications, modify it as needed, port it to other platforms (I'd really appreciate if you open source it as well), re-distribute it with your applications, as long as you explicitly mention that your application uses Swipe and recognize me (Satoshi Nakajima) as the copyright holder.

## Target Applications

* Interactive Comics
* Sound Novels
* Graphical Audio Books and Music Albums
* Interactive Videos
* Media-rich Tutorials and Presentations
* Interactive Arts

## Primary Audiences

* Designers/Illustrators
* Animators
* Comic Writers
* Musicians/Artists
* Videographers/Photographers
* Teachers/Educators
* Weekend Programmers

## Design Principles

* Optimized for touch-enabled devices
* 100% declarative (no programming)
* Rich & interactive animations
* Re-invent the video experience
* Customizable page-transitions
* Designer friendly
* Lightweight & portable

## Is there an authoring tool?

I'm working on it. Stay tuned.

## Specification

The specification is available [here](SPECIFICATION.md).


text:
"""
Django settings for indieitudeapp project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))

ADMINS = [
    # ("Your Name", "your_email@example.com"),
]

MANAGERS = ADMINS

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '9wa0^p255-o_mv%)7yc9*x$%aw!bvk-xf)8e8esoua*=mq@ln('

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'django.contrib.messages',

    ##### Indieitude Apps #####
    "account",
    "products",
    "boutiques",
    "designers",

    #### Third Party Apps #####
    'south',
    "eventlog",
    "metron",
    'imagekit',

    # theme
    "bootstrapform",
    "pinax_theme_bootstrap",
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

# AUTH_PROFILE_MODULE = "profiles.ProfileUser"

ROOT_URLCONF = 'Indieitude.urls'

WSGI_APPLICATION = 'Indieitude.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

SITE_ID = int(os.environ.get("SITE_ID", 1))

######### Static asset configuration ################

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = "/site_media/static/"
STATIC_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "static")

STATICFILES_DIRS = (
    os.path.join(PACKAGE_ROOT, "static"),
)

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

####### Template configuration  ###########

TEMPLATE_DIRS = (
    os.path.join(PACKAGE_ROOT, "templates"),
)

TEMPLATE_LOADERS = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]

TEMPLATE_CONTEXT_PROCESSORS = [
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",
    "account.context_processors.account",
    "pinax_theme_bootstrap.context_processors.theme",
]

####### Media asset configuration  ###########

MEDIA_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "media")

MEDIA_URL = "/site_media/media/"

######### Heroku/Django Setup #########

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config(default='mysql://bac795441f6773:cc9f65ae@us-cdbr-east-05.cleardb.net/heroku_fd684731d31d2c3')

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

#########################################################

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {
        "require_debug_false": {
            "()": "django.utils.log.RequireDebugFalse"
        }
    },
    "handlers": {
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler"
        }
    },
    "loggers": {
        "django.request": {
            "handlers": ["mail_admins"],
            "level": "ERROR",
            "propagate": True,
        },
    }
}

FIXTURE_DIRS = [
    os.path.join(PROJECT_ROOT, "fixtures"),
]

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

ACCOUNT_OPEN_SIGNUP = True
ACCOUNT_USE_OPENID = False
ACCOUNT_REQUIRED_EMAIL = False
ACCOUNT_EMAIL_UNIQUE = True
ACCOUNT_EMAIL_CONFIRMATION_REQUIRED = False
ACCOUNT_LOGIN_REDIRECT_URL = "home"
ACCOUNT_LOGOUT_REDIRECT_URL = "home"
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 2

AUTHENTICATION_BACKENDS = [
    "account.auth_backends.UsernameAuthenticationBackend",
]


text:
package proto.cederj.prownloader.mvp.model;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
 *
 * @author Felipe Santos <live.proto at hotmail.com>
 */
@Entity
public class Video implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String filename;
    private String resolution;
    private String aspectratio;
    private String bitrate;
    private String localFilename;

    @ManyToOne
    private Lesson lesson;

    public Video() {
    }

    public Video(String filename, String resolution, String aspectratio, String bitrate) {
        this.filename = filename;
        this.resolution = resolution;
        this.aspectratio = aspectratio;
        this.bitrate = bitrate;
    }

    public String getFilename() {
        return filename;
    }

    public String getResolution() {
        return resolution;
    }

    public String getAspectratio() {
        return aspectratio;
    }

    public String getBitrate() {
        return bitrate;
    }

    public String getLocalFilename() {
        return localFilename;
    }

    public void setLesson(Lesson lesson) {
        this.lesson = lesson;
    }

    public void setLocalFilename(String localFilename) {
        this.localFilename = localFilename;
    }

    public void update(Video video) {
        this.aspectratio = video.aspectratio;
        this.bitrate = video.bitrate;
        this.filename = video.filename;
        this.resolution = video.resolution;
    }

    public void removeLocalFilename() {
        this.localFilename = null;
    }

    public boolean hasLocalFile() {
        if (localFilename == null) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return filename + " " + resolution + " " + aspectratio + " " + bitrate;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + Objects.hashCode(this.filename);
        hash = 17 * hash + Objects.hashCode(this.resolution);
        hash = 17 * hash + Objects.hashCode(this.aspectratio);
        hash = 17 * hash + Objects.hashCode(this.bitrate);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Video other = (Video) obj;
        if (!Objects.equals(this.filename, other.filename)) {
            return false;
        }
        if (!Objects.equals(this.resolution, other.resolution)) {
            return false;
        }
        if (!Objects.equals(this.aspectratio, other.aspectratio)) {
            return false;
        }
        if (!Objects.equals(this.bitrate, other.bitrate)) {
            return false;
        }
        return true;
    }

}


text:
#include "ns_types.h"
#include "platform/arm_hal_nvm.h"

typedef struct {
    platform_nvm_status status;
    nvm_callback *saved_callback;
    void *saved_context;
} test_platform_nvm_api_data;

test_platform_nvm_api_data test_data = {0, NULL, NULL};

void test_platform_nvm_api_set_retval(platform_nvm_status return_value)
{
    test_data.status = return_value;
}

void test_platform_nvm_api_callback()
{
    test_data.saved_callback(test_data.status, test_data.saved_context);
}

platform_nvm_status platform_nvm_init(nvm_callback *callback, void *context)
{
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}

platform_nvm_status platform_nvm_finalize(nvm_callback *callback, void *context)
{
    (void) callback;
    (void) context;
    return test_data.status;
}

platform_nvm_status platform_nvm_key_create(nvm_callback *callback, const char *key_name, uint16_t value_len, uint32_t flags, void *context)
{
    (void) key_name;
    (void) value_len;
    (void) flags;
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}

platform_nvm_status platform_nvm_key_delete(nvm_callback *callback, const char *key_name, void *context)
{
    (void) callback;
    (void) key_name;
    (void) context;
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}

platform_nvm_status platform_nvm_write(nvm_callback *callback, const char *key_name, const void *data, uint16_t *data_len, void *context)
{
    (void) key_name;
    (void) data;
    (void) data_len;
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}

platform_nvm_status platform_nvm_read(nvm_callback *callback, const char *key_name, void *buf, uint16_t *buf_len, void *context)
{
    (void) key_name;
    (void) buf;
    (void) buf_len;
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}

platform_nvm_status platform_nvm_flush(nvm_callback *callback, void *context)
{
    (void) callback;
    (void) context;
    test_data.saved_callback = callback;
    test_data.saved_context = context;
    return test_data.status;
}


text:
@implementation TSSourceTest {
    dispatch_semaphore_t semaphore;
}

- (id)init
{
    self = [super init];
    if (self) {
        semaphore = dispatch_semaphore_create(0);
    }
    return self;
}

- (void)dealloc
{

}

- (NSString *) identifier
{
    return @"test";
}

- (UIImage *) placeholder
{
    return [UIImage new];
}

- (UIImage *) thumbnailWithSize:(CGSize)size isCancelled:(const BOOL *)isCancelled error:(NSError *__autoreleasing *)error
{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    return [UIImage new];
}

- (void) fire
{
    dispatch_semaphore_signal(semaphore);
}

@end


text:
.log-integration {
  padding: 55px 21px;
}

.log-settings-menu  {
  padding: 10px 10px 20px 10px;
}

.log-settings-menu .item {
  color: rgba(0, 0, 0, 0.35);
  font-size: 1.2em;
  padding-right: 10px;
}

.log-settings-menu .item.active {
  color: #bf9ee9;
  text-decoration: underline;
  font-weight: bold;
}

.service-description {
  padding: 40px 10px;
}

.service-description a {
 color: #333333;
}

.service-description h3 {
  padding-bottom: 20px;
}

.service-description h3::first-letter {
  text-transform: uppercase;
}

.service-description p {
  padding-bottom: 20px;
}


text:
'use strict';

(function(){
    var app = angular.module('play-directives', ['ui.bootstrap']);

    app.directive('questionPlay', function() {
        return {
            restrict: 'E',
            templateUrl: '/modules/play/views/question-play.html'
        };
    });
})();


text:
package org.goldonship.designpattern.creational.prototype;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * * Created by Goldonship on 2017/10/28.
 **/
public class Application {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) throws CloneNotSupportedException {
        Date date = new Date(1241314415234L);
        ShallowCopy shallow = new ShallowCopy("bruce", date);
        ShallowCopy cloneShallow = (ShallowCopy) shallow.clone();
        LOGGER.info(shallow.toString());
        LOGGER.info(cloneShallow.toString());
        date.setTime(2124124124L);
        LOGGER.info(shallow.toString());
        LOGGER.info(cloneShallow.toString());
        LOGGER.info("===============");
        DeepCopy deep = new DeepCopy("smith", date);
        DeepCopy cloneDeep = (DeepCopy) deep.clone();
        LOGGER.info(deep.toString());
        LOGGER.info(cloneDeep.toString());
        date.setTime(2124124167L);
        LOGGER.info(deep.toString());
        LOGGER.info(cloneDeep.toString());

    }

}


text:
/*
 * ClassMethod.java
 * Creation date: (April 4, 2001)
 * By: Bo Ilic
 */
package org.openquark.cal.compiler;

import java.io.IOException;

import org.openquark.cal.internal.serialization.ModuleSerializationTags;
import org.openquark.cal.internal.serialization.RecordInputStream;
import org.openquark.cal.internal.serialization.RecordOutputStream;
import org.openquark.cal.internal.serialization.RecordInputStream.RecordHeaderInfo;

/**
 * ClassMethod models a method in a type class. In other words, it has a name and a type, but not
 * a definition.
 * <p>
 * Some examples of class methods are Prelude.equals, Prelude.lessThan, Prelude.typeOf, and Debug.show.
 *
 * @author Bo Ilic
 */
public final class ClassMethod extends FunctionalAgent{

    private static final int serializationSchema = 0;

    /** the 0-indexed method number of the method within the type class in which it is defined. */
    private int ordinal;

    /**
     * the index for the class method in the dictionary of its type class. This has a special value
     * of -1 if the class method is the single class method of a type class with no ancestors.
     * Otherwise it is equal to the number of ancestors classes (i.e. including grandparents etc) +
     * the ordinal of the method.
     */
    private int dictionaryIndex;

    /**
     * the index of the class type var in the ordered list of overloaded record and type
     * vars in the class method's type. This is used during overload resolution.
     */
    private int classTypeVarPolymorphicIndex;

    /**
     * the name of the default class method i.e. the function to use for the class method at a particular instance
     * if the instance does not provide its own definition. May be null, in which case there is no default for the
     * method.
     *
     * Note that this is the name of a FuncitionEntity (and cannot be a DataConstructor or ClassMethod itself).
     */
    private QualifiedName defaultClassMethodName;

    /**
     * Construct a ClassMethod.
     *
     * @param methodName the name of the class method
     * @param scope the scope of the class method
     * @param namedArguments the explicitly named arguments of the class method (can be null).
     * @param typeExpr the type of the class method
     * @param ordinal the 0-indexed method number of the method within the type class in which it is defined.
     * @param dictionaryIndex the index of the class method in the dictionary for the type class.
     * @param classTypeVarPolymorphicIndex the index of the class type var in the ordered list of overloaded record and type
     *      vars in the class method's type. This is used during overload resolution.
     * @param defaultClassMethodName  the name of the default class method i.e. the function to use for the class
     *       method at a particular instance if the instance does not provide its own definition. May be null,
     *        in which case there is no default for the method.
     */
    ClassMethod(
        QualifiedName methodName,
        Scope scope,
        String[] namedArguments,
        TypeExpr typeExpr,
        int ordinal,
        int dictionaryIndex,
        int classTypeVarPolymorphicIndex,
        QualifiedName defaultClassMethodName) {

        super(methodName, scope, namedArguments, typeExpr, null);

        if (!LanguageInfo.isValidClassMethodName(methodName.getUnqualifiedName())) {
            throw new IllegalArgumentException("ClassMethod constructor: the argument 'methodName' is invalid.");
        }

        if (classTypeVarPolymorphicIndex < 0) {
            throw new IllegalArgumentException("classTypeVarPolymorphicIndex must be non-negative");
        }

        this.ordinal = ordinal;
        this.dictionaryIndex = dictionaryIndex;
        this.classTypeVarPolymorphicIndex = classTypeVarPolymorphicIndex;
        this.defaultClassMethodName = defaultClassMethodName;
    }

    /** Zero argument constructor for serialization. */
    private ClassMethod() {

    }

    /**
     * Returns the kind of entity this is. "Form" is used as a synonym for "kind" or "type"
     * because of the overloaded meanings of the last 2 terms in the type checker!
     * Creation date: (6/4/01 1:49:57 PM)
     * @return FunctionalAgent.Form
     */
    @Override
    public final FunctionalAgent.Form getForm() {
        return FunctionalAgent.Form.CLASS_METHOD;
    }

    /**
     * @return the 0-indexed method number of the method within the type class in which it is defined.
     */
    public final int getOrdinal() {
        return ordinal;
    }

    /**
     * Note the dictionayIndex is for internal compiler use only.
     * @return the index for the class method in the dictionary of its type class. This has a special value
     * of -1 if the class method is the single class method of a type class with no ancestors.
     * Otherwise it is equal to the number of ancestors classes (i.e. including grandparents etc) +
     * the ordinal of the method.
     */
    public final int internal_getDictionaryIndex() {
       return dictionaryIndex;
    }

    /**
     * For internal compiler use only.
     * @return the index of the class type var in the ordered list of overloaded record and type
     *      vars in the class method's type. This is used during overload resolution.
     */
    int getClassTypeVarPolymorphicIndex() {
        return classTypeVarPolymorphicIndex;
    }

    /**
     * @return true if this class method has a default. In particular, what this means is that an instance is not required to
     *   implement an instance method for this class method.
     */
    public final boolean hasDefaultClassMethod() {
        return defaultClassMethodName != null;
    }

    /**
     * the name of the default class method i.e. the function to use for the class method at a particular instance
     * if the instance does not provide its own definition. May be null, in which case there is no default for the
     * method.
     *
     * Note that this is the name of a FuncitionEntity (and cannot be a DataConstructor or ClassMethod itself).
     */
    public final QualifiedName getDefaultClassMethodName() {
        return defaultClassMethodName;
    }

    /**
     * Write an instance of ClassMethod to the RecordOutputStream.
     * @param s
     * @throws IOException
     */
    @Override
    final void write (RecordOutputStream s) throws IOException {
        s.startRecord(ModuleSerializationTags.CLASS_METHOD, serializationSchema);
        super.writeContent(s);
        // Can't compress since dictionaryIndex can be < 0.
        s.writeShort(dictionaryIndex);
        s.writeShortCompressed(ordinal);
        s.writeShortCompressed(classTypeVarPolymorphicIndex);
        s.writeQualifiedName(defaultClassMethodName);
        s.endRecord();
    }

    /**
     * Load an instance of ClassMethod from the RecordInputStream.
     * Read position will be before the record header.
     * @param s
     * @param mti
     * @param msgLogger the logger to which to log deserialization messages.
     * @return an instance of ClassMethod.
     * @throws IOException
     */
    static final ClassMethod load (RecordInputStream s, ModuleTypeInfo mti, CompilerMessageLogger msgLogger) throws IOException {
        // Look for Record header.
        RecordHeaderInfo rhi = s.findRecord(ModuleSerializationTags.CLASS_METHOD);
        if(rhi == null) {
           throw new IOException("Unable to find ClassMethod record header.");
        }
        DeserializationHelper.checkSerializationSchema(rhi.getSchema(), serializationSchema, mti.getModuleName(), "ClassMethod", msgLogger);

        ClassMethod cm = new ClassMethod();
        try {
            cm.read(s, mti, msgLogger);
        } catch (IOException e) {
            // Add context to the error message.
            QualifiedName classMethodName = cm.getName();
            throw new IOException ("Error loading ClassMethod " + (classMethodName == null ? "" : classMethodName.getQualifiedName()) + ": " + e.getLocalizedMessage());
        }
        return cm;
    }

    /**
     * Load the content of an instance of ClassMethod from the RecordInputStream.
     * Read position will be after the record header.
     * @param s
     * @param mti
     * @param msgLogger the logger to which to log deserialization messages.
     * @throws IOException
     */
    private final void read (RecordInputStream s, ModuleTypeInfo mti, CompilerMessageLogger msgLogger) throws IOException {

        super.readContent(s, mti, msgLogger);

        dictionaryIndex = s.readShort();
        ordinal = s.readShortCompressed();
        classTypeVarPolymorphicIndex = s.readShortCompressed();
        defaultClassMethodName = s.readQualifiedName();

        s.skipRestOfRecord();
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        if (defaultClassMethodName != null) {
            return new StringBuilder(super.toString()).append(" default ").append(defaultClassMethodName).toString();
        }
        return super.toString();
    }
}


text:
package com.github.libsml.optimization.linear

import com.github.libsml.math.function.Function
import com.github.libsml.math.linalg.BLAS._
import com.github.libsml.math.linalg.{BLAS, Vector}
import com.github.libsml.math.util.VectorUtils
import collection.mutable

/**
 * Created by huangyu on 15/8/31.
 */
class LinearSearchFunctionOwlqn(val w: Vector, val f: Double, val g: Vector, val sg: Vector, val direct: Vector,
                                val function: Function[Vector], temp: Option[Vector]) extends Function[Double] {

  private[this] var oldStep: Double = 0
  val w0: Vector = {
    val returnW = temp.getOrElse(VectorUtils.newVectorAs(w))
    BLAS.copy(w, returnW)
    returnW
  }

  private[this] def updateIfNessesary(step: Double): Unit = {
    if (step != oldStep) {
      oldStep = step
    }
    copy(w0, w)
    axpy(oldStep, direct, w)
    owlqnProject(w, w0, sg)
  }

  override def isInBound(step: Double): Boolean = {
    updateIfNessesary(step)
    function.isInBound(w)
  }

  override def gradient(step: Double, _g: Double, setZero: Boolean): (Double, Double) = {
    if (step == 0) {
      (dot(g, direct), f)
    } else {
      updateIfNessesary(step)
      val fun = function.gradient(w, g)._2
      (dot(g, direct), fun)
    }
  }

  override def subGradient(step: Double, _f: Double, _g: Double, _sg: Double): (Double, Double) = {
    if (step == 0) {
      (dot(sg, direct), f)
    } else {
      updateIfNessesary(step)
      val fun_ = function.gradient(w, g)._2
      val sf = function.subGradient(w, fun_, g, sg)._2
      (dot(sg, direct), sf)
    }
  }

  override def isDerivable: Boolean = function.isDerivable

  override def isSecondDerivable: Boolean = function.isSecondDerivable

  def owlqnProject(d: Vector, x: Vector, sg: Vector): Unit = {

    val zeroSet = new mutable.HashSet[Int]()
    d.foreachNoZero((k, v) => {
      val sign = if (x(k) == 0.) -sg(k) else x(k)
      if (v * sign <= 0) zeroSet += k
    })
    zeroSet.foreach(k => d(k) = 0)
    zeroSet.clear()
  }

}


text:
<?php
namespace Briz\Base;

use Interop\Container\ContainerInterface;

/**
 * This is our DI Container Class
 *
 * It is designed to store any values that should be avaiable
 * througout the application.
 * you are free to store any values to it.
 */
class Container implements \ArrayAccess, ContainerInterface
{

    /**
     * Container
     * @var array
     */
    private $container = array();

    /**
     *
     * @param array $values initial values
     */
    public function __construct(array $values = array())
    {
        foreach ($values as $key => $value) {
            $this->offsetSet($key, $value);
        }
    }

    /**
     * Finds an entry of the container by its identifier and returns it.
     *
     * @param string $id Identifier of the entry to look for.
     *
     * @return mixed Entry.
     */
    public function get($id)
    {
        if (!$this->has($id)) {
            throw new \InvalidArgumentException(sprintf("the value for '%s' is not available in container", $id));
        }
        return $this->offsetGet($id);
    }

    /**
     * Returns true if the container can return an entry for the given identifier.
     * Returns false otherwise.
     *
     * @param string $id Identifier of the entry to look for.
     *
     * @return boolean
     */
    public function has($id)
    {
        return $this->offsetExists($id);
    }

    /**
     * set a parameter to the container.
     *
     * @param string $offset the key for container
     * @param mixed $value the value stored
     */
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    /**
     * check if a value exists in the container.
     *
     * @param string $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->container[$offset]);
    }

    /**
     * unsets a parameter from container.
     *
     * @param string $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->container[$offset]);
    }

    /**
     * Get a value from container.
     * @param string $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}


text:
package net.qldarch.gson.serialize;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonSerializers {
  JsonSerializer[] value();
}


text:
=====
query
=====

.. code-block:: php

	public function query(): array

Execute the query and return an array of associative rows.

----------

.. rubric:: Return

| Array of rows where each row is an associative array. The key of each column is based on the selected column name or alias, if provided.
| Order of the values in each row will match the order in which the columns were specified.

----------

.. rubric:: Examples

.. code-block:: php
	:linenos:

	$users = $select
		->columnAs('ID', 'UserID')
		->column('Name')
		->from('User')
		->byField('IsLoggedIn', true)
		->query();

	// $users = [ ['UserID' => 1, 'Name' => 'Bob'], ['UserID' => 2, 'Name' => 'Jen'] ]


text:
package org.apache.syncope.core.persistence.beans.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import org.apache.syncope.core.persistence.beans.AbstractAttr;
import org.apache.syncope.core.persistence.beans.AbstractAttrUniqueValue;
import org.apache.syncope.core.persistence.beans.AbstractSchema;

@Entity
public class UAttrUniqueValue extends AbstractAttrUniqueValue {

    private static final long serialVersionUID = -64080804563305387L;

    @Id
    private Long id;

    @OneToOne(optional = false)
    private UAttr attribute;

    @ManyToOne(optional = false)
    @JoinColumn(name = "schema_name")
    private USchema schema;

    @Override
    public Long getId() {
        return id;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends AbstractAttr> T getAttribute() {
        return (T) attribute;
    }

    @Override
    public <T extends AbstractAttr> void setAttribute(final T attribute) {
        if (!(attribute instanceof UAttr)) {
            throw new ClassCastException("expected type UAttr, found: " + attribute.getClass().getName());
        }
        this.attribute = (UAttr) attribute;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends AbstractSchema> T getSchema() {
        return (T) schema;
    }

    @Override
    public <T extends AbstractSchema> void setSchema(final T schema) {
        if (!(schema instanceof USchema)) {
            throw new ClassCastException("expected type USchema, found: " + schema.getClass().getName());
        }
        this.schema = (USchema) schema;
    }
}


text:
using CTPPV5.Infrastructure;
using CTPPV5.Infrastructure.Collections.ProducerConsumer;
using CTPPV5.Rpc.Serial;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using CTPPV5.Client.Daemon.Instruction;
using CTPPV5.Models.CommandModel;

namespace CTPPV5.Client.Daemon.Command
{
    public class SizeAllocCommand : AbstractInstructionCommand
    {
        public SizeAllocCommand(
            ChunkedProducerConsumer<IInstruction> instructionProducer)
            : base(instructionProducer) { }

        public override void OnNotifyInstructionComplete(Rpc.Serial.IInstruction instruction)
        {
            throw new NotImplementedException();
        }

        protected override Rpc.Serial.IInstruction BuildInstruction(Rpc.Net.Message.DuplexMessage commandMessage)
        {
            var instrSet = new InstructionSet(this);
            var sizeAlloc = commandMessage.GetContent<SizeAlloc>();
            using (var scope = ObjectHost.Host.BeginLifetimeScope())
            {
                instrSet.AddInstruction(scope.Resolve<SizeAllocInstruction>(
                    new NamedParameter("command", this),
                    new NamedParameter("parameter", sizeAlloc)));
                foreach (var unit in sizeAlloc.GradeUnits)
                {
                    instrSet.AddInstruction(scope.Resolve<ClassroomProfileSyncInstruction>(
                        new NamedParameter("command", this),
                        new NamedParameter("parameter", unit)));
                }
            }
            return instrSet;
        }
    }
}


text:
package util

import (
	"fmt"
	"net"
)

// TestAddr is an address to use for test servers. Listening on port 0 causes
// the kernel to allocate an unused port.
var TestAddr = NewUnresolvedAddr("tcp", "127.0.0.1:0")

// MakeUnresolvedAddr populates an UnresolvedAddr from a network and raw
// address string.
func MakeUnresolvedAddr(network, addr string) UnresolvedAddr {
	return UnresolvedAddr{
		NetworkField: network,
		AddressField: addr,
	}
}

// NewUnresolvedAddr creates a new UnresolvedAddr from a network and raw
// address string.
func NewUnresolvedAddr(network, addr string) *UnresolvedAddr {
	return &UnresolvedAddr{
		NetworkField: network,
		AddressField: addr,
	}
}

// Note that we make *UnresolvedAddr implement the net.Addr interface, not
// UnresolvedAddr. This is done because assigning a non-empty struct to an
// interface requires an allocation, while assigning a pointer to an interface
// is allocation free. Using an *UnresolvedAddr makes it both clear that an
// allocation is occurring and allows us to avoid an allocation when an
// UnresolvedAddr is a field of a struct (e.g. NodeDescriptor.Address).
var _ net.Addr = &UnresolvedAddr{}

// Network returns the address's network name.
func (a *UnresolvedAddr) Network() string {
	return a.NetworkField
}

// String returns the address's string form.
func (a *UnresolvedAddr) String() string {
	return a.AddressField
}

// Resolve attempts to resolve a into a net.Addr.
func (a UnresolvedAddr) Resolve() (net.Addr, error) {
	switch a.NetworkField {
	case "tcp", "tcp4", "tcp6":
		return net.ResolveTCPAddr(a.NetworkField, a.AddressField)
	case "udp", "udp4", "udp6":
		return net.ResolveUDPAddr(a.NetworkField, a.AddressField)
	case "unix", "unixgram", "unixpacket":
		return net.ResolveUnixAddr(a.NetworkField, a.AddressField)
	}
	return nil, fmt.Errorf("network %s not supported", a.NetworkField)
}


text:
**Ideas**

- logged in vs authenticated (in app, web app, web site context)
- aside vs sidebar (in HTML context)
- regions vs sections (in HTML context)
- site header vs page header (in HTML context)
- site footer vs page footer (in HTML context)
- variation vs version


text:
<!doctype html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="unit1_projects/blog_template.css">
</head>
<body>

</body>
</html>


text:
#ifndef ACTIVATEAPPLICATIONSUBSCRIPTIONRESPONSE_H
#define ACTIVATEAPPLICATIONSUBSCRIPTIONRESPONSE_H

#include <json/json.h>

#include "greentop/JsonResponse.h"
#include "greentop/enum/Status.h"

namespace greentop {

class ActivateApplicationSubscriptionResponse : public JsonResponse {
    public:
        ActivateApplicationSubscriptionResponse();

        ActivateApplicationSubscriptionResponse(const Status& status);

        virtual void fromJson(const Json::Value& json);

        virtual Json::Value toJson() const;

        virtual bool isValid() const;

        const Status& getStatus() const;
        void setStatus(const Status& status);

    private:
        Status status;
};

}

#endif // ACTIVATEAPPLICATIONSUBSCRIPTIONRESPONSE_H


text:
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_save :default_role

  Roles = [ :admin, :pm, :volunteer]

  after_create :send_admin_mail

  has_many :events, through: :attendants
  has_many :tasks
  belongs_to :department

  def default_role
  	self.role ||= 'volunteer'
  end

  def send_admin_mail
    UserMailer.send_welcome_email(self).deliver_later
  end

  def is?( requested_role )
  	self.role == requested_role.to_s
  end
end


text:
package com.github.TKnudsen.ComplexDataObject.model.transformations.descriptors;

import com.github.TKnudsen.ComplexDataObject.data.features.mixedData.MixedDataFeatureVector;
import com.github.TKnudsen.ComplexDataObject.data.interfaces.IDObject;

public interface IMixedDataFeatureVectorDescriptor<I extends IDObject> extends IDescriptor<I, MixedDataFeatureVector> {

}


text:
/*** Delete all of the templates ***/

delete from variable_template where 1=1;
delete from survey_template where 1=1;
delete from battery_template;
delete from template;