text:
package akka.dispatch.verification

import com.typesafe.config.ConfigFactory
import akka.actor.{Actor, Cell, ActorRef, ActorSystem, Props}

import akka.dispatch.Envelope

import scala.collection.mutable.Queue
import scala.collection.mutable.HashMap
import scala.collection.mutable.SynchronizedQueue
import scala.collection.immutable.Set
import scala.collection.mutable.HashSet
import scala.collection.mutable.SynchronizedSet
import scala.collection.mutable.Iterable
import scala.collection.generic.Clearable

import java.util.concurrent.Semaphore
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger

import org.slf4j.LoggerFactory,
       ch.qos.logback.classic.Level,
       ch.qos.logback.classic.Logger

abstract class MessageType()
final case object ExternalMessage extends MessageType
final case object InternalMessage extends MessageType
final case object FailureDetectorQuery extends MessageType
final case object CheckpointReplyMessage extends MessageType

object ExternalEventInjector {
  val log = LoggerFactory.getLogger("ExternalEventInjector")
}

/**
  * A mix-in for schedulers that take external events as input, and generate
  * executions containing both external and internal events as output.
  */
trait ExternalEventInjector[E] {
  // Must be defined in inheritors:
  val schedulerConfig: SchedulerConfig

  var event_orchestrator = new EventOrchestrator[E]()

  // Handler for FailureDetector messages
  var fd : FDMessageOrchestrator = null
  if (schedulerConfig.enableFailureDetector) {
    fd = new FDMessageOrchestrator(enqueue_message)
    event_orchestrator.set_failure_detector(fd)
  } else {
    event_orchestrator.set_failure_detector(null)
  }

  // Handler for Checkpoint responses
  var checkpointer : CheckpointCollector = null
  if (schedulerConfig.enableCheckpointing) {
    checkpointer = new CheckpointCollector
  }

  // A list of all *possible* actors, not just all live actors.
  var actorNamePropPairs: Seq[Tuple2[Props,String]] = null
  // If invoked, will always populate these actors, not just those with Start
  // events.
  def setActorNamePropPairs(_actorNamePropPairs: Seq[Tuple2[Props,String]]) {
    actorNamePropPairs = _actorNamePropPairs
  }

  // Semaphore to wait for trace replay to be done. We initialize the
  // semaphore to 0 rather than 1, so that the main thread blocks upon
  // invoking acquire() until another thread release()'s it.
  var traceSem = new Semaphore(0)

  // Are we currently injecting external events or is someone using
  // this scheduler in some strange way.
  val currentlyInjecting = new AtomicBoolean(false)

  // Semaphore to use when shutting down the scheduler. We initialize the
  // semaphore to 0 rather than 1, so that the main thread blocks upon
  // invoking acquire() until another thread release()'s it.
  var shutdownSem = new Semaphore(0)

  // Semaphore to use when taking a checkpoint. We initialize the
  // semaphore to 0 rather than 1, so that the main thread blocks upon
  // invoking acquire() until another thread release()'s it.
  var checkpointSem = new Semaphore(0)

  // Whether a thread is blocked waiting for a checkpoint.
  var blockedOnCheckpoint = new AtomicBoolean(false)

  // Are we expecting message receives
  val started = new AtomicBoolean(false)

  // Ensure that only one thread is running inside the scheduler when we are
  // dispatching external messages to actors. (Does not guard the scheduler's instance
  // variables.)
  var schedSemaphore = new Semaphore(1)

  // If we enqueued an external message, keep track of it, so that we can
  // later identify it as an external message when it is plumbed through
  // event_produced
  // Assumes that external message objects never == internal message objects.
  // That assumption would be broken if, for example, nodes relayed external
  // messages to eachother...
  // TODO(cs): include receiver here for safety?
  var enqueuedExternalMessages = new MultiSet[Any]

  // A set of external messages to send. Messages sent between actors are not
  // queued here. Tuple is: (sender, receiver, msg)
  var messagesToSend = new SynchronizedQueue[(Option[ActorRef], ActorRef, Any)]()

  // Whether populateActors has been invoked.
  var alreadyPopulated = false

  // In case we're in non-blocking mode.
  var terminationCallback : Option[(EventTrace) => Any] = None

  // If the last event we replayed is WaitCondition, and we've reached
  // quiescence, signal that once we've waited for enqueue_message() to be
  // invoked we should start dispatch again.
  var dispatchAfterEnqueueMessage = new AtomicBoolean(false)

  // An optional callback that will be invoked when a WaitQuiescence has just
  // caused us to arrive at Quiescence.
  type QuiescenceCallback = () => Unit
  var quiescenceCallback : QuiescenceCallback = () => None
  def setQuiescenceCallback(c: QuiescenceCallback) { quiescenceCallback = c }

  // Whether we are currently processing an "UnignorableEvents" block
  var unignorableEvents = new AtomicBoolean(false)

  // All external thread ids that recently began an "AtomicBlock"
  var beganExternalAtomicBlocks = new MultiSet[Long] with SynchronizedSet[Long]

  // All external thread ids that recently ended an "AtomicBlock"
  var endedExternalAtomicBlocks = new MultiSet[Long] with SynchronizedSet[Long]

  // how many external atomic blocks are currently running
  var pendingExternalAtomicBlocks = new AtomicInteger(0)

  /**
   * Until endUnignorableEvents is invoked, mark all events that we record
   * as "unignorable", i.e., during replay, don't ever skip over them.
   *
   * Called by external threads.
   */
  def beginUnignorableEvents() {
    assert(!unignorableEvents.get())
    unignorableEvents.set(true)
    // TODO(cs): should these be placed into messagesToSend too?
    event_orchestrator.events += BeginUnignorableEvents
  }

  /**
   * Pre: beginUnignorableEvents was previously invoked.
   *
   * Called by external threads.
   */
  def endUnignorableEvents() {
    assert(unignorableEvents.get())
    unignorableEvents.set(false)

    // Make sure the marker appears after any currently enqueued external
    // message sends.
    messagesToSend.synchronized {
      messagesToSend += ((None, null, EndUnignorableEvents))
      messagesToSend.notifyAll()
    }
  }

  /**
   * Within receive(), an internal thread has just created an external thread.
   * (before that external thread has necessarily been scheduled).
   *
   * This method indicates the start of the external thread's `atomic block`, where
   * it will now send some number of messages. Upon replay, wait until the end of the
   * atomic block before deciding whether those messages are or are not going
   * to show up.
   */
  def beginExternalAtomicBlock(taskId: Long) {
    ExternalEventInjector.log.debug(s"beginExternalAtomicBlock($taskId)")
    if (Instrumenter()._passThrough.get()) {
      return
    }

    // Place the marker into the current place in messagesToSend; any messages
    // already enqueued before this are not part of the
    // beginExternalAtomicBlock
    beganExternalAtomicBlocks.synchronized {
      // TODO(cs): uncommenting this can lead to deadlocks.
      //endedExternalAtomicBlocks.synchronized {
      //assert(!(beganExternalAtomicBlocks contains taskId) ||
      //        (endedExternalAtomicBlocks contains taskId))
      //}
      beganExternalAtomicBlocks += taskId
      beganExternalAtomicBlocks.notifyAll
    }
    messagesToSend.synchronized {
      messagesToSend += ((None, null, BeginExternalAtomicBlock(taskId)))
    }
    val pendingBlocks = pendingExternalAtomicBlocks.incrementAndGet()
    // We shouldn't be dispatching while the atomic block executes.
    if (pendingBlocks == 1) {
      Instrumenter().stopDispatch.set(true)
    }
  }

  /**
   * Pre: beginExternalAtomicBlock was previously invoked.
   *
   * Called by external threads.
   */
  def endExternalAtomicBlock(taskId: Long) {
    ExternalEventInjector.log.trace(s"endExternalAtomicBlock($taskId)")
    if (Instrumenter()._passThrough.get()) {
      return
    }

    // Place the marker into the current place in messagesToSend; any messages
    // already enqueued before this are part of the atomic block.
    messagesToSend.synchronized {
      messagesToSend += ((None, null, EndExternalAtomicBlock(taskId)))
    }
    // Signal that the main thread should invoke send_external_messages
    endedExternalAtomicBlocks.synchronized {
      // TODO(cs): uncommenting this can lead to deadlocks.
      // beganExternalAtomicBlocks.synchronized {
      //   assert(beganExternalAtomicBlocks contains taskId)
      // }
      endedExternalAtomicBlocks.notifyAll()
    }
    val pendingBlocks = pendingExternalAtomicBlocks.decrementAndGet()
    if (pendingBlocks == 0) {
      var restartDispatch = false
      Instrumenter().stopDispatch.synchronized {
        if (Instrumenter().stopDispatch.get()) {
          // Still haven't stopped dispatch, so don't restart
          Instrumenter().stopDispatch.set(false)
        } else {
          restartDispatch = true
        }
      }
      if (restartDispatch) {
        ExternalEventInjector.log.debug("Restarting dispatch!")
        Instrumenter().start_dispatch
      }
    }
  }

  // Enqueue an external message for future delivery
  def enqueue_message(sender: Option[ActorRef], receiver: String, msg: Any) {
    if (event_orchestrator.actorToActorRef contains receiver) {
      enqueue_message(sender, event_orchestrator.actorToActorRef(receiver), msg)
    } else {
      ExternalEventInjector.log.warn("Unknown message receiver " + receiver)
    }
  }

