Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Last active May 3, 2017 16:08
Show Gist options
  • Save jluismm2311/48e163781edb6081bbbac35b344baffa to your computer and use it in GitHub Desktop.
Save jluismm2311/48e163781edb6081bbbac35b344baffa to your computer and use it in GitHub Desktop.
package com.groovy.testDictionary
class Dictionary {
def key
def value
def dict = [:]
Dictionary (){}
Dictionary (def key, def value){
put(key, value)
}
void validate(def key, def value){
validateNotNullObject(key)
validateNotNullObject(value)
}
void validateNotNullObject(def str){
if(str == null){throw new RuntimeException("can not be null key/value")}
}
def put(def key, def value){
try{
validate(key, value)
this.dict[key] = value
println "have this dictionary: ${this.dict}"
}catch(RuntimeException e){
println "have a exception ${e.getMessage()}"
}
}
void printInfo(){
println "i have this data in the dictionary \n ${this.dict}"
}
}
def myDictionary = new Dictionary()
myDictionary.printInfo()
myDictionary.put('Name','Luis')
myDictionary.printInfo()
myDictionary.put('nullValue',null)
myDictionary.put(null,'nullKey')
myDictionary.printInfo()
myDictionary.put('age',28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment