Skip to content

Instantly share code, notes, and snippets.

@anson0370
Created December 14, 2011 15:56
Show Gist options
  • Save anson0370/1477146 to your computer and use it in GitHub Desktop.
Save anson0370/1477146 to your computer and use it in GitHub Desktop.
code snippets in pikachu
implicit def attrStrOrElse(node: Node) = new {
def attrStrOrElse(attr: String)(default: => String): String = {
node.attribute(attr) match {
case None => default
case Some(Nil) => default
case Some(Text(attrStr)) => attrStr
}
}
}
val innerName = processXML.attrStrOrElse("name") {
throw new IllegalArgumentException("attribute name in process is required")
}
package com.tmall.pokemon.pikachu.j.model;
import com.tmall.pokemon.pikachu.core.model.StateLike;
import com.tmall.pokemon.pikachu.helper.Helper;
import scala.Option;
import scala.Tuple2;
import scala.xml.Node;
import java.util.Map;
/**
* @author <b>guichen</b> JStateLike.java - 2011-10-17
*/
public abstract class JStateLike implements StateLike {
/**
* name of state
*
* @return
*/
public abstract String name();
/**
* prepare before execute the state
*
* @param context context for execute
* @return result set will put to context
*/
public abstract ResultSet prepare(Map<String, Object> context);
/**
* how to execute the state
*
* @param context context for execute
* @return result set will put to context
*/
public abstract ResultSet execute(Map<String, Object> context);
/**
* where the state go
*
* @param context context for execute
* @return the name of next state, if no next state here, return null
*/
public abstract String willGo(Map<String, Object> context);
/**
* parse XML to state
*
* @param xml XML node string described the state
* @return state
*/
public abstract JStateLike parse(String xml);
// /////////////////////////////////////
// final implements of SCALA API //
// /////////////////////////////////////
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public final Tuple2<Object, scala.collection.Map<String, Object>> prepare(
scala.collection.Map context) {
ResultSet resultSet = this.prepare(Helper
.scalaMap_T_S_A_2JavaMap_T_S_O(context));
return new Tuple2(resultSet.isCan(),
Helper.javaMap_T_S_O_2scalaMap_T_S_A(resultSet.getIncrContext()));
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public final Tuple2<Object, scala.collection.Map<String, Object>> execute(
scala.collection.Map context) {
ResultSet resultSet = this.execute(Helper
.scalaMap_T_S_A_2JavaMap_T_S_O(context));
return new Tuple2(resultSet.isCan(),
Helper.javaMap_T_S_O_2scalaMap_T_S_A(resultSet.getIncrContext()));
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public final Option<String> willGo(scala.collection.Map context) {
return Option.apply(this.willGo(Helper
.scalaMap_T_S_A_2JavaMap_T_S_O(context)));
}
@Override
public final StateLike parse(Node xml) {
this.parse(xml.buildString(true));
return this;
}
// /////////////////////////////////////
// final implements of SCALA API //
// /////////////////////////////////////
}
trait TA {
def f = println(1)
def g = println(2)
}
object A extends TA
trait TB extends TA {
override def g = println(3)
}
object B extends TB
B.f // while print 1
B.g // while print 3
def tx[T](exec: => T): T = {
getTransactionTemplate.execute(new TransactionCallback {
def doInTransaction(status: TransactionStatus): AnyRef = {
exec.asInstanceOf[AnyRef]
}
}).asInstanceOf[T]
}
tx {
// transaction code here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment