text:
package org.wildfly.swarm.ribbon.webapp.runtime;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.wildfly.swarm.netflix.ribbon.RibbonTopology;
import org.wildfly.swarm.netflix.ribbon.RibbonTopologyListener;

/**
 * @author Bob McWhirter
 */
@WebServlet(urlPatterns = {"/system/stream"}, asyncSupported = true)
public class RibbonToTheCurbSSEServlet extends HttpServlet {

    private RibbonTopology topology;

    @Override
    public void init(ServletConfig config) throws ServletException {

        super.init(config);

        try {
            InitialContext context = new InitialContext();
            this.topology = (RibbonTopology) context.lookup("jboss/ribbon/cluster");
        } catch (NamingException e) {
            e.printStackTrace();
            throw new ServletException();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/event-stream");
        resp.setCharacterEncoding("UTF-8");

        AsyncContext asyncContext = req.startAsync();
        PrintWriter writer = resp.getWriter();

        Object writeLock = new Object();

        RibbonTopologyListener topologyListener = new RibbonTopologyListener() {
            @Override
            public void onChange(RibbonTopology topology) {
                String json = topologyToJson();
                synchronized (writeLock) {
                    writer.write("event: topologyChange\n");
                    writer.write("data: " + json);
                    writer.flush();
                }
            }
        };

        Thread keepAlive = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(15_000);
                    synchronized (writeLock) {
                        writer.write(":\n\n");
                        writer.flush();
                    }
                } catch (InterruptedException e) {
                    break;
                }
            }
        });

        asyncContext.setTimeout(0);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(AsyncEvent asyncEvent) throws IOException {
                RibbonToTheCurbSSEServlet.this.topology.removeListener(topologyListener);
                keepAlive.interrupt();
            }

            @Override
            public void onTimeout(AsyncEvent asyncEvent) throws IOException {
                RibbonToTheCurbSSEServlet.this.topology.removeListener(topologyListener);
                keepAlive.interrupt();
            }

            @Override
            public void onError(AsyncEvent asyncEvent) throws IOException {
                RibbonToTheCurbSSEServlet.this.topology.removeListener(topologyListener);
                keepAlive.interrupt();
            }

            @Override
            public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
            }
        });

        this.topology.addListener(topologyListener);
        String json = topologyToJson();
        writer.write("event: topologyChange\n");
        writer.write("data: " + json);
        writer.flush();

        keepAlive.start();

    }

    protected String topologyToJson() {
        StringBuilder json = new StringBuilder();

        json.append("{");

        Map<String, List<String>> map = this.topology.asMap();

        Set<String> keys = map.keySet();
        Iterator<String> keyIter = keys.iterator();

        while (keyIter.hasNext()) {
            String key = keyIter.next();
            json.append("  ").append('"').append(key).append('"').append(": [");
            List<String> list = map.get(key);
            Iterator<String> listIter = list.iterator();
            while (listIter.hasNext()) {
                String server = listIter.next();
                json.append("    ").append('"').append(server).append('"');
                if (listIter.hasNext()) {
                    json.append(", ");
                }
                json.append("");
            }

            json.append("  ]");
            if (keyIter.hasNext()) {
                json.append(",");
            }
            json.append("");
        }

        json.append("}\n\n");

        return json.toString();
    }
}


text:
/**
 *   Classes related to AspectJ Aspect Oriented Programming (AOP).
 */
package com.oreilly.aspects;

redpajama_set_name: RedPajamaGithub


