Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 13, 2016
0 parents commit 1db9c20
Show file tree
Hide file tree
Showing 3,507 changed files with 792,799 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added 4220.pdf
Binary file not shown.
Binary file added 4221.pdf
Binary file not shown.
Binary file added 9781430216001.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions JawadSourceCde/code/Chapter01/GroovyFileReader
@@ -0,0 +1,2 @@
f = new File("test.txt")
f.eachLine{println it}
12 changes: 12 additions & 0 deletions JawadSourceCde/code/Chapter01/ListSum.groovy
@@ -0,0 +1,12 @@
class ListSum{
public static int sum(list){
def result = 0
list.each{
result += it
}
return result
}
}

a = [1,2,3,4,5]
println ListSum.sum(a)
9 changes: 9 additions & 0 deletions JawadSourceCde/code/Chapter01/Lists.groovy
@@ -0,0 +1,9 @@
list1 = []; list2 = [];list3 = []
for (element in 0..9){
list1 += element
list2 += element + 1
list3 += (list1[element] + list2[element]) / 2
}
list3.each{
println it
}
1 change: 1 addition & 0 deletions JawadSourceCde/code/Chapter01/SubStringTest.groovy
@@ -0,0 +1 @@
assert "Test_String".substring(0,4) == "Test"
5 changes: 5 additions & 0 deletions JawadSourceCde/code/Chapter01/SubStringTest.java
@@ -0,0 +1,5 @@
public class SubStringTest {
public static void main(String[] args) {
System.out.println("Test_String".substring(0,4));
}
}
@@ -0,0 +1,33 @@
package com.apress.groovygrailsrecipes.chap01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SampleFileReader {
static public String readFile(File file) {
StringBuffer contents = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = null;
while ((line = reader.readLine()) != null) {
contents.append(line).append
(System.getProperty("line.separator"));
}
} finally {
reader.close();
}
} catch (IOException ex) {
contents.append(ex.getMessage());
ex.printStackTrace();
}
return contents.toString();
}

public static void main(String[] args) {
File file = new File("test.txt");
System.out.println(SampleFileReader.readFile(file));
}
}
3 changes: 3 additions & 0 deletions JawadSourceCde/code/Chapter01/test.txt
@@ -0,0 +1,3 @@
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis quis nunc. Etiam mollis velit nec eros. Suspendisse id velit a diam viverra porta. Fusce est lacus, dictum id, hendrerit nec, congue vitae, elit. Nullam vulputate. Quisque ultricies sapien et ipsum. Nunc urna lectus, hendrerit non, semper ut, dapibus et, enim. Ut venenatis faucibus sapien. Pellentesque feugiat, sem eget tincidunt eleifend, quam nulla sodales quam, ut porttitor enim leo in nulla. Proin dolor dolor, faucibus a, vestibulum ut, sagittis lobortis, nisi. Fusce quis justo eu pede dignissim ultricies. Donec vel justo. Vivamus nec sem at ante tincidunt bibendum. Ut ornare. Vivamus dolor. Proin a nunc. Nulla elementum, magna non rutrum euismod, eros lectus molestie lorem, ut ultrices eros neque faucibus nisi. Aenean sed elit id lectus pulvinar porta. Integer leo lectus, condimentum eu, varius eu, molestie ac, risus.

Nam erat neque, rutrum eu, tristique cursus, scelerisque eget, urna. Pellentesque suscipit dolor quis augue. Nunc rutrum leo sit amet elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce blandit massa et est. Vivamus orci. Fusce mollis elementum felis. Fusce dignissim, elit id semper scelerisque, justo urna dignissim justo, at euismod urna arcu a dui. Nullam faucibus. Ut volutpat, urna in adipiscing aliquam, nibh mauris ullamcorper orci, sit amet aliquam elit magna nec mi. Ut lacus. Aliquam eget metus ac nibh hendrerit interdum. Quisque massa augue, facilisis vel, tincidunt in, euismod a, ligula. Aenean aliquam sapien in diam aliquam sollicitudin. Pellentesque eget risus vel leo accumsan malesuada. Mauris enim. Vivamus eleifend. Proin accumsan congue velit. Suspendisse quam magna, hendrerit vehicula, pharetra eu, dapibus sit amet, tortor.
2 changes: 2 additions & 0 deletions JawadSourceCde/code/Chapter02/Assertions.groovy
@@ -0,0 +1,2 @@
println "Welcome to $language"
return "The End"
2 changes: 2 additions & 0 deletions JawadSourceCde/code/Chapter02/AssertionsCustomError.groovy
@@ -0,0 +1,2 @@
a = [1,2,3]
assert a.size() == 2 , "list ${a} must be of size 2"
7 changes: 7 additions & 0 deletions JawadSourceCde/code/Chapter02/AutomaticConversion
@@ -0,0 +1,7 @@
int var = 1
var = 2.7
assert var == 2
assert var.class in Integer
var = '1' as char
assert var == 49
assert var.class in Integer
14 changes: 14 additions & 0 deletions JawadSourceCde/code/Chapter02/CalculateRaise.groovy
@@ -0,0 +1,14 @@
public class Employee{
def salary
public double calculateRaise(c){
return c(salary)
}
}

