text:
package org.apache.spark.scheduler

import java.io.NotSerializableException
import java.nio.ByteBuffer
import java.util.concurrent.ConcurrentLinkedQueue

import scala.collection.mutable.{ArrayBuffer, BitSet, HashMap, HashSet}
import scala.math.max
import scala.util.control.NonFatal

import org.apache.spark._
import org.apache.spark.TaskState.TaskState
import org.apache.spark.internal.{config, Logging}
import org.apache.spark.internal.config._
import org.apache.spark.scheduler.SchedulingMode._
import org.apache.spark.util.{AccumulatorV2, Clock, LongAccumulator, SystemClock, Utils}
import org.apache.spark.util.collection.MedianHeap

/**
 * Schedules the tasks within a single TaskSet in the TaskSchedulerImpl. This class keeps track of
 * each task, retries tasks if they fail (up to a limited number of times), and
 * handles locality-aware scheduling for this TaskSet via delay scheduling. The main interfaces
 * to it are resourceOffer, which asks the TaskSet whether it wants to run a task on one node,
 * and handleSuccessfulTask/handleFailedTask, which tells it that one of its tasks changed state
 *  (e.g. finished/failed).
 *
 * THREADING: This class is designed to only be called from code with a lock on the
 * TaskScheduler (e.g. its event handlers). It should not be called from other threads.
 *
 * @param sched           the TaskSchedulerImpl associated with the TaskSetManager
 * @param taskSet         the TaskSet to manage scheduling for
 * @param maxTaskFailures if any particular task fails this number of times, the entire
 *                        task set will be aborted
 */