text:
module Phidgets

  # This class represents a PhidgetIR.
  class IR

    Klass = Phidgets::FFI::CPhidgetIR
    include Phidgets::Common

	# This represents the encoding parameters needed to transmit a code.
	#
	# Examples:
	#
	#  puts ir.code_info[:bit_count]
	#  puts ir.code_info[:encoding]
	#  puts ir.code_info[:length]
	#  puts ir.code_info[:gap]
	#  puts ir.code_info[:trail]
	#  puts ir.code_info[:header]
	#  puts ir.code_info[:one]
	#  puts ir.code_info[:zero]
	#  puts ir.code_info[:repeat]
	#  puts ir.code_info[:min_repeat]
	#  puts ir.code_info[:toggle_mask]
	#  puts ir.code_info[:carrier_frequency]
	#  puts ir.code_info[:duty_cycle]
	class IR_code_info < ::FFI::Struct

		# Number of bits in the code
		# @return [Integer] returns the number of bits
		attr_accessor :bit_count

		# Encoding used to encode the data
		# @return [Phidgets::FFI::IREncoding] returns the encoding type
		attr_accessor :encoding

		# Constant or variable length encoding
		# @return [Phidgets::FFI::IRLength] returns the length type
		attr_accessor :length

		# Gap time in us
		# @return [Integer] returns the gap time
		attr_accessor :gap

		# Trail time in us - can be 0 for none
		# @return [Integer] returns the the trail time
		attr_accessor :trail

		# Header pulse and space - can be 0 for none
		# @return [FFI::Struct::InlineArray[2<Integer>]] returns the header
		attr_accessor :header

		# Pulse and space times to represent a '1' bit, in us
		# @return [FFI::Struct::InlineArray[2<Integer>]] returns the pulse and space times
		attr_accessor :one

		# Pulse and space times to represent a '0' bit, in us
		# @return [FFI::Struct::InlineArray[2<Integer>]] returns the pulse and space times
		attr_accessor :zero

		# A series or pulse and space times to represent the repeat code. Start and end with pulses and null terminate - can be 0 for none
		# @return [FFI::Struct::InlineArray[26<Integer>]] returns the repeat code
		attr_accessor :repeat

		# Minimum number of times to repeat a code on transmit
		# @return [Integer] returns the minimum repeat
		attr_accessor :min_repeat

		# Bit toggles, which are applied to the code after each transmit
		# @return [FFI::StructLayout::CharArray[16<Integer>]] returns the bit toggles
		attr_accessor :toggle_mask

		# Carrier frequency in Hz - defaults to 38kHz
		# @return [Integer] returns the carrier frequency
		attr_accessor :carrier_frequency

		# Duty cycle in precent(10-50%) - defaults to 33
		# @return [Integer] returns the duty cycle
		attr_accessor :duty_cycle

		layout :bit_count, :int,
			:encoding, Phidgets::FFI::IREncoding,
			:length, Phidgets::FFI::IRLength,
			:gap, :int,
			:trail, :int,
			:header, [:int, 2],
			:one, [:int, 2],
			:zero, [:int, 2],
			:repeat, [:int, 26],
			:min_repeat, :int,
			:toggle_mask, [:uchar, 16],
		    :carrier_frequency, :int,
		    :duty_cycle, :int

	end

    # Sets a raw data handler callback function. This is called whenever a new IR data is available.  Data is in the form of an array of microsecond pulse values. This can be used if the user wishes to do their own data decoding, or for codes that the PhidgetIR cannot automatically recognize.
    #
    # @param [String] obj Object to pass to the callback function. This is optional.
    # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
    # @example
    #   ir.on_raw_data do |device, raw_data, data_length, obj|
    #	    puts "Raw data: #{raw_data}, length: #{data_length}"
    #   end
    # As this runs in it's own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
    # @return [Boolean] returns true or raises an error
    def on_raw_data(obj=nil, &block)
	  @on_raw_data_obj = obj
      @on_raw_data = Proc.new { |device, obj_ptr, raw_data, data_length|
		data = []
		data_length.times { |i|
			data << raw_data[i].get_int(0)
		}

	    yield self, data, data_length, object_for(obj_ptr)
	}
      Klass.set_OnRawData_Handler(@handle, @on_raw_data, pointer_for(obj))
    end

    # Sets a code handler callback function. This is called whenever a new code is recognized.
    #
    # @param [String] obj Object to pass to the callback function. This is optional.
    # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
    # @example
    #   ir.on_code do |device, data, data_length, bit_count, repeat, obj|
    #	    puts "Code #{data} received, length: #{data_length}, bit count: #{bit_count}, repeat: #{repeat}"
    #   end
    # As this runs in it's own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
    # @return [Boolean] returns true or raises an error
    def on_code(obj=nil, &block)
	  @on_code_obj = obj
      @on_code = Proc.new { |device, obj_ptr, data, data_length, bit_count, repeat|
		data_string = []
		data_length.times { |i|
			data_string[i] = data[i].get_uchar(0).to_s(16)
		}

	    yield self, data_string, data_length, bit_count, repeat, object_for(obj_ptr)
	}
      Klass.set_OnCode_Handler(@handle, @on_code, pointer_for(obj))
    end

    # Sets a learn handler callback function. This is called when a new code has been learned. This generally requires the button to be held down for a second or two.
    #
    # @param [String] obj Object to pass to the callback function. This is optional.
    # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
    # @example
    #   ir.on_learn do |device, data, data_length, code_info, obj|
    #     puts "Code #{data} learnt"
    #   end
    # As this runs in it's own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
    # @return [Boolean] returns true or raises an error
    def on_learn(obj=nil, &block)
      @on_learn_obj = obj
      @on_learn = Proc.new { |device, obj_ptr, int_data, data_length, code_info|
		data = []
		data_length.times { |i|
			data[i] = int_data[i].get_uchar(0).to_s(16)
		}

		code_info_struct = IR_code_info.new(code_info)

	    yield self, data, data_length, code_info_struct, object_for(obj_ptr)
	}
      Klass.set_OnLearn_Handler(@handle, @on_learn, pointer_for(obj))
    end

	# Transmits a code
	# @param [Array<String>] data data to send
	# @param [IR_code_info] code_info code info structure specifying the attributes of the code to send. Anything that is not set is set to default.
	# @return [Boolean] returns true if the code was successfully transmitted, or raises an error.
	def transmit(data, code_info)

		pdata = ::FFI::MemoryPointer.new(:uchar, 16)

		data_ffi = []
		data.size.times { |i|
			data_ffi[i] = data[i].to_i(16)
		}

		data_ffi = ::FFI::MemoryPointer.new(:uchar, 16).write_array_of_uchar(data_ffi)

		Klass.Transmit(@handle, data_ffi, code_info)
		true
   end

	# Transmits a repeat of a previously transmitted code, or raises an error.
	# @return [Boolean] returns true if the code was successfully transmitted, or raises an error.
	def transmit_repeat
		Klass.TransmitRepeat(@handle)
	  true
    end

	# Transmits raw data as a series of pulses and spaces.
	# @param [Array] data data to send
	# @param [Integer] length length of the data array
	# @param [Integer] carrier_frequency carrier frequency in Hz. Leave as 0 for default
	# @param [Integer] duty_cycle duty cycle(10-50). Leave as 0 for default
	# @param [Integer] gap_time gap time in us. This guarantees a gap time(no transmitting) after the data is sent, but can be set to 0
	# @return [Boolean] returns true if the raw data was successfully transmitted, or raises an error.
   def transmit_raw(data, length, carrier_frequency, duty_cycle, gap)
		c_data = ::FFI::MemoryPointer.new(:int, data.size).write_array_of_int(data)
		Klass.TransmitRaw(@handle, c_data, length.to_i, carrier_frequency.to_i, duty_cycle.to_i, gap.to_i)
		true
   end

	# Reads any available raw data. This should be polled continuously(every 20ms) to avoid missing data. Read data always starts with a space and ends with a pulse.
	# @return [Array<Integer>] returns true if the raw data was successfully transmitted, or raises an error.
	# @return [Integer] returns the data length, or raises an error.
	def read_raw_data
		data_ffi = ::FFI::MemoryPointer.new(:int, 16)

		data_length = ::FFI::MemoryPointer.new(:int)
		data_length.write_int(16)

		Klass.getRawData(@handle, data_ffi, data_length)

		data = []

		data_length.get_int(0).times { |i|
			data << data_ffi[i].get_int(0)
		}

		[data, data_length.get_int(0)]
	end

	# Gets the last code that was received.
	# @return [Array<String>] returns the last code, or raises an error.
	# @return [Integer] returns the data length, or raises an error.
	# @return [Object] returns the bit count, or raises an error.
	def last_code
		data_ffi = ::FFI::MemoryPointer.new(:uchar, 16)

		data_length = ::FFI::MemoryPointer.new(:int)
		data_length.write_int(16)

		bit_count = ::FFI::MemoryPointer.new(:int)

		Klass.getLastCode(@handle, data_ffi, data_length, bit_count)

		data = []

		data_length.get_int(0).times { |i|
			 data << data_ffi[i].get_uchar(0).to_s(16)
		}

      [data, data_length.get_int(0), bit_count.get_int(0)]

    end

	# Gets the last code that was learned.
	# @return [Array<String>] returns the last learned code, or raises an error.
	# @return [Integer] returns the data length, or raises an error.
	# @return [IR_code_info] returns the code info structure for the learned code, or raises an error.
	def last_learned_code

		data_ffi = ::FFI::MemoryPointer.new(:uchar, 16)

		data_length = ::FFI::MemoryPointer.new(:int)
		data_length.write_int(16)

		code_info = ::FFI::MemoryPointer.new(IR_code_info)

		Klass.getLastLearnedCode(@handle, data_ffi, data_length, code_info)

		data = []

		data_length.get_int(0).times { |i|
			 data << data_ffi[i].get_uchar(0).to_s(16)
		}

        code_info_struct = IR_code_info.new(code_info)

		[data, data_length.get_int(0), code_info_struct]

	end

	private

	def load_device_attributes

	end

	def remove_specific_event_handlers
		Klass.set_OnCode_Handler(@handle, nil, nil)
		Klass.set_OnLearn_Handler(@handle, nil, nil)
		Klass.set_OnRawData_Handler(@handle, nil, nil)
	end
  end

end


text:
package de.plushnikov.intellij.plugin.action.lombok;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.util.PropertyUtilBase;
import de.plushnikov.intellij.plugin.LombokClassNames;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class LombokGetterHandler extends BaseLombokHandler {

  @Override
  protected void processClass(@NotNull PsiClass psiClass) {
    final Map<PsiField, PsiMethod> fieldMethodMap = new HashMap<>();
    for (PsiField psiField : psiClass.getFields()) {
      PsiMethod propertyGetter =
        PropertyUtilBase.findPropertyGetter(psiClass, psiField.getName(), psiField.hasModifierProperty(PsiModifier.STATIC), false);

      if (null != propertyGetter) {
        fieldMethodMap.put(psiField, propertyGetter);
      }
    }

    processIntern(fieldMethodMap, psiClass, LombokClassNames.GETTER);
  }

}


text:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using QuantConnect.Interfaces;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.Setup;
using QuantConnect.Lean.Engine.TransactionHandlers;
using QuantConnect.Logging;
using QuantConnect.Orders;
using QuantConnect.Packets;

namespace QuantConnect.Lean.Engine.Results
{
    /// <summary>
    /// Console local resulthandler passes messages back to the console/local GUI display.
    /// </summary>
    public class ConsoleResultHandler : IResultHandler
    {
        private bool _isActive;
        private bool _exitTriggered;
        private DateTime _updateTime;
        private DateTime _lastSampledTimed;
        private IAlgorithm _algorithm;
        private readonly object _chartLock;
        private IConsoleStatusHandler _algorithmNode;

        //Sampling Periods:
        private DateTime _nextSample;
        private TimeSpan _resamplePeriod;
        private readonly TimeSpan _notificationPeriod;
        private string _chartDirectory;
        private readonly Dictionary<string, List<string>> _equityResults;

        public Dictionary<string, string> FinalStatistics { get; private set; }

        /// <summary>
        /// Messaging to store notification messages for processing.
        /// </summary>
        public ConcurrentQueue<Packet> Messages
        {
            get;
            set;
        }

        /// <summary>
        /// Local object access to the algorithm for the underlying Debug and Error messaging.
        /// </summary>
        public IAlgorithm Algorithm
        {
            get
            {
                return _algorithm;
            }
            set
            {
                _algorithm = value;
            }
        }

        /// <summary>
        /// Charts collection for storing the master copy of user charting data.
        /// </summary>
        public ConcurrentDictionary<string, Chart> Charts
        {
            get;
            set;
        }

        /// <summary>
        /// Boolean flag indicating the result hander thread is busy.
        /// False means it has completely finished and ready to dispose.
        /// </summary>
        public bool IsActive {
            get
            {
                return _isActive;
            }
        }

        /// <summary>
        /// Sampling period for timespans between resamples of the charting equity.
        /// </summary>
        /// <remarks>Specifically critical for backtesting since with such long timeframes the sampled data can get extreme.</remarks>
        public TimeSpan ResamplePeriod
        {
            get
            {
                return _resamplePeriod;
            }
        }

        /// <summary>
        /// How frequently the backtests push messages to the browser.
        /// </summary>
        /// <remarks>Update frequency of notification packets</remarks>
        public TimeSpan NotificationPeriod
        {
            get
            {
                return _notificationPeriod;
            }
        }

        /// <summary>
        /// Console result handler constructor.
        /// </summary>
        public ConsoleResultHandler()
        {
            Messages = new ConcurrentQueue<Packet>();
            Charts = new ConcurrentDictionary<string, Chart>();
            FinalStatistics = new Dictionary<string, string>();
            _chartLock = new Object();
            _isActive = true;
            _notificationPeriod = TimeSpan.FromSeconds(5);
            _equityResults = new Dictionary<string, List<string>>();
        }

        /// <summary>
        /// Initialize the result handler with this result packet.
        /// </summary>
        /// <param name="packet">Algorithm job packet for this result handler</param>
        /// <param name="messagingHandler"></param>
        /// <param name="api"></param>
        /// <param name="dataFeed"></param>
        /// <param name="setupHandler"></param>
        /// <param name="transactionHandler"></param>
        public void Initialize(AlgorithmNodePacket packet, IMessagingHandler messagingHandler, IApi api, IDataFeed dataFeed, ISetupHandler setupHandler, ITransactionHandler transactionHandler)
        {
            // we expect one of two types here, the backtest node packet or the live node packet
            var job = packet as BacktestNodePacket;
            if (job != null)
            {
                _algorithmNode = new BacktestConsoleStatusHandler(job);
            }
            else
            {
                var live = packet as LiveNodePacket;
                if (live == null)
                {
                    throw new ArgumentException("Unexpected AlgorithmNodeType: " + packet.GetType().Name);
                }
                _algorithmNode = new LiveConsoleStatusHandler(live);
            }
            _resamplePeriod = _algorithmNode.ComputeSampleEquityPeriod();

            var time = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");
            _chartDirectory = Path.Combine("../../../Charts/", packet.AlgorithmId, time);
            if (Directory.Exists(_chartDirectory))
            {
                foreach (var file in Directory.EnumerateFiles(_chartDirectory, "*.csv", SearchOption.AllDirectories))
                {
                    File.Delete(file);
                }
                Directory.Delete(_chartDirectory, true);
            }
            Directory.CreateDirectory(_chartDirectory);
        }

        /// <summary>
        /// Entry point for console result handler thread.
        /// </summary>
        public void Run()
        {
            try
            {
                while ( !_exitTriggered || Messages.Count > 0 )
                {
                    Thread.Sleep(100);

                    var now = DateTime.UtcNow;
                    if (now > _updateTime)
                    {
                        _updateTime = now.AddSeconds(5);
                        _algorithmNode.LogAlgorithmStatus(_lastSampledTimed);
                    }
                }

                // Write Equity and EquityPerformance files in charts directory
                foreach (var fileName in _equityResults.Keys)
                {
                    File.WriteAllLines(fileName, _equityResults[fileName]);
                }
            }
            catch (Exception err)
            {
                // unexpected error, we need to close down shop
                Log.Error(err);
                // quit the algorithm due to error
                _algorithm.RunTimeError = err;
            }

            Log.Trace("ConsoleResultHandler: Ending Thread...");
            _isActive = false;
        }

        /// <summary>
        /// Send a debug message back to the browser console.
        /// </summary>
        /// <param name="message">Message we'd like shown in console.</param>
        public void DebugMessage(string message)
        {
            Log.Trace(_algorithm.Time + ": Debug >> " + message);
        }

        /// <summary>
        /// Send a logging message to the log list for storage.
        /// </summary>
        /// <param name="message">Message we'd in the log.</param>
        public void LogMessage(string message)
        {
            Log.Trace(_algorithm.Time + ": Log >> " + message);
        }

        /// <summary>
        /// Send a runtime error message back to the browser highlighted with in red
        /// </summary>
        /// <param name="message">Error message.</param>
        /// <param name="stacktrace">Stacktrace information string</param>
        public void RuntimeError(string message, string stacktrace = "")
        {
            Log.Error(_algorithm.Time + ": Error >> " + message + (!string.IsNullOrEmpty(stacktrace) ? (" >> ST: " + stacktrace) : ""));
        }

        /// <summary>
        /// Send an error message back to the console highlighted in red with a stacktrace.
        /// </summary>
        /// <param name="message">Error message we'd like shown in console.</param>
        /// <param name="stacktrace">Stacktrace information string</param>
        public void ErrorMessage(string message, string stacktrace = "")
        {
            Log.Error(_algorithm.Time + ": Error >> " + message + (!string.IsNullOrEmpty(stacktrace) ? (" >> ST: " + stacktrace) : ""));
        }

