text:
package primefaces_intro;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class PrimeBean {

	int amount;

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

}


text:
package org.opencompare.analysis.analyzer

import de.fau.cs.osr.ptk.common.AstVisitor
import org.opencompare.api.java.io.ImportMatrix
import org.sweble.wikitext.parser.{WikitextParser, WikitextPreprocessor}
import org.sweble.wikitext.parser.nodes.{WtExternalLink, WtHorizontalRule, WtTemplate, _}
import org.sweble.wikitext.parser.utils.SimpleParserConfig

/**
  * Created by gbecan on 05/04/16.
  */
class TemplateAnalyzer extends AstVisitor[WtNode] with CompleteWikitextVisitorNoReturn {

  private val parserConfig = new SimpleParserConfig()
  private val preprocessor = new WikitextPreprocessor(parserConfig)
//  private val parser = new WikitextParser(parserConfig)

  private var results = Map.empty[String, Int]
  private var inTemplate = false
  private var templateName = ""

  def analyzeTemplates(importMatrix: ImportMatrix, title : String) : Map[String, Int] = {
    results = Map.empty[String, Int]

    for (row <- 0 until importMatrix.getNumberOfRows;
         column <- 0 until importMatrix.getNumberOfColumns) {
      val cell = Option(importMatrix.getCell(row, column))

      if (cell.isDefined) {
        val code = "{|\n" +
          "|-\n" +
          "| " +
          cell.get.getRawContent + "\n" +
          "|}"

        val ast = preprocessor.parseArticle(code, title)
        this.go(ast)
      }
    }

    results
  }

  override def visit(n: WtTable): Unit = iterate(n)

  override def visit(n: WtTemplate): Unit = {
    inTemplate = true
    templateName = ""
    dispatch(n.getName)
    results = results  + (templateName -> (results.getOrElse(templateName, 0) + 1))
    inTemplate = false
  }

  override def visit(n: WtTemplateArguments): Unit = iterate(n)

  override def visit(n: WtText): Unit = {
    if (inTemplate) {
      templateName = n.getContent
    }
  }

  override def visit(n: WtTableImplicitTableBody): Unit = iterate(n)

  override def visit(n: WtRedirect): Unit = iterate(n)

  override def visit(n: WtLinkOptionLinkTarget): Unit = iterate(n)

  override def visit(n: WtTableHeader): Unit = iterate(n)

  override def visit(n: WtTableRow): Unit = iterate(n)

  override def visit(n: WtTagExtension): Unit = iterate(n)

  override def visit(n: WtUnorderedList): Unit = iterate(n)

  override def visit(n: WtValue): Unit = iterate(n)

  override def visit(n: WtWhitespace): Unit = iterate(n)

  override def visit(n: WtXmlAttributes): Unit = iterate(n)

  override def visit(n: WtIgnored): Unit = iterate(n)

  override def visit(n: WtLinkOptionGarbage): Unit = iterate(n)

  override def visit(n: WtNewline): Unit = iterate(n)

  override def visit(n: WtPageName): Unit = iterate(n)

  override def visit(n: WtTemplateArgument): Unit = iterate(n)

  override def visit(n: WtXmlElement): Unit = iterate(n)

  override def visit(n: WtImageLink): Unit = iterate(n)

  override def visit(n: WtTemplateParameter): Unit = iterate(n)

  override def visit(n: WtHorizontalRule): Unit = iterate(n)

  override def visit(n: WtIllegalCodePoint): Unit = iterate(n)

  override def visit(n: WtLinkOptionKeyword): Unit = iterate(n)

  override def visit(n: WtLinkOptionResize): Unit = iterate(n)

  override def visit(n: WtXmlAttribute): Unit = iterate(n)

  override def visit(n: WtXmlEmptyTag): Unit = iterate(n)

  override def visit(n: WtXmlStartTag): Unit = iterate(n)

  override def visit(n: WtImStartTag): Unit = iterate(n)

  override def visit(n: WtImEndTag): Unit = iterate(n)

  override def visit(n: WtXmlEndTag): Unit = iterate(n)

  override def visit(n: WtXmlCharRef): Unit = iterate(n)

  override def visit(n: WtUrl): Unit = iterate(n)

  override def visit(n: WtTicks): Unit = iterate(n)

  override def visit(n: WtSignature): Unit = iterate(n)

  override def visit(n: WtPageSwitch): Unit = iterate(n)

  override def visit(n: WtTableCell): Unit = iterate(n)

  override def visit(n: WtTableCaption): Unit = iterate(n)

  override def visit(n: WtSection): Unit = iterate(n)

  override def visit(n: WtInternalLink): Unit = iterate(n)

  override def visit(n: WtExternalLink): Unit = iterate(n)

  override def visit(n: WtXmlEntityRef): Unit = iterate(n)

  override def visit(n: WtNodeList): Unit = iterate(n)

  override def visit(n: WtBody): Unit = iterate(n)

  override def visit(n: WtBold): Unit = iterate(n)

  override def visit(n: WtParsedWikitextPage): Unit = iterate(n)

  override def visit(n: WtOrderedList): Unit = iterate(n)

  override def visit(n: WtOnlyInclude): Unit = iterate(n)

  override def visit(n: WtName): Unit = iterate(n)

  override def visit(n: WtListItem): Unit = iterate(n)

  override def visit(n: WtLinkTitle): Unit = iterate(n)

  override def visit(n: WtLinkOptions): Unit = iterate(n)

  override def visit(n: WtLinkOptionAltText): Unit = iterate(n)

  override def visit(n: WtItalics): Unit = iterate(n)

  override def visit(n: WtHeading): Unit = iterate(n)

  override def visit(n: WtDefinitionListTerm): Unit = iterate(n)

  override def visit(n: WtDefinitionListDef): Unit = iterate(n)

  override def visit(n: WtDefinitionList): Unit = iterate(n)

  override def visit(n: WtPreproWikitextPage): Unit = iterate(n)

  override def visit(n: WtParagraph): Unit = iterate(n)

  override def visit(n: WtSemiPre): Unit = iterate(n)

  override def visit(n: WtSemiPreLine): Unit = iterate(n)

  override def visit(n: WtXmlComment): Unit = iterate(n)

  override def visit(n: WtXmlAttributeGarbage): Unit = iterate(n)

  override def visit(n: WtTagExtensionBody): Unit = iterate(n)
}

redpajama_set_name: RedPajamaGithub


text:
package cromwell.engine.workflow.workflowstore

import java.time.OffsetDateTime

import cats.data.NonEmptyList
import cats.syntax.apply._
import cats.instances.list._
import common.validation.ErrorOr.ErrorOr
import common.validation.Validation._
import cromwell.core.{HogGroup, WorkflowId, WorkflowOptions, WorkflowSourceFilesCollection}
import cromwell.database.sql.SqlConverters._
import cromwell.database.sql.{MetadataSqlDatabase, WorkflowStoreSqlDatabase}
import cromwell.database.sql.tables.WorkflowStoreEntry
import cromwell.engine.workflow.workflowstore.SqlWorkflowStore.WorkflowStoreAbortResponse.WorkflowStoreAbortResponse
import cromwell.engine.workflow.workflowstore.SqlWorkflowStore.WorkflowStoreState.WorkflowStoreState
import cromwell.engine.workflow.workflowstore.SqlWorkflowStore.{DuplicateWorkflowIdsRequested, NotInOnHoldStateException, WorkflowIdsAlreadyInUseException, WorkflowStoreAbortResponse, WorkflowStoreState, WorkflowSubmissionResponse}
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection._

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

object SqlWorkflowStore {
  case class WorkflowSubmissionResponse(state: WorkflowStoreState, id: WorkflowId)

  case class DuplicateWorkflowIdsRequested(workflowIds: Seq[WorkflowId]) extends
    Exception (s"Requested workflow IDs are duplicated: ${workflowIds.mkString(", ")}")

  case class WorkflowIdsAlreadyInUseException(workflowIds: Seq[WorkflowId]) extends
    Exception (s"Requested workflow IDs are already in use: ${workflowIds.mkString(", ")}")

  case class NotInOnHoldStateException(workflowId: WorkflowId) extends
    Exception(
      s"Couldn't change status of workflow $workflowId to " +
        "'Submitted' because the workflow is not in 'On Hold' state"
    )

  object WorkflowStoreState extends Enumeration {
    type WorkflowStoreState = Value
    val Submitted = Value("Submitted")
    val Running = Value("Running")
    val Aborting = Value("Aborting")
    val OnHold = Value("On Hold")
  }

  object WorkflowStoreAbortResponse extends Enumeration {
    type WorkflowStoreAbortResponse = Value
    val NotFound = Value("NotFound")
    val AbortedOnHoldOrSubmitted = Value("AbortedOnHoldOrSubmitted")
    val AbortRequested = Value("AbortRequested")
  }
}