private[spark] class TaskSetManager(
    sched: TaskSchedulerImpl,
    val taskSet: TaskSet,
    val maxTaskFailures: Int,
    blacklistTracker: Option[BlacklistTracker] = None,
    clock: Clock = new SystemClock()) extends Schedulable with Logging {

  private val conf = sched.sc.conf

  // SPARK-21563 make a copy of the jars/files so they are consistent across the TaskSet
  private val addedJars = HashMap[String, Long](sched.sc.addedJars.toSeq: _*)
  private val addedFiles = HashMap[String, Long](sched.sc.addedFiles.toSeq: _*)

  // Quantile of tasks at which to start speculation
  val speculationQuantile = conf.get(SPECULATION_QUANTILE)
  val speculationMultiplier = conf.get(SPECULATION_MULTIPLIER)

  val maxResultSize = conf.get(config.MAX_RESULT_SIZE)

  val speculationEnabled = conf.get(SPECULATION_ENABLED)

  // Serializer for closures and tasks.
  val env = SparkEnv.get
  val ser = env.closureSerializer.newInstance()

  val tasks = taskSet.tasks
  private[scheduler] val partitionToIndex = tasks.zipWithIndex
    .map { case (t, idx) => t.partitionId -> idx }.toMap
  val numTasks = tasks.length
  val copiesRunning = new Array[Int](numTasks)

  // For each task, tracks whether a copy of the task has succeeded. A task will also be
  // marked as "succeeded" if it failed with a fetch failure, in which case it should not
  // be re-run because the missing map data needs to be regenerated first.
  val successful = new Array[Boolean](numTasks)
  private val numFailures = new Array[Int](numTasks)

  // Add the tid of task into this HashSet when the task is killed by other attempt tasks.
  // This happened while we set the `spark.speculation` to true. The task killed by others
  // should not resubmit while executor lost.
  private val killedByOtherAttempt = new HashSet[Long]

  val taskAttempts = Array.fill[List[TaskInfo]](numTasks)(Nil)
  private[scheduler] var tasksSuccessful = 0

  val weight = 1
  val minShare = 0
  var priority = taskSet.priority
  var stageId = taskSet.stageId
  val name = "TaskSet_" + taskSet.id
  var parent: Pool = null
  private var totalResultSize = 0L
  private var calculatedTasks = 0

  private[scheduler] val taskSetBlacklistHelperOpt: Option[TaskSetBlacklist] = {
    blacklistTracker.map { _ =>
      new TaskSetBlacklist(sched.sc.listenerBus, conf, stageId, taskSet.stageAttemptId, clock)
    }
  }

  private[scheduler] val runningTasksSet = new HashSet[Long]

  override def runningTasks: Int = runningTasksSet.size

  def someAttemptSucceeded(tid: Long): Boolean = {
    successful(taskInfos(tid).index)
  }

  // True once no more tasks should be launched for this task set manager. TaskSetManagers enter
  // the zombie state once at least one attempt of each task has completed successfully, or if the
  // task set is aborted (for example, because it was killed).  TaskSetManagers remain in the zombie
  // state until all tasks have finished running; we keep TaskSetManagers that are in the zombie
  // state in order to continue to track and account for the running tasks.
  // TODO: We should kill any running task attempts when the task set manager becomes a zombie.
  private[scheduler] var isZombie = false

  // Whether the taskSet run tasks from a barrier stage. Spark must launch all the tasks at the
  // same time for a barrier stage.
  private[scheduler] def isBarrier = taskSet.tasks.nonEmpty && taskSet.tasks(0).isBarrier

  // Set of pending tasks for each executor. These collections are actually
  // treated as stacks, in which new tasks are added to the end of the
  // ArrayBuffer and removed from the end. This makes it faster to detect
  // tasks that repeatedly fail because whenever a task failed, it is put
  // back at the head of the stack. These collections may contain duplicates
  // for two reasons:
  // (1): Tasks are only removed lazily; when a task is launched, it remains
  // in all the pending lists except the one that it was launched from.
  // (2): Tasks may be re-added to these lists multiple times as a result
  // of failures.
  // Duplicates are handled in dequeueTaskFromList, which ensures that a
  // task hasn't already started running before launching it.
  private val pendingTasksForExecutor = new HashMap[String, ArrayBuffer[Int]]

  // Set of pending tasks for each host. Similar to pendingTasksForExecutor,
  // but at host level.
  private val pendingTasksForHost = new HashMap[String, ArrayBuffer[Int]]

  // Set of pending tasks for each rack -- similar to the above.
  private val pendingTasksForRack = new HashMap[String, ArrayBuffer[Int]]

  // Set containing pending tasks with no locality preferences.
  private[scheduler] var pendingTasksWithNoPrefs = new ArrayBuffer[Int]

  // Set containing all pending tasks (also used as a stack, as above).
  private val allPendingTasks = new ArrayBuffer[Int]

  // Tasks that can be speculated. Since these will be a small fraction of total
  // tasks, we'll just hold them in a HashSet.
  private[scheduler] val speculatableTasks = new HashSet[Int]

  // Task index, start and finish time for each task attempt (indexed by task ID)
  private[scheduler] val taskInfos = new HashMap[Long, TaskInfo]

  // Use a MedianHeap to record durations of successful tasks so we know when to launch
  // speculative tasks. This is only used when speculation is enabled, to avoid the overhead
  // of inserting into the heap when the heap won't be used.
  val successfulTaskDurations = new MedianHeap()

  // How frequently to reprint duplicate exceptions in full, in milliseconds
  val EXCEPTION_PRINT_INTERVAL =
    conf.getLong("spark.logging.exceptionPrintInterval", 10000)

  // Map of recent exceptions (identified by string representation and top stack frame) to
  // duplicate count (how many times the same exception has appeared) and time the full exception
  // was printed. This should ideally be an LRU map that can drop old exceptions automatically.
  private val recentExceptions = HashMap[String, (Int, Long)]()

  // Figure out the current map output tracker epoch and set it on all tasks
  val epoch = sched.mapOutputTracker.getEpoch
  logDebug("Epoch for " + taskSet + ": " + epoch)
  for (t <- tasks) {
    t.epoch = epoch
  }

  // Add all our tasks to the pending lists. We do this in reverse order
  // of task index so that tasks with low indices get launched first.
  for (i <- (0 until numTasks).reverse) {
    addPendingTask(i)
  }

  /**
   * Track the set of locality levels which are valid given the tasks locality preferences and
   * the set of currently available executors.  This is updated as executors are added and removed.
   * This allows a performance optimization, of skipping levels that aren't relevant (eg., skip
   * PROCESS_LOCAL if no tasks could be run PROCESS_LOCAL for the current set of executors).
   */
  private[scheduler] var myLocalityLevels = computeValidLocalityLevels()

  // Time to wait at each level
  private[scheduler] var localityWaits = myLocalityLevels.map(getLocalityWait)

  // Delay scheduling variables: we keep track of our current locality level and the time we
  // last launched a task at that level, and move up a level when localityWaits[curLevel] expires.
  // We then move down if we manage to launch a "more local" task.
  private var currentLocalityIndex = 0 // Index of our current locality level in validLocalityLevels
  private var lastLaunchTime = clock.getTimeMillis()  // Time we last launched a task at this level

  override def schedulableQueue: ConcurrentLinkedQueue[Schedulable] = null

  override def schedulingMode: SchedulingMode = SchedulingMode.NONE

  private[scheduler] var emittedTaskSizeWarning = false

  /** Add a task to all the pending-task lists that it should be on. */
  private[spark] def addPendingTask(index: Int) {
    for (loc <- tasks(index).preferredLocations) {
      loc match {
        case e: ExecutorCacheTaskLocation =>
          pendingTasksForExecutor.getOrElseUpdate(e.executorId, new ArrayBuffer) += index
        case e: HDFSCacheTaskLocation =>
          val exe = sched.getExecutorsAliveOnHost(loc.host)
          exe match {
            case Some(set) =>
              for (e <- set) {
                pendingTasksForExecutor.getOrElseUpdate(e, new ArrayBuffer) += index
              }
              logInfo(s"Pending task $index has a cached location at ${e.host} " +
                ", where there are executors " + set.mkString(","))
            case None => logDebug(s"Pending task $index has a cached location at ${e.host} " +
                ", but there are no executors alive there.")
          }
        case _ =>
      }
      pendingTasksForHost.getOrElseUpdate(loc.host, new ArrayBuffer) += index
      for (rack <- sched.getRackForHost(loc.host)) {
        pendingTasksForRack.getOrElseUpdate(rack, new ArrayBuffer) += index
      }
    }

    if (tasks(index).preferredLocations == Nil) {
      pendingTasksWithNoPrefs += index
    }

    allPendingTasks += index  // No point scanning this whole list to find the old task there
  }

  /**
   * Return the pending tasks list for a given executor ID, or an empty list if
   * there is no map entry for that host
   */
  private def getPendingTasksForExecutor(executorId: String): ArrayBuffer[Int] = {
    pendingTasksForExecutor.getOrElse(executorId, ArrayBuffer())
  }

  /**
   * Return the pending tasks list for a given host, or an empty list if
   * there is no map entry for that host
   */
  private def getPendingTasksForHost(host: String): ArrayBuffer[Int] = {
    pendingTasksForHost.getOrElse(host, ArrayBuffer())
  }

  /**
   * Return the pending rack-local task list for a given rack, or an empty list if
   * there is no map entry for that rack
   */
  private def getPendingTasksForRack(rack: String): ArrayBuffer[Int] = {
    pendingTasksForRack.getOrElse(rack, ArrayBuffer())
  }

  /**
   * Dequeue a pending task from the given list and return its index.
   * Return None if the list is empty.
   * This method also cleans up any tasks in the list that have already
   * been launched, since we want that to happen lazily.
   */
  private def dequeueTaskFromList(
      execId: String,
      host: String,
      list: ArrayBuffer[Int]): Option[Int] = {
    var indexOffset = list.size
    while (indexOffset > 0) {
      indexOffset -= 1
      val index = list(indexOffset)
      if (!isTaskBlacklistedOnExecOrNode(index, execId, host)) {
        // This should almost always be list.trimEnd(1) to remove tail
        list.remove(indexOffset)
        if (copiesRunning(index) == 0 && !successful(index)) {
          return Some(index)
        }
      }
    }
    None
  }

  /** Check whether a task once ran an attempt on a given host */
  private def hasAttemptOnHost(taskIndex: Int, host: String): Boolean = {
    taskAttempts(taskIndex).exists(_.host == host)
  }

  private def isTaskBlacklistedOnExecOrNode(index: Int, execId: String, host: String): Boolean = {
    taskSetBlacklistHelperOpt.exists { blacklist =>
      blacklist.isNodeBlacklistedForTask(host, index) ||
        blacklist.isExecutorBlacklistedForTask(execId, index)
    }
  }

  /**
   * Return a speculative task for a given executor if any are available. The task should not have
   * an attempt running on this host, in case the host is slow. In addition, the task should meet
   * the given locality constraint.
   */
  // Labeled as protected to allow tests to override providing speculative tasks if necessary
  protected def dequeueSpeculativeTask(execId: String, host: String, locality: TaskLocality.Value)
    : Option[(Int, TaskLocality.Value)] =
  {
    speculatableTasks.retain(index => !successful(index)) // Remove finished tasks from set

    def canRunOnHost(index: Int): Boolean = {
      !hasAttemptOnHost(index, host) &&
        !isTaskBlacklistedOnExecOrNode(index, execId, host)
    }

    if (!speculatableTasks.isEmpty) {
      // Check for process-local tasks; note that tasks can be process-local
      // on multiple nodes when we replicate cached blocks, as in Spark Streaming
      for (index <- speculatableTasks if canRunOnHost(index)) {
        val prefs = tasks(index).preferredLocations
        val executors = prefs.flatMap(_ match {
          case e: ExecutorCacheTaskLocation => Some(e.executorId)
          case _ => None
        });
        if (executors.contains(execId)) {
          speculatableTasks -= index
          return Some((index, TaskLocality.PROCESS_LOCAL))
        }
      }

      // Check for node-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.NODE_LOCAL)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          val locations = tasks(index).preferredLocations.map(_.host)
          if (locations.contains(host)) {
            speculatableTasks -= index
            return Some((index, TaskLocality.NODE_LOCAL))
          }
        }
      }

      // Check for no-preference tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.NO_PREF)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          val locations = tasks(index).preferredLocations
          if (locations.size == 0) {
            speculatableTasks -= index
            return Some((index, TaskLocality.PROCESS_LOCAL))
          }
        }
      }

      // Check for rack-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.RACK_LOCAL)) {
        for (rack <- sched.getRackForHost(host)) {
          for (index <- speculatableTasks if canRunOnHost(index)) {
            val racks = tasks(index).preferredLocations.map(_.host).flatMap(sched.getRackForHost)
            if (racks.contains(rack)) {
              speculatableTasks -= index
              return Some((index, TaskLocality.RACK_LOCAL))
            }
          }
        }
      }

      // Check for non-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.ANY)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          speculatableTasks -= index
          return Some((index, TaskLocality.ANY))
        }
      }
    }

    None
  }

  /**
   * Dequeue a pending task for a given node and return its index and locality level.
   * Only search for tasks matching the given locality constraint.
   *
   * @return An option containing (task index within the task set, locality, is speculative?)
   */
  private def dequeueTask(execId: String, host: String, maxLocality: TaskLocality.Value)
    : Option[(Int, TaskLocality.Value, Boolean)] =
  {
    for (index <- dequeueTaskFromList(execId, host, getPendingTasksForExecutor(execId))) {
      return Some((index, TaskLocality.PROCESS_LOCAL, false))
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.NODE_LOCAL)) {
      for (index <- dequeueTaskFromList(execId, host, getPendingTasksForHost(host))) {
        return Some((index, TaskLocality.NODE_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.NO_PREF)) {
      // Look for noPref tasks after NODE_LOCAL for minimize cross-rack traffic
      for (index <- dequeueTaskFromList(execId, host, pendingTasksWithNoPrefs)) {
        return Some((index, TaskLocality.PROCESS_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.RACK_LOCAL)) {
      for {
        rack <- sched.getRackForHost(host)
        index <- dequeueTaskFromList(execId, host, getPendingTasksForRack(rack))
      } {
        return Some((index, TaskLocality.RACK_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.ANY)) {
      for (index <- dequeueTaskFromList(execId, host, allPendingTasks)) {
        return Some((index, TaskLocality.ANY, false))
      }
    }

    // find a speculative task if all others tasks have been scheduled
    dequeueSpeculativeTask(execId, host, maxLocality).map {
      case (taskIndex, allowedLocality) => (taskIndex, allowedLocality, true)}
  }

  /**
   * Respond to an offer of a single executor from the scheduler by finding a task
   *
   * NOTE: this function is either called with a maxLocality which
   * would be adjusted by delay scheduling algorithm or it will be with a special
   * NO_PREF locality which will be not modified
   *
   * @param execId the executor Id of the offered resource
   * @param host  the host Id of the offered resource
   * @param maxLocality the maximum locality we want to schedule the tasks at
   */
  @throws[TaskNotSerializableException]
  def resourceOffer(
      execId: String,
      host: String,
      maxLocality: TaskLocality.TaskLocality)
    : Option[TaskDescription] =
  {
    val offerBlacklisted = taskSetBlacklistHelperOpt.exists { blacklist =>
      blacklist.isNodeBlacklistedForTaskSet(host) ||
        blacklist.isExecutorBlacklistedForTaskSet(execId)
    }
    if (!isZombie && !offerBlacklisted) {
      val curTime = clock.getTimeMillis()

      var allowedLocality = maxLocality

      if (maxLocality != TaskLocality.NO_PREF) {
        allowedLocality = getAllowedLocalityLevel(curTime)
        if (allowedLocality > maxLocality) {
          // We're not allowed to search for farther-away tasks
          allowedLocality = maxLocality
        }
      }

      dequeueTask(execId, host, allowedLocality).map { case ((index, taskLocality, speculative)) =>
        // Found a task; do some bookkeeping and return a task description
        val task = tasks(index)
        val taskId = sched.newTaskId()
        // Do various bookkeeping
        copiesRunning(index) += 1
        val attemptNum = taskAttempts(index).size
        val info = new TaskInfo(taskId, index, attemptNum, curTime,
          execId, host, taskLocality, speculative)
        taskInfos(taskId) = info
        taskAttempts(index) = info :: taskAttempts(index)
        // Update our locality level for delay scheduling
        // NO_PREF will not affect the variables related to delay scheduling
        if (maxLocality != TaskLocality.NO_PREF) {
          currentLocalityIndex = getLocalityIndex(taskLocality)
          lastLaunchTime = curTime
        }
        // Serialize and return the task
        val serializedTask: ByteBuffer = try {
          ser.serialize(task)
        } catch {
          // If the task cannot be serialized, then there's no point to re-attempt the task,
          // as it will always fail. So just abort the whole task-set.
          case NonFatal(e) =>
            val msg = s"Failed to serialize task $taskId, not attempting to retry it."
            logError(msg, e)
            abort(s"$msg Exception during serialization: $e")
            throw new TaskNotSerializableException(e)
        }
        if (serializedTask.limit() > TaskSetManager.TASK_SIZE_TO_WARN_KIB * 1024 &&
          !emittedTaskSizeWarning) {
          emittedTaskSizeWarning = true
          logWarning(s"Stage ${task.stageId} contains a task of very large size " +
            s"(${serializedTask.limit() / 1024} KiB). The maximum recommended task size is " +
            s"${TaskSetManager.TASK_SIZE_TO_WARN_KIB} KiB.")
        }
        addRunningTask(taskId)

        // We used to log the time it takes to serialize the task, but task size is already
        // a good proxy to task serialization time.
        // val timeTaken = clock.getTime() - startTime
        val taskName = s"task ${info.id} in stage ${taskSet.id}"
        logInfo(s"Starting $taskName (TID $taskId, $host, executor ${info.executorId}, " +
          s"partition ${task.partitionId}, $taskLocality, ${serializedTask.limit()} bytes)")

        sched.dagScheduler.taskStarted(task, info)
        new TaskDescription(
          taskId,
          attemptNum,
          execId,
          taskName,
          index,
          task.partitionId,
          addedFiles,
          addedJars,
          task.localProperties,
          serializedTask)
      }
    } else {
      None
    }
  }

  private def maybeFinishTaskSet() {
    if (isZombie && runningTasks == 0) {
      sched.taskSetFinished(this)
      if (tasksSuccessful == numTasks) {
        blacklistTracker.foreach(_.updateBlacklistForSuccessfulTaskSet(
          taskSet.stageId,
          taskSet.stageAttemptId,
          taskSetBlacklistHelperOpt.get.execToFailures))
      }
    }
  }

  /**
   * Get the level we can launch tasks according to delay scheduling, based on current wait time.
   */
  private def getAllowedLocalityLevel(curTime: Long): TaskLocality.TaskLocality = {
    // Remove the scheduled or finished tasks lazily
    def tasksNeedToBeScheduledFrom(pendingTaskIds: ArrayBuffer[Int]): Boolean = {
      var indexOffset = pendingTaskIds.size
      while (indexOffset > 0) {
        indexOffset -= 1
        val index = pendingTaskIds(indexOffset)
        if (copiesRunning(index) == 0 && !successful(index)) {
          return true
        } else {
          pendingTaskIds.remove(indexOffset)
        }
      }
      false
    }
    // Walk through the list of tasks that can be scheduled at each location and returns true
    // if there are any tasks that still need to be scheduled. Lazily cleans up tasks that have
    // already been scheduled.
    def moreTasksToRunIn(pendingTasks: HashMap[String, ArrayBuffer[Int]]): Boolean = {
      val emptyKeys = new ArrayBuffer[String]
      val hasTasks = pendingTasks.exists {
        case (id: String, tasks: ArrayBuffer[Int]) =>
          if (tasksNeedToBeScheduledFrom(tasks)) {
            true
          } else {
            emptyKeys += id
            false
          }
      }
      // The key could be executorId, host or rackId
      emptyKeys.foreach(id => pendingTasks.remove(id))
      hasTasks
    }

    while (currentLocalityIndex < myLocalityLevels.length - 1) {
      val moreTasks = myLocalityLevels(currentLocalityIndex) match {
        case TaskLocality.PROCESS_LOCAL => moreTasksToRunIn(pendingTasksForExecutor)
        case TaskLocality.NODE_LOCAL => moreTasksToRunIn(pendingTasksForHost)
        case TaskLocality.NO_PREF => pendingTasksWithNoPrefs.nonEmpty
        case TaskLocality.RACK_LOCAL => moreTasksToRunIn(pendingTasksForRack)
      }
      if (!moreTasks) {
        // This is a performance optimization: if there are no more tasks that can
        // be scheduled at a particular locality level, there is no point in waiting
        // for the locality wait timeout (SPARK-4939).
        lastLaunchTime = curTime
        logDebug(s"No tasks for locality level ${myLocalityLevels(currentLocalityIndex)}, " +
          s"so moving to locality level ${myLocalityLevels(currentLocalityIndex + 1)}")
        currentLocalityIndex += 1
      } else if (curTime - lastLaunchTime >= localityWaits(currentLocalityIndex)) {
        // Jump to the next locality level, and reset lastLaunchTime so that the next locality
        // wait timer doesn't immediately expire
        lastLaunchTime += localityWaits(currentLocalityIndex)
        logDebug(s"Moving to ${myLocalityLevels(currentLocalityIndex + 1)} after waiting for " +
          s"${localityWaits(currentLocalityIndex)}ms")
        currentLocalityIndex += 1
      } else {
        return myLocalityLevels(currentLocalityIndex)
      }
    }
    myLocalityLevels(currentLocalityIndex)
  }

  /**
   * Find the index in myLocalityLevels for a given locality. This is also designed to work with
   * localities that are not in myLocalityLevels (in case we somehow get those) by returning the
   * next-biggest level we have. Uses the fact that the last value in myLocalityLevels is ANY.
   */
  def getLocalityIndex(locality: TaskLocality.TaskLocality): Int = {
    var index = 0
    while (locality > myLocalityLevels(index)) {
      index += 1
    }
    index
  }

  /**
   * Check whether the given task set has been blacklisted to the point that it can't run anywhere.
   *
   * It is possible that this taskset has become impossible to schedule *anywhere* due to the
   * blacklist.  The most common scenario would be if there are fewer executors than
   * spark.task.maxFailures. We need to detect this so we can avoid the job from being hung.
   * We try to acquire new executor/s by killing an existing idle blacklisted executor.
   *
   * There's a tradeoff here: we could make sure all tasks in the task set are schedulable, but that
   * would add extra time to each iteration of the scheduling loop. Here, we take the approach of
   * making sure at least one of the unscheduled tasks is schedulable. This means we may not detect
   * the hang as quickly as we could have, but we'll always detect the hang eventually, and the
   * method is faster in the typical case. In the worst case, this method can take
   * O(maxTaskFailures + numTasks) time, but it will be faster when there haven't been any task
   * failures (this is because the method picks one unscheduled task, and then iterates through each
   * executor until it finds one that the task isn't blacklisted on).
   */
  private[scheduler] def getCompletelyBlacklistedTaskIfAny(
      hostToExecutors: HashMap[String, HashSet[String]]): Option[Int] = {
    taskSetBlacklistHelperOpt.flatMap { taskSetBlacklist =>
      val appBlacklist = blacklistTracker.get
      // Only look for unschedulable tasks when at least one executor has registered. Otherwise,
      // task sets will be (unnecessarily) aborted in cases when no executors have registered yet.
      if (hostToExecutors.nonEmpty) {
        // find any task that needs to be scheduled
        val pendingTask: Option[Int] = {
          // usually this will just take the last pending task, but because of the lazy removal
          // from each list, we may need to go deeper in the list.  We poll from the end because
          // failed tasks are put back at the end of allPendingTasks, so we're more likely to find
          // an unschedulable task this way.
          val indexOffset = allPendingTasks.lastIndexWhere { indexInTaskSet =>
            copiesRunning(indexInTaskSet) == 0 && !successful(indexInTaskSet)
          }
          if (indexOffset == -1) {
            None
          } else {
            Some(allPendingTasks(indexOffset))
          }
        }

        pendingTask.find { indexInTaskSet =>
          // try to find some executor this task can run on.  Its possible that some *other*
          // task isn't schedulable anywhere, but we will discover that in some later call,
          // when that unschedulable task is the last task remaining.
          hostToExecutors.forall { case (host, execsOnHost) =>
            // Check if the task can run on the node
            val nodeBlacklisted =
              appBlacklist.isNodeBlacklisted(host) ||
                taskSetBlacklist.isNodeBlacklistedForTaskSet(host) ||
                taskSetBlacklist.isNodeBlacklistedForTask(host, indexInTaskSet)
            if (nodeBlacklisted) {
              true
            } else {
              // Check if the task can run on any of the executors
              execsOnHost.forall { exec =>
                appBlacklist.isExecutorBlacklisted(exec) ||
                  taskSetBlacklist.isExecutorBlacklistedForTaskSet(exec) ||
                  taskSetBlacklist.isExecutorBlacklistedForTask(exec, indexInTaskSet)
              }
            }
          }
        }
      } else {
        None
      }
    }
  }

  private[scheduler] def abortSinceCompletelyBlacklisted(indexInTaskSet: Int): Unit = {
    taskSetBlacklistHelperOpt.foreach { taskSetBlacklist =>
      val partition = tasks(indexInTaskSet).partitionId
      abort(s"""
         |Aborting $taskSet because task $indexInTaskSet (partition $partition)
         |cannot run anywhere due to node and executor blacklist.
         |Most recent failure:
         |${taskSetBlacklist.getLatestFailureReason}
         |
         |Blacklisting behavior can be configured via spark.blacklist.*.
         |""".stripMargin)
    }
  }

  /**
   * Marks the task as getting result and notifies the DAG Scheduler
   */
  def handleTaskGettingResult(tid: Long): Unit = {
    val info = taskInfos(tid)
    info.markGettingResult(clock.getTimeMillis())
    sched.dagScheduler.taskGettingResult(info)
  }

  /**
   * Check whether has enough quota to fetch the result with `size` bytes
   */
  def canFetchMoreResults(size: Long): Boolean = sched.synchronized {
    totalResultSize += size
    calculatedTasks += 1
    if (maxResultSize > 0 && totalResultSize > maxResultSize) {
      val msg = s"Total size of serialized results of ${calculatedTasks} tasks " +
        s"(${Utils.bytesToString(totalResultSize)}) is bigger than ${config.MAX_RESULT_SIZE.key} " +
        s"(${Utils.bytesToString(maxResultSize)})"
      logError(msg)
      abort(msg)
      false
    } else {
      true
    }
  }

  /**
   * Marks a task as successful and notifies the DAGScheduler that the task has ended.
   */
  def handleSuccessfulTask(tid: Long, result: DirectTaskResult[_]): Unit = {
    val info = taskInfos(tid)
    val index = info.index
    // Check if any other attempt succeeded before this and this attempt has not been handled
    if (successful(index) && killedByOtherAttempt.contains(tid)) {
      // Undo the effect on calculatedTasks and totalResultSize made earlier when
      // checking if can fetch more results
      calculatedTasks -= 1
      val resultSizeAcc = result.accumUpdates.find(a =>
        a.name == Some(InternalAccumulator.RESULT_SIZE))
      if (resultSizeAcc.isDefined) {
        totalResultSize -= resultSizeAcc.get.asInstanceOf[LongAccumulator].value
      }

      // Handle this task as a killed task
      handleFailedTask(tid, TaskState.KILLED,
        TaskKilled("Finish but did not commit due to another attempt succeeded"))
      return
    }

    info.markFinished(TaskState.FINISHED, clock.getTimeMillis())
    if (speculationEnabled) {
      successfulTaskDurations.insert(info.duration)
    }
    removeRunningTask(tid)

    // Kill any other attempts for the same task (since those are unnecessary now that one
    // attempt completed successfully).
    for (attemptInfo <- taskAttempts(index) if attemptInfo.running) {
      logInfo(s"Killing attempt ${attemptInfo.attemptNumber} for task ${attemptInfo.id} " +
        s"in stage ${taskSet.id} (TID ${attemptInfo.taskId}) on ${attemptInfo.host} " +
        s"as the attempt ${info.attemptNumber} succeeded on ${info.host}")
      killedByOtherAttempt += attemptInfo.taskId
      sched.backend.killTask(
        attemptInfo.taskId,
        attemptInfo.executorId,
        interruptThread = true,
        reason = "another attempt succeeded")
    }
    if (!successful(index)) {
      tasksSuccessful += 1
      logInfo(s"Finished task ${info.id} in stage ${taskSet.id} (TID ${info.taskId}) in" +
        s" ${info.duration} ms on ${info.host} (executor ${info.executorId})" +
        s" ($tasksSuccessful/$numTasks)")
      // Mark successful and stop if all the tasks have succeeded.
      successful(index) = true
      if (tasksSuccessful == numTasks) {
        // clean up finished partitions for the stage when the active TaskSetManager succeed
        if (!isZombie) {
          sched.stageIdToFinishedPartitions -= stageId
          isZombie = true
        }
      }
    } else {
      logInfo("Ignoring task-finished event for " + info.id + " in stage " + taskSet.id +
        " because task " + index + " has already completed successfully")
    }
    // There may be multiple tasksets for this stage -- we let all of them know that the partition
    // was completed.  This may result in some of the tasksets getting completed.
    sched.markPartitionCompletedInAllTaskSets(stageId, tasks(index).partitionId, info)
    // This method is called by "TaskSchedulerImpl.handleSuccessfulTask" which holds the
    // "TaskSchedulerImpl" lock until exiting. To avoid the SPARK-7655 issue, we should not
    // "deserialize" the value when holding a lock to avoid blocking other threads. So we call
    // "result.value()" in "TaskResultGetter.enqueueSuccessfulTask" before reaching here.
    // Note: "result.value()" only deserializes the value when it's called at the first time, so
    // here "result.value()" just returns the value and won't block other threads.
    sched.dagScheduler.taskEnded(tasks(index), Success, result.value(), result.accumUpdates, info)
    maybeFinishTaskSet()
  }

  private[scheduler] def markPartitionCompleted(
      partitionId: Int,
      taskInfo: Option[TaskInfo]): Unit = {
    partitionToIndex.get(partitionId).foreach { index =>
      if (!successful(index)) {
        if (speculationEnabled && !isZombie) {
          taskInfo.foreach { info => successfulTaskDurations.insert(info.duration) }
        }
        tasksSuccessful += 1
        successful(index) = true
        if (tasksSuccessful == numTasks) {
          if (!isZombie) {
            sched.stageIdToFinishedPartitions -= stageId
            isZombie = true
          }
        }
        maybeFinishTaskSet()
      }
    }
  }

  /**
   * Marks the task as failed, re-adds it to the list of pending tasks, and notifies the
   * DAG Scheduler.
   */
  def handleFailedTask(tid: Long, state: TaskState, reason: TaskFailedReason) {
    val info = taskInfos(tid)
    if (info.failed || info.killed) {
      return
    }
    removeRunningTask(tid)
    info.markFinished(state, clock.getTimeMillis())
    val index = info.index
    copiesRunning(index) -= 1
    var accumUpdates: Seq[AccumulatorV2[_, _]] = Seq.empty
    val failureReason = s"Lost task ${info.id} in stage ${taskSet.id} (TID $tid, ${info.host}," +
      s" executor ${info.executorId}): ${reason.toErrorString}"
    val failureException: Option[Throwable] = reason match {
      case fetchFailed: FetchFailed =>
        logWarning(failureReason)
        if (!successful(index)) {
          successful(index) = true
          tasksSuccessful += 1
        }
        isZombie = true

        if (fetchFailed.bmAddress != null) {
          blacklistTracker.foreach(_.updateBlacklistForFetchFailure(
            fetchFailed.bmAddress.host, fetchFailed.bmAddress.executorId))
        }

        None

      case ef: ExceptionFailure =>
        // ExceptionFailure's might have accumulator updates
        accumUpdates = ef.accums
        if (ef.className == classOf[NotSerializableException].getName) {
          // If the task result wasn't serializable, there's no point in trying to re-execute it.
          logError("Task %s in stage %s (TID %d) had a not serializable result: %s; not retrying"
            .format(info.id, taskSet.id, tid, ef.description))
          abort("Task %s in stage %s (TID %d) had a not serializable result: %s".format(
            info.id, taskSet.id, tid, ef.description))
          return
        }
        val key = ef.description
        val now = clock.getTimeMillis()
        val (printFull, dupCount) = {
          if (recentExceptions.contains(key)) {
            val (dupCount, printTime) = recentExceptions(key)
            if (now - printTime > EXCEPTION_PRINT_INTERVAL) {
              recentExceptions(key) = (0, now)
              (true, 0)
            } else {
              recentExceptions(key) = (dupCount + 1, printTime)
              (false, dupCount + 1)
            }
          } else {
            recentExceptions(key) = (0, now)
            (true, 0)
          }
        }
        if (printFull) {
          logWarning(failureReason)
        } else {
          logInfo(
            s"Lost task ${info.id} in stage ${taskSet.id} (TID $tid) on ${info.host}, executor" +
              s" ${info.executorId}: ${ef.className} (${ef.description}) [duplicate $dupCount]")
        }
        ef.exception

      case tk: TaskKilled =>
        // TaskKilled might have accumulator updates
        accumUpdates = tk.accums
        logWarning(failureReason)
        None

      case e: ExecutorLostFailure if !e.exitCausedByApp =>
        logInfo(s"Task $tid failed because while it was being computed, its executor " +
          "exited for a reason unrelated to the task. Not counting this failure towards the " +
          "maximum number of failures for the task.")
        None

      case e: TaskFailedReason =>  // TaskResultLost and others
        logWarning(failureReason)
        None
    }

    if (tasks(index).isBarrier) {
      isZombie = true
    }

    sched.dagScheduler.taskEnded(tasks(index), reason, null, accumUpdates, info)

    if (!isZombie && reason.countTowardsTaskFailures) {
      assert (null != failureReason)
      taskSetBlacklistHelperOpt.foreach(_.updateBlacklistForFailedTask(
        info.host, info.executorId, index, failureReason))
      numFailures(index) += 1
      if (numFailures(index) >= maxTaskFailures) {
        logError("Task %d in stage %s failed %d times; aborting job".format(
          index, taskSet.id, maxTaskFailures))
        abort("Task %d in stage %s failed %d times, most recent failure: %s\nDriver stacktrace:"
          .format(index, taskSet.id, maxTaskFailures, failureReason), failureException)
        return
      }
    }

    if (successful(index)) {
      logInfo(s"Task ${info.id} in stage ${taskSet.id} (TID $tid) failed, but the task will not" +
        s" be re-executed (either because the task failed with a shuffle data fetch failure," +
        s" so the previous stage needs to be re-run, or because a different copy of the task" +
        s" has already succeeded).")
    } else {
      addPendingTask(index)
    }

    maybeFinishTaskSet()
  }

  def abort(message: String, exception: Option[Throwable] = None): Unit = sched.synchronized {
    // TODO: Kill running tasks if we were not terminated due to a Mesos error
    sched.dagScheduler.taskSetFailed(taskSet, message, exception)
    isZombie = true
    maybeFinishTaskSet()
  }

  /** If the given task ID is not in the set of running tasks, adds it.
   *
   * Used to keep track of the number of running tasks, for enforcing scheduling policies.
   */
  def addRunningTask(tid: Long) {
    if (runningTasksSet.add(tid) && parent != null) {
      parent.increaseRunningTasks(1)
    }
  }

  /** If the given task ID is in the set of running tasks, removes it. */
  def removeRunningTask(tid: Long) {
    if (runningTasksSet.remove(tid) && parent != null) {
      parent.decreaseRunningTasks(1)
    }
  }

  override def getSchedulableByName(name: String): Schedulable = {
    null
  }

  override def addSchedulable(schedulable: Schedulable) {}

  override def removeSchedulable(schedulable: Schedulable) {}

  override def getSortedTaskSetQueue(): ArrayBuffer[TaskSetManager] = {
    val sortedTaskSetQueue = new ArrayBuffer[TaskSetManager]()
    sortedTaskSetQueue += this
    sortedTaskSetQueue
  }

  /** Called by TaskScheduler when an executor is lost so we can re-enqueue our tasks */
  override def executorLost(execId: String, host: String, reason: ExecutorLossReason) {
    // Re-enqueue any tasks that ran on the failed executor if this is a shuffle map stage,
    // and we are not using an external shuffle server which could serve the shuffle outputs.
    // The reason is the next stage wouldn't be able to fetch the data from this dead executor
    // so we would need to rerun these tasks on other executors.
    if (tasks(0).isInstanceOf[ShuffleMapTask] && !env.blockManager.externalShuffleServiceEnabled
        && !isZombie) {
      for ((tid, info) <- taskInfos if info.executorId == execId) {
        val index = taskInfos(tid).index
        if (successful(index) && !killedByOtherAttempt.contains(tid)) {
          successful(index) = false
          copiesRunning(index) -= 1
          tasksSuccessful -= 1
          addPendingTask(index)
          // Tell the DAGScheduler that this task was resubmitted so that it doesn't think our
          // stage finishes when a total of tasks.size tasks finish.
          sched.dagScheduler.taskEnded(
            tasks(index), Resubmitted, null, Seq.empty, info)
        }
      }
    }
    for ((tid, info) <- taskInfos if info.running && info.executorId == execId) {
      val exitCausedByApp: Boolean = reason match {
        case exited: ExecutorExited => exited.exitCausedByApp
        case ExecutorKilled => false
        case _ => true
      }
      handleFailedTask(tid, TaskState.FAILED, ExecutorLostFailure(info.executorId, exitCausedByApp,
        Some(reason.toString)))
    }
    // recalculate valid locality levels and waits when executor is lost
    recomputeLocality()
  }

  /**
   * Check for tasks to be speculated and return true if there are any. This is called periodically
   * by the TaskScheduler.
   *
   */
  override def checkSpeculatableTasks(minTimeToSpeculation: Int): Boolean = {
    // Can't speculate if we only have one task, and no need to speculate if the task set is a
    // zombie or is from a barrier stage.
    if (isZombie || isBarrier || numTasks == 1) {
      return false
    }
    var foundTasks = false
    val minFinishedForSpeculation = (speculationQuantile * numTasks).floor.toInt
    logDebug("Checking for speculative tasks: minFinished = " + minFinishedForSpeculation)

    if (tasksSuccessful >= minFinishedForSpeculation && tasksSuccessful > 0) {
      val time = clock.getTimeMillis()
      val medianDuration = successfulTaskDurations.median
      val threshold = max(speculationMultiplier * medianDuration, minTimeToSpeculation)
      // TODO: Threshold should also look at standard deviation of task durations and have a lower
      // bound based on that.
      logDebug("Task length threshold for speculation: " + threshold)
      for (tid <- runningTasksSet) {
        val info = taskInfos(tid)
        val index = info.index
        if (!successful(index) && copiesRunning(index) == 1 && info.timeRunning(time) > threshold &&
          !speculatableTasks.contains(index)) {
          logInfo(
            "Marking task %d in stage %s (on %s) as speculatable because it ran more than %.0f ms"
              .format(index, taskSet.id, info.host, threshold))
          speculatableTasks += index
          sched.dagScheduler.speculativeTaskSubmitted(tasks(index))
          foundTasks = true
        }
      }
    }
    foundTasks
  }

  private def getLocalityWait(level: TaskLocality.TaskLocality): Long = {
    val localityWait = level match {
      case TaskLocality.PROCESS_LOCAL => config.LOCALITY_WAIT_PROCESS
      case TaskLocality.NODE_LOCAL => config.LOCALITY_WAIT_NODE
      case TaskLocality.RACK_LOCAL => config.LOCALITY_WAIT_RACK
      case _ => null
    }

    if (localityWait != null) {
      conf.get(localityWait)
    } else {
      0L
    }
  }

  /**
   * Compute the locality levels used in this TaskSet. Assumes that all tasks have already been
   * added to queues using addPendingTask.
   *
   */
  private def computeValidLocalityLevels(): Array[TaskLocality.TaskLocality] = {
    import TaskLocality.{PROCESS_LOCAL, NODE_LOCAL, NO_PREF, RACK_LOCAL, ANY}
    val levels = new ArrayBuffer[TaskLocality.TaskLocality]
    if (!pendingTasksForExecutor.isEmpty &&
        pendingTasksForExecutor.keySet.exists(sched.isExecutorAlive(_))) {
      levels += PROCESS_LOCAL
    }
    if (!pendingTasksForHost.isEmpty &&
        pendingTasksForHost.keySet.exists(sched.hasExecutorsAliveOnHost(_))) {
      levels += NODE_LOCAL
    }
    if (!pendingTasksWithNoPrefs.isEmpty) {
      levels += NO_PREF
    }
    if (!pendingTasksForRack.isEmpty &&
        pendingTasksForRack.keySet.exists(sched.hasHostAliveOnRack(_))) {
      levels += RACK_LOCAL
    }
    levels += ANY
    logDebug("Valid locality levels for " + taskSet + ": " + levels.mkString(", "))
    levels.toArray
  }

  def recomputeLocality() {
    val previousLocalityLevel = myLocalityLevels(currentLocalityIndex)
    myLocalityLevels = computeValidLocalityLevels()
    localityWaits = myLocalityLevels.map(getLocalityWait)
    currentLocalityIndex = getLocalityIndex(previousLocalityLevel)
  }

  def executorAdded() {
    recomputeLocality()
  }
}

private[spark] object TaskSetManager {
  // The user will be warned if any stages contain a task that has a serialized size greater than
  // this.
  val TASK_SIZE_TO_WARN_KIB = 100
}


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BitPacker
{
    public interface ICustomDeserializer
    {
        Type ContextType { get; }
        bool HasFixedSize { get; }
        int MinSize { get; }

        object Deserialize(BinaryReader reader, object context);
    }
}

redpajama_set_name: RedPajamaGithub


text:
<?php
namespace Spark\Middleware;

use Spark\Contract\MiddlewareInterface;
use Spark\Application;
use Spark\Exception\UnauthorizedException;
use Spark\Auth\JSONWebToken;

class AuthenticateMiddleware implements MiddlewareInterface
{
    public function handler(Application $app)
    {
        if ($app['request']->getMethod() === 'OPTIONS') {
            $app['response']->send();
            exit;
        }
        // 获取x-token
        $token = null;
        $faker = $app->getConfig()->auth->faker;
        if ($faker->enable) {
            $app['session']->set('auth', [
                    'uid' => $faker->uid,
                    'username' => $faker->username,
                    'expireAt' => 0,
                    'type' => 'faker',
                ]);
            return true;
        }
        $token = $app['request']->getHeader('X-TOKEN') ?: $app['request']->get('token');
        $tokenSaveItems = null;
        if ($token) {
            $tokenSaveItems = $app['jwt']->find($token);
        }

        if (isset($tokenSaveItems['uid']) && $tokenSaveItems['uid']) {
            $tokenSaveItems['type'] = 'jwt';
            $tokenSaveItems['token'] = $token;
            $app['session']->set('auth', $tokenSaveItems);
            return true;
        }

        // SESSION存在也通过
        // 并且限定当为faker时不通过
        if ($session = $app['session']->get('auth')) {
            if ($session['uid'] && $session['type'] != 'faker') {
                return true;
            }
        }
        throw new UnauthorizedException('Unauthorized!');
    }

}


text:
<!doctype html>

	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Forced touch mode</title>

	<link rel="stylesheet" href="../../codebase/dhtmlxgantt.css" type="text/css" media="screen" title="no title" charset="utf-8">


text:
phpunit-watchr
==============

A [NodeJS](http://nodejs.org) app to watch directories and run [PHPUnit](http://www.phpunit.de/) tests.

[![Build Status](https://travis-ci.org/jrcryer/phpunit-watchr.png)](https://travis-ci.org/jrcryer/phpunit-watchr)

*This project is no longer maintained.  Please see: https://github.com/SaschaGalley/grunt-phpunit to automate running your PHPUnit test runner.*

Installation
------------
The following command will install the application. Use `-g` to install as a global binary.

```sh
[sudo] npm install [-g] phpunit-watchr
```

Running The Server
------------------
Running the app is easy. If you installed globally, then starting the app is as easy as:

```sh
phpunit-watchr [path] [phpunit-configuration]
```

The path and phpunit-configuration will default to `./tests` and `--colors` respectively.

### Example usage

Setting path:

```sh
phpunit-watchr './php/tests'
```

Setting paths:

```sh
phpunit-watchr ./php/tests,./tests
```

Setting [PHPUnit configuration](http://www.phpunit.de/manual/current/en/appendixes.configuration.html):

```sh
phpunit-watchr './php/tests' '--strict --colors'
```

Future Features
---------------

* Notification integration

Licence
-------
This project is licensed under [MIT](https://github.com/jrcryer/phpunit-watchr/blob/master/LICENSE-MIT)
license.


text:
package org.teavm.classlib.java.util.stream.longimpl;

import java.util.Arrays;
import org.teavm.classlib.java.util.stream.TLongStream;

public class TLongStreamBuilder implements TLongStream.Builder {
    private long[] elements = new long[4];
    private int size;

    @Override
    public void accept(long t) {
        if (size == elements.length) {
            elements = Arrays.copyOf(elements, elements.length * 2);
        }
        elements[size++] = t;
    }

    @Override
    public TLongStream build() {
        return new TArrayLongStreamImpl(elements, 0, size);
    }
}


text:
package com.springsource.emergencias.domain;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import javax.validation.constraints.Size;
import java.util.Date;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.ManyToOne;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(sequenceName = "VISIT_SEQ", finders = { "findVisitsByDescriptionAndVisitDate", "findVisitsByVisitDateBetween", "findVisitsByDescriptionLike" })
public class Visit {

    /**
     */
    @Size(max = 255)
    private String description;

    /**
     */
    @NotNull
    @Past
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date visitDate;

    /**
     */
    @NotNull
    @ManyToOne
    private Pet pet;

    /**
     */
    @ManyToOne
    private Vet vet;
}


text:
package br.com.concretesolutions.canarinho.formatador;

import br.com.concretesolutions.canarinho.DigitoPara;
import br.com.concretesolutions.canarinho.validator.ValidadorBoleto;

/**
 * Transforma a linha digitável de um boleto em um código de boleto e vice-versa. Use o metodo {@link #formata(String)}
 * para transformar a linha digitavel em boleto e {@link #desformata(String)}.
 * Para verificar se um valor esta em linha digitável ou em boleto, usar os métodos:
 *

 *
{@link #estaFormatado(String)}: indicará se está em formata de boleto

 *
{@link #podeSerFormatado(String)}: indicará se é uma linha digitável

 *

 */
public final class FormatadorLinhaDigitavel implements Formatador {

    private FormatadorLinhaDigitavel() {
    }

    static FormatadorLinhaDigitavel getInstance() {
        return SingletonHolder.INSTANCE;
    }

    @Override
    public String formata(String value) {

        if (value == null || value.length() != 44) {
            throw new IllegalArgumentException("Linha digitável deve conter 44 caracteres. " + value);
        }

        if (value.startsWith("8")) {

            final String primeiroBloco = value.substring(0, 11);
            final String segundoBloco = value.substring(11, 22);
            final String terceiroBloco = value.substring(22, 33);
            final String quartoBloco = value.substring(33, 44);

            // o terceiro dígito é o de valor real que define se será mod 10 ou mod 11
            final boolean ehMod10 = value.charAt(2) == '6' || value.charAt(2) == '7';
            final DigitoPara mod = ehMod10 ? ValidadorBoleto.MOD_10 : ValidadorBoleto.MOD_11;

            final String primeiroDigito = mod.calcula(primeiroBloco);
            final String segundoDigito = mod.calcula(segundoBloco);
            final String terceiroDigito = mod.calcula(terceiroBloco);
            final String quartoDigito = mod.calcula(quartoBloco);

            return primeiroBloco + primeiroDigito + segundoBloco + segundoDigito
                    + terceiroBloco + terceiroDigito + quartoBloco + quartoDigito;
        }

        String primeiroBloco = value.substring(0, 4); // 4
        String segundoBloco = value.substring(4, 19); // 15
        String terceiroBloco = value.substring(19, 24); // 5
        String quartoBloco = value.substring(24, 34); // 10
        String quintoBloco = value.substring(34, 44); // 10

        // 1 - 3 - 4 - 5 - 2
        final StringBuilder codigoOrdenado = new StringBuilder(primeiroBloco)
                .append(terceiroBloco)
                .append(quartoBloco)
                .append(quintoBloco)
                .append(segundoBloco);

        primeiroBloco = codigoOrdenado.substring(0, 9);
        segundoBloco = codigoOrdenado.substring(9, 19);
        terceiroBloco = codigoOrdenado.substring(19, 29);
        quartoBloco = codigoOrdenado.substring(29);

        final String primeiroDigito = ValidadorBoleto.MOD_10.calcula(primeiroBloco);
        final String segundoDigito = ValidadorBoleto.MOD_10.calcula(segundoBloco);
        final String terceiroDigito = ValidadorBoleto.MOD_10.calcula(terceiroBloco);

        return primeiroBloco + primeiroDigito + segundoBloco + segundoDigito
                + terceiroBloco + terceiroDigito + quartoBloco;
    }

    @Override
    public String desformata(String valor) {

        if (valor == null || "".equals(valor)) {
            throw new IllegalArgumentException("Valor não pode estar nulo.");
        }

        String valorDesformatadao = Padroes.PADRAO_SOMENTE_NUMEROS.matcher(valor).replaceAll("");

        if (valorDesformatadao.charAt(0) == '8') {

            if (valorDesformatadao.length() != 48) {
                throw new IllegalArgumentException("Valor para boletos que iniciam com 8 deve conter 48 dígitos");
            }

            final StringBuilder builder = new StringBuilder(valorDesformatadao);

            final String primeiroBloco = builder.substring(0, 11);
            final String segundoBloco = builder.substring(12, 23);
            final String terceiroBloco = builder.substring(24, 35);
            final String quartoBloco = builder.substring(36, 47);

            return "" + primeiroBloco + segundoBloco + terceiroBloco + quartoBloco;
        }

        if (valorDesformatadao.length() != 47) {
            throw new IllegalArgumentException("Valor para boletos deve conter 47 digitos");
        }

        String primeiroBloco = valorDesformatadao.substring(0, 9);
        String segundoBloco = valorDesformatadao.substring(10, 20);
        String terceiroBloco = valorDesformatadao.substring(21, 31);
        String quartoBloco = valorDesformatadao.substring(32, valorDesformatadao.length());

        final StringBuilder boletoOrdenado = new StringBuilder(primeiroBloco)
                .append(segundoBloco)
                .append(terceiroBloco)
                .append(quartoBloco);

        // 1 - 3 - 4 - 5 - 2
        primeiroBloco = boletoOrdenado.substring(0, 4); // 4
        segundoBloco = boletoOrdenado.substring(29, 44); // 15
        terceiroBloco = boletoOrdenado.substring(4, 9); // 5
        quartoBloco = boletoOrdenado.substring(9, 19); // 10
        final String quintoBloco = boletoOrdenado.substring(19, 29); // 10

        return "" + primeiroBloco + segundoBloco + terceiroBloco + quartoBloco + quintoBloco;
    }

    @Override
    public boolean estaFormatado(String value) {
        return Formatador.BOLETO.estaFormatado(value);
    }

    @Override
    public boolean podeSerFormatado(String value) {
        final String sanitizado = Padroes.PADRAO_SOMENTE_NUMEROS.matcher(value).replaceAll("");
        return sanitizado.length() == 44;
    }

    private static class SingletonHolder {
        private static final FormatadorLinhaDigitavel INSTANCE = new FormatadorLinhaDigitavel();
    }
}


text:
var express = require('express');
var router = express.Router();

var Promise = Promise || require ("bluebird");

var credentials = require("../credentials.js")
var knex = require("knex")({
    client: "pg",
    connection: credentials.PG_CON_STRING
});

var usersStore = require ("../my_node_modules/usersStore.js")(knex);

/* GET users listing. */
router.get('/', function(req, res, next) {
	usersStore.getUser()
	.then(function (rows){
		res.json(rows);
	});

});

module.exports = router;


text:
/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/*
    Created on : 05-Jul-2015, 19:02:40
    Author     : Peti
*/
.alignleft {
    text-align: left;
}
.alignright {
    text-align: right;
}

.productTable {
    width: 50%;
    border-collapse:collapse;
    border:1px solid #000000;
}

.productTableHeader{
    text-align:center;
    background:none repeat scroll 0 0 #B5B5B5;
    border-bottom:1px solid #000000;
    padding:2px;
}

.productTableOddRow{
    text-align:center;
    background:none repeat scroll 0 0 #FFFFFF;
}

.productTableEvenRow{
    text-align:center;
    background:none repeat scroll 0 0 #D3D3D3;
}

.cartTable {
    width: 50%;
    border-collapse:collapse;
    border:1px solid #000000;
}

.cartTableHeader{
    text-align:center;
    background:none repeat scroll 0 0 #E5E5E5;
    border-bottom:1px solid #000000;
    padding:2px;
}

.cartTableFooter{
    text-align:center;
    font-weight: bold;
    background:none repeat scroll 0 0 #A5A5A5;
    border-bottom:1px solid #000000;
    padding:2px;
}

.cartTableOddRow{
    text-align:center;
    background:none repeat scroll 0 0 #FFFFFF;
}

.cartTableEvenRow{
    text-align:center;
    background:none repeat scroll 0 0 #E3E3E3;
}

.controlTable{
    width: 50%;
}

.controlNavigation,.controlFilterButton{
    width: 25%;
    text-align: left;
}

.controlUser{
    width: 25%;
    text-align: center;
}

.controlLogin,.controlFilterText{
    width: 25%;
    text-align: right;
}


text:
import * as Command from 'ember-cli/lib/models/command';
import * as SilentError from 'silent-error';
import { exec } from 'child_process';
import * as Promise from 'ember-cli/lib/ext/promise';
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as path from 'path';
import * as BuildTask from 'ember-cli/lib/tasks/build';
import * as win from 'ember-cli/lib/utilities/windows-admin';
import * as CreateGithubRepo from '../tasks/create-github-repo';

const fsReadFile = Promise.denodeify(fs.readFile);
const fsWriteFile = Promise.denodeify(fs.writeFile);
const fsReadDir = Promise.denodeify(fs.readdir);
const fsCopy = Promise.denodeify(fse.copy);

module.exports = Command.extend({
  name: 'github-pages:deploy',
  aliases: ['gh-pages:deploy'],
  description: 'Build the test app for production, commit it into a git branch, setup GitHub repo and push to it',
  works: 'insideProject',

  availableOptions: [
    {
      name: 'message',
      type: String,
      default: 'new gh-pages version',
      description: 'The commit message to include with the build, must be wrapped in quotes.'
    }, {
      name: 'environment',
      type: String,
      default: 'production',
      description: 'The Angular environment to create a build for'
    }, {
      name: 'branch',
      type: String,
      default: 'gh-pages',
      description: 'The git branch to push your pages to'
    }, {
      name: 'skip-build',
      type: Boolean,
      default: false,
      description: 'Skip building the project before deploying'
    }, {
      name: 'gh-token',
      type: String,
      default: '',
      description: 'Github token'
    }, {
      name: 'gh-username',
      type: String,
      default: '',
      description: 'Github username'
    }],

  run: function(options, rawArgs) {
    var ui = this.ui;
    var root = this.project.root;
    var execOptions = {
      cwd: root
    };
    var projectName = this.project.pkg.name;

    let initialBranch;

    // declared here so that tests can stub exec
    const execPromise = Promise.denodeify(exec);

    var buildTask = new BuildTask({
      ui: this.ui,
      analytics: this.analytics,
      project: this.project
    });

    var buildOptions = {
      environment: options.environment,
      outputPath: 'dist/'
    };

    var createGithubRepoTask = new CreateGithubRepo({
      ui: this.ui,
      analytics: this.analytics,
      project: this.project
    });

    var createGithubRepoOptions = {
      projectName,
      ghUsername: options.ghUsername,
      ghToken: options.ghToken
    };

    return checkForPendingChanges()
      .then(build)
      .then(saveStartingBranchName)
      .then(createGitHubRepoIfNeeded)
      .then(checkoutGhPages)
      .then(copyFiles)
      .then(updateBaseHref)
      .then(addAndCommit)
      .then(returnStartingBranch)
      .then(pushToGitRepo)
      .then(printProjectUrl)
      .catch(failGracefully);

    function checkForPendingChanges() {
      return execPromise('git status --porcelain')
        .then(stdout => {
          if (/\w+/m.test(stdout)) {
            let msg = 'Uncommitted file changes found! Please commit all changes before deploying.';
            return Promise.reject(new SilentError(msg));
          }
        });
    }

    function build() {
      if (options.skipBuild) return Promise.resolve();
      return win.checkWindowsElevation(ui)
        .then(() => buildTask.run(buildOptions));
    }

    function saveStartingBranchName() {
      return execPromise('git rev-parse --abbrev-ref HEAD')
        .then((stdout) => initialBranch = stdout.replace(/\s/g, ''));
    }

    function createGitHubRepoIfNeeded() {
      return execPromise('git remote -v')
        .then(function(stdout) {
          if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
            return createGithubRepoTask.run(createGithubRepoOptions)
              .then(() => execPromise(`git push -u origin ${initialBranch}`));
          }
        });
    }

    function checkoutGhPages() {
      return execPromise(`git checkout ${options.branch}`)
        .catch(createGhPagesBranch)
    }

    function createGhPagesBranch() {
      return execPromise(`git checkout --orphan ${options.branch}`)
        .then(() => execPromise('git rm --cached -r .', execOptions))
        .then(() => execPromise('git add .gitignore', execOptions))
        .then(() => execPromise('git clean -f -d', execOptions))
        .then(() => execPromise(`git commit -m \"initial ${options.branch} commit\"`));
    }

    function copyFiles() {
      return fsReadDir('dist')
        .then((files) => Promise.all(files.map((file) => fsCopy(path.join('dist', file), path.join('.', file)))))
    }

    function updateBaseHref() {
      let indexHtml = path.join(root, 'index.html');
      return fsReadFile(indexHtml, 'utf8')
        .then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/>"`))
        .then((data) => fsWriteFile(indexHtml, data, 'utf8'));
    }

    function addAndCommit() {
      return execPromise('git add .', execOptions)
        .then(() => execPromise(`git commit -m "${options.message}"`))
        .catch(() => Promise.reject(new SilentError('No changes found. Deployment skipped.')));
    }

    function returnStartingBranch() {
      return execPromise(`git checkout ${initialBranch}`);
    }

    function pushToGitRepo(committed) {
      return execPromise(`git push origin ${options.branch}`);
    }

    function printProjectUrl() {
      return execPromise('git remote -v')
        .then((stdout) => {
          let userName = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m)[1].toLowerCase();
          ui.writeLine(chalk.green(`Deployed! Visit https://${userName}.github.io/${projectName}/`));
          ui.writeLine('Github pages might take a few minutes to show the deployed site.');
        });
    }

    function failGracefully(error) {
      if (error && (/git clean/.test(error.message) || /Permission denied/.test(error.message))) {
        ui.writeLine(error.message);
        let msg = 'There was a permissions error during git file operations, please close any open project files/folders and try again.';
        msg += `\nYou might also need to return to the ${initialBranch} branch manually.`;
        return Promise.reject(new SilentError(msg));
      } else {
        return Promise.reject(error);
      }
    }
  }
});


text:
<?php

namespace INFT\prosgaBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class RegistroFPAdmin extends Admin
{
    /**
     * @param DatagridMapper $datagridMapper
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('codigoDeFicha')
            ->add('nombre')
            ->add('ubicacionArchivoFisico')
            ->add('ubicacionArchivoDigital')
            ->add('tiempoEnArchivo')
            ->add('observacion')
        ;
    }

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('codigoDeFicha')
            ->add('nombre')
            ->add('ubicacionArchivoFisico')
            ->add('ubicacionArchivoDigital')
            ->add('tiempoEnArchivo', 'date', array('format' => 'd/m/Y'))
            ->add('observacion')
            ->add('_action', 'actions', array('label' => 'Acciones',
                'actions' => array(
                    'show' => array(),
                    'edit' => array(),
                    'delete' => array(),
                )
            ))
        ;
    }

    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('fichaDeProceso', 'entity', array('class' => 'prosgaBundle:FichaDeProceso'))
            ->add('personaResponsable', 'entity', array('class' => 'prosgaBundle:Persona', 'label' => 'Responsable'))
            ->add('codigoDeFicha')
            ->add('nombre')
            ->add('ubicacionArchivoFisico', 'text', array('required' => false))
            ->add('ubicacionArchivoDigital', 'text', array('required' => false))
            ->add('tiempoEnArchivo', 'date', array('data' => new \DateTime('now')))
            ->add('observacion', 'textarea', array('label' => 'Observaciones',
                                                   'required' => false))
        ;
    }

    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('codigoDeFicha')
            ->add('nombre')
            ->add('ubicacionArchivoFisico')
            ->add('ubicacionArchivoDigital')
            ->add('tiempoEnArchivo', 'date', array('format' => 'd/m/Y'))
            ->add('observacion')
        ;
    }
}


text:
function [olib] = add_struct(ilib, istruc);
%function [olib] = add_struct(ilib, istruc);
%
% Add structures to gds_library objects
%
% ilib :    a gds_library object
% istruc :  a gds_structure object or a cell array of gds_structure
%           objects
% olib :    gds_library containing old and new structures

% test arguments
if nargin < 2
   error('add : missing argument.');
end

% copy input to output
olib = ilib;

% and add the new element
if isa(istruc, 'gds_structure');
   olib.st{end+1} = istruc;
elseif iscell(istruc)
   for k = 1:length(istruc)
      if ~isa(istruc{k}, 'gds_structure')
         error('gds_library.add :  input cell array member is not a gds_structure.');
      end
      olib.st{end+1} = istruc{k};
   end
else
   error('gds_structure.add : argument must be gds_structure or cell array.');
end

olib.numst = numel(olib.st);

return


text:
<!DOCTYPE html>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>digitaldesignweb</title>
<link rel="stylesheet" href="https://stackedit.io/res-min/themes/base.css" />

External Websites

Any views or opinions presented in this website are solely those of the author and do not necessarily represent those of the University of Nevada, Las Vegas and its employees, Dr. Mei Yang, or the various institutions listed here.  I gain nothing from your use of this site. So now I can say that your mileage may vary, I hold no responsibility or guarantee anything on this site or this site itself helping you in your courses or in life.

Visit

General Design

Doulos - Company specializing in verilog training, tons of useful resources

Main Page

Asic-World – Very Popular place to go

Verilog

Courses

ALL of these are really good, something to watch out for.

Ohio State University's Dr. Roger Traylor ECE 474

Verilog

SystemVerilog

Texas A&M University CSCE350 Verilog Resources Page

University of North Carolina at Chapel Hill's Prof. Montek Singh's

COMP 541: Digital Logic and Computer Design

Verilog & Digital Design

University of California, Berkeley's Dr. Fearing CS150

Really great and very important course

Course page

Cornell University's Dr. Bruce Land ECE 5760

Course Page

Univeristy of California, San Diego

Really great class

2010

2007

Lattice Semiconductors Application Note

University of Michigan's Dr. Mark Brehob EECS 270: Introduction to Logic Design

Course Page

Massachusetts Institute of Technology

6.884

6.375

Self Study

Verilog.com

FPGA

FPGA4Fun

Main Page

HDL Tutorials

HamsterworksFPGA

Main Page

Digilent

A prominent Xilinx reseller with a focus on university students

Learning Resources

University Student Resources

Shopping

Terasic

Analogous to Diligent except for Altera.

Shopping

Tutorial

Cummings

Sutherland and Cummings below are very important figures in the design community. They have published very important papers to the field of design, therefore they need their own sections.

Book Recommendations

Important Verilog Papers

Checkout the verilog papers, some are must reads.

Sutherland

Verilog Papers

Written with StackEdit.


text:
__author__ = 'valentin'

from tastypie.serializers import Serializer

from rdflib import Graph,Literal,BNode,Namespace,RDF,RDFS, URIRef, XSD

from rdflib.namespace import Namespace,DC,DCTERMS, FOAF
from rdflib import ConjunctiveGraph

class OrganizationAssociationFoafSerializer(Serializer):
    formats = ['json','jsonp','rdf']
    content_types = {
        'json':'application/json',
        'jsonp':'text/javascript',
        'rdf':'application/rdf+xml'

    }
    schemaOrg = Namespace('http://www.schema.org/')

# use schema.org to render  an organizational role from a person orientation (contains memberOf)
# http://schema.org/OrganizationRole
#  rendering a person and organization  is redundant

    def to_rdf(self, data, options=None):
        org = data.obj.organization #uri is in data['organization'] or obj.resource_uri
        person = data.obj.person #uri is in data['data'] or obj.resource_uri

        data = self.to_simple(data, options)

        #g = ConjunctiveGraph()
        g= Graph()
        g.bind('dc',DC)
        g.bind('dc',DCTERMS)
        g.bind('foaf',FOAF)
        g.bind('schema',self.schemaOrg)

        #personGraph = Graph(g.store,FOAF.person)
        #roleGraph  =   Graph(g.store,self.schemaOrg.OrganizationRole)
        personGraph = g
        roleGraph = g

        #topNode = BNode()

        #topNode.set(RDF.type, FOAF.Person) # .set replaces all other values
        #topNode.set(RDFS.label, Literal("org2"))

        #topNode = URIRef(data['resource_uri'])

        # if (data['person']):
        #     topNode = URIRef(data['person'])
        #     #personNode =URIRef(data['person'])
        #     personGraph.add( (topNode,RDF.type,FOAF.person) )
        #     personGraph.add( (topNode,FOAF.name,Literal( person.name )) )
        #     personGraph.add( (topNode,self.schemaOrg.sameas,URIRef(data['person'])))
        #
        # if (data['organization']):
        #     orgNode = URIRef(data['organization'])
        #     #personNode =URIRef(data['person'])
        #     personGraph.add( (orgNode,RDF.type,FOAF.organization) )
        #     personGraph.add( (orgNode,FOAF.name,Literal( org.name )) )
        #     personGraph.add( (orgNode,self.schemaOrg.sameas,URIRef(data['organization'])))

        roleNode = URIRef(data['resource_uri'])
        roleGraph.add( (roleNode,RDF.type,self.schemaOrg.OrganizationRole) )

        if(data['jobTitle']):
            roleGraph.add( (roleNode,self.schemaOrg.namedPosition,Literal(data['jobTitle']) ) )

        if (data['organization']):
            roleGraph.add( (roleNode,self.schemaOrg.name,Literal( org.name) ) )
            roleGraph.add( (roleNode,self.schemaOrg.memberOf,URIRef(data['organization'])))

        if (data['beginDate']):
            roleGraph.add( (roleNode,self.schemaOrg.startDate,Literal(data['beginDate'],datatype=XSD.Date)))
        if (data['endDate']):
            roleGraph.add( (roleNode,self.schemaOrg.endData,Literal(data['beginDate'],datatype=XSD.Date)))

        # add role to person
        #personGraph.add( (topNode, self.schemaOrg.memberOf,roleNode) )
        #personGraph.add( (orgNode, self.schemaOrg.member,roleNode) )
        # if ( data['businessAddress'] ):
        #     g.add( (topNode,self.schemaOrg.address,Literal( data['businessAddress'] ) ) )

        # if(data['jobTitle']):
        #      g.add( (topNode,FOAF.homepage,URIRef(data['jobTitle'])) )

        # if( data['logoUrl']):
        #     g.add ( (topNode,FOAF.img, URIRef(data['logoUrl'])) )

        #g.add( (topNode,FOAF.nick,Literal("name",lang='en') ) )

        return g.serialize(format='xml')

    pass


text:
@interface XPlaneDataSource : DataSource {

}

@end


text:
A MySQL query builder and ORM that is designed to work as part of the [JellyStyle
Framework](https://github.com/jellybeansoup/php-framework). It is designed to be
lightweight and unintrusive, and to allow far simple interactions with the database.

This library is, now and always, a work-in-progress. Using it in production
environments is not recommended at this stage.

##Released under the BSD License

Copyright © 2014 Daniel Farrelly

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

*	Redistributions of source code must retain the above copyright notice, this list
	of conditions and the following disclaimer.
*	Redistributions in binary form must reproduce the above copyright notice, this
	list of conditions and the following disclaimer in the documentation and/or
	other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


text:
package Log::Dispatch;

require 5.005;

use strict;
use vars qw[ $VERSION %LEVELS ];

use base qw( Log::Dispatch::Base );

use Carp ();

$VERSION = '2.18';

1;

BEGIN
{
    no strict 'refs';
    foreach my $l ( qw( debug info notice warning err error crit critical alert emerg emergency ) )
    {
	*{$l} = sub { my $self = shift;
		      $self->log( level => $l, message => "@_" ); };
	$LEVELS{$l} = 1;
    }
}

sub new
{
    my $proto = shift;
    my $class = ref $proto || $proto;
    my %p = @_;

    my $self = bless {}, $class;

    my @cb = $self->_get_callbacks(%p);
    $self->{callbacks} = \@cb if @cb;

    return $self;
}

sub add
{
    my $self = shift;
    my $object = shift;

    # Once 5.6 is more established start using the warnings module.
    if (exists $self->{outputs}{$object->name} && $^W)
    {
	Carp::carp("Log::Dispatch::* object ", $object->name, " already exists.");
    }

    $self->{outputs}{$object->name} = $object;
}

sub remove
{
    my $self = shift;
    my $name = shift;

    return delete $self->{outputs}{$name};
}

sub log
{
    my $self = shift;
    my %p = @_;

    return unless $self->would_log( $p{level} );

    $p{message} = $p{message}->()
        if ref $p{message} eq 'CODE';

    $p{message} = $self->_apply_callbacks(%p)
	if $self->{callbacks};

    foreach (keys %{ $self->{outputs} })
    {
	$p{name} = $_;
	$self->_log_to(%p);
    }
}

sub log_to
{
    my $self = shift;
    my %p = @_;

    $p{message} = $self->_apply_callbacks(%p)
	if $self->{callbacks};

    $self->_log_to(%p);
}

sub _log_to
{
    my $self = shift;
    my %p = @_;
    my $name = $p{name};

    if (exists $self->{outputs}{$name})
    {
	$self->{outputs}{$name}->log(@_);
    }
    elsif ($^W)
    {
	Carp::carp("Log::Dispatch::* object named '$name' not in dispatcher\n");
    }
}

sub output
{
    my $self = shift;
    my $name = shift;

    return unless exists $self->{outputs}{$name};

    return $self->{outputs}{$name};
}

sub level_is_valid
{
    shift;
    return $LEVELS{ shift() };
}

sub would_log
{
    my $self = shift;
    my $level = shift;

    return 0 unless $self->level_is_valid($level);

    foreach ( values %{ $self->{outputs} } )
    {
	return 1 if $_->_should_log($level);
    }

    return 0;
}

__END__

=head1 NAME

Log::Dispatch - Dispatches messages to one or more outputs

=head1 SYNOPSIS

  use Log::Dispatch;

  my $dispatcher = Log::Dispatch->new;

  $dispatcher->add( Log::Dispatch::File->new( name => 'file1',
                                              min_level => 'debug',
                                              filename => 'logfile' ) );

  $dispatcher->log( level => 'info',
                    message => 'Blah, blah' );

  my $sub = sub { my %p = @_;  return reverse $p{message}; };
  my $reversing_dispatcher = Log::Dispatch->new( callbacks => $sub );

=head1 DESCRIPTION

This module manages a set of Log::Dispatch::* objects, allowing you to
add and remove output objects as desired.

=head1 METHODS

=over 4

=item * new

Returns a new Log::Dispatch object.  This method takes one optional
parameter:

=over 8

=item * callbacks( \& or [ \&, \&, ... ] )

This parameter may be a single subroutine reference or an array
reference of subroutine references.  These callbacks will be called in
the order they are given and passed a hash containing the following keys:

 ( message => $log_message, level => $log_level )

In addition, any key/value pairs passed to a logging method will be
passed onto your callback.

The callbacks are expected to modify the message and then return a
single scalar containing that modified message.  These callbacks will
be called when either the C<log> or C<log_to> methods are called and
will only be applied to a given message once.  If they do not return
the message then you will get no output.  Make sure to return the
message!

=back

=item * add( Log::Dispatch::* OBJECT )

Adds a new a Log::Dispatch::* object to the dispatcher.  If an object
of the same name already exists, then that object is replaced.  A
warning will be issued if the C<$^W> is true.

NOTE: This method can really take any object that has methods called
'name' and 'log'.

=item * remove($)

Removes the object that matches the name given to the remove method.
The return value is the object being removed or undef if no object
matched this.

=item * log( level => $, message => $ or \& )

Sends the message (at the appropriate level) to all the
Log::Dispatch::* objects that the dispatcher contains (by calling the
C<log_to> method repeatedly).

This method also accepts a subroutine reference as the message
argument. This reference will be called only if there is an output
that will accept a message of the specified level.

B<WARNING>: This is the only logging method that does something
intelligent with a subroutine reference as the message. Other methods,
like C<log_to()> or the C<log()> method of an output object, will just
stringify the reference.

=item * log_to( name => $, level => $, message => $ )

Sends the message only to the named object.

=item * level_is_valid( $string )

Returns true or false to indicate whether or not the given string is a
valid log level.  Can be called as either a class or object method.

=item * would_log( $string )

Given a log level, returns true or false to indicate whether or not
anything would be logged for that log level.

=item * output( $name )

Returns an output of the given name.  Returns undef or an empty list,
depending on context, if the given output does not exist.

=back

=head1 CONVENIENCE METHODS

Version 1.6 of Log::Dispatch adds a number of convenience methods for
logging.  You may now call any valid log level (including valid
abbreviations) as a method on the Log::Dispatch object with a single
argument that is the message to be logged.  This is converted into a
call to the C<log> method with the appropriate level.

For example:

 $dispatcher->alert('Strange data in incoming request');

translates to:

 $dispatcher->log( level => 'alert', message => 'Strange data in incoming request' );

These methods act like Perl's C<print> built-in when given a list of
arguments.  Thus, the following calls are equivalent:

 my @array = ('Something', 'bad', 'is', here');
 $dispatcher->alert(@array);

 my $scalar = "@array";
 $dispatcher->alert($scalar);

One important caveat about these methods is that its not that forwards
compatible.  If I were to add more parameters to the C<log> call, it
is unlikely that these could be integrated into these methods without
breaking existing uses.  This probably means that any future
parameters to the C<log> method will never be integrated into these
convenience methods.  OTOH, I don't see any immediate need to expand
the parameters given to the C<log> method.

=head2 Log Levels

The log levels that Log::Dispatch uses are taken directly from the
syslog man pages (except that I expanded them to full words).  Valid
levels are:

 debug
 info
 notice
 warning
 error
 critical
 alert
 emergency

Alternately, the numbers 0 through 7 may be used (debug is 0 and
emergency is 7).  The syslog standard of 'err', 'crit', and 'emerg'
is also acceptable.

=head1 USAGE

This module is designed to be used as a one-stop logging system.  In
particular, it was designed to be easy to subclass so that if you want
to handle messaging in a way not implemented in this package, you
should be able to add this with minimal effort.

The basic idea behind Log::Dispatch is that you create a Log::Dispatch
object and then add various logging objects to it (such as a file
logger or screen logger).  Then you call the C<log> method of the
dispatch object, which passes the message to each of the objects,
which in turn decide whether or not to accept the message and what to
do with it.

This makes it possible to call single method and send a message to a
log file, via email, to the screen, and anywhere else, all with very
little code needed on your part, once the dispatching object has been
created.

The logging levels that Log::Dispatch uses are borrowed from the
standard UNIX syslog levels, except that where syslog uses partial
words ("err") Log::Dispatch also allows the use of the full word as
well ("error").

=head2 Making your own logging objects

Making your own logging object is generally as simple as subclassing
Log::Dispatch::Output and overriding the C<new> and C<log> methods.
See the L<Log::Dispatch::Output> docs for more details.

If you would like to create your own subclass for sending email then
it is even simpler.  Simply subclass L<Log::Dispatch::Email> and
override the C<send_email> method.  See the L<Log::Dispatch::Email>
docs for more details.

=head2 Why doesn't Log::Dispatch add a newline to the message?

A few people have written email to me asking me to add something that
would tack a newline onto the end of all messages that don't have one.
This will never happen.  There are several reasons for this.  First of
all, Log::Dispatch was designed as a simple system to broadcast a
message to multiple outputs.  It does not attempt to understand the
message in any way at all.  Adding a newline implies an attempt to
understand something about the message and I don't want to go there.
Secondly, this is not very cross-platform and I don't want to go down
the road of testing Config values to figure out what to tack onto
messages based on OS.

I think people's desire to do this is because they are too focused on
just the logging to files aspect of this module.  In this case
newlines make sense.  However, imagine someone is using this module to
log to a remote server and the interactions between the client and
server use newlines as part of the control flow.  Casually adding a
newline could cause serious problems.

However, the 1.2 release adds the callbacks parameter for the
Log::Dispatch object which you can easily use to add newlines to
messages if you so desire.

=head1 RELATED MODULES

=head2 Log::Dispatch::DBI

Written by Tatsuhiko Miyagawa.  Log output to a database table.

=head2 Log::Dispatch::FileRotate

Written by Mark Pfeiffer.  Rotates log files periodically as part of
its usage.

=head2 Log::Dispatch::File::Stamped

Written by Eric Cholet.  Stamps log files with date and time
information.

=head2 Log::Dispatch::Jabber

Written by Aaron Straup Cope.  Logs messages via Jabber.

=head2 Log::Dispatch::Tk

Written by Dominique Dumont.  Logs messages to a Tk window.

=head2 Log::Dispatch::Win32EventLog

Written by Arthur Bergman.  Logs messages to the Windows event log.

=head2 Log::Log4perl

An implementation of Java's log4j API in Perl, using Log::Dispatch to
do the actual logging.  Created by Mike Schilli and Kevin Goess.

=head2 Log::Dispatch::Config

Written by Tatsuhiko Miyagawa.  Allows configuration of logging via a
text file similar (or so I'm told) to how it is done with log4j.
Simpler than Log::Log4perl.

=head2 Log::Agent

A very different API for doing many of the same things that
Log::Dispatch does.  Originally written by Raphael Manfredi.

=head1 SUPPORT

Please submit bugs and patches to the CPAN RT system at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log%3A%3ADispatch
or via email at bug-log-dispatch@rt.cpan.org.

Support questions can be sent to me at my email address, shown below.

The code repository is at https://svn.urth.org/svn/Log-Dispatch/

=head1 AUTHOR

Dave Rolsky, <autarch@urth.org>

=head1 COPYRIGHT

Copyright (c) 1999-2006 David Rolsky.  All rights reserved.  This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included
with this module.

=head1 SEE ALSO

Log::Dispatch::ApacheLog, Log::Dispatch::Email,
Log::Dispatch::Email::MailSend, Log::Dispatch::Email::MailSender,
Log::Dispatch::Email::MailSendmail, Log::Dispatch::Email::MIMELite,
Log::Dispatch::File, Log::Dispatch::File::Locked,
Log::Dispatch::Handle, Log::Dispatch::Output, Log::Dispatch::Screen,
Log::Dispatch::Syslog

=cut


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

#endregion //Using Directives

namespace Figlut.MonoDroid.DataBox.Utilities
{
	public class LoginCredentials
	{
		#region Constructors

		public LoginCredentials(string userName, string password)
		{
			_userName = userName;
			_password = password;
		}

		#endregion //Constructors

		#region Fields

		private string _userName;
		private string _password;

		#endregion //Fields

		#region Properies

		public string UserName
		{
			get{ return _userName; }
		}

		public string Password
		{
			get{ return _password; }
		}

		#endregion //Properties
	}
}


text:
import React from 'react';
import configureStore from 'redux-mock-store';
import sinon from 'sinon';
import expect from 'expect';
import thunk from 'redux-thunk';
import { shallow, mount } from 'enzyme';

import LoginForm from '../components/LoginForm';
import LoginContainer from '../components/Login';
import Preloader from '../components/Preloader';

const initialLoginState = {
  login: {
    formState: {
      username: '',
      password: ''
    },
    error: '',
    sending: false,
    loggedIn: false,
    currentlySending: false
  }
};

const push = jest.fn();
const history = {
  push
};
const match = {
  params: 1
};

const FakeErrorMessage = React.createClass({
  render: () =>
Error Message
,
});

const FakeLoginForm = React.createClass({
  render: () =>
Login Form
,
});

const FakePreloader = React.createClass({
  render: () =>
Login Form
,
});

LoginForm.__Rewire__('ErrorMessage', FakeErrorMessage);
LoginContainer.__Rewire__('LoginForm', FakeLoginForm);
LoginContainer.__Rewire__('Preloader', FakePreloader);

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

describe('Login Form component', () => {
  const data = {
    username: 'albert',
    password: 'albert'
  };
  const onSubmit = jest.fn();
  const dispatch = jest.fn();

  it('should have four(4) div html element', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find('div').length).toEqual(4);
  });

  it('should have one(1) form html element', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find('form').length).toEqual(1);
  });

  it('should have two(2) form html element', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find('input').length).toEqual(2);
  });

  it('should have one(1) button html element', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find('button').length).toEqual(1);
  });

  it('should have one(1) Link react component', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find('Link').length).toEqual(1);
  });

  it('should call onSubmit() when form is submitted', () => {
    const preventDefault = jest.fn();
    const event = {
      preventDefault
    };
    sinon.stub(document, 'getElementById');
    document.getElementById.withArgs('username').returns('');
    document.getElementById.withArgs('password').returns('');
    sinon.spy(LoginForm.prototype, 'onSubmit');
    const wrapper = shallow(<LoginForm
      data={data}
      error=''
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    wrapper.find('form').simulate('submit', event);
    expect(LoginForm.prototype.onSubmit.calledOnce).toEqual(true);
  });

  it('should have one(1) ErrorMessage component', () => {
    const wrapper = shallow(<LoginForm
      data={data}
      error='Type Error'
      onSubmit={onSubmit}
      dispatch={dispatch}
    />);
    expect(wrapper.find(FakeErrorMessage).length).toEqual(1);
  });
});

describe('Login Containers', () => {
  const store = mockStore(initialLoginState);
  it('should have one(4) div html', () => {
    const wrapper = mount(<LoginContainer store={store} history={history} match={match} />);
    expect(wrapper.find('div').length).toEqual(4);
  });

  it('should have one(1) LoginForm component', () => {
    const wrapper = mount(<LoginContainer store={store} history={history} match={match} />);
    expect(wrapper.find(FakeLoginForm).length).toEqual(1);
  });

  it('should have one(1) ErrorMessage component', () => {
    const errorState = {
      login: {
        formState: {
          username: '',
          password: ''
        },
        error: '',
        sending: false,
        loggedIn: false,
        currentlySending: true
      }
    };
    const errorStore = mockStore(errorState);
    const wrapper = mount(<LoginContainer store={errorStore} history={history} match={match} />);
    expect(wrapper.find(FakePreloader).length).toEqual(1);
  });

  it('should call componentWillReceiveProps once', () => {
    const nextProps = {
      data: {
        login: {
          username: 'albert',
          password: 'Machine Learning',
          loggedIn: true
        }
      }
    };
    sinon.spy(LoginContainer.prototype, 'componentWillReceiveProps');
    const wrapper = mount(<LoginContainer store={store} history={history} match={match} />);
    const instance = wrapper.instance();
    instance.componentWillReceiveProps(nextProps);
    expect(LoginContainer.prototype.componentWillReceiveProps.calledOnce).toEqual(true);
    // expect(instance).toBeInstanceOf(wrapper);
  });
});


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-parent</artifactId>
    <version>7.7.0-SNAPSHOT</version>
  </parent>
  <groupId>org.activiti</groupId>
  <artifactId>activiti-dependencies</artifactId>
  <packaging>pom</packaging>
  <name>Activiti :: BOM (Bill of Materials)</name>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-api-dependencies</artifactId>
        <version>${project.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-core-common-dependencies</artifactId>
        <version>${project.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-core-dependencies</artifactId>
        <version>${project.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-api-dependencies</artifactId>
      <version>${project.version}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-core-common-dependencies</artifactId>
      <version>${project.version}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-core-dependencies</artifactId>
      <version>${project.version}</version>
      <type>pom</type>
    </dependency>
  </dependencies>
</project>


text:
package cn.hybris.controller.test;

public interface TestService
{
	void speak(String a);
}


text:
#define SetInteractorStyleMacro(style) \
void SetInteractorStyleTo##style () { \
	if (CurrentStyle != ##style) { \
		if (this->CurrentStyle) { \
			this->CurrentStyle->SetInteractor(0); \
	} \
		this->CurrentStyle =(vtkInteractorStyle*) this->##style;\
	} \
	if (this->CurrentStyle) { \
		this->CurrentStyle->SetInteractor(this->Interactor);\
		this->CurrentStyle->SetTDxStyle(this->TDxStyle);\
	} \
	this->InternalUpdate();\
}

#define CurrentStyleMacro(style) \
bool CurrentStyleIs##style () {\
	return CurrentStyle == ##style; \
}

#define SetInteractorStyle3DMacro(style) \
void SetInteractorStyleTo3D##style () { \
	if (!this->CurrentStyleIs3D##style ()) { \
		if (this->CurrentStyle) { \
			this->CurrentStyle->SetInteractor(0); \
	} \
		this->CurrentStyle = this->##style;\
	} \
	if (this->CurrentStyle) { \
		this->CurrentStyle->SetInteractor(this->Interactor);\
		this->CurrentStyle->SetTDxStyle(this->TDxStyle);\
	} \
	this->InternalUpdate();\
}

#define CurrentStyle3DMacro(style) \
bool CurrentStyleIs3D##style () {\
	return CurrentStyle == ##style; \
}


text:
'use strict';

var path   = require('path');
var fs     = require('fs');
var esNext = require('broccoli-traceur');

function TraceurPlugin(options) {
  this.name = 'ember-cli-traceur';
  this.ext = 'js';
  this.options = options || {};
}

TraceurPlugin.prototype.toTree = function(tree) {
  return esNext(tree, this.options);
};

function EmberTraceur(project) {
  this.project = project;
  this.name    = 'Ember CLI Traceur';
}

EmberTraceur.prototype.treeFor = function treeFor() {
};

EmberTraceur.prototype.included = function included(app) {
  var registry = app.registry;
  this.app = app;

  var plugin = new TraceurPlugin(this.app.options.esnextOptions);

  registry.add('js', plugin);
};

module.exports = EmberTraceur;


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:s="urn:java:ee"
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

	<interceptors>
		<class>com.sysv.pedidovenda.util.jpa.TransactionInterceptor</class>
	</interceptors>

</beans>


text:
var express =require ('express') ;
var request =require ('request') ;
var bodyParser =require ('body-parser') ;

// http://garann.github.io/template-chooser/
var app =express () ;
//app.use (bodyParser.urlencoded ({ extended: false })) ;
app.use (bodyParser.json ()) ;
app.use (express.static (__dirname + '/../www')) ;
app.set ('view engine', 'ejs') ;

app.set ('port', process.env.PORT || 80) ;

module.exports =app ;


text:
class PostsController < ApplicationController
  respond_to :html, :json

  def index
    @posts = Post.published
    respond_with( @posts )
  end

  def show
    @post = Post.find( params[:id] )
    respond_with( @post )
  end
end