        /// <summary>
        /// Add a sample to the chart specified by the chartName, and seriesName.
        /// </summary>
        /// <param name="chartName">String chart name to place the sample.</param>
        /// <param name="chartType">Type of chart we should create if it doesn't already exist.</param>
        /// <param name="seriesName">Series name for the chart.</param>
        /// <param name="seriesType">Series type for the chart.</param>
        /// <param name="time">Time for the sample</param>
        /// <param name="value">Value for the chart sample.</param>
        /// <param name="unit">Unit for the sample axis</param>
        /// <remarks>Sample can be used to create new charts or sample equity - daily performance.</remarks>
        public void Sample(string chartName, ChartType chartType, string seriesName, SeriesType seriesType, DateTime time, decimal value, string unit = "$")
        {
            var chartFilename = Path.Combine(_chartDirectory, chartName + "-" + seriesName + ".csv");

            lock (_chartLock)
            {
                // Add line to list in dictionary, will be written to file at the end
                List<string> rows;
                if (!_equityResults.TryGetValue(chartFilename, out rows))
                {
                    rows = new List<string>();
                    _equityResults[chartFilename] = rows;
                }
                rows.Add(time + "," + value.ToString("F2", CultureInfo.InvariantCulture));

                //Add a copy locally:
                if (!Charts.ContainsKey(chartName))
                {
                    Charts.AddOrUpdate<string, Chart>(chartName, new Chart(chartName, chartType));
                }

                //Add the sample to our chart:
                if (!Charts[chartName].Series.ContainsKey(seriesName))
                {
                    Charts[chartName].Series.Add(seriesName, new Series(seriesName, seriesType));
                }

                //Add our value:
                Charts[chartName].Series[seriesName].Values.Add(new ChartPoint(time, value));
            }
        }

        /// <summary>
        /// Sample the strategy equity at this moment in time.
        /// </summary>
        /// <param name="time">Current time</param>
        /// <param name="value">Current equity value</param>
        public void SampleEquity(DateTime time, decimal value)
        {
            Sample("Strategy Equity", ChartType.Stacked, "Equity", SeriesType.Candle, time, value);
            _lastSampledTimed = time;
        }

        /// <summary>
        /// Sample today's algorithm daily performance value.
        /// </summary>
        /// <param name="time">Current time.</param>
        /// <param name="value">Value of the daily performance.</param>
        public void SamplePerformance(DateTime time, decimal value)
        {
            Sample("Strategy Equity", ChartType.Overlay, "Daily Performance", SeriesType.Line, time, value, "%");
        }

        /// <summary>
        /// Sample the current benchmark performance directly with a time-value pair.
        /// </summary>
        /// <param name="time">Current backtest date.</param>
        /// <param name="value">Current benchmark value.</param>
        /// <seealso cref="IResultHandler.Sample"/>
        public void SampleBenchmark(DateTime time, decimal value)
        {
            Sample("Benchmark", ChartType.Stacked, "Benchmark", SeriesType.Line, time, value);
        }

        /// <summary>
        /// Analyse the algorithm and determine its security types.
        /// </summary>
        /// <param name="types">List of security types in the algorithm</param>
        public void SecurityType(List<SecurityType> types)
        {
            //NOP
        }

        /// <summary>
        /// Send an algorithm status update to the browser.
        /// </summary>
        /// <param name="algorithmId">Algorithm id for the status update.</param>
        /// <param name="status">Status enum value.</param>
        /// <param name="message">Additional optional status message.</param>
        /// <remarks>In backtesting we do not send the algorithm status updates.</remarks>
        public void SendStatusUpdate(string algorithmId, AlgorithmStatus status, string message = "")
        {
            Log.Trace("ConsoleResultHandler.SendStatusUpdate(): Algorithm Status: " + status + " : " + message);
        }

        /// <summary>
        /// Sample the asset prices to generate plots.
        /// </summary>
        /// <param name="symbol">Symbol we're sampling.</param>
        /// <param name="time">Time of sample</param>
        /// <param name="value">Value of the asset price</param>
        public void SampleAssetPrices(string symbol, DateTime time, decimal value)
        {
            //NOP. Don't sample asset prices in console.
        }

        /// <summary>
        /// Add a range of samples to the store.
        /// </summary>
        /// <param name="updates">Charting updates since the last sample request.</param>
        public void SampleRange(List<Chart> updates)
        {
            lock (_chartLock)
            {
                foreach (var update in updates)
                {
                    //Create the chart if it doesn't exist already:
                    if (!Charts.ContainsKey(update.Name))
                    {
                        Charts.AddOrUpdate(update.Name, new Chart(update.Name, update.ChartType));
                    }

                    //Add these samples to this chart.
                    foreach (var series in update.Series.Values)
                    {
                        //If we don't already have this record, its the first packet
                        if (!Charts[update.Name].Series.ContainsKey(series.Name))
                        {
                            Charts[update.Name].Series.Add(series.Name, new Series(series.Name, series.SeriesType));
                        }

                        //We already have this record, so just the new samples to the end:
                        Charts[update.Name].Series[series.Name].Values.AddRange(series.Values);
                    }
                }
            }
        }

        /// <summary>
        /// Algorithm final analysis results dumped to the console.
        /// </summary>
        /// <param name="job">Lean AlgorithmJob task</param>
        /// <param name="orders">Collection of orders from the algorithm</param>
        /// <param name="profitLoss">Collection of time-profit values for the algorithm</param>
        /// <param name="holdings">Current holdings state for the algorithm</param>
        /// <param name="statistics">Statistics information for the algorithm (empty if not finished)</param>
        /// <param name="banner">Runtime statistics banner information</param>
        public void SendFinalResult(AlgorithmNodePacket job, Dictionary<int, Order> orders, Dictionary<DateTime, decimal> profitLoss, Dictionary<string, Holding> holdings, Dictionary<string, string> statistics, Dictionary<string, string> banner)
        {
            // uncomment these code traces to help write regression tests
            //Console.WriteLine("var statistics = new Dictionary<string, string>();");

            // Bleh. Nicely format statistical analysis on your algorithm results. Save to file etc.
            foreach (var pair in statistics)
            {
                Log.Trace("STATISTICS:: " + pair.Key + " " + pair.Value);
                //Console.WriteLine(string.Format("statistics.Add(\"{0}\",\"{1}\");", pair.Key, pair.Value));
            }

            FinalStatistics = statistics;
        }

        /// <summary>
        /// Set the Algorithm instance for ths result.
        /// </summary>
        /// <param name="algorithm">Algorithm we're working on.</param>
        /// <remarks>While setting the algorithm the backtest result handler.</remarks>
        public void SetAlgorithm(IAlgorithm algorithm)
        {
            _algorithm = algorithm;
        }

        /// <summary>
        /// Terminate the result thread and apply any required exit proceedures.
        /// </summary>
        public void Exit()
        {
            _exitTriggered = true;
        }

        /// <summary>
        /// Send a new order event to the browser.
        /// </summary>
        /// <remarks>In backtesting the order events are not sent because it would generate a high load of messaging.</remarks>
        /// <param name="newEvent">New order event details</param>
        public void OrderEvent(OrderEvent newEvent)
        {
            Log.Debug("ConsoleResultHandler.OrderEvent(): id:" + newEvent.OrderId + " >> Status:" + newEvent.Status + " >> Fill Price: " + newEvent.FillPrice.ToString("C") + " >> Fill Quantity: " + newEvent.FillQuantity);
        }