case class SqlWorkflowStore(sqlDatabase: WorkflowStoreSqlDatabase, metadataSqlDatabase: MetadataSqlDatabase) extends WorkflowStore {
  /** This is currently hardcoded to success but used to do stuff, left in place for now as a useful
    *  startup initialization hook. */
  override def initialize(implicit ec: ExecutionContext): Future[Unit] = Future.successful(())

  override def abort(id: WorkflowId)(implicit ec: ExecutionContext): Future[WorkflowStoreAbortResponse] = {
    sqlDatabase.deleteOrUpdateWorkflowToState(
      workflowExecutionUuid = id.toString,
      workflowStateToDelete1 = WorkflowStoreState.OnHold.toString,
      workflowStateToDelete2 = WorkflowStoreState.Submitted.toString,
      workflowStateForUpdate = WorkflowStoreState.Aborting.toString
    ) map {
      case Some(true) =>
        WorkflowStoreAbortResponse.AbortedOnHoldOrSubmitted
      case Some(false) =>
        WorkflowStoreAbortResponse.AbortRequested
      case None =>
        WorkflowStoreAbortResponse.NotFound
    }
  }

  override def findWorkflows(cromwellId: String)(implicit ec: ExecutionContext): Future[Iterable[WorkflowId]] = {
    sqlDatabase.findWorkflows(cromwellId) map { _ map WorkflowId.fromString }
  }

  override def findWorkflowsWithAbortRequested(cromwellId: String)(implicit ec: ExecutionContext): Future[Iterable[WorkflowId]] = {
    sqlDatabase.findWorkflowsWithAbortRequested(cromwellId) map { _ map WorkflowId.fromString }
  }

  override def abortAllRunning()(implicit ec: ExecutionContext): Future[Unit] = {
    sqlDatabase.setStateToState(WorkflowStoreState.Running.toString, WorkflowStoreState.Aborting.toString)
  }

  override def stats(implicit ec: ExecutionContext): Future[Map[WorkflowStoreState, Int]] = {
    sqlDatabase.workflowStateCounts.map {
      _ map {
        case (key, value) => WorkflowStoreState.withName(key) -> value
      }
    }
  }

  /**
    * Retrieves up to n workflows which have not already been pulled into the engine and sets their pickedUp
    * flag to true
    */
  override def fetchStartableWorkflows(n: Int, cromwellId: String, heartbeatTtl: FiniteDuration, excludedGroups: Set[String])(implicit ec: ExecutionContext): Future[List[WorkflowToStart]] = {
    import cats.syntax.traverse._
    import common.validation.Validation._
    sqlDatabase.fetchWorkflowsInState(
      n,
      cromwellId,
      heartbeatTtl.ago,
      OffsetDateTime.now.toSystemTimestamp,
      WorkflowStoreState.Submitted.toString,
      WorkflowStoreState.Running.toString,
      WorkflowStoreState.OnHold.toString,
      excludedGroups
    ) map {
      // .get on purpose here to fail the future if something went wrong
      _.toList.traverse(fromWorkflowStoreEntry).toTry.get
    }
  }

  override def writeWorkflowHeartbeats(workflowIds: Set[(WorkflowId, OffsetDateTime)],
                                       heartbeatDateTime: OffsetDateTime)
                                      (implicit ec: ExecutionContext): Future[Int] = {
    val sortedWorkflowIds = workflowIds.toList sortBy(_._2) map (_._1.toString)
    sqlDatabase.writeWorkflowHeartbeats(sortedWorkflowIds, heartbeatDateTime.toSystemTimestamp)
  }

  def workflowAlreadyExists(workflowId: WorkflowId)(implicit ec: ExecutionContext): Future[Boolean] = {
    Future.sequence(Seq(
      sqlDatabase.checkWhetherWorkflowExists(workflowId.id.toString),
      metadataSqlDatabase.getWorkflowStatus(workflowId.id.toString).map(_.nonEmpty)
    )).map(_.exists(_ == true))
  }

  def findPreexistingWorkflowIds(workflowIds: Seq[WorkflowId])(implicit ec: ExecutionContext): Future[Seq[WorkflowId]] = {
    Future.sequence(workflowIds.map(wfid => {
      workflowAlreadyExists(wfid).map {
        case true => Option(wfid)
        case false => None
      }
    })).map { _.collect { case Some(existingId) => existingId } }
  }

  /**
    * Adds the requested WorkflowSourceFiles to the store and returns a WorkflowId for each one (in order)
    * for tracking purposes.
    */
  override def add(sources: NonEmptyList[WorkflowSourceFilesCollection])(implicit ec: ExecutionContext): Future[NonEmptyList[WorkflowSubmissionResponse]] = {

    val requestedWorkflowIds = sources.map(_.requestedWorkflowId).collect { case Some(id) => id }
    val duplicatedIds = requestedWorkflowIds.diff(requestedWorkflowIds.toSet.toSeq)

    if(duplicatedIds.nonEmpty) {
      Future.failed(DuplicateWorkflowIdsRequested(duplicatedIds))
    } else {
      findPreexistingWorkflowIds(requestedWorkflowIds) flatMap { preexistingIds =>
        if (preexistingIds.nonEmpty) {
          Future.failed(WorkflowIdsAlreadyInUseException(preexistingIds))
        } else {
          val asStoreEntries = sources map toWorkflowStoreEntry
          val returnValue = asStoreEntries map { workflowStore =>
            WorkflowSubmissionResponse(
              WorkflowStoreState.withName(workflowStore.workflowState),
              WorkflowId.fromString(workflowStore.workflowExecutionUuid)
            )
          }

          // The results from the Future aren't useful, so on completion map it into the precalculated return value instead. Magic!
          sqlDatabase.addWorkflowStoreEntries(asStoreEntries.toList) map { _ => returnValue }
        }
      }
    }
  }

  override def switchOnHoldToSubmitted(id: WorkflowId)(implicit ec: ExecutionContext): Future[Unit] = {
    for {
      updated <- sqlDatabase.updateWorkflowState(
        id.toString,
        WorkflowStoreState.OnHold.toString,
        WorkflowStoreState.Submitted.toString
      )
      _ <- if (updated == 0) Future.failed(NotInOnHoldStateException(id)) else Future.successful(())
    } yield ()
  }

  override def deleteFromStore(workflowId: WorkflowId)(implicit ec: ExecutionContext): Future[Int] = {
    sqlDatabase.removeWorkflowStoreEntry(workflowId.toString)
  }

  private def fromWorkflowStoreEntry(workflowStoreEntry: WorkflowStoreEntry): ErrorOr[WorkflowToStart] = {

    val workflowOptionsValidation: ErrorOr[WorkflowOptions] = WorkflowOptions.fromJsonString(workflowStoreEntry.workflowOptions.toRawString).toErrorOr
    val startableStateValidation = workflowStoreStateToStartableState(workflowStoreEntry)

    (startableStateValidation, workflowOptionsValidation) mapN { (startableState, workflowOptions) =>

      val id = WorkflowId.fromString(workflowStoreEntry.workflowExecutionUuid)

      val sources = WorkflowSourceFilesCollection(
        workflowSource = workflowStoreEntry.workflowDefinition.toRawStringOption,
        workflowUrl = workflowStoreEntry.workflowUrl,
        workflowRoot = workflowStoreEntry.workflowRoot,
        workflowType = workflowStoreEntry.workflowType,
        workflowTypeVersion = workflowStoreEntry.workflowTypeVersion,
        inputsJson = workflowStoreEntry.workflowInputs.toRawString,
        workflowOptions = workflowOptions,
        labelsJson = workflowStoreEntry.customLabels.toRawString,
        importsFile = workflowStoreEntry.importsZip.toBytesOption,
        warnings = Vector.empty,
        workflowOnHold = false,
        requestedWorkflowId = Option(id)
      )

      val hogGroup: HogGroup = workflowStoreEntry.hogGroup.map(HogGroup(_)).getOrElse(HogGroup.decide(workflowOptions, id))

      WorkflowToStart(
        id = id,
        submissionTime = workflowStoreEntry.submissionTime.toSystemOffsetDateTime,
        sources = sources,
        state = startableState,
        hogGroup = hogGroup
      )
    }
  }

  private def workflowSubmissionState(workflowSourceFiles: WorkflowSourceFilesCollection) = {
    if (workflowSourceFiles.workflowOnHold)
      WorkflowStoreState.OnHold
    else
      WorkflowStoreState.Submitted
  }

  private def toWorkflowStoreEntry(workflowSourceFiles: WorkflowSourceFilesCollection): WorkflowStoreEntry = {
    import eu.timepit.refined._
    val nonEmptyJsonString: String Refined NonEmpty  = refineMV[NonEmpty]("{}")

    val actualWorkflowState = workflowSubmissionState(workflowSourceFiles)

    val workflowId = workflowSourceFiles.requestedWorkflowId.getOrElse(WorkflowId.randomId())
    val hogGroup = HogGroup.decide(workflowSourceFiles.workflowOptions, workflowId)

    WorkflowStoreEntry(
      workflowExecutionUuid = workflowId.toString,
      workflowDefinition = workflowSourceFiles.workflowSource.toClobOption,
      workflowUrl = workflowSourceFiles.workflowUrl,
      workflowRoot = workflowSourceFiles.workflowRoot,
      workflowType = workflowSourceFiles.workflowType,
      workflowTypeVersion = workflowSourceFiles.workflowTypeVersion,
      workflowInputs = workflowSourceFiles.inputsJson.toClobOption,
      workflowOptions = workflowSourceFiles.workflowOptions.asPrettyJson.toClobOption,
      customLabels = workflowSourceFiles.labelsJson.toClob(default = nonEmptyJsonString),
      workflowState = actualWorkflowState.toString,
      cromwellId = None,
      heartbeatTimestamp = None,
      submissionTime = OffsetDateTime.now.toSystemTimestamp,
      importsZip = workflowSourceFiles.importsZipFileOption.toBlobOption,
      hogGroup = Option(hogGroup.value)
    )
  }

  private def workflowStoreStateToStartableState(workflowStoreEntry: WorkflowStoreEntry): ErrorOr[StartableState] = {
    val workflowState = WorkflowStoreState.withName(workflowStoreEntry.workflowState)
    import cats.syntax.validated._
    // A workflow is startable if it's in Submitted, Running or Aborting state.
    workflowState match {
      case WorkflowStoreState.Submitted => Submitted.validNel
      case WorkflowStoreState.Running => RestartableRunning.validNel
      case WorkflowStoreState.Aborting => RestartableAborting.validNel
      case _ =>
        "Workflow %s in state %s cannot be started and should not have been fetched."
          .format(workflowStoreEntry.workflowExecutionUuid, workflowState)
          .invalidNel
    }
  }
}


text: <input id="link-searchbar" type="text" list="search-results" name="link-searchbar" maxlength="140" placeholder="Search {{ site.posts | size }} Links"/> Search


text:
/**
 * This class was adapted from NiFi NAR Utils
 * https://github.com/apache/nifi/tree/master/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils
 */
package org.apache.pulsar.common.nar;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 *

 * A <tt>ClassLoader</tt> for loading NARs (NiFi archives). NARs are designed to allow isolating bundles of code
 * (comprising one-or-more NiFi <tt>FlowFileProcessor</tt>s, <tt>FlowFileComparator</tt>s and their dependencies) from
 * other such bundles; this allows for dependencies and processors that require conflicting, incompatible versions of
 * the same dependency to run in a single instance of NiFi.
 *

 *
 *

 * <tt>NarClassLoader</tt> follows the delegation model described in {@link ClassLoader#findClass(java.lang.String)
 * ClassLoader.findClass(...)}; classes are first loaded from the parent <tt>ClassLoader</tt>, and only if they cannot
 * be found there does the <tt>NarClassLoader</tt> provide a definition. Specifically, this means that resources are
 * loaded from NiFi's <tt>conf</tt> and <tt>lib</tt> directories first, and if they cannot be found there, are loaded
 * from the NAR.
 *

 *
 *

 * The packaging of a NAR is such that it is a ZIP file with the following directory structure:
 *
 *

 *   +META-INF/
 *   +-- bundled-dependencies/
 *   +-- <JAR files>
 *   +-- MANIFEST.MF
 *

 *

 *
 *

 * The MANIFEST.MF file contains the same information as a typical JAR file but also includes two additional NiFi
 * properties: {@code Nar-Id} and {@code Nar-Dependency-Id}.
 *

 *
 *

 * The {@code Nar-Id} provides a unique identifier for this NAR.
 *

 *
 *

 * The {@code Nar-Dependency-Id} is optional. If provided, it indicates that this NAR should inherit all of the
 * dependencies of the NAR with the provided ID. Often times, the NAR that is depended upon is referred to as the
 * Parent. This is because its ClassLoader will be the parent ClassLoader of the dependent NAR.
 *

 *
 *

 * If a NAR is built using NiFi's Maven NAR Plugin, the {@code Nar-Id} property will be set to the artifactId of the
 * NAR. The {@code Nar-Dependency-Id} will be set to the artifactId of the NAR that is depended upon. For example, if
 * NAR A is defined as such:
 *
 *

 * ...
 * <artifactId>nar-a</artifactId>
 * <packaging>nar</packaging>
 * ...
 * <dependencies>
 *   <dependency>
 *     <groupId>group</groupId>
 *     <artifactId>nar-z</artifactId>
 *     <type>nar</type>
 *   </dependency>
 * </dependencies>
 *

 *

 *
 *
 *

 * Then the MANIFEST.MF file that is created for NAR A will have the following properties set:
 *

 *
{@code Nar-Id: nar-a}

 *
{@code Nar-Dependency-Id: nar-z}

 *

 *

 *
 *

 * Note, above, that the {@code type} of the dependency is set to {@code nar}.
 *

 *
 *

 * If the NAR has more than one dependency of {@code type} {@code nar}, then the Maven NAR plugin will fail to build the
 * NAR.
 *

 */
@Slf4j
public class NarClassLoader extends URLClassLoader {