  def enqueue_message(sender: Option[ActorRef], actor: ActorRef, msg: Any) {
    enqueuedExternalMessages.synchronized {
      enqueuedExternalMessages += msg
    }

    // signal to the main thread that it should wake up if it's blocked on
    // external messages
    messagesToSend.synchronized {
      messagesToSend += ((sender, actor, msg))
      messagesToSend.notifyAll()
    }

    dispatchAfterEnqueueMessage.synchronized {
      if (dispatchAfterEnqueueMessage.get()) {
        dispatchAfterEnqueueMessage.set(false)
        ExternalEventInjector.log.debug("dispatching after enqueue_message")
        started.set(true)
        send_external_messages()
        Instrumenter().start_dispatch()
      }
    }
  }

  // Enqueue a timer message for future delivery
  def handle_timer(receiver: String, msg: Any): Unit = {
    if (schedulerConfig.ignoreTimers) {
      return
    }

    if (event_orchestrator.actorToActorRef contains receiver) {
      // signal to the main thread that it should wake up if it's blocked on
      // external messages
      messagesToSend.synchronized {
        messagesToSend += ((None, event_orchestrator.actorToActorRef(receiver), msg))
        messagesToSend.notifyAll()
      }
    } else {
      ExternalEventInjector.log.warn("Unknown timer receiver " + receiver)
    }
  }

  def send_external_messages() {
    send_external_messages(true)
  }

  // Initiates message sends for all messages in messagesToSend. Note that
  // delivery does not occur immediately! These messages will subsequently show
  // up in event_produced as messages to be scheduled by schedule_new_message.
  def send_external_messages(acquireSemaphore: Boolean) {
    // Ensure that only one thread is accessing shared scheduler structures
    if (acquireSemaphore) {
      schedSemaphore.acquire
    }

    try {
      assert(started.get, "!started.get:" + Thread.currentThread.getName)

      // Send all pending fd responses
      if (fd != null) {
        fd.send_all_pending_responses()
      }
      // Drain message queue
      Instrumenter().sendKnownExternalMessages(() => {
        messagesToSend.synchronized {
          for ((senderOpt, receiver, msg) <- messagesToSend) {
            // Check if the message is actually a special marker
            msg match {
              case EndUnignorableEvents =>
                event_orchestrator.events += EndUnignorableEvents
              case BeginExternalAtomicBlock(taskId) =>
                event_orchestrator.events += BeginExternalAtomicBlock(taskId)
              case EndExternalAtomicBlock(taskId) =>
                endedExternalAtomicBlocks.synchronized {
                  endedExternalAtomicBlocks += taskId
                  endedExternalAtomicBlocks.notifyAll()
                }
                event_orchestrator.events += EndExternalAtomicBlock(taskId)
              case s @ ScheduleBlock(f, cell) =>
                Instrumenter().scheduler.event_produced(cell, s.envelope)
              case _ =>
                // It's a normal message
                if (!Instrumenter().receiverIsAlive(receiver)) {
                  ExternalEventInjector.log.warn(
                    "Dropping message to non-existent receiver: " +
                          receiver + " " + msg)
                } else {
                  senderOpt match {
                    case Some(sendRef) =>
                      receiver.!(msg)(sendRef)
                    case None =>
                      receiver ! msg
                  }
                }
            }
          }
          messagesToSend.clear()
        }
      })

      // Wait to make sure all messages are enqueued
      Instrumenter().await_enqueue()
    } finally {
      // schedule_new_message is reenterant, hence release before calling.
      if (acquireSemaphore) {
        schedSemaphore.release
      }
    }
  }

  // When deserializing an event trace, we need the actors to be prepopulated
  // so we can resolve serialized ActorRefs. Here we populate the actor system
  // give the names and props of all actors that will eventually appear in the
  // execution.
  def populateActorSystem(_actorNamePropPairs: Seq[Tuple2[Props,String]]) = {
    alreadyPopulated = true
    for ((props, name) <- _actorNamePropPairs) {
      // Just start and isolate all actors we might eventually care about
      Instrumenter().actorSystem.actorOf(props, name)
      event_orchestrator.isolate_node(name)
    }
  }

  // Given an external event trace, see the events produced
  // if terminationCallback != None, don't block the main thread!
  def execute_trace (_trace: Seq[E with ExternalEvent],
                     _terminationCallback: Option[(EventTrace) => Any]=None)
                   : EventTrace = {
    terminationCallback = _terminationCallback
    MessageTypes.sanityCheckTrace(_trace)
    event_orchestrator.set_trace(_trace)
    event_orchestrator.reset_events

    if (schedulerConfig.enableFailureDetector) {
      fd.startFD(Instrumenter().actorSystem)
    }
    if (schedulerConfig.enableCheckpointing) {
      checkpointer.startCheckpointCollector(Instrumenter().actorSystem)
    }

    if (!alreadyPopulated) {
      if (actorNamePropPairs != null) {
        populateActorSystem(actorNamePropPairs)
      } else {
        populateActorSystem(_trace flatMap {
          case Start(propCtor,name) => Some((propCtor(), name))
          case _ => None
        })
      }
    }

    currentlyInjecting.set(true)
    // Start playing back trace
    advanceTrace()
    terminationCallback match {
      case None =>
        // Have this thread wait until the trace is down. This allows us to safely notify
        // the caller.
        traceSem.acquire
        currentlyInjecting.set(false)
        return event_orchestrator.events
      case Some(f) =>
        // Don't block. We'll call terminationCallback when we're done.
        return null
    }
  }

  // Advance the trace
  def advanceTrace() {
    // Make sure the actual scheduler makes no progress until we have injected all
    // events.
    schedSemaphore.acquire
    var should_dispatch = true
    try {
      started.set(true)
      should_dispatch = event_orchestrator.inject_until_quiescence(enqueue_message)
    } finally {
      schedSemaphore.release
    }
    // Since this is always called during quiescence, once we have processed all
    // events, let us start dispatching
    if (should_dispatch) {
      Instrumenter().start_dispatch()
    }
  }

  /**
   * It is the responsibility of the caller to schedule CheckpointRequest
   * messages in schedule_new_message() and return to quiescence once all
   * CheckpointRequests have been sent.
   *
   * N.B. this method blocks. The system must be quiescent before
   * you invoke this. (See RandomScheduler.scala for an example of how to take
   * checkpoints mid-execution without blocking.)
   */
  def takeCheckpoint() : HashMap[String, Option[CheckpointReply]] = {
    ExternalEventInjector.log.debug("Initiating checkpoint")
    if (!schedulerConfig.enableCheckpointing) {
      throw new IllegalStateException("Trying to take checkpoint, yet !schedulerConfig.enableCheckpointing")
    }
    blockedOnCheckpoint.set(true)
    prepareCheckpoint()
    require(!started.get)
    started.set(true)
    Instrumenter().start_dispatch()
    checkpointSem.acquire()
    blockedOnCheckpoint.set(false)
    return checkpointer.checkpoints
  }

  def prepareCheckpoint() = {
    if (!schedulerConfig.enableCheckpointing) {
      throw new IllegalStateException("Trying to take checkpoint, yet !schedulerConfig.enableCheckpointing")
    }
    val actorRefs = event_orchestrator.actorToActorRef.
                      filterNot({case (k,v) => ActorTypes.systemActor(k)}).
                      values.toSeq
    val checkpointRequests = checkpointer.prepareRequests(actorRefs)
    // Put our requests at the front of the queue, and any existing requests
    // at the end of the queue.
    messagesToSend.synchronized {
      val existingExternals = new Queue[(Option[ActorRef], ActorRef, Any)] ++ messagesToSend
      messagesToSend.clear
      for ((actor, request) <- checkpointRequests) {
        enqueue_message(None, actor, request)
      }
      messagesToSend ++= existingExternals
    }
  }

  /**
   * Return `Internal` object if the event is an internal event,
   * `External` if the event is an externally triggered messages destined
   * towards an actor, else `System` if it is not destined towards an actor.
   */
  def handle_event_produced(snd: String, rcv: String, envelope: Envelope) : MessageType = {
    // Intercept any messages sent towards the failure detector
    if (rcv == FailureDetector.fdName) {
      if (schedulerConfig.enableFailureDetector) {
        fd.handle_fd_message(envelope.message, snd)
      }
      return FailureDetectorQuery
    } else if (rcv == CheckpointSink.name) {
      if (schedulerConfig.enableCheckpointing) {
        checkpointer.handleCheckpointResponse(envelope.message, snd)
      }
      return CheckpointReplyMessage
    } else if (enqueuedExternalMessages.synchronized {
        enqueuedExternalMessages.contains(envelope.message)
        }) {
      return ExternalMessage
    } else {
      return InternalMessage
    }
  }

  def crosses_partition(snd: String, rcv: String) : Boolean = {
    return event_orchestrator.crosses_partition(snd, rcv)
  }

  def handle_spawn_produced(event: Event) {
    event match {
      case event : SpawnEvent =>
        event_orchestrator.handle_spawn_produced(event)
    }
  }

  def handle_spawn_consumed(event: Event) {
    val spawn_event = event.asInstanceOf[SpawnEvent]
    event_orchestrator.handle_spawn_consumed(spawn_event)
  }