        /// <summary>
        /// Set the current runtime statistics of the algorithm
        /// </summary>
        /// <param name="key">Runtime headline statistic name</param>
        /// <param name="value">Runtime headline statistic value</param>
        public void RuntimeStatistic(string key, string value)
        {
            Log.Trace("ConsoleResultHandler.RuntimeStatistic(): "  + key + " : " + value);
        }

        /// <summary>
        /// Clear the outstanding message queue to exit the thread.
        /// </summary>
        public void PurgeQueue()
        {
            Messages.Clear();
        }

        /// <summary>
        /// Store result on desktop.
        /// </summary>
        /// <param name="packet">Packet of data to store.</param>
        /// <param name="async">Store the packet asyncronously to speed up the thread.</param>
        /// <remarks>Async creates crashes in Mono 3.10 if the thread disappears before the upload is complete so it is disabled for now.</remarks>
        public void StoreResult(Packet packet, bool async = false)
        {
            // Do nothing.
        }

        /// <summary>
        /// Provides an abstraction layer for live vs backtest packets to provide status/sampling to the AlgorithmManager
        /// </summary>
        /// <remarks>
        /// Since we can run both live and back test from the console, we need two implementations of what to do
        /// at certain times
        /// </remarks>
        private interface IConsoleStatusHandler
        {
            void LogAlgorithmStatus(DateTime current);
            TimeSpan ComputeSampleEquityPeriod();
        }

        // uses a const 2 second sample equity period and does nothing for logging algorithm status
        private class LiveConsoleStatusHandler : IConsoleStatusHandler
        {
            private readonly LiveNodePacket _job;
            public LiveConsoleStatusHandler(LiveNodePacket _job)
            {
                this._job = _job;
            }
            public void LogAlgorithmStatus(DateTime current)
            {
                // later we can log daily %Gain if possible
            }
            public TimeSpan ComputeSampleEquityPeriod()
            {
                return TimeSpan.FromSeconds(2);
            }
        }
        // computes sample equity period from 4000 samples evenly spaced over the backtest interval and logs %complete to log file
        private class BacktestConsoleStatusHandler : IConsoleStatusHandler
        {
            private readonly BacktestNodePacket _job;
            private double? _backtestSpanInDays;
            public BacktestConsoleStatusHandler(BacktestNodePacket _job)
            {
                this._job = _job;
            }
            public void LogAlgorithmStatus(DateTime current)
            {
                if (!_backtestSpanInDays.HasValue)
                {
                    _backtestSpanInDays = Math.Round((_job.PeriodFinish - _job.PeriodStart).TotalDays);
                    if (_backtestSpanInDays == 0.0)
                    {
                        _backtestSpanInDays = null;
                    }
                }

                // we need to wait until we've called initialize on the algorithm
                // this is not ideal at all
                if (_backtestSpanInDays.HasValue)
                {
                    var daysProcessed = (current - _job.PeriodStart).TotalDays;
                    if (daysProcessed < 0) daysProcessed = 0;
                    if (daysProcessed > _backtestSpanInDays.Value) daysProcessed = _backtestSpanInDays.Value;
                    Log.Trace("Progress: " + (daysProcessed * 100 / _backtestSpanInDays.Value).ToString("F2") + "% Processed: " + daysProcessed.ToString("0.000") + " days of total: " + (int)_backtestSpanInDays.Value);
                }
                else
                {
                    Log.Trace("Initializing...");
                }
            }

            public TimeSpan ComputeSampleEquityPeriod()
            {
                const double samples = 4000;
                const double minimumSamplePeriod = 4 * 60;
                double resampleMinutes = minimumSamplePeriod;

                var totalMinutes = (_job.PeriodFinish - _job.PeriodStart).TotalMinutes;

                // before initialize is called this will be zero
                if (totalMinutes > 0.0)
                {
                    resampleMinutes = (totalMinutes < (minimumSamplePeriod * samples)) ? minimumSamplePeriod : (totalMinutes / samples);
                }

                // set max value
                if (resampleMinutes < minimumSamplePeriod)
                {
                    resampleMinutes = minimumSamplePeriod;
                }

                return TimeSpan.FromMinutes(resampleMinutes);
            }
        }

        /// <summary>
        /// Not used
        /// </summary>
        public void SetChartSubscription(string symbol)
        {
            //
        }

        /// <summary>
        /// Process the synchronous result events, sampling and message reading.
        /// This method is triggered from the algorithm manager thread.
        /// </summary>
        /// <remarks>Prime candidate for putting into a base class. Is identical across all result handlers.</remarks>
        public void ProcessSynchronousEvents(bool forceProcess = false)
        {
            var time = _algorithm.Time;

            if (time > _nextSample || forceProcess)
            {
                //Set next sample time: 4000 samples per backtest
                _nextSample = time.Add(ResamplePeriod);

                //Sample the portfolio value over time for chart.
                SampleEquity(time, Math.Round(_algorithm.Portfolio.TotalPortfolioValue, 4));

                //Also add the user samples / plots to the result handler tracking:
                SampleRange(_algorithm.GetChartUpdates());

                //Sample the asset pricing:
                foreach (var security in _algorithm.Securities.Values)
                {
                    SampleAssetPrices(security.Symbol, time, security.Price);
                }
            }

            //Send out the debug messages:
            _algorithm.DebugMessages.ForEach(x => DebugMessage(x));
            _algorithm.DebugMessages.Clear();

            //Send out the error messages:
            _algorithm.ErrorMessages.ForEach(x => ErrorMessage(x));
            _algorithm.ErrorMessages.Clear();

            //Send out the log messages:
            _algorithm.LogMessages.ForEach(x => LogMessage(x));
            _algorithm.LogMessages.Clear();

            //Set the running statistics:
            foreach (var pair in _algorithm.RuntimeStatistics)
            {
                RuntimeStatistic(pair.Key, pair.Value);
            }
        }

    } // End Result Handler Thread:

} // End Namespace


text:
function check_up() {
    service=$1
    host=$2
    port=$3

    max=13 # 1 minute

    counter=1
    while true;do
        python -c "import socket;s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);s.connect(('$host', $port))" \
        >/dev/null 2>/dev/null && break || \
        echo "Waiting that $service on ${host}:${port} is started (sleeping for 5)"

        if [[ ${counter} == ${max} ]];then
            echo "Could not connect to ${service} after some time"
            echo "Investigate locally the logs with fig logs"
            exit 1
        fi

        sleep 5

        (( counter++ ))
    done
}


text:
<?php
namespace Synga\PhpStormMeta\Command;

use Synga\ConsoleAbstraction\ConsoleInteractionInterface;

class IndependentCommandAbstract
{
    /**
     * @var ConsoleInteractionInterface
     */
    protected $output;

    public function setOutput(ConsoleInteractionInterface $consoleInteraction) {
        $this->output = $consoleInteraction;
    }
}