Employee employee1 = new Employee(salary:1000)
def raise1 = {salary -> (salary * 1.5) }
assert employee1.calculateRaise(raise1) == 1500

Employee employee2 = new Employee(salary:500)
def raise2 = {salary -> (salary + 300) }
assert employee2.calculateRaise(raise2) == 800
18 changes: 18 additions & 0 deletions JawadSourceCde/code/Chapter02/Closures.groovy
@@ -0,0 +1,18 @@
//Simple closure with no arguments
def clos1 = { println "hello world!" }
//Calling the closure
clos1()

//A closure with arguments
def clos2 = {arg1,arg2 -> println arg1+arg2}
clos2(3,4)

//A closure defined inside a method. The closure is bound to the
//variables within its scope
def method1(book){
def prefix = "The title of the book is: "
return {println prefix + book}
}

def clos3 = method1("Groovy")
clos3()
4 changes: 4 additions & 0 deletions JawadSourceCde/code/Chapter02/DoubleQuotesString.groovy
@@ -0,0 +1,4 @@
def language = "Groovy"
def text = "Welcome to $language"
assert text == "Welcome to Groovy"
assert text as groovy.lang.GString
4 changes: 4 additions & 0 deletions JawadSourceCde/code/Chapter02/DynamicTyping.groovy
@@ -0,0 +1,4 @@
def var = 1
assert var.class in java.lang.Integer
var = 'Hello World'
assert var.class in java.lang.String
16 changes: 16 additions & 0 deletions JawadSourceCde/code/Chapter02/FileCreator.java
@@ -0,0 +1,16 @@
//FileCreator.java:

import java.io.File;
import java.io.IOException;