    private static final FileFilter JAR_FILTER = pathname -> {
        final String nameToTest = pathname.getName().toLowerCase();
        return nameToTest.endsWith(".jar") && pathname.isFile();
    };

    /**
     * The NAR for which this <tt>ClassLoader</tt> is responsible.
     */
    private final File narWorkingDirectory;

    private static final String TMP_DIR_PREFIX = "pulsar-nar";

    public static final String DEFAULT_NAR_EXTRACTION_DIR = System.getProperty("nar.extraction.tmpdir") != null
            ? System.getProperty("nar.extraction.tmpdir") : System.getProperty("java.io.tmpdir");

    static NarClassLoader getFromArchive(File narPath, Set<String> additionalJars, ClassLoader parent,
                                                String narExtractionDirectory)
        throws IOException {
        File unpacked = NarUnpacker.unpackNar(narPath, getNarExtractionDirectory(narExtractionDirectory));
        return AccessController.doPrivileged(new PrivilegedAction<NarClassLoader>() {
            @SneakyThrows
            @Override
            public NarClassLoader run() {
                return new NarClassLoader(unpacked, additionalJars, parent);
            }
        });
    }

    private static File getNarExtractionDirectory(String configuredDirectory) {
        return new File(configuredDirectory + "/" + TMP_DIR_PREFIX);
    }

    /**
     * Construct a nar class loader.
     *
     * @param narWorkingDirectory
     *            directory to explode nar contents to
     * @param parent
     * @throws IllegalArgumentException
     *             if the NAR is missing the Java Services API file for <tt>FlowFileProcessor</tt> implementations.
     * @throws ClassNotFoundException
     *             if any of the <tt>FlowFileProcessor</tt> implementations defined by the Java Services API cannot be
     *             loaded.
     * @throws IOException
     *             if an error occurs while loading the NAR.
     */
    private NarClassLoader(final File narWorkingDirectory, Set<String> additionalJars, ClassLoader parent)
            throws ClassNotFoundException, IOException {
        super(new URL[0], parent);
        this.narWorkingDirectory = narWorkingDirectory;

        // process the classpath
        updateClasspath(narWorkingDirectory);

        if (additionalJars != null) {
            for (String jar : additionalJars) {
                addURL(Paths.get(jar).toUri().toURL());
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("Created class loader with paths: {}", Arrays.toString(getURLs()));
        }
    }

    public File getWorkingDirectory() {
        return narWorkingDirectory;
    }

    /**
     * Read a service definition as a String.
     */
    public String getServiceDefinition(String serviceName) throws IOException {
        String serviceDefPath = narWorkingDirectory + "/META-INF/services/" + serviceName;
        return new String(Files.readAllBytes(Paths.get(serviceDefPath)), StandardCharsets.UTF_8);
    }

    public List<String> getServiceImplementation(String serviceName) throws IOException {
        List<String> impls = new ArrayList<>();

        String serviceDefPath = narWorkingDirectory + "/META-INF/services/" + serviceName;

        try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(new FileInputStream(serviceDefPath), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!line.isEmpty() && !line.startsWith("#")) {
                    final int indexOfPound = line.indexOf("#");
                    final String effectiveLine = (indexOfPound > 0) ? line.substring(0, indexOfPound) : line;
                    impls.add(effectiveLine);
                }
            }
        }

        return impls;
    }

    /**
     * Adds URLs for the resources unpacked from this NAR:
     *

     *
the root: for classes, <tt>META-INF</tt>, etc.

     *
<tt>META-INF/dependencies</tt>: for config files, <tt>.so</tt>s, etc.

     *
<tt>META-INF/dependencies/*.jar</tt>: for dependent libraries

     *

     *
     * @param root
     *            the root directory of the unpacked NAR.
     * @throws IOException
     *             if the URL list could not be updated.
     */
    private void updateClasspath(File root) throws IOException {
        addURL(root.toURI().toURL()); // for compiled classes, META-INF/, etc.

        File dependencies = new File(root, "META-INF/bundled-dependencies");
        if (!dependencies.isDirectory()) {
            log.warn("{} does not contain META-INF/bundled-dependencies!", narWorkingDirectory);
        }
        addURL(dependencies.toURI().toURL());
        if (dependencies.isDirectory()) {
            final File[] jarFiles = dependencies.listFiles(JAR_FILTER);
            if (jarFiles != null) {
                Arrays.sort(jarFiles, Comparator.comparing(File::getName));
                for (File libJar : jarFiles) {
                    addURL(libJar.toURI().toURL());
                }
            }
        }
    }

    @Override
    protected String findLibrary(final String libname) {
        File dependencies = new File(narWorkingDirectory, "META-INF/bundled-dependencies");
        if (!dependencies.isDirectory()) {
            log.warn("{} does not contain META-INF/bundled-dependencies!", narWorkingDirectory);
        }

        final File nativeDir = new File(dependencies, "native");
        final File libsoFile = new File(nativeDir, "lib" + libname + ".so");
        final File dllFile = new File(nativeDir, libname + ".dll");
        final File soFile = new File(nativeDir, libname + ".so");
        if (libsoFile.exists()) {
            return libsoFile.getAbsolutePath();
        } else if (dllFile.exists()) {
            return dllFile.getAbsolutePath();
        } else if (soFile.exists()) {
            return soFile.getAbsolutePath();
        }

        // not found in the nar. try system native dir
        return null;
    }

    @Override
    public String toString() {
        return NarClassLoader.class.getName() + "[" + narWorkingDirectory.getPath() + "]";
    }
}


text:
"""Meter entities for the plant integration"""

from __future__ import annotations

from datetime import timedelta
import logging

from homeassistant.components.integration.const import METHOD_TRAPEZOIDAL
from homeassistant.components.integration.sensor import IntegrationSensor
from homeassistant.components.sensor import (
    RestoreSensor,
    SensorDeviceClass,
    SensorStateClass,
)
from homeassistant.components.utility_meter.const import DAILY
from homeassistant.components.utility_meter.sensor import UtilityMeterSensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
    ATTR_NAME,
    ATTR_UNIT_OF_MEASUREMENT,
    STATE_UNAVAILABLE,
    STATE_UNKNOWN,
    TIME_SECONDS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity, async_generate_entity_id
from homeassistant.helpers.event import async_track_state_change_event

from .const import (
    ATTR_CONDUCTIVITY,
    DATA_UPDATED,
    DEFAULT_LUX_TO_PPFD,
    DOMAIN,
    FLOW_PLANT_INFO,
    FLOW_SENSOR_CONDUCTIVITY,
    FLOW_SENSOR_HUMIDITY,
    FLOW_SENSOR_ILLUMINANCE,
    FLOW_SENSOR_MOISTURE,
    FLOW_SENSOR_TEMPERATURE,
    READING_CONDUCTIVITY,
    READING_DLI,
    READING_HUMIDITY,
    READING_ILLUMINANCE,
    READING_MOISTURE,
    READING_PPFD,
    READING_TEMPERATURE,
    UNIT_DLI,
    UNIT_PPFD,
)

_LOGGER = logging.getLogger(__name__)

class PlantCurrentStatus(RestoreSensor):
    """Parent class for the meter classes below"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the Plant component."""
        self._hass = hass
        self._config = config
        self._default_state = 0
        self._plant = plantdevice
        # self._conf_check_days = self._plant.check_days
        self.entity_id = async_generate_entity_id(
            f"{DOMAIN}.{{}}", self.name, current_ids={}
        )
        if not self._attr_native_value or self._attr_native_value == STATE_UNKNOWN:
            self._attr_native_value = self._default_state

    @property
    def state_class(self):
        return SensorStateClass.MEASUREMENT

    @property
    def extra_state_attributes(self) -> dict:
        if self._external_sensor:
            attributes = {
                "external_sensor": self.external_sensor,
                # "history_max": self._history.max,
                # "history_min": self._history.min,
            }
            return attributes

    @property
    def external_sensor(self) -> str:
        """The external sensor we are tracking"""
        return self._external_sensor

    def replace_external_sensor(self, new_sensor: str | None) -> None:
        """Modify the external sensor"""
        _LOGGER.info("Setting %s external sensor to %s", self.entity_id, new_sensor)
        # pylint: disable=attribute-defined-outside-init
        self._external_sensor = new_sensor
        async_track_state_change_event(
            self._hass,
            list([self.entity_id, self._external_sensor]),
            self._state_changed_event,
        )

        self.async_write_ha_state()

    async def async_added_to_hass(self) -> None:
        """Handle entity which will be added."""
        await super().async_added_to_hass()
        state = await self.async_get_last_state()

        # We do not restore the state for these they are read from the external sensor anyway
        self._attr_native_value = STATE_UNKNOWN
        if state:
            if "external_sensor" in state.attributes:
                self.replace_external_sensor(state.attributes["external_sensor"])
        tracker = [self.entity_id]
        if self._external_sensor:
            tracker.append(self._external_sensor)
        async_track_state_change_event(
            self._hass,
            tracker,
            self._state_changed_event,
        )
        async_dispatcher_connect(
            self._hass, DATA_UPDATED, self._schedule_immediate_update
        )

    @callback
    def _schedule_immediate_update(self):
        self.async_schedule_update_ha_state(True)

    @callback
    def _state_changed_event(self, event):
        """Sensor state change event."""
        self.state_changed(event.data.get("entity_id"), event.data.get("new_state"))

    @callback
    def state_changed(self, entity_id, new_state):
        """Run on every update to allow for changes from the GUI and service call"""
        if not self.hass.states.get(self.entity_id):
            return
        current_attrs = self.hass.states.get(self.entity_id).attributes
        if current_attrs.get("external_sensor") != self._external_sensor:
            self.replace_external_sensor(current_attrs.get("external_sensor"))
        if self._external_sensor:
            external_sensor = self.hass.states.get(self._external_sensor)
            if external_sensor:
                self._attr_native_value = external_sensor.state
                self._attr_native_unit_of_measurement = external_sensor.attributes[
                    ATTR_UNIT_OF_MEASUREMENT
                ]
            else:
                self._attr_native_value = STATE_UNKNOWN
        else:
            self._attr_native_value = STATE_UNKNOWN

        if self.state == STATE_UNKNOWN or self.state is None:
            return

class PlantCurrentIlluminance(PlantCurrentStatus):
    """Entity class for the current illuminance meter"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = (
            f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_ILLUMINANCE}"
        )
        self._attr_unique_id = f"{config.entry_id}-current-illuminance"
        self._attr_icon = "mdi:brightness-6"
        self._external_sensor = config.data[FLOW_PLANT_INFO].get(
            FLOW_SENSOR_ILLUMINANCE
        )
        _LOGGER.info(
            "Added external sensor for %s %s", self.entity_id, self._external_sensor
        )
        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> str:
        """Device class"""
        return SensorDeviceClass.ILLUMINANCE

class PlantCurrentConductivity(PlantCurrentStatus):
    """Entity class for the current conductivity meter"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = (
            f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_CONDUCTIVITY}"
        )
        self._attr_unique_id = f"{config.entry_id}-current-conductivity"
        self._attr_icon = "mdi:spa-outline"
        self._external_sensor = config.data[FLOW_PLANT_INFO].get(
            FLOW_SENSOR_CONDUCTIVITY
        )

        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> None:
        """Device class - not defined for conductivity"""
        return ATTR_CONDUCTIVITY