  def handle_event_consumed(cell: Cell, envelope: Envelope) = {
    val rcv = cell.self.path.name
    val msg = envelope.message
    enqueuedExternalMessages.synchronized {
      if (enqueuedExternalMessages.contains(msg)) {
        enqueuedExternalMessages -= msg
      }
    }
    assert(started.get)
    //event_orchestrator.events += ChangeContext(rcv)
  }

  def handle_quiescence(): Unit = {
    assert(started.get)
    started.set(false)
    if (blockedOnCheckpoint.get()) {
      checkpointSem.release()
      return
    }

    if (!event_orchestrator.trace_finished) {
      // If waiting for quiescence.
      event_orchestrator.events += Quiescence
      event_orchestrator.current_event match {
        case WaitCondition(cond) =>
          if (cond()) {
            event_orchestrator.trace_advanced()
            advanceTrace()
          } else {
            // wait for enqueue_message to be invoked.
            ExternalEventInjector.log.debug("waiting for enqueue_message...")
            dispatchAfterEnqueueMessage.synchronized {
              dispatchAfterEnqueueMessage.set(true)
            }
          }
        case _ =>
          quiescenceCallback()
          advanceTrace()
      }
    } else {
      quiescenceCallback()
      if (currentlyInjecting.get) {
        // Tell the calling thread we are done
        terminationCallback match {
          case None => traceSem.release
          case Some(f) => f(event_orchestrator.events)
        }
      } else {
        throw new RuntimeException("currentlyInjecting.get returned false")
      }
    }
  }

  def handle_shutdown () {
    alreadyPopulated = false
    Instrumenter().restart_system
    shutdownSem.acquire
  }

  def handle_start_trace () {
    shutdownSem.release
  }

  def handle_before_receive (cell: Cell) : Unit = {
    //event_orchestrator.events += ChangeContext(cell.self.path.name)
  }

  def handle_after_receive (cell: Cell) : Unit = {
    //event_orchestrator.events += ChangeContext("scheduler")
  }

  // Return true if we found the timer in our messagesToSend
  def handle_timer_cancel(rcv: String, msg: Any): Boolean = {
    messagesToSend.synchronized {
      messagesToSend.dequeueFirst(tuple =>
        tuple._2 != null &&
        tuple._2.path.name == rcv && tuple._3 == msg) match {
        case Some(_) => return true
        case None => return false
      }
    }
  }

  def handle_enqueue_code_block(cell: Cell, envelope: Envelope) {
    // signal to the main thread that it should wake up if it's blocked on
    // external messages
    assert(envelope.message.isInstanceOf[ScheduleBlock], envelope.message)
    messagesToSend.synchronized {
      messagesToSend += ((None, null, envelope.message))
      messagesToSend.notifyAll()
    }

    dispatchAfterEnqueueMessage.synchronized {
      if (dispatchAfterEnqueueMessage.get()) {
        dispatchAfterEnqueueMessage.set(false)
        ExternalEventInjector.log.debug("dispatching after enqueue_code_block")
        started.set(true)
        Instrumenter().start_dispatch()
      }
    }
  }

  /**
   * Reset ourselves and the Instrumenter to a initial clean state.
   */
  def reset_state (shutdown: Boolean) = {
    ExternalEventInjector.log.debug("resetting state...")
    if (shutdown) {
      handle_shutdown()
    }
    event_orchestrator = new EventOrchestrator[E]()
    fd = new FDMessageOrchestrator(enqueue_message)
    if (schedulerConfig.enableFailureDetector) {
      event_orchestrator.set_failure_detector(fd)
    }
    if (schedulerConfig.enableCheckpointing) {
      checkpointer = new CheckpointCollector
    }
    blockedOnCheckpoint = new AtomicBoolean(false)
    checkpointSem = new Semaphore(0)
    traceSem = new Semaphore(0)
    currentlyInjecting.set(false)
    shutdownSem = new Semaphore(0)
    started.set(false)
    schedSemaphore = new Semaphore(1)
    enqueuedExternalMessages = new MultiSet[Any]
    unignorableEvents = new AtomicBoolean(false)
    beganExternalAtomicBlocks = new MultiSet[Long] with SynchronizedSet[Long]
    endedExternalAtomicBlocks = new MultiSet[Long] with SynchronizedSet[Long]
    pendingExternalAtomicBlocks = new AtomicInteger(0)
    dispatchAfterEnqueueMessage = new AtomicBoolean(false)
    messagesToSend = new SynchronizedQueue[(Option[ActorRef], ActorRef, Any)]
    alreadyPopulated = false
    ExternalEventInjector.log.debug("state reset.")
  }
}


text:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/hooks.html
|
*/

$hook['pre_controller'] = array(
	'class'    => 'MyHook',
	'function' => 'checkLogin',
	'filename' => 'MyHook.php',
	'filepath' => 'hooks',
	//'params'   => array('username', 'params')
);

redpajama_set_name: RedPajamaGithub


text:
+++
date = "2015-10-13T22:01:13+03:00"
draft = false
title = "Using T-Digest: Median calculation and anomaly detection"
slug = "using-t-digest"
+++

In this article you can find information about using `t-digest` library in order to measure average value of some quantity(average session time).
There is also an answer for the question: What and why should you use to make such the measurement mean or median?
Besides, list and comparison of different implementations is presented below in the article.

## Main problem
So in what cases we have need to calculate mean/median? For example we have a site and we want to understand how much time an average user spent on our site.
In order to do it we should calculate an average duration of a user session.
And there are at least two ways to do it - calculate an arithmetic mean(or just mean) or calculate a median.
The calculation of mean is very simple. You need two fields: one for a sum of elements and another for their count. But it doesn`t work very well with anomalies in the data.
I mean the case when one or several elements differ greatly from others.
Lets assume that we have such values for our session durations(in milliseconds):

```
3000, 2000, 3000, 5000, 3000, 4000, 4500, 3200, 2700, 3380
```
mean = `(3000+2000+3000+5000+3000+4000+4500+3200+2700+3380) / 10 = 3378 msecs`. In this case all is ok.

But what if one of these users opens the site, forgets to close a browser tab and goes afk for an hour(3.600.000 msecs):

```
3000, 2000, 3000, 5000, 3000, 4000, 4500, 3200, 2700, 3600000
```

mean = `(3000+2000+3000+5000+3000+4000+4500+3200+2700+3600000) / 10 = 363040 msecs`. Just one of the users influences strongly on mean value.
Generally speaking, the mean is only representative if the distribution of the data is symmetric, otherwise it may be heavily influenced by outlying measurements.
In simple cases it is possible to use some kind of a filter.
But often we just don't know what threshold we should use to filter values.
Whereas the median value is the same in both cases and is equal to `3100`. So in the cases like this the median will be more useful then the mean.
However the calculation of the median in general case needs a lot of memory - O(n)

<!--more-->

## T-digest

In order to deal with memory consumption [`Q-Digest` algorithm](http://www.cs.virginia.edu/~son/cs851/papers/ucsb.sensys04.pdf "Pdf: Medians and Beyond: New Aggregation Techniques
for Sensor Networks") has been developed and after that `T-digest` has been created as improvement of `Q-digest`.
The main idea of these algorithms is to sacrifice accuracy of calculation for decrease of required amount of memory.

## List of implementations

I've found these libs for median calculation:

* [tdunning/t-digest](https://github.com/tdunning/t-digest "GitHub: t-digest") - a java library with implementation of `t-digest` algorithm
* [addthis/stream-lib](https://github.com/addthis/stream-lib "GitHub: stream-lib") - we already saw it in [previous article](/posts/comparison-of-hll). It's a library of streaming algorithms written in Java.
* [apache/commons-math](http://commons.apache.org/proper/commons-math/ "Commons Math: The Apache Commons Mathematics Library") - it's a library of different mathematics and statistics components from apache.

`apache/commons-math` implements a regular algorithm for median calculation. It is used for the estimation of calculation accuracy of viewed `t-digest` implementations.

## tdunning/t-digest: simple example
A short description from github page:

```text
A new data structure for accurate on-line accumulation of rank-based statistics such as quantiles and trimmed means. The t-digest algorithm is also very parallel friendly making it useful in map-reduce and parallel streaming applications.
```

Friendliness for a parallel calculation shows itself by support of a merging operation. You can see it in the example below.

Example of the code for t-digest:

```scala
object TDigestExample {
  import com.madhukaraphatak.sizeof.SizeEstimator
  import com.tdunning.math.stats.TDigest