public class FileCreator {
public static void main (String args[]){
File file = new File("C:\\temp\\groovy.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

34 changes: 34 additions & 0 deletions JawadSourceCde/code/Chapter02/GDK.groovy
@@ -0,0 +1,34 @@
//java.lang.Object
def a = [1,2,3]
assert a.any {it > 2} //At least one element satisfies the condition
assert a.every{it > 0} //All elements must satisfy the condition
//Iterate over all the elements calling the closure on each item
assert a.collect{it * 2} == [2,4,6]
assert a.findAll{it > 2} == [3] //Finds all elements that satisfy the condition
a.print(a) //Prints the values of a, can be also written as print(a)

//java.lang.Number
def x = 10
assert x.abs() == 10 //Returns absolute value
assert x.compareTo(3) == 1 //Compares two numbers
assert x.div(2) == 5 //Divides two numbers
def total = 0
x.downto(5) {
number -> total += number} //Sums the numbers from 10 to 5 inclusive
assert total == 45
total = 0
x.upto(15){
number -> total += number} //Sums the numbers from 10 to 15 inclusive
assert total == 75

//java.io.File
def f = new File("C:\\temp\\groovy.txt") //Marks a file for creation
f.text = "Groovy rocks!" //File will be created if it doesn't exist
assert f.exists()
assert f.text == "Groovy rocks!"
f.append("Doesn't?") //Appends text to the file
assert f.text =="Groovy rocks!Doesn't?"
f.renameTo(new File("C:\\temp\\groovyRenamed.txt")) //Renames a file
assert f.name == "groovy.txt" //Files are immutable
[new File("C:\\temp\\groovy.txt"),new File("C:\\temp\\groovyRenamed.txt")].
each{it.delete()} //Deletes both files
14 changes: 14 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyBeans.groovy
@@ -0,0 +1,14 @@
class Person{
String firstName
String lastName
def getName(){
firstName + ' ' + lastName
}
static void main(args) {
def person = new Person()
person.firstName = 'Bashar'
person.lastName = 'Abdul'
assert person.firstName == 'Bashar'
assert person.name == 'Bashar Abdul'
}
}
3 changes: 3 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyCastException.groovy
@@ -0,0 +1,3 @@
int var = 1
assert var as java.lang.Integer
var = 'Hello World'
19 changes: 19 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyClassLoaderExample.java
@@ -0,0 +1,19 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;

public class GroovyClassLoaderExample{

public static void main(String args[]) {
try {
GroovyClassLoader loader = new GroovyClassLoader();
Class fileCreator = loader.parseClass
(new File("GroovySimpleFileCreator.groovy"));
GroovyObject object = (GroovyObject) fileCreator.newInstance();
object.invokeMethod("createFile", "C:\\temp\\emptyFile.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
20 changes: 20 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyClassLoaderExample2.java
@@ -0,0 +1,20 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;

public class GroovyClassLoaderExample2{

public static void main(String args[]) {
try {
GroovyClassLoader loader = new GroovyClassLoader();
Class groovyClass = loader.parseClass(new File("Square.groovy"));
GroovyObject object = (GroovyObject) groovyClass.newInstance();
object.invokeMethod("setX", 10);
Shape shape = (Shape) object;
assert shape.calculateArea() == 100;
} catch (Exception e) {
e.printStackTrace();
}
}
}
6 changes: 6 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyFileCreator.groovy
@@ -0,0 +1,6 @@
class GroovyFileCreator {
static void main(args) {
File file = new File("C:\\temp\\groovy.txt");
file.createNewFile();
}
}
22 changes: 22 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyJSR223Example.java
@@ -0,0 +1,22 @@
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class GroovyJSR223Example {

public static void main(String args[]) {
try {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
String HelloLanguage = "def hello(language) {return \"Hello $language\"}";
engine.eval(HelloLanguage);
Invocable inv = (Invocable) engine;
Object[] params = { new String("Groovy") };
Object result = inv.invokeFunction("hello", params);
assert result.equals("Hello Groovy");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
18 changes: 18 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyScriptEngineExample.java
@@ -0,0 +1,18 @@
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;

public class GroovyScriptEngineExample {

public static void main(String args[]) {
try {
GroovyScriptEngine engine = new GroovyScriptEngine("");
Binding binding = new Binding();
binding.setVariable("language", "Groovy");
Object value = engine.run("SimpleScript.groovy", binding);
assert value.equals("The End");
} catch (Exception e) {
e.printStackTrace();
}

}
}
17 changes: 17 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovyShellExample.java
@@ -0,0 +1,17 @@
import groovy.lang.Binding;
import groovy.lang.GroovyShell;

public class GroovyShellExample {

public static void main(String args[]) {
Binding binding = new Binding();
binding.setVariable("x", 10);
binding.setVariable("language", "Groovy");
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate
("println \"Welcome to $language\"; y = x * 2; z = x * 3; return x ");
assert value.equals(10);
assert binding.getVariable("y").equals(20);
assert binding.getVariable("z").equals(30);
}
}
6 changes: 6 additions & 0 deletions JawadSourceCde/code/Chapter02/GroovySimpleFileCreator.groovy
@@ -0,0 +1,6 @@
class GroovySimpleFileCreator {
public createFile(String fileName){
File file = new File(fileName);
file.createNewFile();
}
}
31 changes: 31 additions & 0 deletions JawadSourceCde/code/Chapter02/Lists.groovy
@@ -0,0 +1,31 @@
def a = [] //Empty list
a += [1,2,3] //Adding elements to a list
assert a == [1,2,3]
assert a.size == 3
a << 4 << 5 //Another way of adding elements to a list
assert a == [1,2,3,4,5]
a.add(6) //A third way of adding elements to a list
assert a == [1,2,3,4,5,6]

//Accessing elements of a list
assert a[0] == 1 //Using a subscript
assert a.get(0) == 1 //Using get
assert a.getAt(0) == 1 //Using getAt
assert a[-1] == 6 //Last element index starts at -1 backwards

//Modifying elements in a list
a.putAt(1,1)
assert a == [1,1,3,4,5,6]
assert a.set(1,2) == 1 //Will return the old value
assert a == [1,2,3,4,5,6]

//Iterating over a list
a.each{ println "$it"}
//Printing items in a list with their index
a.eachWithIndex{it, index -> println item : "$it", index : "$index"}

//Removing items from a list
a -= 1 //Remove number 1
assert a == [2,3,4,5,6]
a = a.minus([2,3,4]) //Remove the sublist [2,3,4]
assert a == [5,6]
25 changes: 25 additions & 0 deletions JawadSourceCde/code/Chapter02/Maps.groovy
@@ -0,0 +1,25 @@
//Creating a map
def map = ['name':'Bashar','age':26,skills:['Java','Groovy'], 'author':true]
assert map.size() == 4

//Adding a key/value pair to a map
map += ['city':'Tucson']
assert map == ['name':'Bashar','age':26,skills:['Java','Groovy'],
'author':true, 'city':'Tucson']
//Alternative way of adding a key/value pair to a map
map['state'] = 'AZ'
assert map == ['name':'Bashar','age':26,skills:['Java','Groovy'],
'author':true, 'city':'Tucson', 'state':'AZ']
//Accessing map elements
assert map.city == 'Tucson'
assert map['city'] == 'Tucson'
assert map.get('city') == 'Tucson'
assert map.getAt('city') == 'Tucson'
assert map.skills[0] == 'Java'

//Keys are unique
assert ['name':'Bashar','name':'Abdul'] == ['name':'Abdul']

//Iterating over a map
map.each{ it -> println it.key + ":" + it.value}
map.eachWithIndex{ it, index -> println "item $index - " + it.key + ":" + it.value}

0 comments on commit 1db9c20

Please sign in to comment.