Commits

Zemian Deng committed 24be72d

Added kotlin demos.

Comments (0)

Files changed (12)

 .project
 .classpath
 .DS_Store
-scripts/test.*
+.idea

kotlin-demo/kotlin-demo.iml

+<?xml version="1.0" encoding="UTF-8"?>
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="false">
+    <output url="file://$MODULE_DIR$/target/classes" />
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
+      <excludeFolder url="file://$MODULE_DIR$/target" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:0.4.68" level="project" />
+    <orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-runtime:0.4.68" level="project" />
+  </component>
+</module>
+

kotlin-demo/scripts/run-java

+#!/usr/bin/env bash
+#
+# Copyright 2012 Zemian Deng
+#
+# 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
+#
+#    http://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.
+
+# A wrapper script that run any Java application in unix/cygwin bash env.
+#
+# This script is assumed to be located in an application's "bin" directory. It will
+# auto resolve its directory location relative to the application path (which is one
+# parent up from the script.) Therefore, this script can be run any where in the file
+# system and it will still reference the same application directory.
+#
+# This script will by default auto setup a Java classpath that picks up any "config"
+# and "lib" directories under the application directory. It also will also add a
+# any typical Maven project output directories such as "target/test-classes",
+# "target/classes", and "target/dependency" into classpath. This can be disable by
+# setting RUN_JAVA_NO_AUTOCP=1.
+#
+# If the "Default parameters" section bellow doesn't match to user's env, then user
+# may override these variables in their terminal session or preset them in shell's
+# profile startup script. The values of all path should be in cygwin/unix path,
+# and this script will auto convert them into Windows path where is needed.
+#
+# User may customize the Java classpath by setting RUN_JAVA_CP, which will prefix to existing
+# classpath, or use the "-cp" option, which will postfix to existing classpath.
+#
+# Usage:
+#   run-java [java_opts] <java_main_class> [-cp /more/classpath] [-Dsysprop=value]
+#
+# Example:
+#   run-java example.Hello
+#   run-java example.Hello -Dname=World
+#   run-java org.junit.runner.JUnitCore example.HelloTest -cp "$HOME/apps/junit/lib/*"
+#
+# Created by: Zemian Deng 03/09/2012
+
+# This run script dir (resolve to absolute path)
+SCRIPT_DIR=$(cd $(dirname $0) && pwd)    # This dir is where this script live.
+APP_DIR=$(cd $SCRIPT_DIR/.. && pwd)      # Assume the application dir is one level up from script dir.
+
+# Default parameters
+JAVA_HOME=${JAVA_HOME:=$HOME/apps/jdk}     # This is the home directory of Java development kit.
+RUN_JAVA_CP=${RUN_JAVA_CP:=$CLASSPATH}     # A classpath prefix before -classpath option, default to $CLASSPATH
+RUN_JAVA_OPTS=${RUN_JAVA_OPTS:=}           # Java options (-Xmx512m -XX:MaxPermSize=128m etc)
+RUN_JAVA_DEBUG=${RUN_JAVA_DEBUG:=}         # If not empty, print the full java command line before executing it.
+RUN_JAVA_NO_PARSE=${RUN_JAVA_NO_PARSE:=}   # If not empty, skip the auto parsing of -D and -cp options from script arguments.
+RUN_JAVA_NO_AUTOCP=${RUN_JAVA_NO_AUTOCP:=} # If not empty, do not auto setup Java classpath
+RUN_JAVA_DRY=${RUN_JAVA_DRY:=}             # If not empty, do not exec Java command, but just print
+
+# OS specific support.  $var _must_ be set to either true or false.
+CYGWIN=false;
+case "`uname`" in
+  CYGWIN*) CYGWIN=true ;;
+esac
+
+# Define where is the java executable is
+JAVA_CMD=java
+if [ -d "$JAVA_HOME" ]; then
+	JAVA_CMD="$JAVA_HOME/bin/java"
+fi
+
+# Auto setup applciation's Java Classpath (only if they exists)
+if [ -z "$RUN_JAVA_NO_AUTOCP" ]; then
+	if $CYGWIN; then
+		# Provide Windows directory conversion
+		JAVA_HOME_WIN=$(cygpath -aw "$JAVA_HOME")
+		APP_DIR_WIN=$(cygpath -aw "$APP_DIR")
+
+		if [ -d "$APP_DIR_WIN\config" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\config" ; fi
+		if [ -d "$APP_DIR_WIN\target\test-classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\test-classes" ; fi
+		if [ -d "$APP_DIR_WIN\target\classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\classes" ; fi
+		if [ -d "$APP_DIR_WIN\target\dependency" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\dependency\*" ; fi
+		if [ -d "$APP_DIR_WIN\lib" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\lib\*" ; fi
+	else
+		if [ -d "$APP_DIR/config" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/config" ; fi
+		if [ -d "$APP_DIR/target/test-classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/test-classes" ; fi
+		if [ -d "$APP_DIR/target/classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/classes" ; fi
+		if [ -d "$APP_DIR/target/dependency" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/dependency/*" ; fi
+		if [ -d "$APP_DIR/lib" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/lib/*" ; fi
+	fi
+fi
+
+# Parse addition "-cp" and "-D" after the Java main class from script arguments
+#   This is done for convenient sake so users do not have to export RUN_JAVA_CP and RUN_JAVA_OPTS
+#   saparately, but now they can pass into end of this run-java script instead.
+#   This can be disable by setting RUN_JAVA_NO_PARSE=1.
+if [ -z "$RUN_JAVA_NO_PARSE" ]; then
+	# Prepare variables for parsing
+	FOUND_CP=
+	declare -a NEW_ARGS
+	IDX=0
+
+	# Parse all arguments and look for "-cp" and "-D"
+	for ARG in "$@"; do
+		if [[ -n $FOUND_CP ]]; then
+			if [ "$OS" = "Windows_NT" ]; then
+				# Can't use cygpath here, because cygpath will auto expand "*", which we do not
+				# want. User will just have to use OS path when specifying "-cp" option.
+				#ARG=$(cygpath -w -a $ARG)
+				RUN_JAVA_CP="$RUN_JAVA_CP;$ARG"
+			else
+				RUN_JAVA_CP="$RUN_JAVA_CP:$ARG"
+			fi
+			FOUND_CP=
+		else
+			case $ARG in
+			'-cp')
+				FOUND_CP=1
+				;;
+			'-D'*)
+				RUN_JAVA_OPTS="$RUN_JAVA_OPTS $ARG"
+				;;
+			*)
+				NEW_ARGS[$IDX]="$ARG"
+				let IDX=$IDX+1
+				;;
+			esac
+		fi
+	done
+
+	# Display full Java command.
+	if [ -n "$RUN_JAVA_DEBUG" ] || [ -n "$RUN_JAVA_DRY" ]; then
+		echo "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "${NEW_ARGS[@]}"
+	fi
+
+	# Run Java Main class using parsed variables
+	if [ -z "$RUN_JAVA_DRY" ]; then
+		"$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "${NEW_ARGS[@]}"
+	fi
+else
+	# Display full Java command.
+	if [ -n "$RUN_JAVA_DEBUG" ] || [ -n "$RUN_JAVA_DRY" ]; then
+		echo "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "$@"
+	fi
+
+	# Run Java Main class
+	if [ -z "$RUN_JAVA_DRY" ]; then
+		"$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "$@"
+	fi
+fi

kotlin-demo/scripts/run.sh

+#!/usr/bin/env bash
+# Author: Zemian Deng, Date: 2012-11-13_22:53:54
+#
+# Run Kotlin Program. User only needs to give the package name, and it will auto add the .namepace suffix.
+# Example: scripts/run.sh hello
+#
+DIR=$(cd $(dirname $0) && pwd)
+PKG=$1
+shift
+$DIR/run-java "$PKG.namespace" "$@"

kotlin-demo/src/main/kotlin/ClassDemo.kt

+package classdemo
+open class B
+class C : B()
+fun main(args : Array<String>) {
+    println(C())
+}

kotlin-demo/src/main/kotlin/CollectionDemo.kt

+package collectiondemo
+fun main(args : Array<String>) {
+	println(array(11.00, 12.25, 13.99, 14.01))
+    println(arrayList(1,2,3))
+    println(hashSet("a", "b", "c", "a")) // There should only be 3 unique elements
+    println(hashMap("a" to 11, "b" to 12, "c" to 13))
+}

kotlin-demo/src/main/kotlin/DataClassDemo.kt

+package dataclassdemo
+data class Employee(val id : Int, val name : String)
+fun main(args : Array<String>) {
+    val list = arrayList(
+        Employee(101, "Zemian"),
+        Employee(102, "Julie")
+    )
+    println("List $list")
+}

kotlin-demo/src/main/kotlin/FactoryDemo.kt

+package factorydemo
+
+fun main(args : Array<String>) {
+	println("Hello")
+
+	val max : Int = if (args.size >= 1) args[0].toInt() else 10
+	for (i in (0..max))
+		println(fact(i))
+
+	for (i in (0..max))
+		println(fact2(i))
+}
+
+fun fact(n : Int) : Int {
+	if (n <= 1) return 1
+	else return n * fact(n -1)
+}
+
+fun fact2(n : Int) : Int {
+	when (n) {
+		0, 1 -> return 1
+		else -> return n * fact2(n - 1)
+	}
+}

kotlin-demo/src/main/kotlin/Hello.kt

-package deng
-
+package hello
 fun main(args : Array<String>) {
-	println("Hello")
-
-	val max : Int = if (args.size >= 1) args[0].toInt() else 10
-	for (i in (0..max))
-		println(fact2(i))
+    println("Hello2")
 }
-
-fun fact(n : Int) : Int {
-	if (n <= 1) return 1
-	else return n * fact(n -1)
-}
-
-fun fact2(n : Int) : Int {
-	when (n) {
-		0, 1 -> return 1
-		else -> return n * fact2(n - 1)
-	}
-}

kotlin-demo/src/main/kotlin/Hello2.kt

-package deng2
-
-fun main(args : Array<String>) {
-	println("Hello2")
-}

kotlin-demo/src/main/kotlin/LoopDemo.kt

+package loopdemo
+fun main(args : Array<String>) {
+    for (s in args)
+    	println("#1 Arg: $s")
+
+    var i = 0
+    while (i < args.size)
+    	println("#2 Arg: ${args[i++]}")
+
+    /*for ((name, value) in System.getProperties())
+    	if (name.startsWith("java"))
+    		println("Sys prop: $name=$value")*/
+}

kotlin-demo/src/main/kotlin/Test.kt

+package test
+fun main(args : Array<String>) {
+	for ((name, value) in System.getProperties())
+		if (name.startsWith("java"))
+			println("Sys prop: $name=$value")
+}