Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Groovy's Builder

Groovy's Builder

2010-12-20のGlassfish & JavaFX合同勉強会で発表した資料です。

Yasuharu Nakano

March 25, 2023
Tweet

More Decks by Yasuharu Nakano

Other Decks in Programming

Transcript

  1. Ad::(&YDFM"1* 㾎ΈΜͳେ޷͖&YDFM΁ͷΞΫηε͕௒؆୯ʹ 㾎IUUQTHJUIVCDPNOPCFBOTHFYDFMBQJ @GrabConfig(systemClassLoader=true) // for GroovyServ @GrabResolver(name="kobo-repo", root="http://github.com/kobo/maven-repo/raw/master/release") @Grab("org.jggug.kobo:gexcelapi:0.2")

    import org.jggug.kobo.gexcelapi.GExcel def book = GExcel.open(args[0]) def sheet = book[0] println sheet.A1.value sheet.A1.value = "New value of A1" sheet.A_.each{ cell -> println cell.value } sheet._1.each{ cell -> println cell.value } sheet.A1_B6.each{ row -> row.each { cell -> println it.value } }
  2. Sample of +BWB'9 4DSJQU import  javafx.stage.*; import  javafx.scene.*; import  javafx.scene.shape.*;

    import  javafx.scene.paint.*; Stage  {        title:  "Sample  by  JavaFX  Script"        width:  600        height:  450        scene:  Scene  {                fill:  Color.LIGHTSKYBLUE                content:  Rectangle  {                        x:  25,  y:  40                        width:  100,  height:  50                        fill:  Color.RED                }        } }
  3. 6

  4. Sample of 4XJOH #VJMEFS import  groovy.swing.SwingBuilder import  java.awt.BorderLayout  as  BL

    int  count  =  0 new  SwingBuilder().edt  {        frame(                title:'Hello',                size:[600,  450],                show:true        )  {                borderLayout()                textLabel  =  label(                        text:'Sample  of  SwingBuilder',                        constraints:  BL.NORTH                )                button(                        text:  'Click  Me!',                        actionPerformed:  {                                count++                                textLabel.text  =                                  "Clicked  ${count}  time(s)."                                println  "clicked"                        },                        constraints:  BL.SOUTH                )        } }
  5. Sample of '9 #VJMEFS import  griffon.builder.fx.FxBuilder import  javafx.stage.* import  javafx.scene.*

    import  javafx.scene.shape.* import  javafx.scene.paint.* new  FxBuilder().edt  {        stage(                title:  "Sample  by  FxBuilder  of  Groovy",                width:  600,                height:  450,                scene:  scene(fill:  Color.$LIGHTSKYBLUE)  {                        rectangle(                                x:  25,  y:  40,                                width:  100,  height:  50,                                fill:  Color.$RED                        )                }        ) } http://griffon.codehaus.org/FxBuilder+Plugin
  6. import  griffon.builder.fx.FxBuilder import  javafx.stage.* import  javafx.scene.* import  javafx.scene.shape.* import  javafx.scene.paint.*

    new  FxBuilder().edt  {        stage(                title:  "Sample  by  FxBuilder  of  Groovy",                width:  600,                height:  450,                scene:  scene(fill:Color.$LIGHTSKYBLUE){                        rectangle(                                x:  25,  y:  40,                                width:  100,  height:  50,                                fill:  Color.$RED                        )                }        ) } import  javafx.stage.*; import  javafx.scene.*; import  javafx.scene.shape.*; import  javafx.scene.paint.*; Stage  {        title:  "Sample  by  JavaFX  Script",        width:  600        height:  450        scene:  Scene  {                fill:  Color.LIGHTSKYBLUE                content:  Rectangle  {                        x:  25,  y:  40                        width:  100,  height:  50                        fill:  Color.RED                }        } } FxBuilder ΄΅Ұக ≒
  7. $MPTVSF groovy.lang.Closure  closure  =  {  String  arg  -­‐>    

       return  "hoge:${arg}" } //  ΫϩʔδϟͷධՁ closure.call(“foo”)    //=>  “hoge:foo” //  γϯλοΫεγϡΨʔ൛ closure(“foo”)              //=>  “hoge:foo” Java8ͷΫϩʔδϟͱͷҧ͍͸ # ͕෇͔ͳ͍ͱ͜Ζʢݟͨ໨্ʣ
  8. //  Ҿ਺ͳ͠(໌ࣔత) {  -­‐>  "hoge"  }.call()        

                         //=>  "hoge" //  Ҿ਺͋Γ(ܕলུ) {  num  -­‐>  "hoge:${num}"  }.call(123)  //=>  "hoge:123" //  Ҿ਺͋Γ(ෳ਺) {  num,  String  id,  option  -­‐>        return  "hoge:${num}:${id}:${option}" }.call(123,  “0A”,  “X”)                          //=>  "hoge:123:0A:X" //  ҉໧Ҿ਺ͷit {  "hoge:${it}"  }.call(123)                  //=>  "hoge:123" Args of $MPTVSF
  9. //  ؙׅހ෇͖(໌ࣔత) someMethod({  num  -­‐>  num  *  2  }) //

     Ϋϩʔδϟͷ৔߹͸ɺؙׅހ͸লུͰ͖Δ someMethod  {  num  -­‐>  num  *  2  } //  ଞͷҾ਺ͱͷ૊߹ͤ someMethod(arg1,  arg2,  {  num  -­‐>  num  *  2  }) someMethod(arg1,  arg2)  {  num  -­‐>  num  *  2  }  //্ͱ౳Ձ $MPTVSF as Args
  10. MOP::NFUIPE.JTTJOH class  MopSample  {        def  methodMissing(String  name,

     args)  {                println  "Method:  $name($args)"        } } def  sample  =  new  MopSample() sample.hoge() //  =>  “Method:  hoge([])” sample.foo(123,  456) //  =>  “Method:  foo([123,  456])” ະఆٛͷ ϝιουݺͼग़ ͠ΛϑοΫ͢Δ
  11. MOP::QSPQFSUZ.JTTJOH class  MopSample  {        def  propertyMissing(String  name)

     {  //  for  getter                println  "Property:  $name"        }        def  propertyMissing(String  name,  value)  {  //  for  setter                println  "Property:  $name  =  $value"        } } sample  =  new  MopSample2() sample.hoge              //=>  "Property:  hoge" sample.bar                //=>  "Property:  bar" sample.baz  =  "!!"  //=>  "Property:  baz  =  !!" ະఆٛͷ ϓϩύςΟ ΞΫηεΛ ϑοΫ͢Δ
  12. ex. .BSLVQ#VJMEFS def  writer  =  new  StringWriter() def  builder  =

     new  groovy.xml.MarkupBuilder(writer) builder.books  {        book(published:"2010-­‐12-­‐20")  {                author  "nobeans"                title    "GroovyϏϧμೖ໳"        }        3.times  {                book  "Groovy఻આ  ୈ${it+1}ר"        } } println  writer.toString() <books>    <book  published='2010-­‐12-­‐20'>        <author>nobeans</author>        <title>GroovyϏϧμೖ໳</title>    </book>    <book>Groovy఻આ  ୈ1ר</book>    <book>Groovy఻આ  ୈ2ר</book>    <book>Groovy఻આ  ୈ3ר</book> </books> ←Describe here Format as Xml→
  13. Basic Theory of #VJMEFS 㾎CVJMEFSͷCPPLTϝιουʹɺͭͷΫϩʔδϟ ΛҾ਺ͱͯ͠౉͍ͯ͠Δ 㾎CPPLTϝιουͰΫϩʔδϟΛධՁ͢Δͱ builder.books  {  

         book(published:"2010-­‐12-­‐20")  {                author  "nobeans"                title    "GroovyϏϧμೖ໳"        }        3.times  {                book  "Groovy఻આ  ୈ${it+1}ר"        } }
  14. Basic Theory of #VJMEFS 㾎CPPLϝιου͕ݺ͹ΕΔͱ 㾎 B ϝιουະఆٛͳͷͰNFUIPE.JTTJOH͕ݺ͹ΕΔ 㾎 C

    lCPPLz༻ʹొ࿥͞Εͨ'BDUPSZ͕ݺ͹ΕΔ builder.books  {        book(published:"2010-­‐12-­‐20")  {                author  "nobeans"                title    "GroovyϏϧμೖ໳"        }        3.times  {                book  "Groovy఻આ  ୈ${it+1}ר"        } }
  15. Classification of #VJMEFS Dynamically Defined Method Statically Defined Method Extends

    Abstract Class Extends Abstract Class Independent extends BuilderSupport extends FactoryBuilderSupport MarkupBuilder NodeBuilder DOMBuilder AntBuilder SAXBuilder SwingBuilder FxBuilder ObjectGraphBuilder JmxBuilder ConfigSlurper CliBuilder
  16. #VJMEFS4VQQPSU public  abstract  class  BuilderSupport  ...  {      //

     ...snip...      protected  abstract  void  setParent(Object  parent,  Object  child);      protected  abstract  Object  createNode(Object  name);      protected  abstract  Object  createNode(Object  name,  Object  value);      protected  abstract  Object  createNode(Object  name,  Map  attributes);      protected  abstract  Object  createNode(Object  name,  Map  attributes,                                                                                Object  value);      //  ...snip... } 㾎4"9ύʔα෩ 㾎ඞཁͳDSFBUF/PEFϝιουͳͲΛΦʔόϥΠυ͓ͯ͘͠ 㾎%4-هड़ΛॱʹධՁ͍͖ͯ͠ɺݟ͚ͭͨཁૉʹରԠ͢Δ ϝιου͕ݺ͹ΕΔ
  17. 'BDUPSZ#VJMEFS4VQQPSU public  class  SwingBuilder  extends  FactoryBuilderSupport  {      

     //  ...snip...        def  registerSupportNodes()  {                registerFactory("action",  new  ActionFactory())                registerFactory("actions",  new  CollectionFactory())                registerFactory("map",  new  MapFactory())                registerFactory("imageIcon",  new  ImageIconFactory())                registerFactory("buttonGroup",  new  ButtonGroupFactory())                addAttributeDelegate(ButtonGroupFactory.&buttonGroupAttributeDelegate)                //  ...snip...        }        //  ...snip... } 㾎%4-ཁૉͱͦΕʹର͢ΔݸผͷϑΝΫτϦΛ͋Β͔͡Ίొ ࿥͓ͯ͘͠ 㾎%4-هड़ΛॱʹධՁ͍͖ͯ͠ɺݟ͚ͭͨཁૉʹରԠ͢Δ ϑΝΫτϦ͕ݺ͹ΕΔ
  18. CREATE  TABLE  book  (        id  INTEGER,  

         title  TEXT,        published  TIMESTAMP,        author  INTEGER ) CREATE  TABLE  author  (        id  INTEGER,        name  TEXT ) def  writer  =  new  StringWriter() new  DdlBuilder(writer).scheme  {    book  {        id                type:int        title          type:String        published  type:Date        author        type:int    }    author  {        id                type:int        name            type:String    } } println  writer.toString() 6TBHF
  19. class  DdlBuilder  {        def  writer    

       DdlBuilder(writer)  {  this.writer  =  writer  }        def  scheme(Closure  cls)  {                new  NodeBuilder().scheme(cls).each  {  table  -­‐>                        writer.println  "CREATE  TABLE  ${table.name()}  ("                        writer.println  table.collect  {  col  -­‐>                                "        ${col.name()}  ${type(col.attribute('type'))}"                        }.join(",\n")                        writer.println  ")"                }        }        private  type(clazz)  {                switch  (clazz)  {                        case  String:  return  "TEXT"                        case  int:        return  "INTEGER"                        case  Date:      return  "TIMESTAMP"                        default:                            throw  new  RuntimeException("unsupported  type:  $clazz")                }        } } $PEF