text:
(async function() {
  TestRunner.addResult(`Tests that adding a new rule can be undone.\n`);
  await TestRunner.loadLegacyModule('elements'); await TestRunner.loadTestModule('elements_test_runner');
  await TestRunner.showPanel('elements');
  await TestRunner.loadHTML(`

Text

    `);

  ElementsTestRunner.selectNodeAndWaitForStyles('inspected', step1);

  var treeElement;

  function step1() {
    addNewRuleAndSelectNode('other', step2);
  }

  function step2() {
    addNewRuleAndSelectNode('inspected', step3);
  }

  async function step3() {
    TestRunner.addResult('After adding new rule:');
    await ElementsTestRunner.dumpSelectedElementStyles(true, false, true);
    printStyleSheetAndCall(step4);
  }

  function step4() {
    SDK.domModelUndoStack.undo();
    ElementsTestRunner.selectNodeAndWaitForStyles('other', step5);
  }

  async function step5() {
    TestRunner.addResult('After undo:');
    await ElementsTestRunner.dumpSelectedElementStyles(true, false, true);
    printStyleSheetAndCall(step6);
  }

  function step6() {
    SDK.domModelUndoStack.redo();
    ElementsTestRunner.selectNodeAndWaitForStyles('inspected', step7);
  }

  async function step7() {
    TestRunner.addResult('After redo:');
    await ElementsTestRunner.dumpSelectedElementStyles(true, false, true);
    printStyleSheetAndCall(step8);
  }

  function step8() {
    TestRunner.completeTest();
  }

  function addNewRuleAndSelectNode(nodeId, next) {
    function selectNode() {
      ElementsTestRunner.selectNodeAndWaitForStyles(nodeId, next);
    }

    ElementsTestRunner.addNewRule('div.foo', selectNode);
  }

  async function printStyleSheetAndCall(next) {
    var section = ElementsTestRunner.firstMatchedStyleSection();
    var id = section.style().styleSheetId;
    var styleSheetText = await TestRunner.CSSAgent.getStyleSheetText(id);
    TestRunner.addResult('===== Style sheet text: =====');
    TestRunner.addResult(styleSheetText);
    TestRunner.addResult('=============================');
    next();
  }
})();


text:
layout:     post
title:      Generating networks from rasters with R
date:       2020-05-16 23:15:00
summary:    Making networks out of rasters turned out to be trivial with R.
categories: codemonkey r graph-theory
commentIssueId: 44
---