  def main(args: Array[String]) {
    //define constants for experiments
    val oneSecond = 1000
    val twoMinutes = 2 * 60 * 1000
    val tenMinutes = 10 * 60 * 1000
    val twoHours = 2 * 60 * 60 * 1000

    val mainValues = 10000000
    val badValues = 100000

    //generate 10.000.000 pseudo-random values for normal user session durations
    val mainData = Generator.generate(count = mainValues, from = oneSecond, to = twoMinutes)

    //generate 100.000(1%) pseudo-random values for invalid user session durations
    val badData = Generator.generate(count = badValues, from = tenMinutes, to = twoHours)

    //generate one united collection. For further details see below
    val totalData = {
      Generator.generate(count = mainValues, from = oneSecond, to = twoMinutes) ++
        Generator.generate(count = badValues, from = tenMinutes, to = twoHours)
    }

    // Experiment #1
    // All data in one digest object
    // all values from one collection(totalData) are added to one digest object

    // recommend default value of compression = 100
    val totalDigest = TDigest.createArrayDigest(100)

    //the timing of the median calculation
    val startTime = System.currentTimeMillis()
    totalData.foreach(value => totalDigest.add(value))
    val median = totalDigest.quantile(0.5d)               //the median is a 0.5 quantile
    val calcTime = System.currentTimeMillis() - startTime
    println("calcTime: " + calcTime)

    // Experiment #2
    // Separate objects for normal(mainData) and bad data(badData) to check accuracy of t-digest merging
    val mainDigest = TDigest.createArrayDigest(100)
    mainData.foreach(value => mainDigest.add(value))

    val badDigest = TDigest.createArrayDigest(100)
    badData.foreach(value => badDigest.add(value))

    //to check accuracy of merging several digests
    val mergedDigest = TDigest.createArrayDigest(100)
    mergedDigest.add(mainDigest)
    mergedDigest.add(badDigest)

    val mergedMedian = mergedDigest.quantile(0.5d)
    println("median:       " + median)
    println("mergedMedian: " + mergedMedian)

    //how many bytes is needed to serialize t-digest object
    println("byte size:    " + totalDigest.byteSize())

    //how much memory is needed for totalDigest object
    val sizeInMem = SizeEstimator.estimate(totalDigest)
    println(s"size in mem:  $sizeInMem bytes")
  }
}
```

## addthis/stream-lib

In general there is no much difference in the interface between `tdunning/t-digest` and `addthis/stream-lib`. The example for `addthis/stream-lib` you can find in [`StreamLibExample.scala` file](https://github.com/coffius/koffio-t-digest/blob/master/src/main/scala/io/koff/t_digest/StreamLibExample.scala).
However `tdunning/t-digest` has more accuracy and less calculation time. More details about lib comparison are below.

## Comparison
Now lets compare our libraries. The libraries are compared by:

* Time of calculation
* Calculation accuracy
* Serialization size
* Needed memory

**Results:**

|                  | Median Value | Calc.Time(msec) | Calc. Acc.(%) | Serial. size(bytes) | Memory(bytes) |
|------------------|--------------|-----------------|---------------|---------------------|--------------:|
| **commons-math** |   61084,0000 |           5.922 |    100,000000 | not-supported       |   134.218.824 |
| **t-digest**     |   61084,6668 |           8.234 |    100,001090 | 15740               |        29.688 |
| **stream-lib**   |   60763,2110 |          20.142 |     99,474839 | 24820               |       231.784 |

## Anomaly detection

One more interesting application of the median is anomaly detection. In order to find anomalies in your data sequence threshold should be calculated.
For this you need to get 99.9%-quantile which means that we expect ~0.1% of anomalies in the data sequence. Look below to see how to do it using `tdunning/t-digest`.

```scala
/**
 * Example of anomaly detection
 */