class PlantCurrentMoisture(PlantCurrentStatus):
    """Entity class for the current moisture meter"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = (
            f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_MOISTURE}"
        )
        self._attr_unique_id = f"{config.entry_id}-current-moisture"
        self._external_sensor = config.data[FLOW_PLANT_INFO].get(FLOW_SENSOR_MOISTURE)
        self._attr_icon = "mdi:water"

        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> str:
        """Device class"""
        return SensorDeviceClass.HUMIDITY

class PlantCurrentTemperature(PlantCurrentStatus):
    """Entity class for the current temperature meter"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = (
            f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_TEMPERATURE}"
        )
        self._attr_unique_id = f"{config.entry_id}-current-temperature"
        self._external_sensor = config.data[FLOW_PLANT_INFO].get(
            FLOW_SENSOR_TEMPERATURE
        )
        self._attr_icon = "mdi:thermometer"
        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> str:
        """Device class"""
        return SensorDeviceClass.TEMPERATURE

class PlantCurrentHumidity(PlantCurrentStatus):
    """Entity class for the current humidity meter"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = (
            f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_HUMIDITY}"
        )
        self._attr_unique_id = f"{config.entry_id}-current-humidity"
        self._external_sensor = config.data[FLOW_PLANT_INFO].get(FLOW_SENSOR_HUMIDITY)
        self._attr_icon = "mdi:water-percent"
        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> str:
        """Device class"""
        return SensorDeviceClass.HUMIDITY

class PlantCurrentPpfd(PlantCurrentStatus):
    """Entity reporting current PPFD calculated from LX"""

    def __init__(
        self, hass: HomeAssistant, config: ConfigEntry, plantdevice: Entity
    ) -> None:
        """Initialize the sensor"""
        self._attr_name = f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_PPFD}"

        self._attr_unique_id = f"{config.entry_id}-current-ppfd"
        self._attr_unit_of_measurement = UNIT_PPFD
        self._attr_native_unit_of_measurement = UNIT_PPFD

        self._plant = plantdevice

        self._external_sensor = self._plant.sensor_illuminance.entity_id
        self._attr_icon = "mdi:white-balance-sunny"
        super().__init__(hass, config, plantdevice)

    @property
    def device_class(self) -> str:
        """Device class"""
        return SensorDeviceClass.ILLUMINANCE

    def ppfd(self, value: float | int | str) -> float | str:
        """
        Returns a calculated PPFD-value from the lx-value

        See https://community.home-assistant.io/t/light-accumulation-for-xiaomi-flower-sensor/111180/3
        https://www.apogeeinstruments.com/conversion-ppfd-to-lux/
        μmol/m²/s
        """
        if value is not None and value != STATE_UNAVAILABLE and value != STATE_UNKNOWN:
            value = float(value) * DEFAULT_LUX_TO_PPFD / 1000000

        return value

    @callback
    def state_changed(self, entity_id: str, new_state: str) -> None:
        """Run on every update to allow for changes from the GUI and service call"""
        if not self.hass.states.get(self.entity_id):
            return
        if self._external_sensor != self._plant.sensor_illuminance.entity_id:
            self.replace_external_sensor(self._plant.sensor_illuminance.entity_id)
        if self._external_sensor:
            external_sensor = self.hass.states.get(self._external_sensor)
            if external_sensor:
                self._attr_native_value = self.ppfd(external_sensor.state)
            else:
                self._attr_native_value = STATE_UNKNOWN
        else:
            self._attr_native_value = STATE_UNKNOWN

class PlantTotalLightIntegral(IntegrationSensor):
    """Entity class to calculate PPFD from LX"""

    def __init__(
        self,
        hass: HomeAssistant,
        config: ConfigEntry,
        illuminance_ppfd_sensor: Entity,
    ) -> None:
        """Initialize the sensor"""
        super().__init__(
            integration_method=METHOD_TRAPEZOIDAL,
            name=f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} Total {READING_PPFD} Integral",
            round_digits=2,
            source_entity=illuminance_ppfd_sensor.entity_id,
            unique_id=f"{config.entry_id}-ppfd-integral",
            unit_prefix=None,
            unit_time=TIME_SECONDS,
        )
        self._unit_of_measurement = UNIT_PPFD

    def _unit(self, source_unit: str) -> str:
        """Override unit"""
        return self._unit_of_measurement

class PlantDailyLightIntegral(UtilityMeterSensor):
    """Entity class to calculate Daily Light Integral from PPDF"""

    def __init__(
        self,
        hass: HomeAssistant,
        config: ConfigEntry,
        illuminance_integration_sensor: Entity,
    ) -> None:
        """Initialize the sensor"""
        super().__init__(
            cron_pattern=None,
            delta_values=None,
            meter_offset=timedelta(seconds=0),
            meter_type=DAILY,
            name=f"{config.data[FLOW_PLANT_INFO][ATTR_NAME]} {READING_DLI}",
            net_consumption=None,
            parent_meter=config.entry_id,
            source_entity=illuminance_integration_sensor.entity_id,
            tariff_entity=None,
            tariff=None,
            unique_id=f"{config.entry_id}-dli",
            suggested_entity_id=None,
        )

        self._unit_of_measurement = UNIT_DLI


text:
import { GET_PAGE_DETAILS } from '../../actions/actionTypes';
import pageInfo from '../../reducers/pageInfo';

describe('Test for pageInfo reducer should', () => {
  it('update store correctly when GET_PAGE_DETAILS is called', () => {
    const initialState = {
      recipes: { }
    };
    const action = {
      type: GET_PAGE_DETAILS,
      details: {
        currentPage: 3,
        limit: 6,
        pages: 3,
        numberOfItems: 20
      }
    };
    const newState = pageInfo(initialState, action);
    expect(newState.currentPage).toEqual(3);
    expect(newState.limit).toEqual(6);
    expect(newState.pages).toEqual(3);
    expect(newState.numberOfItems).toEqual(20);
  });
});


text:
package com.sunzequn.sdfs.test;

import com.sunzequn.sdfs.node.DataNode;
import com.sunzequn.sdfs.node.NodeInfo;

import java.io.File;

/**
 * Created by Sloriac on 2016/12/18.
 */
public class SockClient2Test {

    public static void main(String[] args) {
        NodeInfo selfInfo = new NodeInfo("2", "localhost", 2222);
        NodeInfo leader = new NodeInfo("0", "localhost", 1111);
        DataNode dataNode = new DataNode(selfInfo, leader, "/home/sloriac/data/2/");
        dataNode.start(true);
    }
}


text:
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

/** Client for the pw_transfer service, which transmits data over pw_rpc. */

import {
  BidirectionalStreamingCall,
  BidirectionalStreamingMethodStub,
  ServiceClient,
} from 'pigweedjs/pw_rpc';
import {Status} from 'pigweedjs/pw_status';
import {Chunk} from 'pigweedjs/protos/pw_transfer/transfer_pb';

import {
  ReadTransfer,
  ProgressCallback,
  Transfer,
  WriteTransfer,
} from './transfer';

type TransferDict = {
  [key: number]: Transfer;
};

const DEFAULT_MAX_RETRIES = 3;
const DEFAULT_RESPONSE_TIMEOUT_S = 2;
const DEFAULT_INITIAL_RESPONSE_TIMEOUT = 4;

/**
 *  A manager for transmitting data through an RPC TransferService.
 *
 *  This should be initialized with an active Manager over an RPC channel. Only
 *  one instance of this class should exist for a configured RPC TransferService
 *  -- the Manager supports multiple simultaneous transfers.
 *
 *  When created, a Manager starts a separate thread in which transfer
 *  communications and events are handled.
 */
export class Manager {
  // Ongoing transfers in the service by ID
  private readTransfers: TransferDict = {};
  private writeTransfers: TransferDict = {};

  // RPC streams for read and write transfers. These are shareable by
  // multiple transfers of the same type.
  private readStream?: BidirectionalStreamingCall;
  private writeStream?: BidirectionalStreamingCall;

  /**
   * Initializes a Manager on top of a TransferService.
   *
   * Args:
   * @param{ServiceClient} service: the pw_rpc transfer service
   * client
   * @param{number} defaultResponseTimeoutS: max time to wait between receiving
   * packets
   * @param{number} initialResponseTimeoutS: timeout for the first packet; may
   * be longer to account for transfer handler initialization
   * @param{number} maxRetries: number of times to retry after a timeout
   */
  constructor(
    private service: ServiceClient,
    private defaultResponseTimeoutS = DEFAULT_RESPONSE_TIMEOUT_S,
    private initialResponseTimeoutS = DEFAULT_INITIAL_RESPONSE_TIMEOUT,
    private maxRetries = DEFAULT_MAX_RETRIES
  ) {}

  /**
   * Receives ("downloads") data from the server.
   *
   * @throws Throws an error when the transfer fails to complete.
   */
  async read(
    resourceId: number,
    progressCallback?: ProgressCallback
  ): Promise<Uint8Array> {
    if (resourceId in this.readTransfers) {
      throw new Error(
        `Read transfer for resource ${resourceId} already exists`
      );
    }
    const transfer = new ReadTransfer(
      resourceId,
      this.sendReadChunkCallback,
      this.defaultResponseTimeoutS,
      this.maxRetries,
      progressCallback
    );

    this.startReadTransfer(transfer);

    const status = await transfer.done;

    delete this.readTransfers[transfer.id];
    if (status !== Status.OK) {
      throw new TransferError(transfer.id, transfer.status);
    }
    return transfer.data;
  }

  /** Begins a new read transfer, opening the stream if it isn't. */
  startReadTransfer(transfer: Transfer): void {
    this.readTransfers[transfer.id] = transfer;

    if (this.readStream === undefined) {
      this.openReadStream();
    }
    console.debug(`Starting new read transfer ${transfer.id}`);
    transfer.begin();
  }

  /**
  Transmits (uploads) data to the server.
   *
   * @param{number} resourceId: ID of the resource to which to write.
   * @param{Uint8Array} data: Data to send to the server.
   */
  async write(
    resourceId: number,
    data: Uint8Array,
    progressCallback?: ProgressCallback
  ): Promise<void> {
    const transfer = new WriteTransfer(
      resourceId,
      data,
      this.sendWriteChunkCallback,
      this.defaultResponseTimeoutS,
      this.initialResponseTimeoutS,
      this.maxRetries,
      progressCallback
    );
    this.startWriteTransfer(transfer);

    const status = await transfer.done;

    delete this.writeTransfers[transfer.id];
    if (transfer.status !== Status.OK) {
      throw new TransferError(transfer.id, transfer.status);
    }
  }

  sendReadChunkCallback = (chunk: Chunk) => {
    this.readStream!.send(chunk);
  };

  sendWriteChunkCallback = (chunk: Chunk) => {
    this.writeStream!.send(chunk);
  };

  /** Begins a new write transfer, opening the stream if it isn't */
  startWriteTransfer(transfer: Transfer): void {
    this.writeTransfers[transfer.id] = transfer;

    if (!this.writeStream) {
      this.openWriteStream();
    }

    console.debug(`Starting new write transfer ${transfer.id}`);
    transfer.begin();
  }

  private openReadStream(): void {
    const readRpc = this.service.method(
      'Read'
    )! as BidirectionalStreamingMethodStub;
    this.readStream = readRpc.invoke(
      (chunk: Chunk) => {
        this.handleChunk(this.readTransfers, chunk);
      },
      () => {},
      this.onReadError
    );
  }

  private openWriteStream(): void {
    const writeRpc = this.service.method(
      'Write'
    )! as BidirectionalStreamingMethodStub;
    this.writeStream = writeRpc.invoke(
      (chunk: Chunk) => {
        this.handleChunk(this.writeTransfers, chunk);
      },
      () => {},
      this.onWriteError
    );
  }

  /**
   * Callback for an RPC error in the read stream.
   */
  private onReadError = (status: Status) => {
    if (status === Status.FAILED_PRECONDITION) {
      // FAILED_PRECONDITION indicates that the stream packet was not
      // recognized as the stream is not open. This could occur if the
      // server resets during an active transfer. Re-open the stream to
      // allow pending transfers to continue.
      this.openReadStream();
      return;
    }

    // Other errors are unrecoverable. Clear the stream and cancel any
    // pending transfers with an INTERNAL status as this is a system
    // error.
    this.readStream = undefined;

    for (const key in this.readTransfers) {
      const transfer = this.readTransfers[key];
      transfer.finish(Status.INTERNAL);
    }
    this.readTransfers = {};
    console.error(`Read stream shut down ${Status[status]}`);
  };

  private onWriteError = (status: Status) => {
    if (status === Status.FAILED_PRECONDITION) {
      // FAILED_PRECONDITION indicates that the stream packet was not
      // recognized as the stream is not open. This could occur if the
      // server resets during an active transfer. Re-open the stream to
      // allow pending transfers to continue.
      this.openWriteStream();
    } else {
      // Other errors are unrecoverable. Clear the stream and cancel any
      // pending transfers with an INTERNAL status as this is a system
      // error.
      this.writeStream = undefined;

      for (const key in this.writeTransfers) {
        const transfer = this.writeTransfers[key];
        transfer.finish(Status.INTERNAL);
      }
      this.writeTransfers = {};
      console.error(`Write stream shut down: ${Status[status]}`);
    }
  };

  /**
   * Processes an incoming chunk from a stream.
   *
   * The chunk is dispatched to an active transfer based on its ID. If the
   * transfer indicates that it is complete, the provided completion callback
   * is invoked.
   */
  private async handleChunk(transfers: TransferDict, chunk: Chunk) {
    const transfer = transfers[chunk.getTransferId()];
    if (transfer === undefined) {
      console.error(
        `TransferManager received chunk for unknown transfer ${chunk.getTransferId()}`
      );
      return;
    }
    transfer.handleChunk(chunk);
  }
}

/**
 * Exception raised when a transfer fails.
 *
 * Stores the ID of the failed transfer and the error that occured.
 */
class TransferError extends Error {
  id: number;
  status: Status;

  constructor(id: number, status: Status) {
    super(`Transfer ${id} failed with status ${Status[status]}`);
    this.status = status;
    this.id = id;
  }
}


text:
var ampule = function(melody, totalDuration) {
	//Initialize the variables (build the context, node, etc.)
	var context = new webkitAudioContext();
	var node = context.createJavaScriptNode(1024, 0, 1);
	node.onaudioprocess = process;
	var S = context.sampleRate;
	var pointer = currentFloat = 0;

	buffer = new ArrayBuffer(4 * Math.ceil(totalDuration * S/1000));
	genData = new Float32Array(buffer);

	//Fill the genData buffer with the rendered music.
	for(var i=0; i<melody.length; i++) {
		var note = melody[i];

		//Adding polyphony. If note is a single number then make it an array of 1.
		if(!note.note[0])
			note.note = [note.note];

		var duration = note.duration * S/1000;
		var ar = note.ar || 1;

		note.shape = note.shape || 1;
		if(!note.shape[0])
			note.shape = [note.shape];

		for (var samp=currentFloat; samp<(duration + currentFloat); samp++) {
			genData[samp] = 0;

			for(var n in note.note) {
				for(var s in note.shape) {
					var freq = 440*Math.pow(2, note.note[n]/12);
					var shape = note.shape[s];
					var modSamp = samp%(S/freq)
					var maxSamp = S/freq;

					//Render wave from given shape.
					if(shape == 0)
						genData[samp] += 0;
					if(shape == 1)
						genData[samp] += Math.sin(samp*freq*2*Math.PI / S);
					if(shape == 2)
						genData[samp] += 2*(modSamp / maxSamp) - 1;
					if(shape == 3)
						genData[samp] += modSamp > maxSamp*.5 ? 1.0 : -1.0;
					if(shape == 4)
						genData[samp] += 2*Math.random() - 1;
				}
			}

			//Scale wave to fit simple AR envelope
			genData[samp] *= Math.pow((duration-samp+currentFloat)/duration, ar);

			//Finally, scale the wave to the appropriate size
			genData[samp] /= (note.note.length * note.shape.length);
		}

		currentFloat += duration;
	}

	function process(e) {
		var data = e.outputBuffer.getChannelData(0);

		for(var i=0; i<data.length && pointer<genData.length; ++i) {
			data[i] = genData[pointer++];
		}

		if(pointer == genData.length)
			node.disconnect();
	};

	return function() { node.connect(context.destination); };
};


text:
var chai = require('chai');

//https://github.com/domenic/chai-as-promised/
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

var expect = chai.expect;

var Idea = require('../../server/api/idea/idea.model');

var myStepDefinitionsWrapper = function () {

  this.Given(/^I go to the ideas backlog$/, function (callback) {
    browser.get('http://localhost:9000/idea');
    callback();
  });

  this.When(/^I rate the idea "([^"]*)" with (\d+) stars$/, function (idea, stars, callback) {
    element(by.model('idea.rating')).click()
    callback();

  });

  this.Then(/^I see the mean value of (.*) stars for the idea "([^"]*)"$/, function (arg1, arg2, callback) {
    callback.pending();
  });

  this.When(/^I enter "([^"]*)" as idea's title$/, function (input, callback) {
    element(by.model('idea.title')).sendKeys(input);
    callback();
  });

  this.When(/^I enter "([^"]*)" as idea's description$/, function (input, callback) {
    element(by.model('idea.description')).sendKeys(input);
    callback();
  });

  this.Then(/^the ideas backlog should contain the idea "([^"]*)" with the description  "([^"]*)"$/, function (title, arg2, callback) {
    element.all(by.css('.idea-name')).filter(function(elem, index) {
      return elem.getText().then(function(text) {
        return text === title;
      });
    }).then(function(filteredElements) {
      expect(filteredElements).to.have.length.least(1);
      callback();
    });
  });
};
module.exports = myStepDefinitionsWrapper;


text:
<?php

namespace Zotlabs\Module\Settings;

class Oauth2 {

	function post() {

		if(x($_POST,'remove')){
			check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2');

			$key = $_POST['remove'];
			q("DELETE FROM tokens WHERE id='%s' AND uid=%d",
				dbesc($key),
				local_channel());
			goaway(z_root()."/settings/oauth2/");
			return;
		}

		if((argc() > 2) && (argv(2) === 'edit' || argv(2) === 'add') && x($_POST,'submit')) {

			check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2');

			$name   	= ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : '');
			$secret		= ((x($_POST,'secret')) ? escape_tags(trim($_POST['secret'])) : '');
			$redirect	= ((x($_POST,'redirect')) ? escape_tags(trim($_POST['redirect'])) : '');
			$grant		= ((x($_POST,'grant')) ? escape_tags(trim($_POST['grant'])) : '');
			$scope		= ((x($_POST,'scope')) ? escape_tags(trim($_POST['scope'])) : '');

			$ok = true;
			if($name == '' || $secret == '') {
				$ok = false;
				notice( t('Name and Secret are required') . EOL);
			}

			if($ok) {
				if ($_POST['submit']==t("Update")){
					$r = q("UPDATE oauth_clients SET
								client_id = '%s',
								client_secret = '%s',
								redirect_uri = '%s',
								grant_types = '%s',
								scope = '%s',
								user_id = '%s'
							WHERE client_id='%s'",
							dbesc($name),
							dbesc($secret),
							dbesc($redirect),
							dbesc($grant),
							dbesc($scope),
							dbesc(local_channel()),
							dbesc($name));
				} else {
					$r = q("INSERT INTO oauth_clients (client_id, client_secret, redirect_uri, grant_types, scope, user_id)
						VALUES ('%s','%s','%s','%s','%s','%s')",
						dbesc($name),
						dbesc($secret),
						dbesc($redirect),
						dbesc($grant),
						dbesc($scope),
						dbesc(local_channel())
					);
					$r = q("INSERT INTO xperm (xp_client, xp_channel, xp_perm) VALUES ('%s', %d, '%s') ",
						dbesc($name),
						intval(local_channel()),
						dbesc('all')
					);
				}
			}
			goaway(z_root()."/settings/oauth2/");
			return;
		}
	}

	function get() {

		if((argc() > 2) && (argv(2) === 'add')) {
			$tpl = get_markup_template("settings_oauth2_edit.tpl");
			$o .= replace_macros($tpl, array(
				'$form_security_token' => get_form_security_token("settings_oauth2"),
				'$title'	=> t('Add OAuth2 application'),
				'$submit'	=> t('Submit'),
				'$cancel'	=> t('Cancel'),
				'$name'		=> array('name', t('Name'), '', t('Name of application')),
				'$secret'	=> array('secret', t('Consumer Secret'), random_string(16), t('Automatically generated - change if desired. Max length 20')),
				'$redirect'	=> array('redirect', t('Redirect'), '', t('Redirect URI - leave blank unless your application specifically requires this')),
				'$grant'     => array('grant', t('Grant Types'), '', t('leave blank unless your application sepcifically requires this')),
				'$scope'     => array('scope', t('Authorization scope'), '', t('leave blank unless your application sepcifically requires this')),
			));
			return $o;
		}

		if((argc() > 3) && (argv(2) === 'edit')) {
			$r = q("SELECT * FROM oauth_clients WHERE client_id='%s' AND user_id= '%s'",
					dbesc(argv(3)),
					dbesc(local_channel())
			);

			if (! $r){
				notice(t('OAuth2 Application not found.'));
				return;
			}

			$app = $r[0];

			$tpl = get_markup_template("settings_oauth2_edit.tpl");
			$o .= replace_macros($tpl, array(
				'$form_security_token' => get_form_security_token("settings_oauth2"),
				'$title'	=> t('Add application'),
				'$submit'	=> t('Update'),
				'$cancel'	=> t('Cancel'),
				'$name'		=> array('name', t('Name'), $app['client_id'], t('Name of application')),
				'$secret'	=> array('secret', t('Consumer Secret'), $app['client_secret'], t('Automatically generated - change if desired. Max length 20')),
				'$redirect'	=> array('redirect', t('Redirect'), $app['redirect_uri'], t('Redirect URI - leave blank unless your application specifically requires this')),
				'$grant'     => array('grant', t('Grant Types'), $app['grant_types'], t('leave blank unless your application sepcifically requires this')),
				'$scope'     => array('scope', t('Authorization scope'), $app['scope'], t('leave blank unless your application sepcifically requires this')),
			));
			return $o;
		}

		if((argc() > 3) && (argv(2) === 'delete')) {
			check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2', 't');

			$r = q("DELETE FROM oauth_clients WHERE client_id = '%s' AND user_id = '%s'",
					dbesc(argv(3)),
					dbesc(local_channel())
			);
			goaway(z_root()."/settings/oauth2/");
			return;
		}

		$r = q("SELECT oauth_clients.*, oauth_access_tokens.access_token as oauth_token, (oauth_clients.user_id = '%s') AS my
				FROM oauth_clients
				LEFT JOIN oauth_access_tokens ON oauth_clients.client_id=oauth_access_tokens.client_id
				WHERE oauth_clients.user_id IN ('%s',0)",
				dbesc(local_channel()),
				dbesc(local_channel())
		);

		$tpl = get_markup_template("settings_oauth2.tpl");
		$o .= replace_macros($tpl, array(
			'$form_security_token' => get_form_security_token("settings_oauth2"),
			'$baseurl'	=> z_root(),
			'$title'	=> t('Connected OAuth2 Apps'),
			'$add'		=> t('Add application'),
			'$edit'		=> t('Edit'),
			'$delete'		=> t('Delete'),
			'$consumerkey' => t('Client key starts with'),
			'$noname'	=> t('No name'),
			'$remove'	=> t('Remove authorization'),
			'$apps'		=> $r,
		));
		return $o;

	}

}


text:
package com.dfyy.bussiness;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "express")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Express implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	@XmlElement
	private Integer id;

	@Column(name = "code")
	@XmlElement
	private String code;

	@Column(name = "expname")
	@XmlElement
	private String expname;

	@Column(name = "status")
	@XmlElement
	private Integer status;

	public Express() {

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getExpname() {
		return expname;
	}

	public void setExpname(String expname) {
		this.expname = expname;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Override
	public String toString() {
		return "Express [id=" + id + ", code=" + code + ", expname=" + expname + ", status=" + status + "]";
	}

}


text:
package org.apache.spark.sql

import java.io.{File, FileNotFoundException}
import java.util.Locale

import scala.collection.mutable

import org.apache.hadoop.fs.Path
import org.scalatest.BeforeAndAfterAll

import org.apache.spark.SparkException
import org.apache.spark.scheduler.{SparkListener, SparkListenerTaskEnd}
import org.apache.spark.sql.TestingUDT.{IntervalData, IntervalUDT, NullData, NullUDT}
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSQLContext
import org.apache.spark.sql.types._

class FileBasedDataSourceSuite extends QueryTest with SharedSQLContext with BeforeAndAfterAll {
  import testImplicits._

  override def beforeAll(): Unit = {
    super.beforeAll()
    spark.sessionState.conf.setConf(SQLConf.ORC_IMPLEMENTATION, "native")
  }

  override def afterAll(): Unit = {
    try {
      spark.sessionState.conf.unsetConf(SQLConf.ORC_IMPLEMENTATION)
    } finally {
      super.afterAll()
    }
  }

  private val allFileBasedDataSources = Seq("orc", "parquet", "csv", "json", "text")
  private val nameWithSpecialChars = "sp&cial%c hars"

  allFileBasedDataSources.foreach { format =>
    test(s"Writing empty datasets should not fail - $format") {
      withTempPath { dir =>
        Seq("str").toDS().limit(0).write.format(format).save(dir.getCanonicalPath)
      }
    }
  }

  // `TEXT` data source always has a single column whose name is `value`.
  allFileBasedDataSources.filterNot(_ == "text").foreach { format =>
    test(s"SPARK-23072 Write and read back unicode column names - $format") {
      withTempPath { path =>
        val dir = path.getCanonicalPath

        // scalastyle:off nonascii
        val df = Seq("a").toDF("한글")
        // scalastyle:on nonascii

        df.write.format(format).option("header", "true").save(dir)
        val answerDf = spark.read.format(format).option("header", "true").load(dir)

        assert(df.schema.sameType(answerDf.schema))
        checkAnswer(df, answerDf)
      }
    }
  }

  // Only ORC/Parquet support this. `CSV` and `JSON` returns an empty schema.
  // `TEXT` data source always has a single column whose name is `value`.
  Seq("orc", "parquet").foreach { format =>
    test(s"SPARK-15474 Write and read back non-empty schema with empty dataframe - $format") {
      withTempPath { file =>
        val path = file.getCanonicalPath
        val emptyDf = Seq((true, 1, "str")).toDF().limit(0)
        emptyDf.write.format(format).save(path)

        val df = spark.read.format(format).load(path)
        assert(df.schema.sameType(emptyDf.schema))
        checkAnswer(df, emptyDf)
      }
    }
  }

  Seq("orc", "parquet").foreach { format =>
    test(s"SPARK-23271 empty RDD when saved should write a metadata only file - $format") {
      withTempPath { outputPath =>
        val df = spark.emptyDataFrame.select(lit(1).as("i"))
        df.write.format(format).save(outputPath.toString)
        val partFiles = outputPath.listFiles()
          .filter(f => f.isFile && !f.getName.startsWith(".") && !f.getName.startsWith("_"))
        assert(partFiles.length === 1)

        // Now read the file.
        val df1 = spark.read.format(format).load(outputPath.toString)
        checkAnswer(df1, Seq.empty[Row])
        assert(df1.schema.equals(df.schema.asNullable))
      }
    }
  }

  allFileBasedDataSources.foreach { format =>
    test(s"SPARK-23372 error while writing empty schema files using $format") {
      withTempPath { outputPath =>
        val errMsg = intercept[AnalysisException] {
          spark.emptyDataFrame.write.format(format).save(outputPath.toString)
        }
        assert(errMsg.getMessage.contains(
          "Datasource does not support writing empty or nested empty schemas"))
      }

      // Nested empty schema
      withTempPath { outputPath =>
        val schema = StructType(Seq(
          StructField("a", IntegerType),
          StructField("b", StructType(Nil)),
          StructField("c", IntegerType)
        ))
        val df = spark.createDataFrame(sparkContext.emptyRDD[Row], schema)
        val errMsg = intercept[AnalysisException] {
          df.write.format(format).save(outputPath.toString)
        }
        assert(errMsg.getMessage.contains(
          "Datasource does not support writing empty or nested empty schemas"))
      }
    }
  }

  allFileBasedDataSources.foreach { format =>
    test(s"SPARK-22146 read files containing special characters using $format") {
      withTempDir { dir =>
        val tmpFile = s"$dir/$nameWithSpecialChars"
        spark.createDataset(Seq("a", "b")).write.format(format).save(tmpFile)
        val fileContent = spark.read.format(format).load(tmpFile)
        checkAnswer(fileContent, Seq(Row("a"), Row("b")))
      }
    }
  }

  // Separate test case for formats that support multiLine as an option.
  Seq("json", "csv").foreach { format =>
    test("SPARK-23148 read files containing special characters " +
      s"using $format with multiline enabled") {
      withTempDir { dir =>
        val tmpFile = s"$dir/$nameWithSpecialChars"
        spark.createDataset(Seq("a", "b")).write.format(format).save(tmpFile)
        val reader = spark.read.format(format).option("multiLine", true)
        val fileContent = reader.load(tmpFile)
        checkAnswer(fileContent, Seq(Row("a"), Row("b")))
      }
    }
  }

  allFileBasedDataSources.foreach { format =>
    testQuietly(s"Enabling/disabling ignoreMissingFiles using $format") {
      def testIgnoreMissingFiles(): Unit = {
        withTempDir { dir =>
          val basePath = dir.getCanonicalPath

          Seq("0").toDF("a").write.format(format).save(new Path(basePath, "first").toString)
          Seq("1").toDF("a").write.format(format).save(new Path(basePath, "second").toString)

          val thirdPath = new Path(basePath, "third")
          val fs = thirdPath.getFileSystem(spark.sessionState.newHadoopConf())
          Seq("2").toDF("a").write.format(format).save(thirdPath.toString)
          val files = fs.listStatus(thirdPath).filter(_.isFile).map(_.getPath)

          val df = spark.read.format(format).load(
            new Path(basePath, "first").toString,
            new Path(basePath, "second").toString,
            new Path(basePath, "third").toString)

          // Make sure all data files are deleted and can't be opened.
          files.foreach(f => fs.delete(f, false))
          assert(fs.delete(thirdPath, true))
          for (f <- files) {
            intercept[FileNotFoundException](fs.open(f))
          }

          checkAnswer(df, Seq(Row("0"), Row("1")))
        }
      }

      withSQLConf(SQLConf.IGNORE_MISSING_FILES.key -> "true") {
        testIgnoreMissingFiles()
      }

      withSQLConf(SQLConf.IGNORE_MISSING_FILES.key -> "false") {
        val exception = intercept[SparkException] {
          testIgnoreMissingFiles()
        }
        assert(exception.getMessage().contains("does not exist"))
      }
    }
  }

  // Text file format only supports string type
  test("SPARK-24691 error handling for unsupported types - text") {
    withTempDir { dir =>
      // write path
      val textDir = new File(dir, "text").getCanonicalPath
      var msg = intercept[AnalysisException] {
        Seq(1).toDF.write.text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support int data type"))

      msg = intercept[AnalysisException] {
        Seq(1.2).toDF.write.text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support double data type"))

      msg = intercept[AnalysisException] {
        Seq(true).toDF.write.text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support boolean data type"))

      msg = intercept[AnalysisException] {
        Seq(1).toDF("a").selectExpr("struct(a)").write.text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support struct<a:int> data type"))

      msg = intercept[AnalysisException] {
        Seq((Map("Tesla" -> 3))).toDF("cars").write.mode("overwrite").text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support map<string,int> data type"))

      msg = intercept[AnalysisException] {
        Seq((Array("Tesla", "Chevy", "Ford"))).toDF("brands")
          .write.mode("overwrite").text(textDir)
      }.getMessage
      assert(msg.contains("Text data source does not support array<string> data type"))

      // read path
      Seq("aaa").toDF.write.mode("overwrite").text(textDir)
      msg = intercept[AnalysisException] {
        val schema = StructType(StructField("a", IntegerType, true) :: Nil)
        spark.read.schema(schema).text(textDir).collect()
      }.getMessage
      assert(msg.contains("Text data source does not support int data type"))

      msg = intercept[AnalysisException] {
        val schema = StructType(StructField("a", DoubleType, true) :: Nil)
        spark.read.schema(schema).text(textDir).collect()
      }.getMessage
      assert(msg.contains("Text data source does not support double data type"))

      msg = intercept[AnalysisException] {
        val schema = StructType(StructField("a", BooleanType, true) :: Nil)
        spark.read.schema(schema).text(textDir).collect()
      }.getMessage
      assert(msg.contains("Text data source does not support boolean data type"))
    }
  }

  // Unsupported data types of csv, json, orc, and parquet are as follows;
  //  csv -> R/W: Null, Array, Map, Struct
  //  json -> R/W: Interval
  //  orc -> R/W: Interval, W: Null
  //  parquet -> R/W: Interval, Null
  test("SPARK-24204 error handling for unsupported Array/Map/Struct types - csv") {
    withTempDir { dir =>
      val csvDir = new File(dir, "csv").getCanonicalPath
      var msg = intercept[AnalysisException] {
        Seq((1, "Tesla")).toDF("a", "b").selectExpr("struct(a, b)").write.csv(csvDir)
      }.getMessage
      assert(msg.contains("CSV data source does not support struct<a:int,b:string> data type"))

      msg = intercept[AnalysisException] {
        val schema = StructType.fromDDL("a struct<b: Int>")
        spark.range(1).write.mode("overwrite").csv(csvDir)
        spark.read.schema(schema).csv(csvDir).collect()
      }.getMessage
      assert(msg.contains("CSV data source does not support struct<b:int> data type"))

      msg = intercept[AnalysisException] {
        Seq((1, Map("Tesla" -> 3))).toDF("id", "cars").write.mode("overwrite").csv(csvDir)
      }.getMessage
      assert(msg.contains("CSV data source does not support map<string,int> data type"))

      msg = intercept[AnalysisException] {
        val schema = StructType.fromDDL("a map<int, int>")
        spark.range(1).write.mode("overwrite").csv(csvDir)
        spark.read.schema(schema).csv(csvDir).collect()
      }.getMessage
      assert(msg.contains("CSV data source does not support map<int,int> data type"))

      msg = intercept[AnalysisException] {
        Seq((1, Array("Tesla", "Chevy", "Ford"))).toDF("id", "brands")
          .write.mode("overwrite").csv(csvDir)
      }.getMessage
      assert(msg.contains("CSV data source does not support array<string> data type"))

      msg = intercept[AnalysisException] {
         val schema = StructType.fromDDL("a array<int>")
         spark.range(1).write.mode("overwrite").csv(csvDir)
         spark.read.schema(schema).csv(csvDir).collect()
       }.getMessage
      assert(msg.contains("CSV data source does not support array<int> data type"))

      msg = intercept[AnalysisException] {
        Seq((1, new UDT.MyDenseVector(Array(0.25, 2.25, 4.25)))).toDF("id", "vectors")
          .write.mode("overwrite").csv(csvDir)
      }.getMessage
      assert(msg.contains("CSV data source does not support array<double> data type"))

      msg = intercept[AnalysisException] {
        val schema = StructType(StructField("a", new UDT.MyDenseVectorUDT(), true) :: Nil)
        spark.range(1).write.mode("overwrite").csv(csvDir)
        spark.read.schema(schema).csv(csvDir).collect()
      }.getMessage
      assert(msg.contains("CSV data source does not support array<double> data type."))
    }
  }

  test("SPARK-24204 error handling for unsupported Interval data types - csv, json, parquet, orc") {
    withTempDir { dir =>
      val tempDir = new File(dir, "files").getCanonicalPath

      // write path
      Seq("csv", "json", "parquet", "orc").foreach { format =>
        var msg = intercept[AnalysisException] {
          sql("select interval 1 days").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.contains("Cannot save interval data type into external storage."))

        msg = intercept[AnalysisException] {
          spark.udf.register("testType", () => new IntervalData())
          sql("select testType()").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support calendarinterval data type."))
      }

      // read path
      Seq("parquet", "csv").foreach { format =>
        var msg = intercept[AnalysisException] {
          val schema = StructType(StructField("a", CalendarIntervalType, true) :: Nil)
          spark.range(1).write.format(format).mode("overwrite").save(tempDir)
          spark.read.schema(schema).format(format).load(tempDir).collect()
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support calendarinterval data type."))

        msg = intercept[AnalysisException] {
          val schema = StructType(StructField("a", new IntervalUDT(), true) :: Nil)
          spark.range(1).write.format(format).mode("overwrite").save(tempDir)
          spark.read.schema(schema).format(format).load(tempDir).collect()
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support calendarinterval data type."))
      }
    }
  }

  test("SPARK-24204 error handling for unsupported Null data types - csv, parquet, orc") {
    withTempDir { dir =>
      val tempDir = new File(dir, "files").getCanonicalPath

      Seq("orc").foreach { format =>
        // write path
        var msg = intercept[AnalysisException] {
          sql("select null").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))

        msg = intercept[AnalysisException] {
          spark.udf.register("testType", () => new NullData())
          sql("select testType()").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))

        // read path
        // We expect the types below should be passed for backward-compatibility

        // Null type
        var schema = StructType(StructField("a", NullType, true) :: Nil)
        spark.range(1).write.format(format).mode("overwrite").save(tempDir)
        spark.read.schema(schema).format(format).load(tempDir).collect()

        // UDT having null data
        schema = StructType(StructField("a", new NullUDT(), true) :: Nil)
        spark.range(1).write.format(format).mode("overwrite").save(tempDir)
        spark.read.schema(schema).format(format).load(tempDir).collect()
      }

      Seq("parquet", "csv").foreach { format =>
        // write path
        var msg = intercept[AnalysisException] {
          sql("select null").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))

        msg = intercept[AnalysisException] {
          spark.udf.register("testType", () => new NullData())
          sql("select testType()").write.format(format).mode("overwrite").save(tempDir)
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))

        // read path
        msg = intercept[AnalysisException] {
          val schema = StructType(StructField("a", NullType, true) :: Nil)
          spark.range(1).write.format(format).mode("overwrite").save(tempDir)
          spark.read.schema(schema).format(format).load(tempDir).collect()
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))

        msg = intercept[AnalysisException] {
          val schema = StructType(StructField("a", new NullUDT(), true) :: Nil)
          spark.range(1).write.format(format).mode("overwrite").save(tempDir)
          spark.read.schema(schema).format(format).load(tempDir).collect()
        }.getMessage
        assert(msg.toLowerCase(Locale.ROOT)
          .contains(s"$format data source does not support null data type."))
      }
    }
  }

  Seq("parquet", "orc").foreach { format =>
    test(s"Spark native readers should respect spark.sql.caseSensitive - ${format}") {
      withTempDir { dir =>
        val tableName = s"spark_25132_${format}_native"
        val tableDir = dir.getCanonicalPath + s"/$tableName"
        withTable(tableName) {
          val end = 5
          val data = spark.range(end).selectExpr("id as A", "id * 2 as b", "id * 3 as B")
          withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
            data.write.format(format).mode("overwrite").save(tableDir)
          }
          sql(s"CREATE TABLE $tableName (a LONG, b LONG) USING $format LOCATION '$tableDir'")

          withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") {
            checkAnswer(sql(s"select a from $tableName"), data.select("A"))
            checkAnswer(sql(s"select A from $tableName"), data.select("A"))

            // RuntimeException is triggered at executor side, which is then wrapped as
            // SparkException at driver side
            val e1 = intercept[SparkException] {
              sql(s"select b from $tableName").collect()
            }
            assert(
              e1.getCause.isInstanceOf[RuntimeException] &&
                e1.getCause.getMessage.contains(
                  """Found duplicate field(s) "b": [b, B] in case-insensitive mode"""))
            val e2 = intercept[SparkException] {
              sql(s"select B from $tableName").collect()
            }
            assert(
              e2.getCause.isInstanceOf[RuntimeException] &&
                e2.getCause.getMessage.contains(
                  """Found duplicate field(s) "b": [b, B] in case-insensitive mode"""))
          }

          withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
            checkAnswer(sql(s"select a from $tableName"), (0 until end).map(_ => Row(null)))
            checkAnswer(sql(s"select b from $tableName"), data.select("b"))
          }
        }
      }
    }
  }

  test("SPARK-25237 compute correct input metrics in FileScanRDD") {
    withTempPath { p =>
      val path = p.getAbsolutePath
      spark.range(1000).repartition(1).write.csv(path)
      val bytesReads = new mutable.ArrayBuffer[Long]()
      val bytesReadListener = new SparkListener() {
        override def onTaskEnd(taskEnd: SparkListenerTaskEnd) {
          bytesReads += taskEnd.taskMetrics.inputMetrics.bytesRead
        }
      }
      sparkContext.addSparkListener(bytesReadListener)
      try {
        spark.read.csv(path).limit(1).collect()
        sparkContext.listenerBus.waitUntilEmpty(1000L)
        assert(bytesReads.sum === 7860)
      } finally {
        sparkContext.removeSparkListener(bytesReadListener)
      }
    }
  }
}

object TestingUDT {

  @SQLUserDefinedType(udt = classOf[IntervalUDT])
  class IntervalData extends Serializable

  class IntervalUDT extends UserDefinedType[IntervalData] {

    override def sqlType: DataType = CalendarIntervalType
    override def serialize(obj: IntervalData): Any =
      throw new NotImplementedError("Not implemented")
    override def deserialize(datum: Any): IntervalData =
      throw new NotImplementedError("Not implemented")
    override def userClass: Class[IntervalData] = classOf[IntervalData]
  }

  @SQLUserDefinedType(udt = classOf[NullUDT])
  private[sql] class NullData extends Serializable

  private[sql] class NullUDT extends UserDefinedType[NullData] {

    override def sqlType: DataType = NullType
    override def serialize(obj: NullData): Any = throw new NotImplementedError("Not implemented")
    override def deserialize(datum: Any): NullData =
      throw new NotImplementedError("Not implemented")
    override def userClass: Class[NullData] = classOf[NullData]
  }
}


text:
<?php namespace Analogue\ORM\Commands;

use Analogue\ORM\Exceptions\MappingException;

class Delete extends Command {

	/**
	 * Execute the Delete Statement
	 *
	 * @return void
	 */
	public function execute()
	{
		$entity = $this->entity;
		$mapper = $this->mapper;

		if ($mapper->fireEvent('deleting', $entity) === false)
		{
			return false;
		}

		$keyName = $this->entityMap->getKeyName();

		$id = $this->entity->getEntityAttribute($keyName);

		if (is_null($id) )
		{
			throw new MappingException('Executed a delete command on an entity with a null primary key');
		}

		$this->query->where($keyName, '=', $id)->delete();

		$mapper->fireEvent('deleted', $entity, false);

		// Once the Entity is successfully deleted, we'll just set the primary key to null.
		$entity->setEntityAttribute($keyName, null);
	}

}


text:
package io.shuqi.graduation.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import io.shuqi.graduation.dao.CourseClassDao;
import io.shuqi.graduation.domain.Course;
import io.shuqi.graduation.domain.CourseClass;
import io.shuqi.graduation.domain.User;
import io.shuqi.graduation.domain.bo.Pager;

@Repository
public class CourseClassDaoImpl extends CourseClassDao {

	@Override
	public CourseClass getByName(String title) {
		return (CourseClass) getSession().createQuery(" from "+CourseClass.class.getName()+" ccs where ccs.title=:title")
										 .setParameter("title", title)
										 .uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<CourseClass> getPagerByUser(User currentUser, Pager<CourseClass> pager) {
		List<CourseClass> datas = new ArrayList<>();
		datas = getSession().createQuery("from "+CourseClass.class.getName()+" ccs  where ccs.createUser=:user")
					.setParameter("user", currentUser)
					.setMaxResults(pager.getPagesize())
					.setFirstResult(((Number)((pager.getCurrentPage()-1)*pager.getPagesize())).intValue())
					.list();
		pager.setDatas(datas);
		//构建pager
		Long total = getCountByUser(currentUser);
		pager.setTotal(total);
		pager.setTotalPage(total%pager.getPagesize()==0?total/pager.getPagesize():total/pager.getPagesize()+1);
		return pager;
	}

	@Override
	public Long getCountByUser(User currentUser) {
		return (Long) getSession().createQuery("select count(*) from "+CourseClass.class.getName()+" as  ccs where ccs.createUser=:user ")
						   .setParameter("user", currentUser)
						   .uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CourseClass> findAllByUser(User currentUser) {
		return  getSession().createQuery("from "+CourseClass.class.getName()+" as ccs where ccs.createUser=:user")
						    .setParameter("user", currentUser)
							.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CourseClass> getByCourse(Course course) {
		return getSession().createQuery("from "+CourseClass.class.getName()+" as ccs  where ccs.course=:course")
			    .setParameter("course", course)
				.list();
	}

}


text:
'use strict';

const models = require('./index');

/**
 * @summary A specification for uploading files from an Azure Batch node to
 * another location after the Batch service has finished executing the task
 * process.
  *
 */
class OutputFile {
  /**
   * Create a OutputFile.
   * @member {string} filePattern A pattern indicating which file(s) to upload.
   * Both relative and absolute paths are supported. Relative paths are
   * relative to the task working directory. The following wildcards are
   * supported: * matches 0 or more characters (for example pattern abc* would
   * match abc or abcdef), ** matches any directory, ? matches any single
   * character, [abc] matches one character in the brackets, and [a-c] matches
   * one character in the range. Brackets can include a negation to match any
   * character not specified (for example [!abc] matches any character but a,
   * b, or c). If a file name starts with "." it is ignored by default but may
   * be matched by specifying it explicitly (for example *.gif will not match
   * .a.gif, but .*.gif will). A simple example: **\*.txt matches any file that
   * does not start in '.' and ends with .txt in the task working directory or
   * any subdirectory. If the filename contains a wildcard character it can be
   * escaped using brackets (for example abc[*] would match a file named abc*).
   * Note that both \ and / are treated as directory separators on Windows, but
   * only / is on Linux. Environment variables (%var% on Windows or $var on
   * Linux) are expanded prior to the pattern being applied.
   * @member {object} destination The destination for the output file(s).
   * @member {object} [destination.container]
   * @member {string} [destination.container.path] If filePattern refers to a
   * specific file (i.e. contains no wildcards), then path is the name of the
   * blob to which to upload that file. If filePattern contains one or more
   * wildcards (and therefore may match multiple files), then path is the name
   * of the blob virtual directory (which is prepended to each blob name) to
   * which to upload the file(s). If omitted, file(s) are uploaded to the root
   * of the container with a blob name matching their file name.
   * @member {string} [destination.container.containerUrl] The URL must include
   * a Shared Access Signature (SAS) granting write permissions to the
   * container.
   * @member {object} uploadOptions Additional options for the upload
   * operation, including under what conditions to perform the upload.
   * @member {string} [uploadOptions.uploadCondition] The default is
   * taskCompletion. Possible values include: 'taskSuccess', 'taskFailure',
   * 'taskCompletion'
   */
  constructor() {
  }

  /**
   * Defines the metadata of OutputFile
   *
   * @returns {object} metadata of OutputFile
   *
   */
  mapper() {
    return {
      required: false,
      serializedName: 'OutputFile',
      type: {
        name: 'Composite',
        className: 'OutputFile',
        modelProperties: {
          filePattern: {
            required: true,
            serializedName: 'filePattern',
            type: {
              name: 'String'
            }
          },
          destination: {
            required: true,
            serializedName: 'destination',
            type: {
              name: 'Composite',
              className: 'OutputFileDestination'
            }
          },
          uploadOptions: {
            required: true,
            serializedName: 'uploadOptions',
            type: {
              name: 'Composite',
              className: 'OutputFileUploadOptions'
            }
          }
        }
      }
    };
  }
}

module.exports = OutputFile;


text:
// Generated from /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar

#pragma once

#include <fwd-POI.hpp>
#include <java/util/fwd-POI.hpp>
#include <java/lang/Object.hpp>

struct default_init_tag;

class java::util::Formatter_DateTime
    : public virtual ::java::lang::Object
{

public:
    typedef ::java::lang::Object super;

public: /* package */
    static constexpr char16_t AM_PM { u'p' };
    static constexpr char16_t CENTURY { u'C' };
    static constexpr char16_t DATE { u'D' };
    static constexpr char16_t DATE_TIME { u'c' };
    static constexpr char16_t DAY_OF_MONTH { u'e' };
    static constexpr char16_t DAY_OF_MONTH_0 { u'd' };
    static constexpr char16_t DAY_OF_YEAR { u'j' };
    static constexpr char16_t HOUR { u'l' };
    static constexpr char16_t HOUR_0 { u'I' };
    static constexpr char16_t HOUR_OF_DAY { u'k' };
    static constexpr char16_t HOUR_OF_DAY_0 { u'H' };
    static constexpr char16_t ISO_STANDARD_DATE { u'F' };
    static constexpr char16_t MILLISECOND { u'L' };
    static constexpr char16_t MILLISECOND_SINCE_EPOCH { u'Q' };
    static constexpr char16_t MINUTE { u'M' };
    static constexpr char16_t MONTH { u'm' };
    static constexpr char16_t NAME_OF_DAY { u'A' };
    static constexpr char16_t NAME_OF_DAY_ABBREV { u'a' };
    static constexpr char16_t NAME_OF_MONTH { u'B' };
    static constexpr char16_t NAME_OF_MONTH_ABBREV { u'b' };
    static constexpr char16_t NAME_OF_MONTH_ABBREV_X { u'h' };
    static constexpr char16_t NANOSECOND { u'N' };
    static constexpr char16_t SECOND { u'S' };
    static constexpr char16_t SECONDS_SINCE_EPOCH { u's' };
    static constexpr char16_t TIME { u'T' };
    static constexpr char16_t TIME_12_HOUR { u'r' };
    static constexpr char16_t TIME_24_HOUR { u'R' };
    static constexpr char16_t YEAR_2 { u'y' };
    static constexpr char16_t YEAR_4 { u'Y' };
    static constexpr char16_t ZONE { u'Z' };
    static constexpr char16_t ZONE_NUMERIC { u'z' };

    /*void ctor(); (private) */
    static bool isValid(char16_t c);

    // Generated

public:
    Formatter_DateTime();
protected:
    Formatter_DateTime(const ::default_init_tag&);

public:
    static ::java::lang::Class *class_();

private:
    virtual ::java::lang::Class* getClass0();
};


text:
This 'analysis-model' project is a library to read static analysis reports into a Java object model.
Currently it is used only by [Jenkins' warnings plug-in](https://wiki.jenkins.io/display/JENKINS/Warnings+Plugin).
Since this library has no dependencies to the Jenkins project it might be used by other static analysis visualization
tools as well in the future.

All source code is licensed under the MIT license.

Die automatisch prüfbaren Richtlinien können teilweise direkt als Warnungen in der Entwicklungsumgebung
[IntelliJ](https://www.jetbrains.com/idea/) angezeigt werden (Checkstyle und IntelliJ Inspections). Somit ist sichergestellt,
dass immer die gleichen Warnungen angezeigt werden - egal wie und wo die Java Dateien weiterverarbeitet werden.
Für FindBugs und PMD ist der Umweg über das Build Management Tool [Maven](http://maven.apache.org/) erforderlich
(die entsprechenden IntelliJ Plugins sind leider noch nicht ausgereift genug).
Die Verwendung von Maven hat zudem den Vorteil, dass die Ergebnisse hinterher leicht in den Continuous Integration Server
[Jenkins](https://jenkins.io/) eingebunden werden können.

Die Richtlinien sind in den Vorlesungen 2014/2015 entstanden und werden laufend ergänzt.
Aktuell bestehen diese aus den folgenden Abschnitten:

- [Formatierung](../master/doc/Formatierung.md)
- [Namensgebung](../master/doc/Namensgebung.md)
- [Kommentare](../master/doc/Kommentare.md)
- [Testen](../master/doc/Testen.md)
- [Testen von Schnittstellen und Basisklassen](../master/doc/Abstract-Test-Pattern.md)
- [Fehlerbehandlung](../master/doc/Fehlerbehandlung.md)
- [Best Practice](../master/doc/Best-Practice.md)

Geplant sind u.a. noch folgende Themen, die im Rahmen der Vorlesung Testen mit objektorientierten Sprachen behandelt werden:
- Testen mit Stubs und Mocks
- Verwenden von Annotations zum Markieren von Schnittstellen-Verträgen

Zur besseren Verdeutlichung der angesprochenen Themen sind diesem Projekt auch [Java Beispiele](../master/src/) angefügt,
die sich möglichst genau an diese Richtlinien halten.

Ideen und Inhalte für diesen Styleguide lieferten verschiedene Bücher, insbesondere aber das Buch
"The Elements of Java Style" [1]. Diese Bücher sind allesamt wegweisend für die Softwareentwicklung und sind
damit Pflichtlektüre für Berufstätige in der Softwareentwicklung:
- [1] "The Elements of Java Style", Vermeulen, Ambler, Bumgardner, Metz, Misfeldt, Shur und Thompson, Cambridge University Press, 2000
- [2] "The Pragmatic Programmer. From Journeyman to Master", Andrew Hunt, David Thomas, Ward Cunningham, Addison Wesley, 1999
- [3] "Code Complete: A Practical Handbook of Software Construction", Steve McConnell, Microsoft Press, 2004
- [4] "Clean Code: A Handbook of Agile Software Craftsmanship", Robert C. Martin, Prentice Hall, 2008
- [5] "Effective Java", Second Edition, Joshua Bloch, Addison Wesley, 2008
- [6] "Refactoring: Improving the Design of Existing Code", Martin Fowler, Addison Wesley, 1999

Die gesamten Dokumente dieser Kodierungsrichtlinien unterliegen der
[Creative Commons Attribution 4.0 International Lizenz](http://creativecommons.org/licenses/by/4.0/). Der
Quelltext aller Beispiele und Klassen unterliegt der [MIT Lizenz](http://opensource.org/licenses/MIT).


text:
package demo.project.model;

import java.util.Set;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

import org.caleydo.vis.lineup.model.ARankColumnModel;
import org.caleydo.vis.lineup.model.CategoricalRankColumnModel;

/**
 * @author Samuel Gratzl
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class CategoricalRankColumnSpec extends AFilterableRankColumnSpec {
	private Set<String> selection;
	private boolean filterNA = false;

	@Override
	public void load(ARankColumnModel model) {
		super.load(model);
		@SuppressWarnings("unchecked")
		CategoricalRankColumnModel<String> m = (CategoricalRankColumnModel<String>) model;
		m.setFilter(selection, filterNA, isGlobalFilter, isRankIndependentFilter);
	}

	@Override
	public void save(ARankColumnModel model) {
		super.save(model);
		@SuppressWarnings("unchecked")
		CategoricalRankColumnModel<String> m = (CategoricalRankColumnModel<String>) model;
		this.filterNA = m.isFilterNA();
		this.selection = m.getSelection();
	}

}


text:
<?php

namespace Gyn\Helper\Form\Element;

use Gyn\Helper\Interfaces\HelperInterface;
use Gyn\Helper\Helper;

/**
 *
 * @license new BSD
 * @author Andre Naves
 */
class Reset extends Helper implements HelperInterface
{
	/**
	 *
	 * @param string $id
	 * @param string $value
	 * @param array $options
	 * @return string
	 */
	public function reset($id, $value = null, array $options = array())
	{
		$attributes = '';
		$erros      = '';
		foreach ($options as $url => $opt) {
			$attributes .= " {$url}=\"{$opt}\"";
		}
		return "<input type=\"reset\" id=\"{$id}\" value=\"{$value}\"{$attributes} />";
	}
}


text:
struct tab_select_effect_t{
    struct tab_t parent;

    GHandle btn_effectTypes[EFFECT_NUM + 1];
    GHandle btn_back;
};

struct tab_t *tab_select_effect_init(struct tab_select_effect_t* opaque);

#endif //__TAB_SELECT_EFFECT_H__


text:
<!DOCTYPE html>

    <title>CSS Reference</title>

    <link href="stylesheets/screen.css" rel="stylesheet" />
    <link href="stylesheets/mozilla.css" rel="stylesheet" />

    Sorry, this service requires JavaScript.  Feel free to browse the files directly here, or visit the origin site for them at https://developer.mozilla.org/en/CSS or Fork this on Github