I've been doing a lot of work with networks and graph theory over the years,
both for my job at DWR and as a central component of my dissertation.
I've been doing some stuff with
[spatial graphs](https://en.wikipedia.org/wiki/Spatial_network)
as part of an analysis of salmonid habitat in the
[Russian River estuary](https://www.sonomawater.org/russian-river-estuary),
and this work involves analyses based on network representations of gridded
data.

In a
[previous post]({% post_url 2020-01-30-generating-networks-from-polylines-with-arcgis %})
I showed you a very long and arduous process to convert polylines to
networks using ArcGIS. I initially turned to ArcGIS for converting
*rasters* to networks as well, focusing on using the
[Create Fishnet](https://pro.arcgis.com/en/pro-app/tool-reference/data-management/create-fishnet.htm)
and
[Polygon Neighbors](https://pro.arcgis.com/en/pro-app/tool-reference/analysis/polygon-neighbors.htm)
tools to convert rasters to shapefiles and generate the network edge list.
This turned out to be a very chalenging approach: generating the fishnet
took massive computing power, and my computer repeatedly failed to complete
the polygon neighbor analysis due to the large number of polygons. I needed
a different solution.

I eventually realized that the
[`raster`](https://cran.r-project.org/package=raster)
package for R had the tools I needed to generate a network directly
from the raster---no shapefiles needed! And it's only a few lines of
code. Generating the node and edge list of the network are just a single
function call each: the node list and their attributes can be generated
using the function `getValues`, while the `adjacent` function generates
a two-column matrix that is equivalent to a (bi)directional edge list.

I'll use the test raster provided by the package to demonstrate:

```r
library(raster)
r = raster(system.file("external/test.grd", package="raster"))
```

First we generate the node list using `getValues()`. We also get the location
info with a call to `coordinates()`:

```r
library(tidyverse)
nodes = tibble(value = getValues(r)) %>%
  bind_cols(as_tibble(coordinates(r)))
```

The edge list is generated with a call to `adjacent()`. We want the
adjacency information for every pixel, so we pass the complete set
of pixel indices for the `cells` argument. In this case only ask for
adjacency in the four cardinal directions, i.e. we don't check for
diagonal adjacency:

```r
edges = as_tibble(adjacent(r, cell = 1:ncell(r), directions = 4, sorted = TRUE))
```

Note that the node and edge lists include `nodata` pixels, which we
almost certainly don't care about. It doesn't noticeably impact the process
for this example, but for larger rasters it's probably smart to filter
out the `nodata` pixels beforehand and only retrieve adjacency info
for valid pixels. Here we simply filter out the `nodata` nodes after
creating the graph via the
[`tidygraph`](https://cran.r-project.org/package=tidygraph)
package:

```r
library(tidygraph)
g = tbl_graph(nodes, edges, TRUE) %N>%
  filter(!is.na(value))
```

And that's it! This is fine for smaller rasters, but you'll start
hitting problems with memory allocation as your rasters get larger.
I'm still working on how to handle that, but the fact that you can
choose to analyze adjacencies for a subset of pixels when using
`adjacent()` means that it's easy to break up the analysis into chunks.
I'll write another post once I work out a good process.


text:
SERVERLESS_PROJECT="qbank-admin"
SERVERLESS_STAGE="dev"
SERVERLESS_REGION="eu-central-1"
LOCAL_DDB_ENDPOINT="http://192.168.99.100:8000"

docker-machine start default
eval "$(docker-machine env default)"
docker-compose up -d
SERVERLESS_STAGE=$SERVERLESS_STAGE SERVERLESS_REGION=$SERVERLESS_REGION SERVERLESS_PROJECT=$SERVERLESS_PROJECT LOCAL_DDB_ENDPOINT=$LOCAL_DDB_ENDPOINT ./node_modules/.bin/mocha specs/
docker-compose down
docker-machine stop default


text:
@implementation UIImage (ZXCompress)
+ (UIImage *)zx_resizableHalfImage:(NSString *)name
{
    UIImage *normal = [UIImage imageNamed:name];

    CGFloat imageW = normal.size.width * 0.5;
    CGFloat imageH = normal.size.height * 0.5;
    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(imageH, imageW, imageH, imageW)];
}

+ (NSData *)zx_compressImage:(UIImage *)image maxLength:(NSInteger)maxLength maxWidth:(NSInteger)maxWidth;
{
    NSAssert(maxLength > 0, @"图片的大小必须大于 0");
    NSAssert(maxWidth > 0, @"图片的最大边长必须大于 0");

    CGSize newSize = [self zx_scaleImage:image length:maxWidth];
    UIImage *newImage = [self zx_resizeImage:image newSize:newSize];

    CGFloat compress = 0.9f;
    NSData *data = UIImageJPEGRepresentation(newImage, compress);

    while (data.length > maxLength && compress > 0.01) {
        compress -= 0.02f;

        data = UIImageJPEGRepresentation(newImage, compress);
    }
    return data;
}
- (UIImage *)zx_imageWithNewSize:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    //第一个参数,size,是缩放图片的尺寸,第二个参数,isOpaque 是用来决定透明通道是否被渲染。对没有透明度的图片设置这个参数为false,可能导致图片有粉红色调。第三个参数scale是显示缩放系数。当设置成0.0,主屏幕的缩放系数将被使用,对视网膜屏显示是2.0或者更高

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈

    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片

    return newImage;
}
+ (UIImage *)zx_resizeImage:(UIImage *)image newSize:(CGSize)newSize;
{

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

+ (CGSize)zx_scaleImage:(UIImage *)image length:(CGFloat)imageLength{

    CGFloat newWidth = 0.0f;
    CGFloat newHeight = 0.0f;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;

    if (width > imageLength || height > imageLength){

        if (width > height) {

            newWidth = imageLength;
            newHeight = newWidth * height / width;

        }else if(height > width){

            newHeight = imageLength;
            newWidth = newHeight * width / height;

        }else{

            newWidth = imageLength;
            newHeight = imageLength;
        }

    }else{
        return CGSizeMake(width, height);
    }

    return CGSizeMake(newWidth, newHeight);
}

@end


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">iowatch</string>
    <string name="config_name">iowatch Config</string>

    <!-- Default values -->
    <integer name="LinesR">152</integer>
    <integer name="LinesG">164</integer>
    <integer name="LinesB">163</integer>

    <integer name="SecondHandR">170</integer>
    <integer name="SecondHandG">91</integer>
    <integer name="SecondHandB">52</integer>

    <integer name="InnerBGR">30</integer>
    <integer name="InnerBGG">35</integer>
    <integer name="InnerBGB">39</integer>

    <integer name="OuterBGR">34</integer>
    <integer name="OuterBGG">41</integer>
    <integer name="OuterBGB">47</integer>

    <integer name="SquareBGR">40</integer>
    <integer name="SquareBGG">49</integer>
    <integer name="SquareBGB">56</integer>

    <integer name="EnableTicks">1</integer>
</resources>


text:
<?php defined('SYSPATH') OR die('No direct script access.');

class Upload_Source extends Kohana_Upload_Source {}


text:
/*************************************************************************/
/* spdlog - an extremely fast and easy to use c++11 logging library.     */
/* Copyright (c) 2014 Gabi Melman.                                       */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

// async log helper :
// Process logs asynchronously using a back thread.
//
// If the internal queue of log messages reaches its max size,
// then the client call will block until there is more room.
//
// If the back thread throws during logging, a spdlog::spdlog_ex exception
// will be thrown in client's thread when tries to log the next message

#pragma once

#include <chrono>
#include <thread>
#include <atomic>
#include <functional>

#include "../common.h"
#include "../sinks/sink.h"
#include "./mpmc_bounded_q.h"
#include "./log_msg.h"
#include "./format.h"

namespace spdlog
{
namespace details
{

class async_log_helper
{
    // Async msg to move to/from the queue
    // Movable only. should never be copied
    struct async_msg
    {
        std::string logger_name;
        level::level_enum level;
        log_clock::time_point time;
        std::string txt;

        async_msg() = default;
        ~async_msg() = default;

        async_msg(async_msg&& other) = default;
        async_msg& operator=(async_msg&& other) = default;

        // never copy or assign. should only be moved..
        async_msg(const async_msg&) = delete;
        async_msg& operator=(async_msg& other) = delete;

        // construct from log_msg
        async_msg(const details::log_msg& m) :
            logger_name(m.logger_name),
            level(m.level),
            time(m.time),
            txt(m.raw.data(), m.raw.size())
        {}

        // copy into log_msg
        void fill_log_msg(log_msg &msg)
        {
            msg.clear();
            msg.logger_name = logger_name;
            msg.level = level;
            msg.time = time;
            msg.raw << txt;
        }
    };

public:

    using item_type = async_msg;
    using q_type = details::mpmc_bounded_queue<item_type>;

    using clock = std::chrono::steady_clock;

    async_log_helper(formatter_ptr formatter,
                     const std::vector<sink_ptr>& sinks,
                     size_t queue_size,
                     const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
                     const std::function<void()>& worker_warmup_cb = nullptr);

    void log(const details::log_msg& msg);

    // stop logging and join the back thread
    ~async_log_helper();

    void set_formatter(formatter_ptr);

private:
    formatter_ptr _formatter;
    std::vector<std::shared_ptr<sinks::sink>> _sinks;

    // queue of messages to log
    q_type _q;

    // last exception thrown from the worker thread
    std::shared_ptr<spdlog_ex> _last_workerthread_ex;

    // overflow policy
    const async_overflow_policy _overflow_policy;

    // worker thread warmup callback - one can set thread priority, affinity, etc
    const std::function<void()> _worker_warmup_cb;

    // worker thread
    std::thread _worker_thread;

    // throw last worker thread exception or if worker thread is not active
    void throw_if_bad_worker();

    // worker thread main loop
    void worker_loop();

    // pop next message from the queue and process it
    // return true if a message was available (queue was not empty), will set the last_pop to the pop time
    bool process_next_msg(clock::time_point& last_pop);

    // sleep,yield or return immediatly using the time passed since last message as a hint
    static void sleep_or_yield(const clock::time_point& last_op_time);

};
}
}

///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb):
    _formatter(formatter),
    _sinks(sinks),
    _q(queue_size),
    _overflow_policy(overflow_policy),
    _worker_warmup_cb(worker_warmup_cb),
    _worker_thread(&async_log_helper::worker_loop, this)
{}

// Send to the worker thread termination message(level=off)
// and wait for it to finish gracefully
inline spdlog::details::async_log_helper::~async_log_helper()
{

    try
    {
        log(log_msg(level::off));
        _worker_thread.join();
    }
    catch (...) //Dont crash if thread not joinable
    {}
}

//Try to push and block until succeeded
inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
{
    throw_if_bad_worker();
    async_msg new_msg(msg);
    if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_overflow_policy::discard_log_msg)
    {
        auto last_op_time = clock::now();
        do
        {
            sleep_or_yield(last_op_time);
        }
        while (!_q.enqueue(std::move(new_msg)));
    }

}

inline void spdlog::details::async_log_helper::worker_loop()
{
    try
    {
        if (_worker_warmup_cb) _worker_warmup_cb();
        clock::time_point last_pop = clock::now();
        while(process_next_msg(last_pop));
    }
    catch (const std::exception& ex)
    {
        _last_workerthread_ex = std::make_shared<spdlog_ex>(std::string("async_logger worker thread exception: ") + ex.what());
    }
    catch (...)
    {
        _last_workerthread_ex = std::make_shared<spdlog_ex>("async_logger worker thread exception");
    }
}

// process next message in the queue
// return true if this thread should still be active (no msg with level::off was received)
inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_point& last_pop)
{

    async_msg incoming_async_msg;
    log_msg incoming_log_msg;

    if (_q.dequeue(incoming_async_msg))
    {
        last_pop = clock::now();

        if(incoming_async_msg.level == level::off)
            return false;

        incoming_async_msg.fill_log_msg(incoming_log_msg);
        _formatter->format(incoming_log_msg);
        for (auto &s : _sinks)
            s->log(incoming_log_msg);
    }
    else //empty queue
    {
        sleep_or_yield(last_pop);
    }
    return true;
}

inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
{
    _formatter = msg_formatter;
}

// sleep,yield or return immediatly using the time passed since last message as a hint
inline void spdlog::details::async_log_helper::sleep_or_yield(const clock::time_point& last_op_time)
{
    using std::chrono::milliseconds;
    using namespace std::this_thread;

    auto time_since_op = clock::now() - last_op_time;

    // spin upto 1 ms
    if (time_since_op <= milliseconds(1))
        return;

    // yield upto 10ms
    if (time_since_op <= milliseconds(10))
        return yield();

    // sleep for half of duration since last op
    if (time_since_op <= milliseconds(100))
        return sleep_for(time_since_op / 2);

    return sleep_for(milliseconds(100));
}

// throw if the worker thread threw an exception or not active
inline void spdlog::details::async_log_helper::throw_if_bad_worker()
{
    if (_last_workerthread_ex)
    {
        auto ex = std::move(_last_workerthread_ex);
        throw *ex;
    }
}


text:
package ch.shaktipat.saraswati.internal.web.transform;

import ch.shaktipat.saraswati.internal.web.annotations.TransformerMarker;
import ch.shaktipat.saraswati.pobject.oid.PObjectOID;

@TransformerMarker
public class PObjectOIDTransformer implements Transformer< PObjectOID >
{
	@Override
	public String getString( PObjectOID pObjectOID )
	{
		return pObjectOID.toString();
	}

	@Override
	public PObjectOID getValue( String pObjectOIDjAsString )
	{
		throw new UnsupportedOperationException();
	}
}


text:
package com.xfggh.blog.mapper;

import com.xfggh.blog.entity.Test;

import java.util.List;

public interface TestMapper {
    List<Test> list();
}


text:
<?php
use Kahlan\Filter\Filter;
use Kahlan\Reporter\Coverage\Exporter\Coveralls;

/** @var \Kahlan\Cli\Kahlan $this */
/** @var \Kahlan\Cli\CommandLine $args */
$args = $this->commandLine();
$args->option('coverage', 'default', 3);

Filter::register('phalcon.namespace', function ($chain) {
    $this->_autoloader->addPsr4('Spec\\Models\\', __DIR__ . '/spec/models/');
});

Filter::register('phalcon.coverage-exporter', function ($chain) {
    $reporter = $this->reporters()->get('coverage');
    if (!$reporter) return;

    Coveralls::write([
        'collector' => $reporter,
        'file' => 'coveralls.json',
        'service_name' => 'travis-ci',
        'service_job_id' => getenv('TRAVIS_JOB_ID') ?: null
    ]);
    return $chain->next();
});

Filter::apply($this, 'namespaces', 'phalcon.namespace');
Filter::apply($this, 'reporting', 'phalcon.coverage-exporter');

$di = new \Phalcon\Di\FactoryDefault();
$di->setShared('db', function () {
    return new \Phalcon\Db\Adapter\Pdo\Sqlite([
        'dbname' => __DIR__ . '/spec/db.sqlite',
    ]);
});
\Phalcon\Di::setDefault($di);


text:
namespace MobileVoting.Core.Models
{
    public class VoteOptionWeight
    {
        public int OptionId { get; set; }
        public int Weight { get; set; }

        public virtual VoteOption VoteOption { get; set; }
    }
}


text:
    fprintf(__fp, "%s\n", [[NSString stringWithFormat:__VA_ARGS__] UTF8String])

@interface PGCLI ()

@property (nonatomic) PGGenerator * generator;
@property (nonatomic) NSString * grammarFile;

@end

@implementation PGCLI

-(instancetype)init {
    self = [super init];
    if (self) {
        _generator = [[PGGenerator alloc] init];
    }
    return self;
}

-(BOOL)willHandleCommandLine {
    [self parseArgs];

    return (self.grammarFile != nil);
}

-(int)handleCommandLine {
    if (self.generator.destinationPath.length == 0 || self.grammarFile.length == 0) {
        nsfprintf(stderr,
                  @"\n"
                  @"Usage:\n"
                  @"\n"
                  @"    ParserGenApp -grammar <file path> -destPath <directory path> <options>\n"
                  @"\n"
                  @"Options are any combination of:\n"
                  @"    -parserName <name>\n"
                  @"    -enableARC 1\n"
                  @"    -enableAutomaticErrorRecovery 1\n"
                  @"    -enableHybridDFA 1\n"
                  @"    -enableMemoization 1\n");
        return 1;
    }

    BOOL ok = [self loadGrammar];
    if (!ok) {
        return 1;
    }

    ok = [self generate];
    if (!ok) {
        return 1;
    }

    return 0;
}

-(void)parseArgs {
    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];

    self.grammarFile = [ud stringForKey:@"grammar"];

    self.generator.destinationPath = [ud stringForKey:@"destPath"];
    if (self.generator.destinationPath == nil) {
        self.generator.destinationPath = [ud stringForKey:@"destinationPath"];
    }
    self.generator.enableARC = [ud boolForKey:@"enableARC"];
    self.generator.enableAutomaticErrorRecovery = [ud boolForKey:@"enableAutomaticErrorRecovery"];
    self.generator.enableHybridDFA = [ud boolForKey:@"enableHybridDFA"];
    self.generator.enableMemoization = [ud boolForKey:@"enableMemoization"];
    self.generator.parserName = [ud stringForKey:@"parserName"];
}

-(BOOL)loadGrammar {
    NSString * path = [self.grammarFile stringByExpandingTildeInPath];
    NSError * err = nil;
    NSString * grammar = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
    if (grammar == nil) {
        nsfprintf(stderr, NSLocalizedString(@"Failed to load grammar: %@", nil), [err localizedDescription]);
        return NO;
    }
    else {
        self.generator.grammar = grammar;
        return YES;
    }
}

-(BOOL)generate {
    bool ok = [self.generator generate];
    if (!ok) {
        nsfprintf(stderr, NSLocalizedString(@"Failed to generate: %@", nil), [self.generator.error localizedDescription]);
    }
    return ok;
}

@end


text:
/*
 * mpls in net namespaces
 */

#ifndef __NETNS_MPLS_H__
#define __NETNS_MPLS_H__

struct mpls_route;
struct ctl_table_header;

struct netns_mpls {
	size_t platform_labels;
	struct mpls_route __rcu * __rcu *platform_label;
	struct ctl_table_header *ctl;
};

#endif /* __NETNS_MPLS_H__ */


text:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AuthServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		ServletConfig config = getServletConfig();
		String authUsername = config.getInitParameter("username");
		String authPassword = config.getInitParameter("password");
		PrintWriter pw = response.getWriter();
		if (username.equals(authUsername) && password.equals(authPassword)) {
			request.setAttribute("username", username);
			getServletContext().getRequestDispatcher("/WelcomeServlet").forward(request, response);
		} else {
			pw.println("
Sorry username or password error!
");
			getServletContext().getRequestDispatcher("/task1.html").include(request, response);
			pw.close();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}