object AnomalyDetectionExample {
  import com.tdunning.math.stats.TDigest
  def main(args: Array[String]) {
    //define constants for experiments
    val oneSecond = 1000
    val twoMinutes = 2 * 60 * 1000
    val tenMinutes = 10 * 60 * 1000
    val twoHours = 2 * 60 * 60 * 1000

    val mainValues = 10000000
    val badValues = 10000

    //generate 10.000.000 pseudo-random values for normal user session durations
    val mainData = Generator.generate(count = mainValues, from = oneSecond, to = twoMinutes)

    //generate 100.000(1%) pseudo-random values for invalid user session durations
    val badData = Generator.generate(count = badValues, from = tenMinutes, to = twoHours)

    val totalData = mainData ++ badData

    val totalDigest = TDigest.createArrayDigest(100)

    totalData.foreach(value => totalDigest.add(value))
    //this threshold means that we expect ~0.1% of data is anomalies
    val threshold = totalDigest.quantile(0.999d).toInt

    println(s"threshold: $threshold msec")
  }
}
```

## Links

* [Example project](https://github.com/coffius/koffio-t-digest "GitHub: Example project") - sources for this article
* [tdunning/t-digest](https://github.com/tdunning/t-digest "GitHub: t-digest") - github page of `t-digest`
* [addthis/stream-lib](https://github.com/addthis/stream-lib "GitHub: stream-lib")
* [apache/math](https://commons.apache.org/proper/commons-math/) - the apache commons mathematics library


text:
package crawl

import (
	"fmt"
	"net/url"
	"strings"
)

func normalize(rawURL string) (*url.URL, error) {
	if rawURL == "" {
		return nil, fmt.Errorf("crawl: empty URLs are invalid")
	}
	parsed, err := url.Parse(rawURL)
	if err != nil {
		return nil, err
	}
	// If no host was determined, split on the first slash and set
	// that as host
	if parsed.Host == "" {
		parts := strings.SplitN(parsed.Path, "/", 2)
		// There will always be at least one part
		parsed.Host = parts[0]
		if len(parts) > 1 && parts[1] != "" {
			parsed.Path = "/" + parts[1]
		} else {
			parsed.Path = ""
		}
	}
	// Assume http if not given
	if parsed.Scheme == "" {
		parsed.Scheme = "http"
	}
	return parsed, nil
}


text:
/*global module:false*/
module.exports = function(grunt) {

  // Helper methods
  function sub (str) {
    return str.replace(/%s/g, LIBRARY_NAME);
  }

  function wrapModules (head, tail) {
    return head.concat(MODULE_LIST).concat(tail);
  }

  var LIBRARY_NAME = 'wireio';

  var MODULE_LIST = [
    ];

  var DIST_HEAD_LIST = [
			sub('src/libs/jquery.js'),
			sub('src/%s.meta.js'),
      sub('src/%s.core.js')
    ];

  // This is the same as DIST_HEAD_LIST, just without *.const.js (which is just
  // there UglifyJS conditional compilation).
  var DEV_HEAD_LIST = [
			sub('src/libs/jquery.js'),
			sub('src/%s.meta.js'),
		  sub('src/%s.core.js')
    ];

  var TAIL_LIST = [
    ];

  // Gets inserted at the top of the generated files in dist/.
  var BANNER = [
      '/*! <%= pkg.name %> - v<%= pkg.version %> - ',
      '<%= grunt.template.today("yyyy-mm-dd") %> - <%= pkg.author %> */\n'
    ].join('');

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-qunit');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      dist: {
        options: {
          banner: BANNER
        },
        src: wrapModules(DIST_HEAD_LIST, TAIL_LIST),
        dest: sub('dist/%s.js')
      },
      dev: {
        options: {
          banner: BANNER
        },
        src: wrapModules(DEV_HEAD_LIST, TAIL_LIST),
        dest: sub('dist/%s.dev.js')
      }
    },
    uglify: {
      dist: {
        files: (function () {
            // Using an IIFE so that the destination property name can be
            // created dynamically with sub().
            var obj = {};
            obj[sub('dist/%s.min.js')] = [sub('dist/%s.js')];
            return obj;
          } ())
      },
      options: {
        banner: BANNER
      }
    }
  });

  grunt.registerTask('default', [
      'jshint',
      'build',
      'qunit'
    ]);
  grunt.registerTask('build', [
      'concat:dist',
      'concat:dev',
      'uglify:dist'
    ]);
};


text:
<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

	<meta name="author" content="" />
	<meta name="copyright" content="© 2016, " />

	<meta http-equiv="content-language" content="en" />
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<title>faq</title>

    <link href="/jekyll-modern-business/css/bootstrap.min.css" rel="stylesheet">

    <link href="/jekyll-modern-business/css/modern-business.css" rel="stylesheet">

    <link href="/jekyll-modern-business/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

			<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
				Toggle navigation

			</button>
			Your Organization

					About

					Services

					Contact

					Portfolio

							1 Column Portfolio

							2 Column Portfolio

							3 Column Portfolio

							4 Column Portfolio

					Blog

							Blog Home 1

							Blog Home 2

					Other Pages

							Sidebar Page

							FAQ

							404

							Pricing Table

faq Subheading

Home

faq<meta property="position" content="1" />

						Lorem ipsum dolor sit amet, consectetur adipiscing elit?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Curabitur eget leo at velit imperdiet varius. In eu ipsum vitae velit congue iaculis vitae at risus?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Aenean consequat lorem ut felis ullamcorper?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Lorem ipsum dolor sit amet, consectetur adipiscing elit?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Curabitur eget leo at velit imperdiet varius. In eu ipsum vitae velit congue iaculis vitae at risus?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Aenean consequat lorem ut felis ullamcorper?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Lorem ipsum dolor sit amet, consectetur adipiscing elit?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Curabitur eget leo at velit imperdiet varius. In eu ipsum vitae velit congue iaculis vitae at risus?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

						Aenean consequat lorem ut felis ullamcorper?

						Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Copyright © Your Organization – 2016

Contact

 google

terms of service

link 4


text:
package org.onosproject.oxp.protocol;

/**
 * Created by cr on 16-7-21.
 */
public enum OXPVportState {
    LINK_DOWN,
    BLOCKED,
    LIVE;
}


text:
using System;

namespace Basics.Logging
{
    internal sealed class NullLogger : ILogger
    {
        void ILogger.Log(LogType logType, object message, Exception exception, IFormatProvider formatProvider)
        {
        }

        bool ILogger.IsEnabledFor(LogType logType) => false;
    }
}


text:
using System.Web;
using Sample.Web.ApplicationServices.Memberships.Authentication;
using Sample.Web.ApplicationServices.Memberships.Authentication.Identity;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using StructureMap.Configuration.DSL;

namespace Sample.Web.ApplicationServices.DependencyResolution
{
    public class MembershipRegistry : Registry
    {
        public MembershipRegistry()
        {
            For<IAuthenticationManager>().Use(ctx => HttpContext.Current.GetOwinContext().Authentication);
            For<IUserStore<ApplicationUser>>().Use<UserStore>();
        }
    }
}


text:
package org.apache.ibatis.submitted.ognl_enum;

import java.util.List;

public interface PersonMapper {

  interface PersonType {
    Person.Type getType();
  }

  List<Person> selectAllByType(Person.Type type);

  List<Person> selectAllByTypeNameAttribute(Person.Type type);

  List<Person> selectAllByTypeWithInterface(PersonType personType);

  List<Person> selectAllByTypeNameAttributeWithInterface(PersonType personType);
}


text:
Madden Fantasy Average Draft Position

Madden Fantasy ADP

    <form class="form-inline">
        <select class="posSelect" ng-model="pos" placeholder="Select By Position">
            <option value="" disabled selected>Sort by Pos</option>
            <option value="All">All</option>
            <option value="QB">QB</option>
            <option value="HB">HB</option>
            <option value="WR">WR</option>
            <option value="TE">TE</option>
            <option value="OL">OL</option>
            <option value="DE">DE</option>
            <option value="DT">DT</option>
            <option value="LB">LB</option>
            <option value="CB">CB</option>
            <option value="S">S</option>
        </select>
        <input class="roundFilter" ng-init="round=1" ng-model="round" placeholder="Sort by Round" />
        <input type="checkbox" id="hideImages" ng-model="hideImages">Hide Images

    </form>

                    Round - ADP

                    Name

                    Position - Overall

            {{player.round}} - {{player.adp}}

            {{ player.fullName }}

            {{ player.pos }} - {{player.ovr}}


text:
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'auto_do_something'
Bundler.require(:default, :development)

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
end

# TODO: add spec for ActiveRecord

class User
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks
  include AutoDoSomething

  # mass assignment
  def initialize(attrs)
    attrs.each do |k, v|
      send("#{k}=", v)
    end
  end

  attr_accessor :nickname, :email, :info

  # i want strip these fields
  auto :strip, :for => [:nickname, :email, :info]

  # and i want downcase all email address
  auto :downcase, :for => :email

  # if info is too long, then just truncate it
  auto :[], :with => 0...10, :for => :info
  # or
  #auto_do :[], :with => [0, 10], :for => :info

  def persisted?
    false
  end
end


text:
namespace Take.Blip.Builder.Models
{
    /// <summary>
    /// Defines a type that can be validated.
    /// </summary>
    public interface IValidable
    {
        /// <summary>
        /// Validate the current instance.
        /// </summary>
        void Validate();
    }
}


text:
package org.eclipse.jdt.internal.ui.preferences;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;

/**
 * The page for setting the editor options.
 */
public final class MarkOccurrencesPreferencePage extends AbstractConfigurationBlockPreferencePage {

	/*
	 * @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#getHelpId()
	 */
	@Override
	protected String getHelpId() {
		return IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE;
	}

	/*
	 * @see org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setDescription()
	 */
	@Override
	protected void setDescription() {
		String description= PreferencesMessages.MarkOccurrencesConfigurationBlock_title;
		setDescription(description);
	}

	/*
	 * @see org.org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setPreferenceStore()
	 */
	@Override
	protected void setPreferenceStore() {
		setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
	}

	@Override
	protected Label createDescriptionLabel(Composite parent) {
		return null; // no description for new look.
	}

	/*
	 * @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#createConfigurationBlock(org.eclipse.ui.internal.editors.text.OverlayPreferenceStore)
	 */
	@Override
	protected IPreferenceConfigurationBlock createConfigurationBlock(OverlayPreferenceStore overlayPreferenceStore) {
		return new MarkOccurrencesConfigurationBlock(overlayPreferenceStore);
	}

}


text:
package creditcardvalidator;

public class LuhnAlgorithm {

	public static final String[] MaestroIIN = { "5018", "5020", "5038", "5612",
			"5893", "6304", "6759", "6761", "6762", "6763", "0604", "6390" };

	public static boolean isValidCreditCard(String input) {

		if (!input.matches("\\d+")) {
			return false;
		}

		if (input.length() < 8 || input.length() > 19) {
			return false;
		}

		// Get all numbers for checksum validation, ignore check digit, i.e. the last digit
		int[] digits = getDigits(input.substring(0, input.length() - 1));

		// Get last digit of creditcard number
		int checkDigit = Integer.parseInt(input.substring(input.length() - 1,
				input.length()));

		if (!isCheckSumValid(digits, checkDigit)) {
			return false;
		}

		return true;
	}

	private static boolean isCheckSumValid(int[] digits, int checkDigit) {
		int checksum = 0;

		for (int i = 0; i < digits.length; i++) {

			// forward loop but start with rightmost digit
			int digit = digits[digits.length - 1 - i];

			if (i % 2 == 0) {
				digit *= 2;
			}

			checksum += digitSum(digit);

		}

		// finally add checkDigit to get correct checksum
		checksum += checkDigit;
		return checksum % 10 == 0;
	}

	private static int digitSum(int digit) {
		if (digit > 9) {
			return digit - 9;
		}

		return digit;
	}

	private static int[] getDigits(String number) {
		int[] digits = new int[number.length()];

		for (int i = 0; i < digits.length; i++) {
			digits[i] = Integer.parseInt(number.substring(i, i + 1));
		}

		return digits;
	}

	public static String getIssuer(String number) {

		// Visa has all 4XXXXXX...
		String first1 = number.substring(0, 1);
		if (first1.equals("4")) {
			return "Visa";
		}

		// Mastercard has all 50-55XXXXXXX...
		int first2 = Integer.parseInt(number.substring(0, 2));
		if (first2 >= 50 && first2 <= 55) {
			return "Mastercard";
		}

		// Maestro's first 4 digits are given
		String first4 = number.substring(0, 4);
		for (int i = 0; i < MaestroIIN.length; i++) {
			if (MaestroIIN[i].equals(first4))
				return "Maestro";
		}

		return "Unknown issuer";
	}

}


text:
#ifndef FileReaderLoader_h
#define FileReaderLoader_h

#include "core/CoreExport.h"
#include "core/fileapi/FileError.h"
#include "core/loader/ThreadableLoaderClient.h"
#include "platform/weborigin/KURL.h"
#include "wtf/ArrayBufferBuilder.h"
#include "wtf/Forward.h"
#include "wtf/OwnPtr.h"
#include "wtf/text/TextEncoding.h"
#include "wtf/text/WTFString.h"

namespace blink {

class BlobDataHandle;
class DOMArrayBuffer;
class ExecutionContext;
class FileReaderLoaderClient;
class Stream;
class TextResourceDecoder;
class ThreadableLoader;

class CORE_EXPORT FileReaderLoader final : public ThreadableLoaderClient {
public:
    enum ReadType {
        ReadAsArrayBuffer,
        ReadAsBinaryString,
        ReadAsBlob,
        ReadAsText,
        ReadAsDataURL,
        ReadByClient
    };

    // If client is given, do the loading asynchronously. Otherwise, load synchronously.
    FileReaderLoader(ReadType, FileReaderLoaderClient*);
    virtual ~FileReaderLoader();

    void start(ExecutionContext*, PassRefPtr<BlobDataHandle>);
    void start(ExecutionContext*, const Stream&, unsigned readSize);
    void cancel();

    // ThreadableLoaderClient
    virtual void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<WebDataConsumerHandle>) override;
    virtual void didReceiveData(const char*, unsigned) override;
    virtual void didFinishLoading(unsigned long, double) override;
    virtual void didFail(const ResourceError&) override;

    String stringResult();
    PassRefPtr<DOMArrayBuffer> arrayBufferResult() const;

    // Returns the total bytes received. Bytes ignored by m_rawData won't be
    // counted.
    //
    // This value doesn't grow more than numeric_limits<unsigned> when
    // m_readType is not set to ReadByClient.
    long long bytesLoaded() const { return m_bytesLoaded; }

    // Before didReceiveResponse() is called: Returns -1.
    // After didReceiveResponse() is called:
    // - If the size of the resource is known (from
    //   m_response.expectedContentLength() or once didFinishLoading() is
    //   called), returns it.
    // - Otherwise, returns -1.
    long long totalBytes() const { return m_totalBytes; }

    FileError::ErrorCode errorCode() const { return m_errorCode; }

    void setEncoding(const String&);
    void setDataType(const String& dataType) { m_dataType = dataType; }

private:
    void startInternal(ExecutionContext&, const Stream*, PassRefPtr<BlobDataHandle>);
    void terminate();
    void cleanup();

    void failed(FileError::ErrorCode);
    void convertToText();
    void convertToDataURL();

    static FileError::ErrorCode httpStatusCodeToErrorCode(int);

    ReadType m_readType;
    FileReaderLoaderClient* m_client;
    WTF::TextEncoding m_encoding;
    String m_dataType;

    KURL m_urlForReading;
    bool m_urlForReadingIsStream;
    RefPtr<ThreadableLoader> m_loader;

    OwnPtr<ArrayBufferBuilder> m_rawData;
    bool m_isRawDataConverted;

    String m_stringResult;

    // The decoder used to decode the text data.
    OwnPtr<TextResourceDecoder> m_decoder;

    bool m_finishedLoading;
    long long m_bytesLoaded;
    // If the total size of the resource is unknown, m_totalBytes is set to -1
    // until completion of loading, and the buffer for receiving data is set to
    // dynamically grow. Otherwise, m_totalBytes is set to the total size and
    // the buffer for receiving data of m_totalBytes is allocated and never grow
    // even when extra data is appeneded.
    long long m_totalBytes;

    bool m_hasRange;
    unsigned m_rangeStart;
    unsigned m_rangeEnd;

    FileError::ErrorCode m_errorCode;
};

} // namespace blink

#endif // FileReaderLoader_h


text:
@implementation BDADataSource

NSMutableArray* _birthdayOfFriends;

+(id)sharedInstance {
    static dispatch_once_t p = 0;

    __strong static id _sharedObject = nil;

    dispatch_once(&p, ^{
        _sharedObject = [[self alloc] init];
    });

    return _sharedObject;
}

-(id)init {
    self =[super init];
    if (self) {
        //initialize Default Data here.
        [self initializeDefaultDataList];
    }
    return self;
}

-(void)initializeDefaultDataList {
    _fbFriends = [[NSMutableDictionary alloc] init];
    _birthdayOfFriends = [[NSMutableArray alloc] init];

}

-(void)reset {
    [_fbFriends removeAllObjects];
    [_birthdayOfFriends removeAllObjects];
}

@end


text:
	@private
	@class LoaderUtils
]=]

local loader = script.Parent
local BounceTemplateUtils = require(script.Parent.BounceTemplateUtils)
local GroupInfoUtils = require(script.Parent.GroupInfoUtils)
local PackageInfoUtils = require(script.Parent.PackageInfoUtils)
local ScriptInfoUtils = require(script.Parent.ScriptInfoUtils)
local Utils = require(script.Parent.Utils)

local LoaderUtils = {}
LoaderUtils.Utils = Utils -- TODO: Remove this

LoaderUtils.ContextTypes = Utils.readonly({
	CLIENT = "client";
	SERVER = "server";
})
LoaderUtils.IncludeBehavior = Utils.readonly({
	NO_INCLUDE = "noInclude";
	INCLUDE_ONLY = "includeOnly";
	INCLUDE_SHARED = "includeShared";
})

function LoaderUtils.toWallyFormat(instance, isPlugin)
	assert(typeof(instance) == "Instance", "Bad instance")
	assert(type(isPlugin) == "boolean", "Bad isPlugin")

	local topLevelPackages = {}
	LoaderUtils.discoverTopLevelPackages(topLevelPackages, instance)
	LoaderUtils.injectLoader(topLevelPackages)

	local packageInfoList = {}
	local packageInfoMap = {}
	local defaultReplicationType = isPlugin
		and ScriptInfoUtils.ModuleReplicationTypes.PLUGIN
		or ScriptInfoUtils.ModuleReplicationTypes.SHARED

	for _, folder in pairs(topLevelPackages) do
		local packageInfo = PackageInfoUtils.getOrCreatePackageInfo(folder, packageInfoMap, "", defaultReplicationType)
		table.insert(packageInfoList, packageInfo)
	end

	PackageInfoUtils.fillDependencySet(packageInfoList)

	if isPlugin then
		local pluginGroup = GroupInfoUtils.groupPackageInfos(packageInfoList,
			ScriptInfoUtils.ModuleReplicationTypes.PLUGIN)

		local publishSet = LoaderUtils.getPublishPackageInfoSet(packageInfoList)

		local pluginFolder = Instance.new("Folder")
		pluginFolder.Name = "PluginPackages"

		LoaderUtils.reifyGroupList(pluginGroup, publishSet, pluginFolder, ScriptInfoUtils.ModuleReplicationTypes.PLUGIN)

		return pluginFolder
	else
		local clientGroupList = GroupInfoUtils.groupPackageInfos(packageInfoList,
			ScriptInfoUtils.ModuleReplicationTypes.CLIENT)
		local serverGroupList = GroupInfoUtils.groupPackageInfos(packageInfoList,
			ScriptInfoUtils.ModuleReplicationTypes.SERVER)
		local sharedGroupList = GroupInfoUtils.groupPackageInfos(packageInfoList,
			ScriptInfoUtils.ModuleReplicationTypes.SHARED)

		local publishSet = LoaderUtils.getPublishPackageInfoSet(packageInfoList)

		local clientFolder = Instance.new("Folder")
		clientFolder.Name = "Packages"

		local sharedFolder = Instance.new("Folder")
		sharedFolder.Name = "SharedPackages"

		local serverFolder = Instance.new("Folder")
		serverFolder.Name = "Packages"

		LoaderUtils.reifyGroupList(clientGroupList, publishSet, clientFolder, ScriptInfoUtils.ModuleReplicationTypes.CLIENT)
		LoaderUtils.reifyGroupList(serverGroupList, publishSet, serverFolder, ScriptInfoUtils.ModuleReplicationTypes.SERVER)
		LoaderUtils.reifyGroupList(sharedGroupList, publishSet, sharedFolder, ScriptInfoUtils.ModuleReplicationTypes.SHARED)

		return clientFolder, serverFolder, sharedFolder
	end
end

function LoaderUtils.isPackage(folder)
	assert(typeof(folder) == "Instance", "Bad instance")

	for _, item in pairs(folder:GetChildren()) do
		if item:IsA("Folder") or item:IsA("Camera") then
			if item.Name == "Server"
				or item.Name == "Client"
				or item.Name == "Shared"
				or item.Name == ScriptInfoUtils.DEPENDENCY_FOLDER_NAME then
				return true
			end
		end
	 end

	 return false
end

function LoaderUtils.injectLoader(topLevelPackages)
	for _, item in pairs(topLevelPackages) do
		-- If we're underneath the hierachy or if we're in the actual item...
		if item == loader or loader:IsDescendantOf(item) then
			return
		end
	end

	-- We need the loader injected!
	table.insert(topLevelPackages, loader)
end

function LoaderUtils.discoverTopLevelPackages(packages, instance)
	assert(type(packages) == "table", "Bad packages")
	assert(typeof(instance) == "Instance", "Bad instance")

	if LoaderUtils.isPackage(instance) then
		table.insert(packages, instance)
	elseif instance:IsA("ObjectValue") then
		local linkedValue = instance.Value
		if linkedValue and LoaderUtils.isPackage(linkedValue) then
			table.insert(packages, linkedValue)
		end
	else
		-- Loop through all folders
		for _, item in pairs(instance:GetChildren()) do
			if item:IsA("Folder") or item:IsA("Camera") then
				LoaderUtils.discoverTopLevelPackages(packages, item)
			elseif item:IsA("ObjectValue") then
				local linkedValue = item.Value
				if linkedValue and LoaderUtils.isPackage(linkedValue) then
					table.insert(packages, linkedValue)
				end
			elseif item:IsA("ModuleScript") then
				table.insert(packages, item)
			end
		end
	end
end

function LoaderUtils.reifyGroupList(groupInfoList, publishSet, parent, replicationMode)
	assert(type(groupInfoList) == "table", "Bad groupInfoList")
	assert(type(publishSet) == "table", "Bad publishSet")
	assert(typeof(parent) == "Instance", "Bad parent")
	assert(type(replicationMode) == "string", "Bad replicationMode")

	local folder = Instance.new("Folder")
	folder.Name = "_Index"

	for _, groupInfo in pairs(groupInfoList) do
		if LoaderUtils.needToReify(groupInfo, replicationMode) then
			LoaderUtils.reifyGroup(groupInfo, folder, replicationMode)
		end
	end

	-- Publish
	for packageInfo, _ in pairs(publishSet) do
		for scriptName, scriptInfo in pairs(packageInfo.scriptInfoLookup[replicationMode]) do
			local link = BounceTemplateUtils.create(scriptInfo.instance, scriptName)
			link.Parent = parent
		end
	end

	folder.Parent = parent
end

function LoaderUtils.getPublishPackageInfoSet(packageInfoList)
	local packageInfoSet = {}
	for _, packageInfo in pairs(packageInfoList) do
		packageInfoSet[packageInfo] = true
		-- First level declared dependencies too (assuming we're importing just one item)
		for dependentPackageInfo, _ in pairs(packageInfo.explicitDependencySet) do
			packageInfoSet[dependentPackageInfo] = true
		end
	end
	return packageInfoSet
end

function LoaderUtils.needToReify(groupInfo, replicationMode)
	for _, scriptInfo in pairs(groupInfo.packageScriptInfoMap) do
		if scriptInfo.replicationMode == replicationMode then
			return true
		end
	end

	return false
end

function LoaderUtils.reifyGroup(groupInfo, parent, replicationMode)
	assert(type(groupInfo) == "table", "Bad groupInfo")
	assert(typeof(parent) == "Instance", "Bad parent")
	assert(type(replicationMode) == "string", "Bad replicationMode")

	local folder = Instance.new("Folder")
	folder.Name = assert(next(groupInfo.packageSet).fullName, "Bad package fullName")

	for scriptName, scriptInfo in pairs(groupInfo.packageScriptInfoMap) do
		assert(scriptInfo.name == scriptName, "Bad scriptInfo.name")

		if scriptInfo.replicationMode == replicationMode then
			if scriptInfo.instance == loader and loader.Parent == game:GetService("ReplicatedStorage") then
				-- Hack to prevent reparenting of loader in legacy mode
				local link = BounceTemplateUtils.create(scriptInfo.instance, scriptName)
				link.Parent = folder
			else
				scriptInfo.instance.Name = scriptName
				scriptInfo.instance.Parent = folder
			end
		else
			if scriptInfo.instance == loader then
				local link = BounceTemplateUtils.create(scriptInfo.instance, scriptName)
				link.Parent = folder
			else
				-- Turns out creating these links are a LOT faster than cloning a module script
				local link = BounceTemplateUtils.createLink(scriptInfo.instance, scriptName)
				link.Parent = folder
			end
		end
	end

	-- Link all of the other dependencies
	for scriptName, scriptInfo in pairs(groupInfo.scriptInfoMap) do
		assert(scriptInfo.name == scriptName, "Bad scriptInfo.name")

		if not groupInfo.packageScriptInfoMap[scriptName] then
			if scriptInfo.instance == loader then
				local link = BounceTemplateUtils.create(scriptInfo.instance, scriptName)
				link.Parent = folder
			else
				-- Turns out creating these links are a LOT faster than cloning a module script
				local link = BounceTemplateUtils.createLink(scriptInfo.instance, scriptName)
				link.Parent = folder
			end
		end
	end

	folder.Parent = parent
end

return LoaderUtils


text:
The Creative Tab Bracket Handler gives you access to the creative Tabs in the game.

They are referenced in the Creative Tab handler this way:

```zenscript
<creativetab:name>

