myGroovy

2787 ワード

1. one static map call another static String as the key, (groovy ugly) here^^!
 
class Test1{
 
  Map partnerRetailers = [
          "${RetailerSyncFactoryService.MACYS}" : 1,
          "${RetailerSyncFactoryService.TARGET}": 2,
          "${RetailerSyncFactoryService.CRATE}" : 3,
          "${RetailerSyncFactoryService.BLOOMINGDALES}" : 4,
          "${RetailerSyncFactoryService.WILLIAMSSONOMA}" : 5,
          "${RetailerSyncFactoryService.POTTERYBARN}" : 6
          ]
           /* 
  static Map partnerRetailers = [:]
  static {
      partnerRetailers.put(RetailerSyncFactoryService.TARGET, 2)
     }
     */
}

class RetailerSyncFactoryService {
    public static final String MACYS = "macy's"
    public static final String BLOOMINGDALES = "bloomingdale's"
    public static final String TARGET = "target"
    public static final String CRATE = "crate and barrel"
    public static final String WILLIAMSSONOMA = "williams-sonoma"
    public static final String POTTERYBARN = "pottery barn"    
}


def temp = new Test1()
temp.partnerRetailers.put(RetailerSyncFactoryService.MACYS, 1)
def result = temp.partnerRetailers.keySet() as List
result.each {
    //println temp.partnerRetailers.keySet().contains(it)
}
println temp.partnerRetailers.keySet().contains("${RetailerSyncFactoryService.MACYS}")
println temp.partnerRetailers.keySet().contains("macy's")
println temp.partnerRetailers.keySet().contains("target")

  
2. in ( instanceof) + switch case
 
def console(property){   
    if (property in List){   
        switch(property.size()) {   
            case 1: println 1; break  
            case 2: println 2; break  
            case 3: println 3; break  
            case 4: println 4; break  
              
        }          
    }   
}   
  
def property1 = "str"  
def property2 = ["p1","p2","p3"]   
console(property2)  

3. Data + simpleDataFormat
 
  static Date getDateLaunch = {
    // the Calendar month is from 1
    Calendar calendar = Calendar.getInstance(); calendar.set(2010, 3, 1, 0, 0, 0); return calendar.getTime();
  }
//  private static final Date LAUNCH_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-04-01 12:00:00")