<creativetab:misc>
```

If the Creative Tab is found, this will return an ICreativeTab Object. Please refer to the [respective Wiki entry](/Vanilla/CreativeTabs/ICreativeTab/) for further information on what you can do with these.


text: export { default } from 'ember-aframe/macros/stringify-coordinates';


text:
[![Build Status](https://travis-ci.org/cloudera/spark-timeseries.svg)](https://travis-ci.org/cloudera/spark-timeseries)

spark-ts
=============

A Scala / Java / Python library for interacting with time series data on Apache Spark.

Docs are available at http://cloudera.github.io/spark-timeseries.

Scaladoc is available at http://cloudera.github.io/spark-timeseries/0.2.0/scaladocs/index.html.

Javadoc is available at http://cloudera.github.io/spark-timeseries/0.2.0/apidocs/index.html.

Python doc is available at http://cloudera.github.io/spark-timeseries/0.2.0/pydoc/py-modindex.html.

The aim here is to provide
* A set of abstractions for manipulating large time series data sets, similar to
  what's provided for smaller data sets in
  [Pandas](http://pandas.pydata.org/pandas-docs/dev/timeseries.html),
  [Matlab](http://www.mathworks.com/help/matlab/time-series.html), and R's
  [zoo](http://cran.r-project.org/web/packages/zoo/index.html) and
  [xts](http://cran.r-project.org/web/packages/xts/index.html) packages.
* Models, tests, and functions that enable dealing with time series from a statistical perspective,
  similar to what's provided in [StatsModels](http://statsmodels.sourceforge.net/devel/tsa.html)
  and a variety of Matlab and R packages.

The library is geared towards use cases in finance (munging tick data, building risk models), but
intends to be general enough that other fields with continuous time series data, like meteorology,
can make use of it.

The library currently expects that individual univariate time series can easily fit in memory on each
machine, but that collections of univariate time series may need to be distributed across many
machines. While time series that violate this expectation pose a bunch of fun distributed
programming problems, they don't tend to come up very often in finance, where an array holding
a value for every minute of every trading day for ten years needs less than a couple million
elements.

The library sits on a few other excellent Java and Scala libraries.
* [Breeze](https://github.com/scalanlp/breeze) for NumPy-like, BLAS-able linear algebra.
* [java.time](https://docs.oracle.com/javase/8/docs/api/index.html?java/time/package-summary.html) for dates and times.
* [Apache Commons Math](https://commons.apache.org/proper/commons-math/) for general math and
  statistics functionality.
* [Apache Spark](https://spark.apache.org/) for distributed computation with in-memory
  capabilities.

Abstractions
-------------

The central abstraction of the library is the `TimeSeriesRDD`, a lazy distributed collection of univariate series with a conformed time dimension. It is lazy in the sense that it is an RDD: it encapsulates all the information needed to generate its elements, but doesn't materialize them upon instantiation. It is distributed in the sense that different univariate series within the collection can be stored and processed on different nodes.  Within each univariate series, observations are not distributed. The time dimension is conformed in the sense that a single `DateTimeIndex` applies to all the univariate series. Each univariate series within the RDD has a key to identify it.

TimeSeriesRDDs then support efficient series-wise operations like slicing, imputing missing values
based on surrounding elements, and training time-series models.  In Scala:

    val tsRdd: TimeSeriesRDD = ...

    // Find a sub-slice between two dates
    val subslice = tsRdd.slice(new DateTime("2015-4-10"), new DateTime("2015-4-14"))

    // Fill in missing values based on linear interpolation
    val filled = subslice.fill("linear")

    // Use an AR(1) model to remove serial correlations
    val residuals = filled.mapSeries(series => ar(series, 1).removeTimeDependentEffects(series))

In Python:

    tsrdd = ...

    # Find a sub-slice between two dates
    subslice = tsrdd['2015-04-10':'2015-04-14']

    # Fill in missing values based on linear interpolation
    filled = subslice.fill('linear')

Functionality
--------------------------

### Time Series Manipulation
* Aligning
* Slicing by date-time
* Missing value imputation

### Time Series Math and Stats

* Exponentially weighted moving average (EWMA) models
* Autoregressive integrated moving average (ARIMA) models
* Generalized autoregressive conditional heteroskedastic (GARCH) models
* Missing data imputation
* Augmented Dickey-Fuller test
* Durbin-Watson test
* Breusch-Godfrey test
* Breusch-Pagan test

### General Prob / Stats

* Multivariate T Distribution

### Risk

Value at Risk (VaR) and Expected Shortfall (CVaR) through
* Monte Carlo simulation
* Bootstrapped historical simulation

Building
--------

We use [Maven](https://maven.apache.org/) for building Java / Scala. To compile, run tests, and build
jars:

    mvn package

To run a spark-shell with spark-ts and its dependencies on the classpath:

    spark-shell --jars target/sparkts-$VERSION-jar-with-dependencies.jar

To run Python tests (requires [nose](https://nose.readthedocs.org/en/latest/)):

    cd python
    export SPARK_HOME=<location of local Spark installation>
    nosetests

To publish docs, easiest is to clone a separate version of this repo in some location we'll refer
to as DOCS_REPO.  Then:

    # Build main doc
    mvn site -Ddependency.locations.enabled=false

    # Build scaladoc
    mvn scala:doc

    # Build javadoc
    mvn javadoc:javadoc

    # Build Python doc
    cd python
    export SPARK_HOME=<location of local Spark installation>
    export PYTHONPATH=$PYTHONPATH::$SPARK_HOME/python:$SPARK_HOME/python/lib/*
    make html
    cd ..

    cp -r target/site/* $DOCS_REPO
    cp -r python/build/html/ $DOCS_REPO/pydoc
    cd $DOCS_REPO
    git checkout gh-pages
    git add -A
    git commit -m "Some message that includes the hash of the relevant commit in master"
    git push origin gh-pages

To build a Python source distribution, first build with Maven, then:

    cp target/sparkts-$VERSION-jar-with-dependencies.jar python/sparkts/
    cd python
    python setup.py sdist

To release Java/Scala packages (based on http://oryxproject.github.io/oryx/docs/how-to-release.html):

    mvn -Darguments="-DskipTests" -DreleaseVersion=$VERSION \
        -DdevelopmentVersion=$VERSION-SNAPSHOT release:prepare

    mvn -s private-settings.xml -Darguments="-DskipTests" release:perform


text:
layout: post
title: "Service Security 2016 NTHU Notes"
author: billy_rick
modified: 2016-08-28
comments: true
excerpt: "Service Security 2016 NTHU Notes"
tags: []
---

## Information Entropy
1. Privacy vs Security (Panopticon (Psychologically affect humans))
2. Information Security: Base 6 usage, can be used to represent a larger set of numbers.
3. The difficulty of compression: explain (Information is more valuabe based on how compressable it is/ not how meaningful it is to me)
4. Predictability & Compressability define the Entropy of Information
5. Wheather (atmospheric noise) -> the best way to randomize information
6. Zipf Law (use this law to see what is the entrop of a password for the most common words (20%))
7. Check what sites are using to check the password?

## Information Integrity
1. Constants can be initialized with a variable
2. Write your test first
3. Tests are a contract between your clients and your organization
4. Follow Test Driven Development (TDD)
5. Just write code that passes the tests
6. You make your code more beautiful after you have passed your tests
7. Analyze the problem, pass your tests, then write beautiful code
8. (Protobuffer):  to send very compressed data; very different from the transferring of JSON and XML types
9. Jumpee - Atom's plugin to jump around your code
10. [RuboCop](https://github.com/bbatsov/ruby-style-guide) - to check your code for violation of syntax uses in Ruby
11. Ruby methods are great (readable) if 5 lines of code are used.
12. Map / Reduce to iterate over arrays, etc., instead of for loops.
13. In order to understand how to get the most optimized code you must run benchmarks

Bonus:

~~~ ruby
# Ruby OOP Metaprogramming
attr_accessor

describe 'test' do
	it 'test body'
		# ...
	end
end

# minitest
xxx.must_equal true

~~~

Domain Specific Language (DSL)- looks like new code(methods) being embedded into a programming language

YAML: A form of serializing data

## Cryptography
1. Keyspace: all the possible spaces that's needed to cover the plaintext
2. Confusion: encrypt -> ciphertext
3. Diffusion:  plaintext -> encrypt -> ciphertext
4. Permutation: scramble cipher
5. High Diffusion doesn't necessarily mean High Confusions
6. A cipher is secure if there are no real (shortcuts) patterns.
7. Kerckhoff's Principle
8. Double Transposition Cipher: Problems with the size of column
9. One time pad: keep changing the key

## Cryptography Part B
1. SK-Cryptography: Stream Ciphers
2. RC4 operates with bytes and A5/1 Ciphers operate at bit level (more computational complexity)
3. Diffusion is low for A5/1 Ciphers
4. Diffusion keeps changing steadily
5. WEP weak because of CRC (Cyclic Redundancy Check)

## Asymmetric Cryptography
1. PK-Cryptography ensures confidentiality (Agent (securing) Handler)
2. PK-Cryptography ensures non-repudiation (Agent (signing) Handler)
3. First we sign then secure so that the other side decrypts and then remove the signature with Agents public key.
4. In the end it all boils down to one human problem: TRUST
5. The luhn validator is easy to check and easy to solve
5. All P problems and are NP Problems.

## Assignments ([Coded in Ruby](https://github.com/wisebits))
1. Luhn Algorithm
2. Permutation, Substitution and Double Transposition Ciphers
3. Advanced Cryptography and Hashing


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Cat. Lich. Univers. 2: 413 (1923)

#### Original name
Graphis macgregorii Vain.

### Remarks
null


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      	http://www.springframework.org/schema/context
      	http://www.springframework.org/schema/context/spring-context-4.1.xsd"
	default-lazy-init="true">

	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<property name="loginUrl" value="/admin/login.jsp" />
		<property name="successUrl" value="/admin/common/main.jhtml" />
		<property name="unauthorizedUrl" value="/admin/login.jsp" />
		<property name="filterChainDefinitions">
			<value>
				/admin/logout.jsp = logout
				/admin/common/captcha.jhtml = anon

				/admin/product/** = perms["admin:product"]
				/admin/product_category/** = perms["admin:productCategory"]
				/admin/parameter_group/** = perms["admin:parameterGroup"]
				/admin/attribute/** = perms["admin:attribute"]
				/admin/specification/** = perms["admin:specification"]
				/admin/brand/** = perms["admin:brand"]
				/admin/product_notify/** = perms["admin:productNotify"]

				/admin/order/** = perms["admin:order"]
				/admin/print/** = perms["admin:print"]
				/admin/payment/** = perms["admin:payment"]
				/admin/refunds/** = perms["admin:refunds"]
				/admin/shipping/** = perms["admin:shipping"]
				/admin/returns/** = perms["admin:returns"]
				/admin/delivery_center/** = perms["admin:deliveryCenter"]
				/admin/delivery_template/** = perms["admin:deliveryTemplate"]

				/admin/member/** = perms["admin:member"]
				/admin/member_rank/** = perms["admin:memberRank"]
				/admin/member_attribute/** = perms["admin:memberAttribute"]
				/admin/review/** = perms["admin:review"]
				/admin/consultation/** = perms["admin:consultation"]

				/admin/navigation/** = perms["admin:navigation"]
				/admin/article/** = perms["admin:article"]
				/admin/article_category/** = perms["admin:articleCategory"]
				/admin/tag/** = perms["admin:tag"]
				/admin/friend_link/** = perms["admin:friendLink"]
				/admin/ad_position/** = perms["admin:adPosition"]
				/admin/ad/** = perms["admin:ad"]
				/admin/template/** = perms["admin:template"]
				/admin/cache/** = perms["admin:cache"]
				/admin/static/** = perms["admin:static"]
				/admin/index/** = perms["admin:index"]

				/admin/promotion/** = perms["admin:promotion"]
				/admin/coupon/** = perms["admin:coupon"]
				/admin/seo/** = perms["admin:seo"]
				/admin/sitemap/** = perms["admin:sitemap"]

				/admin/statistics/** = perms["admin:statistics"]
				/admin/sales/** = perms["admin:sales"]
				/admin/sales_ranking/** = perms["admin:salesRanking"]
				/admin/purchase_ranking/** = perms["admin:purchaseRanking"]
				/admin/deposit/** = perms["admin:deposit"]

				/admin/setting/** = perms["admin:setting"]
				/admin/area/** = perms["admin:area"]
				/admin/payment_method/** = perms["admin:paymentMethod"]
				/admin/shipping_method/** = perms["admin:shippingMethod"]
				/admin/delivery_corp/** = perms["admin:deliveryCorp"]
				/admin/payment_plugin/** = perms["admin:paymentPlugin"]
				/admin/storage_plugin/** = perms["admin:storagePlugin"]
				/admin/admin/** = perms["admin:admin"]
				/admin/role/** = perms["admin:role"]
				/admin/message/** = perms["admin:message"]
				/admin/log/** = perms["admin:log"]

				/admin/** = authc
			</value>
		</property>
		<property name="filters">
			<map>
				<entry key="authc" value-ref="authenticationFilter" />
			</map>
		</property>
	</bean>

	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="authenticationRealm" />
		<property name="cacheManager" ref="shiroCacheManager" />
	</bean>

	<bean id="authenticationRealm" class="net.shopxx.AuthenticationRealm">
		<property name="authorizationCacheName" value="authorization" />
	</bean>

	<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManager" ref="ehCacheManager" />
	</bean>

	<bean id="authenticationFilter" class="net.shopxx.filter.AuthenticationFilter" />

	<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
		<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" />
		<property name="arguments" ref="securityManager" />
	</bean>

</beans>


text:
<!DOCTYPE HTML>
<html>
    <body class='yui3-skin-sam'>
        <button id="my-button" class="yui3-button">my button</button>

        <script type="text/javascript" src="../../../../build/yui/yui.js"></script>
        <script type="text/javascript">

        YUI({
            filter: 'raw'
        }).use('button-core', function(Y) {

            var button = new Y.ButtonCore({
                host: Y.one('#my-button')
            })

            button.getNode().on('click', function(){
                Y.log('a');
            });

        });
        </script>
    </body>
</html>


text:
{{detail.detailId}}

		{{detail.content}}

Hallo
Hallo
Hallo
TestHallo

<input class="btn btn-info btn-xs" type="button" value="Click me!" ng-model="expand" ng-click="expand=!expand"/>


text:
ACCEPTED

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Mycotaxon 27: 591 (1986)

#### Original name
Xanthoparmelia olivetorica Hale

### Remarks
null


text:
package org.apache.sshd.common.io;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.OptionalFeature;
import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;

/**
 * @author Apache MINA SSHD Project
 */
public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFactoryFactory>, OptionalFeature {
    NIO2(Nio2ServiceFactoryFactory.class),
    MINA("org.apache.sshd.common.io.mina.MinaServiceFactoryFactory"),
    NETTY("org.apache.sshd.netty.NettyIoServiceFactoryFactory");

    public static final Set<BuiltinIoServiceFactoryFactories> VALUES =
        Collections.unmodifiableSet(EnumSet.allOf(BuiltinIoServiceFactoryFactories.class));

    private final Class<? extends IoServiceFactoryFactory> factoryClass;
    private final String factoryClassName;

    BuiltinIoServiceFactoryFactories(Class<? extends IoServiceFactoryFactory> clazz) {
        factoryClass = clazz;
        factoryClassName = null;
    }

    BuiltinIoServiceFactoryFactories(String clazz) {
        factoryClass = null;
        factoryClassName = clazz;
    }

    public final String getFactoryClassName() {
        if (factoryClass != null) {
            return factoryClass.getName();
        } else {
            return factoryClassName;
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public final Class<? extends IoServiceFactoryFactory> getFactoryClass() {
        if (factoryClass != null) {
            return factoryClass;
        }

        try {
            return (Class) Class.forName(factoryClassName, true, BuiltinIoServiceFactoryFactories.class.getClassLoader());
        } catch (ClassNotFoundException e) {
            try {
                return (Class) Class.forName(factoryClassName, true, Thread.currentThread().getContextClassLoader());
            } catch (ClassNotFoundException e1) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public final String getName() {
        return name().toLowerCase();
    }

    @Override
    public final IoServiceFactoryFactory create() {
        Class<? extends IoServiceFactoryFactory> clazz = getFactoryClass();
        try {
            return clazz.newInstance();
        } catch (Throwable e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public boolean isSupported() {
        try {
            return getFactoryClass() != null;
        } catch (RuntimeException e) {
            return false;
        }
    }

    public static BuiltinIoServiceFactoryFactories fromFactoryName(String name) {
        return NamedResource.findByName(name, String.CASE_INSENSITIVE_ORDER, VALUES);
    }

    public static BuiltinIoServiceFactoryFactories fromFactoryClass(Class<?> clazz) {
        if ((clazz == null) || (!IoServiceFactoryFactory.class.isAssignableFrom(clazz))) {
            return null;
        }

        for (BuiltinIoServiceFactoryFactories f : VALUES) {
            if (!f.isSupported()) {
                continue;
            }

            if (clazz.isAssignableFrom(f.getFactoryClass())) {
                return f;
            }
        }

        return null;
    }

}