diff --git a/itext-java-html-pdf/.classpath b/itext-java-html-pdf/.classpath
index 33a301cc..8d216a15 100644
--- a/itext-java-html-pdf/.classpath
+++ b/itext-java-html-pdf/.classpath
@@ -3,6 +3,17 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/itext-java-html-pdf/index.html b/itext-java-html-pdf/index.html
index db6616a9..054b0e51 100644
--- a/itext-java-html-pdf/index.html
+++ b/itext-java-html-pdf/index.html
@@ -7,8 +7,10 @@
HTML to PDF
- itext 5.4.2 converting HTML to PDF
+ itext 7.1.9
+ converting HTML to PDF
+
| Title |
diff --git a/itext-java-html-pdf/java.png b/itext-java-html-pdf/java.png
new file mode 100644
index 00000000..c1e3d023
Binary files /dev/null and b/itext-java-html-pdf/java.png differ
diff --git a/itext-java-html-pdf/pdf.pdf b/itext-java-html-pdf/pdf.pdf
deleted file mode 100644
index 16654fcc..00000000
Binary files a/itext-java-html-pdf/pdf.pdf and /dev/null differ
diff --git a/itext-java-html-pdf/pom.xml b/itext-java-html-pdf/pom.xml
index d764eeb2..f7910a6b 100644
--- a/itext-java-html-pdf/pom.xml
+++ b/itext-java-html-pdf/pom.xml
@@ -12,18 +12,20 @@
UTF-8
+ RELEASE
- com.itextpdf
- itextpdf
- 5.4.2
-
-
- com.itextpdf.tool
- xmlworker
- 5.4.1
+ com.itextpdf
+ itext7-core
+ 7.1.9
+ pom
+
+
+ com.itextpdf
+ html2pdf
+ 2.1.6
diff --git a/itext-java-html-pdf/src/main/java/com/hmkcode/App.java b/itext-java-html-pdf/src/main/java/com/hmkcode/App.java
index 0e6c9723..0be911e5 100644
--- a/itext-java-html-pdf/src/main/java/com/hmkcode/App.java
+++ b/itext-java-html-pdf/src/main/java/com/hmkcode/App.java
@@ -1,29 +1,31 @@
package com.hmkcode;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
-import com.itextpdf.text.Document;
-import com.itextpdf.text.DocumentException;
-import com.itextpdf.text.pdf.PdfWriter;
-import com.itextpdf.tool.xml.XMLWorkerHelper;
+
+import com.itextpdf.html2pdf.HtmlConverter;
public class App
{
- public static void main( String[] args ) throws DocumentException, IOException
+ public static final String HTML = "Hello
"
+ + "This was created using iText
"
+ + "hmkcode.com";
+
+
+ public static void main( String[] args ) throws FileNotFoundException, IOException
{
- // step 1
- Document document = new Document();
- // step 2
- PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
- // step 3
- document.open();
- // step 4
- XMLWorkerHelper.getInstance().parseXHtml(writer, document,
- new FileInputStream("index.html"));
- //step 5
- document.close();
-
+
+ // String to PDF
+ HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
+
+
+ // HTML file to PDF
+ HtmlConverter.convertToPdf(new FileInputStream("index.html"),
+ new FileOutputStream("index-to-pdf.pdf"));
+
+
System.out.println( "PDF Created!" );
}
}
diff --git a/java-combinations/Combination.java b/java-combinations/Combination.java
new file mode 100644
index 00000000..ebe54224
--- /dev/null
+++ b/java-combinations/Combination.java
@@ -0,0 +1,95 @@
+package com.hmkcode;
+
+
+
+public class Combination {
+
+
+ public static void main(String[] args){
+ Object[] elements = new Object[] {'A','B','C','D','E'};
+ combination(elements,3);
+ }
+
+
+ public static void combination(Object[] elements, int K){
+
+ // get the length of the array
+ // e.g. for {'A','B','C','D'} => N = 4
+ int N = elements.length;
+
+ if(K > N){
+ System.out.println("Invalid input, K > N");
+ return;
+ }
+
+ // calculate the possible combinations
+ c(N,K);
+
+ // init combination index array
+ int combination[] = new int[K];
+
+
+ int r = 0; // index for combination array
+ int i = 0; // index for elements array
+
+ while(r >= 0){
+
+ // forward step if i < (N + (r-K))
+ if(i <= (N + (r - K))){
+ combination[r] = i;
+
+ // if combination array is full print and increment i;
+ if(r == K-1){
+ print(combination, elements);
+ i++;
+ }
+ else{
+ // if combination is not full yet, select next element
+ i = combination[r]+1;
+ r++;
+ }
+
+ }
+
+ // backward step
+ else{
+ r--;
+ if(r >= 0)
+ i = combination[r]+1;
+
+ }
+ }
+ }
+
+
+
+ private static int c(int n, int r){
+ int nf=fact(n);
+ int rf=fact(r);
+ int nrf=fact(n-r);
+ int npr=nf/nrf;
+ int ncr=npr/rf;
+
+ System.out.println("C("+n+","+r+") = "+ ncr);
+
+ return ncr;
+ }
+
+ private static int fact(int n)
+ {
+ if(n == 0)
+ return 1;
+ else
+ return n * fact(n-1);
+ }
+
+
+ private static void print(int[] combination, Object[] elements){
+
+ String output = "";
+ for(int z = 0 ; z < combination.length;z++){
+ output += elements[combination[z]];
+ }
+ System.out.println(output);
+ }
+}
diff --git a/java-combinations/README.md b/java-combinations/README.md
new file mode 100644
index 00000000..402992b5
--- /dev/null
+++ b/java-combinations/README.md
@@ -0,0 +1,8 @@
+Algorithms for Finding all Possible Combinations of k Elements in an Array with Java Implementation
+===================================================================================================
+
+Refer to the [http://hmkcode.github.io/calculate-find-all-possible-combinations-of-an-array-using-java/](http://hmkcode.github.io/calculate-find-all-possible-combinations-of-an-array-using-java/) for more info.
+
+Given an array of size N e.g. `e={'A','B','C','D','E'}` **N=5**, we want to find all possible combinations of K elements in that array. For example, if K=3 then one possible combination is of array **e** is `{'A','B','C'}. Here we have three different algorithms for finding *k*-combinations of an array.
+
+
diff --git a/java-combinations/src/main/java/com/hmkcode/ForwardBackward.java b/java-combinations/src/main/java/com/hmkcode/ForwardBackward.java
new file mode 100644
index 00000000..42f94932
--- /dev/null
+++ b/java-combinations/src/main/java/com/hmkcode/ForwardBackward.java
@@ -0,0 +1,95 @@
+package com.hmkcode;
+
+
+
+public class ForwardBackward {
+
+
+ public static void main(String[] args){
+ Object[] e = new Object[] {'A','B','C','D','E'};
+ int k = 3;
+ combination(e,k);
+ }
+
+
+ public static void combination(Object[] elements, int k){
+
+ // get the length of the array
+ // e.g. for {'A','B','C','D'} => N = 4
+ int N = elements.length;
+
+ if(k > N){
+ System.out.println("Invalid input, K > N");
+ return;
+ }
+
+ // calculate the possible combinations
+ c(N,k);
+
+ // init combination index array
+ int pointers[] = new int[k];
+
+
+ int r = 0; // index for combination array
+ int i = 0; // index for elements array
+
+ while(r >= 0){
+
+ // forward step if i < (N + (r-K))
+ if(i <= (N + (r - k))){
+ pointers[r] = i;
+
+ // if combination array is full print and increment i;
+ if(r == k-1){
+ print(pointers, elements);
+ i++;
+ }
+ else{
+ // if combination is not full yet, select next element
+ i = pointers[r]+1;
+ r++;
+ }
+ }
+
+ // backward step
+ else{
+ r--;
+ if(r >= 0)
+ i = pointers[r]+1;
+
+ }
+ }
+ }
+
+
+
+ private static int c(int n, int r){
+ int nf=fact(n);
+ int rf=fact(r);
+ int nrf=fact(n-r);
+ int npr=nf/nrf;
+ int ncr=npr/rf;
+
+ System.out.println("C("+n+","+r+") = "+ ncr);
+
+ return ncr;
+ }
+
+ private static int fact(int n)
+ {
+ if(n == 0)
+ return 1;
+ else
+ return n * fact(n-1);
+ }
+
+
+ private static void print(int[] combination, Object[] elements){
+
+ String output = "";
+ for(int z = 0 ; z < combination.length;z++){
+ output += elements[combination[z]];
+ }
+ System.out.println(output);
+ }
+}
diff --git a/java-combinations/src/main/java/com/hmkcode/Recursive.java b/java-combinations/src/main/java/com/hmkcode/Recursive.java
new file mode 100644
index 00000000..a4acc9ac
--- /dev/null
+++ b/java-combinations/src/main/java/com/hmkcode/Recursive.java
@@ -0,0 +1,45 @@
+package com.hmkcode;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class Recursive {
+
+ public static void main(String[] args) {
+
+ List e = Arrays.asList("A", "B", "C", "D", "E");
+ int k = 3;
+ combination(e, k, "");
+
+ }
+ static int counter = 0;
+ public static void combination(List e, int k, String accumulated){
+
+ // 1. stop
+ if(e.size() < k)
+ return;
+
+ // 2. add each element in e to accumulated
+ if(k == 1)
+ for(String s:e)
+ print(accumulated+s);
+
+ // 3. add all elements in e to accumulated
+ else if(e.size() == k){
+ for(String s:e)
+ accumulated+=s;
+ print(accumulated);
+ }
+
+ // 4. for each element, call combination
+ else if(e.size() > k)
+ for(int i = 0 ; i < e.size() ; i++)
+ combination(e.subList(i+1, e.size()), k-1, accumulated+e.get(i));
+
+ }
+
+ public static void print(String c){
+ counter++;
+ System.out.println(counter+"\t"+c);
+ }
+}
diff --git a/java-combinations/src/main/java/com/hmkcode/Shifting.java b/java-combinations/src/main/java/com/hmkcode/Shifting.java
new file mode 100644
index 00000000..d81ce2b5
--- /dev/null
+++ b/java-combinations/src/main/java/com/hmkcode/Shifting.java
@@ -0,0 +1,66 @@
+package com.hmkcode;
+
+public class Shifting
+{
+ public static void main( String[] args )
+ {
+ String[] e = {"A","B","C","D","E"};
+ int k = 3;
+ combination(e,k);
+ }
+ public static void combination(Object[] e, int k){
+
+ int[] ignore = new int[e.length-k]; // --> [0][0]
+ int[] combination = new int[k]; // --> [][][]
+
+ // set initial ignored elements
+ //(last k elements will be ignored)
+ for(int w = 0; w < ignore.length; w++)
+ ignore[w] = e.length - k +(w+1);
+
+ int i = 0, r = 0, g = 0;
+
+ boolean terminate = false;
+ while(!terminate){
+
+ // selecting N-k non-ignored elements
+ while(i < e.length && r < k){
+
+ if(i != ignore[g]){
+ combination[r] = i;
+ r++; i++;
+ }
+ else{
+ if(g != ignore.length-1)
+ g++;
+ i++;
+ }
+ }
+ print(combination, e);
+ i = 0; r = 0; g = 0;
+
+ terminate = true;
+
+ // shifting ignored indices
+ for(int w = 0 ; w < ignore.length; w++){
+ if(ignore[w] > w){
+ ignore[w]--;
+
+ if(w > 0)
+ ignore[w-1] = ignore[w]-1;
+ terminate = false;
+ break;
+ }
+ }
+ }
+ }
+
+ private static void print(int[] combination, Object[] elements){
+
+ String output = "";
+ for(int z = 0 ; z < combination.length;z++){
+ output += elements[combination[z]];
+ }
+ System.out.println(output);
+ }
+}
\ No newline at end of file
diff --git a/java-jasper/pom.xml b/java-jasper/pom.xml
new file mode 100644
index 00000000..37666f32
--- /dev/null
+++ b/java-jasper/pom.xml
@@ -0,0 +1,36 @@
+
+
+
+ 4.0.0
+
+ com.hmkcode
+ java-jasper
+ 1.0-SNAPSHOT
+
+ java-jasper
+
+ http://www.example.com
+
+
+ UTF-8
+ 1.7
+ 1.7
+
+
+
+
+ net.sf.jasperreports
+ jasperreports
+ 6.10.0
+
+
+
+
+ org.springframework
+ spring-core
+ 5.2.3.RELEASE
+
+
+
+
diff --git a/java-jasper/src/main/java/com/hmkcode/App.java b/java-jasper/src/main/java/com/hmkcode/App.java
new file mode 100644
index 00000000..72853237
--- /dev/null
+++ b/java-jasper/src/main/java/com/hmkcode/App.java
@@ -0,0 +1,61 @@
+package com.hmkcode;
+
+import net.sf.jasperreports.engine.*;
+import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
+import org.springframework.util.ResourceUtils;
+
+import java.io.*;
+import java.util.*;
+
+public class App
+{
+ // name and destination of output file e.g. "report.pdf"
+ private static String destFileName = "report.pdf";
+ public static void main( String[] args ) throws FileNotFoundException, JRException {
+
+ System.out.println( "generating jasper report..." );
+
+ // 1. compile template ".jrxml" file
+ JasperReport jasperReport = getJasperReport();
+
+ // 2. parameters "empty"
+ Map parameters = getParameters();
+
+ // 3. datasource "java object"
+ JRDataSource dataSource = getDataSource();
+
+ JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
+ JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);
+
+ }
+
+ private static JasperReport getJasperReport() throws FileNotFoundException, JRException {
+ File template = ResourceUtils.getFile("classpath:report.jrxml");
+ return JasperCompileManager.compileReport(template.getAbsolutePath());
+ }
+ private static Map getParameters(){
+ Map parameters = new HashMap<>();
+ parameters.put("createdBy", "hmkcode");
+ return parameters;
+ }
+
+ private static JRDataSource getDataSource(){
+
+ List countries = new LinkedList<>();
+
+ countries.add(new Country("IS", "Iceland", "https://i.pinimg.com/originals/72/b4/49/72b44927f220151547493e528a332173.png"));
+ countries.add(new Country("TR", "Turkey", "https://i.pinimg.com/originals/82/63/23/826323bba32e6e5a5996062c3a3c662f.png"));
+ countries.add(new Country("ZA", "South Africa", "https://i.pinimg.com/originals/f5/c7/8d/f5c78da001b46e26481c04fb93473454.png"));
+ countries.add(new Country("PL", "Poland", "https://i.pinimg.com/originals/7f/ae/21/7fae21c4854010b11127218ead743863.png"));
+ countries.add(new Country("CA", "Canada", "https://i.pinimg.com/originals/4d/d4/01/4dd401733ba25e6442fc8696e04e5846.png"));
+
+ countries.add(new Country("PA", "Panama", "https://i.pinimg.com/originals/84/dc/e4/84dce49e52ebfb5b3814393069807e0a.png"));
+ countries.add(new Country("HR", "Croatia", "https://i.pinimg.com/originals/f5/8c/94/f58c94a2a2b3221328fc1e2b7acfa656.png"));
+ countries.add(new Country("JP", "Japan", "https://i.pinimg.com/originals/a5/d6/88/a5d688289cd6850016f14fe93b17da01.png"));
+ countries.add(new Country("DE", "Germany", "https://i.pinimg.com/originals/af/c9/b2/afc9b2592a9f1cf591e8a52256ae1e9f.png"));
+ countries.add(new Country("BR", "Brazil", "https://i.pinimg.com/originals/e4/03/c4/e403c4447a3bd8940459ae4f50856bed.png"));
+
+
+ return new JRBeanCollectionDataSource(countries);
+ }
+}
diff --git a/java-jasper/src/main/java/com/hmkcode/Country.java b/java-jasper/src/main/java/com/hmkcode/Country.java
new file mode 100644
index 00000000..ecbc501d
--- /dev/null
+++ b/java-jasper/src/main/java/com/hmkcode/Country.java
@@ -0,0 +1,47 @@
+package com.hmkcode;
+
+public class Country {
+
+ private String code;
+ private String name;
+ private String url;
+
+ public Country(String code, String name, String url) {
+ this.code = code;
+ this.name = name;
+ this.url = url;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public void setCode(String code) {
+ this.code = code;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ @Override
+ public String toString() {
+ return "Country{" +
+ "code='" + code + '\'' +
+ ", name='" + name + '\'' +
+ ", url='" + url + '\'' +
+ '}';
+ }
+}
diff --git a/java-jasper/src/main/resources/report.jrxml b/java-jasper/src/main/resources/report.jrxml
new file mode 100644
index 00000000..113a787a
--- /dev/null
+++ b/java-jasper/src/main/resources/report.jrxml
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/java-lambda/README.md b/java-lambda/README.md
new file mode 100644
index 00000000..8213fe8e
--- /dev/null
+++ b/java-lambda/README.md
@@ -0,0 +1,151 @@
+Using Lambda to Implement Functional Interface Method
+====================================================
+
+- We have a *virtual* `Button` that when clicked will call an abstract method **onClick()** of a listener interface `OnClickListener`.
+- We need to implement the **onClick()** so that it prints the `Button` name.
+
+`OnClickListener.java`
+
+```java
+package com.hmkcode;
+
+public interface OnClickListener {
+ void onClick(Button button);
+}
+```
+
+`Button.java`
+
+```java
+package com.hmkcode;
+
+public class Button {
+
+ private OnClickListener onClickListener;
+ private String name;
+
+ // click the button
+ public void click(){
+ this.onClickListener.onClick(this);
+ }
+
+ // getters & setters
+}
+```
+
+- We have three ways to achieve that:
+ 1. **Implement** `OnClickListener` and override **onClick()** method.
+ 2. Use `OnClickListener` as an anonymous class.
+ 3. Use Lambda.
+
+- Our main class is `App.java`
+
+```java
+package com.hmkcode;
+
+public class App
+{
+ public static void main( String[] args ){
+ System.out.println( "Running App..." );
+ new App().run();
+ }
+
+ public void run(){
+
+ Button myButton = new Button();
+ myButton.setName("MyButton");
+
+ // 1. implements onClickListener
+
+ // 2. anonymous class
+
+ // 3. lambda
+
+
+ // click the button
+ myButton.click();
+ }
+}
+```
+
+### 1. **Implement** `OnClickListener` and override **onClick()** method
+
+```java
+public class App implements OnClickListener
+{
+ public static void main( String[] args ){...}
+
+ public void run(){
+
+ Button myButton = new Button();
+ myButton.setName("MyButton");
+
+ // 1. implements onClickListener
+ myButton.setOnClickListener(this);
+
+ // click the button
+ myButton.click();
+ }
+
+ @Override
+ public void onClick(Button button) {
+ System.out.println(button.getName() +" Clicked! - implements interface");
+
+ }
+}
+```
+
+### 2. Use `OnClickListener` as an anonymous class
+
+```java
+public class App
+{
+ public static void main( String[] args ){...}
+
+ public void run(){
+
+ Button myButton = new Button();
+ myButton.setName("MyButton");
+
+ // 2. anonymous class
+ myButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(Button button) {
+ System.out.println(button.getName() +" Clicked! - anonymous class");
+ }
+ });
+
+ // click the button
+ myButton.click();
+ }
+}
+```
+
+### 3. Use Lambda
+
+```java
+public class App
+{
+ public static void main( String[] args ){...}
+
+ public void run(){
+
+ Button myButton = new Button();
+ myButton.setName("MyButton");
+
+ // 3. lambda
+ OnClickListener lambda = button -> { System.out.println(button.getName()+" Clicked! - lambda"); } ;
+ myButton.setOnClickListener(lambda);
+
+ // click the button
+ myButton.click();
+ }
+}
+```
+
+*To run the code use*
+
+```
+java-lambda>mvn exec:java
+```
+
diff --git a/java-lambda/pom.xml b/java-lambda/pom.xml
new file mode 100644
index 00000000..8240ec70
--- /dev/null
+++ b/java-lambda/pom.xml
@@ -0,0 +1,34 @@
+
+
+
+ 4.0.0
+
+ com.hmkcode
+ java-lambda
+ 1.0-SNAPSHOT
+
+ java-lambda
+
+
+
+
+ maven-compiler-plugin
+ 3.8.0
+
+ 1.8
+ 1.8
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 1.6.0
+
+ com.hmkcode.App
+
+
+
+
+
+
diff --git a/java-lambda/src/main/java/com/hmkcode/App.java b/java-lambda/src/main/java/com/hmkcode/App.java
new file mode 100644
index 00000000..c1cdd001
--- /dev/null
+++ b/java-lambda/src/main/java/com/hmkcode/App.java
@@ -0,0 +1,42 @@
+package com.hmkcode;
+
+
+public class App implements OnClickListener
+{
+ public static void main( String[] args ){
+ System.out.println( "Running App..." );
+ new App().run();
+ }
+
+ public void run(){
+
+ Button myButton = new Button();
+ myButton.setName("MyButton");
+
+ // 1. implements onClickListener
+ //myButton.setOnClickListener(this);
+
+ // 2. anonymous class
+ /*myButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(Button button) {
+ System.out.println(button.getName() +" Clicked! - anonymous class");
+ }
+ }); */
+
+ // 3. lambda
+ OnClickListener lambda = button -> { System.out.println(button.getName()+" Clicked! - lambda"); } ;
+ myButton.setOnClickListener(lambda);
+
+
+ // click the button
+ myButton.click();
+ }
+
+
+ @Override
+ public void onClick(Button button) {
+ System.out.println(button.getName() +" Clicked! - implements interface");
+
+ }
+}
diff --git a/java-lambda/src/main/java/com/hmkcode/Button.java b/java-lambda/src/main/java/com/hmkcode/Button.java
new file mode 100644
index 00000000..adc65baf
--- /dev/null
+++ b/java-lambda/src/main/java/com/hmkcode/Button.java
@@ -0,0 +1,31 @@
+package com.hmkcode;
+
+public class Button {
+
+ private OnClickListener onClickListener;
+ private String name;
+
+ public void click(){
+ this.onClickListener.onClick(this);
+ }
+
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public OnClickListener getOnClickListener() {
+ return onClickListener;
+ }
+
+ public void setOnClickListener(OnClickListener onClickListener) {
+ this.onClickListener = onClickListener;
+ }
+
+
+
+}
diff --git a/java-lambda/src/main/java/com/hmkcode/OnClickListener.java b/java-lambda/src/main/java/com/hmkcode/OnClickListener.java
new file mode 100644
index 00000000..662f65c7
--- /dev/null
+++ b/java-lambda/src/main/java/com/hmkcode/OnClickListener.java
@@ -0,0 +1,7 @@
+package com.hmkcode;
+
+
+public interface OnClickListener {
+ void onClick(Button button);
+
+}
diff --git a/java-servlet-json/.classpath b/java-servlet-json/.classpath
index fd0d555d..3579da99 100644
--- a/java-servlet-json/.classpath
+++ b/java-servlet-json/.classpath
@@ -1,11 +1,10 @@
-
-
-
-
+
+
+
\ No newline at end of file
diff --git a/java-servlet-json/pom.xml b/java-servlet-json/pom.xml
index dd7dc447..f37f61f0 100644
--- a/java-servlet-json/pom.xml
+++ b/java-servlet-json/pom.xml
@@ -5,7 +5,7 @@
com.hmkcode
java-servlet-json
1.0-SNAPSHOT
- jar
+ war
java-servlet-json
http://maven.apache.org
@@ -15,36 +15,39 @@
-
+
javax.servlet
javax.servlet-api
3.1.0
+ provided
+
+
com.fasterxml.jackson.core
jackson-core
- 2.2.2
+ 2.9.6
com.fasterxml.jackson.core
jackson-databind
- 2.2.2
+ 2.9.6
com.fasterxml.jackson.core
jackson-annotations
- 2.2.2
+ 2.9.6
java-servlet-json
-
- org.eclipse.jetty
+
+ org.eclipse.jetty
jetty-maven-plugin
- 9.0.4.v20130625
-
+ 9.4.11.v20180605
+
diff --git a/java-servlet-json/src/main/java/com/hmkcode/JSONServlet.java b/java-servlet-json/src/main/java/com/hmkcode/JSONServlet.java
index afccf9b4..67415b87 100644
--- a/java-servlet-json/src/main/java/com/hmkcode/JSONServlet.java
+++ b/java-servlet-json/src/main/java/com/hmkcode/JSONServlet.java
@@ -6,6 +6,7 @@
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -13,6 +14,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;
+@WebServlet("/jsonservlet")
public class JSONServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@@ -32,21 +34,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String json = "";
if(br != null){
json = br.readLine();
+ System.out.println(json);
}
// 2. initiate jackson mapper
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = new ObjectMapper();
- // 3. Convert received JSON to Article
- Article article = mapper.readValue(json, Article.class);
+ // 3. Convert received JSON to Article
+ Article article = mapper.readValue(json, Article.class);
// 4. Set response type to JSON
response.setContentType("application/json");
- // 5. Add article to List
+ // 5. Add article to List
articles.add(article);
// 6. Send List as JSON to client
- mapper.writeValue(response.getOutputStream(), articles);
+ mapper.writeValue(response.getOutputStream(), articles);
}
}
diff --git a/java-servlet-json/src/main/webapp/WEB-INF/web.xml b/java-servlet-json/src/main/webapp/WEB-INF/web.xml
deleted file mode 100644
index ac732e11..00000000
--- a/java-servlet-json/src/main/webapp/WEB-INF/web.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
- Java Servlet JSON
-
- index.html
-
-
-
- jsonservlet
- com.hmkcode.JSONServlet
-
-
- jsonservlet
- /jsonservlet
-
-
\ No newline at end of file
diff --git a/java-spi/java-spi-api/pom.xml b/java-spi/java-spi-api/pom.xml
new file mode 100644
index 00000000..e7c1e87d
--- /dev/null
+++ b/java-spi/java-spi-api/pom.xml
@@ -0,0 +1,27 @@
+
+
+
+ 4.0.0
+
+ com.hmkcode.api
+ java-spi-api
+ 1.0-SNAPSHOT
+
+ java-spi-api
+
+ http://www.example.com
+
+
+ UTF-8
+ 1.7
+ 1.7
+
+
+
+
+
+
+
+
+
diff --git a/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyService.java b/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyService.java
new file mode 100644
index 00000000..4928f453
--- /dev/null
+++ b/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyService.java
@@ -0,0 +1,6 @@
+package com.hmkcode.api;
+
+public interface MyService {
+
+ void doSomething();
+}
diff --git a/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyServiceProviderInterface.java b/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyServiceProviderInterface.java
new file mode 100644
index 00000000..8e166424
--- /dev/null
+++ b/java-spi/java-spi-api/src/main/java/com/hmkcode/api/MyServiceProviderInterface.java
@@ -0,0 +1,6 @@
+package com.hmkcode.api;
+
+public interface MyServiceProviderInterface {
+
+ MyService getService();
+}
diff --git a/java-spi/java-spi-app/lib/java-spi-api-1.0-SNAPSHOT.jar b/java-spi/java-spi-app/lib/java-spi-api-1.0-SNAPSHOT.jar
new file mode 100644
index 00000000..23a1c6be
Binary files /dev/null and b/java-spi/java-spi-app/lib/java-spi-api-1.0-SNAPSHOT.jar differ
diff --git a/java-spi/java-spi-app/lib/java-spi-impl1-1.0-SNAPSHOT.jar b/java-spi/java-spi-app/lib/java-spi-impl1-1.0-SNAPSHOT.jar
new file mode 100644
index 00000000..c8007874
Binary files /dev/null and b/java-spi/java-spi-app/lib/java-spi-impl1-1.0-SNAPSHOT.jar differ
diff --git a/java-spi/java-spi-app/pom.xml b/java-spi/java-spi-app/pom.xml
new file mode 100644
index 00000000..74906a4f
--- /dev/null
+++ b/java-spi/java-spi-app/pom.xml
@@ -0,0 +1,40 @@
+
+
+
+ 4.0.0
+
+ com.hmkcode.app
+ java-spi-app
+ 1.0-SNAPSHOT
+
+ java-spi-app
+
+ http://www.example.com
+
+
+ UTF-8
+ 1.7
+ 1.7
+
+
+
+
+
+ com.hmkcode.api
+ java-spi-api
+ 1.0-SNAPSHOT
+ system
+ ${project.basedir}/lib/java-spi-api-1.0-SNAPSHOT.jar
+
+
+ com.hmkcode.impl
+ java-spi-impl1
+ 1.0-SNAPSHOT
+ system
+ ${project.basedir}/lib/java-spi-impl1-1.0-SNAPSHOT.jar
+
+
+
+
+
diff --git a/java-spi/java-spi-app/src/main/java/com/hmkcode/app/App.java b/java-spi/java-spi-app/src/main/java/com/hmkcode/app/App.java
new file mode 100644
index 00000000..8e46d684
--- /dev/null
+++ b/java-spi/java-spi-app/src/main/java/com/hmkcode/app/App.java
@@ -0,0 +1,9 @@
+package com.hmkcode.app;
+
+public class App
+{
+ public static void main( String[] args )
+ {
+ MyServiceLoader.defaultProvider().getService().doSomething();
+ }
+}
diff --git a/java-spi/java-spi-app/src/main/java/com/hmkcode/app/MyServiceLoader.java b/java-spi/java-spi-app/src/main/java/com/hmkcode/app/MyServiceLoader.java
new file mode 100644
index 00000000..2efc5d74
--- /dev/null
+++ b/java-spi/java-spi-app/src/main/java/com/hmkcode/app/MyServiceLoader.java
@@ -0,0 +1,31 @@
+package com.hmkcode.app;
+
+import java.nio.file.ProviderNotFoundException;
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import com.hmkcode.api.MyServiceProviderInterface;
+
+
+public class MyServiceLoader {
+
+ private static final String DEFAULT_PROVIDER = "com.hmkcode.impl.MyServiceProviderImpl1";
+
+ public static MyServiceProviderInterface defaultProvider() {
+ return provider(DEFAULT_PROVIDER);
+ }
+
+ public static MyServiceProviderInterface provider(String providerName) {
+ ServiceLoader loader = ServiceLoader.load(MyServiceProviderInterface.class);
+
+ Iterator it = loader.iterator();
+ while (it.hasNext()) {
+ MyServiceProviderInterface provider = it.next();
+ if (providerName.equals(provider.getClass().getName())) {
+ return provider;
+ }
+ }
+ throw new ProviderNotFoundException("provider " + providerName + " not found");
+ }
+
+}
diff --git a/java-spi/java-spi-impl1/lib/java-spi-api-1.0-SNAPSHOT.jar b/java-spi/java-spi-impl1/lib/java-spi-api-1.0-SNAPSHOT.jar
new file mode 100644
index 00000000..23a1c6be
Binary files /dev/null and b/java-spi/java-spi-impl1/lib/java-spi-api-1.0-SNAPSHOT.jar differ
diff --git a/java-spi/java-spi-impl1/pom.xml b/java-spi/java-spi-impl1/pom.xml
new file mode 100644
index 00000000..2e234c96
--- /dev/null
+++ b/java-spi/java-spi-impl1/pom.xml
@@ -0,0 +1,31 @@
+
+
+
+ 4.0.0
+
+ com.hmkcode.impl
+ java-spi-impl1
+ 1.0-SNAPSHOT
+
+ java-spi-impl1
+
+ http://www.example.com
+
+
+ UTF-8
+ 1.7
+ 1.7
+
+
+
+
+ com.hmkcode.api
+ java-spi-api
+ 1.0-SNAPSHOT
+ system
+ ${project.basedir}/lib/java-spi-api-1.0-SNAPSHOT.jar
+
+
+
+
diff --git a/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceImpl1.java b/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceImpl1.java
new file mode 100644
index 00000000..24afcfa4
--- /dev/null
+++ b/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceImpl1.java
@@ -0,0 +1,12 @@
+package com.hmkcode.impl;
+
+import com.hmkcode.api.MyService;
+
+public class MyServiceImpl1 implements MyService{
+
+ @Override
+ public void doSomething() {
+ System.out.println("MyServiceImpl1");
+
+ }
+}
diff --git a/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceProviderImpl1.java b/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceProviderImpl1.java
new file mode 100644
index 00000000..252b7a3e
--- /dev/null
+++ b/java-spi/java-spi-impl1/src/main/java/com/hmkcode/impl/MyServiceProviderImpl1.java
@@ -0,0 +1,13 @@
+package com.hmkcode.impl;
+
+import com.hmkcode.api.MyService;
+import com.hmkcode.api.MyServiceProviderInterface;
+
+public class MyServiceProviderImpl1 implements MyServiceProviderInterface {
+
+ @Override
+ public MyService getService() {
+ return new MyServiceImpl1();
+ }
+
+}
diff --git a/java-spi/java-spi-impl1/src/main/resources/META-INF/services/com.hmkcode.api.MyServiceProviderInterface b/java-spi/java-spi-impl1/src/main/resources/META-INF/services/com.hmkcode.api.MyServiceProviderInterface
new file mode 100644
index 00000000..008c8c11
--- /dev/null
+++ b/java-spi/java-spi-impl1/src/main/resources/META-INF/services/com.hmkcode.api.MyServiceProviderInterface
@@ -0,0 +1 @@
+com.hmkcode.impl.MyServiceProviderImpl1
\ No newline at end of file
diff --git a/java-stream/src/main/java/com/hmkcode/App.java b/java-stream/src/main/java/com/hmkcode/App.java
new file mode 100644
index 00000000..b97bbd63
--- /dev/null
+++ b/java-stream/src/main/java/com/hmkcode/App.java
@@ -0,0 +1,87 @@
+package com.hmkcode;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class App
+{
+ public static void main( String[] args )
+ {
+ String[] arr = new String[]{"a", "b", "c", "d"};
+ Stream stream = Arrays.stream(arr);
+
+ stream = Stream.of("a", "b", "c", "d");
+
+ List list = new LinkedList();
+ list.add("a");
+ list.add("b");
+ stream = list.stream();
+
+ // forEach()
+ stream = Stream.of("a", "b", "c", "d");
+ stream.forEach(e -> System.out.println(e));
+
+ // distinct() | count()
+ stream = Stream.of("a", "b", "c", "d");
+ System.out.println(stream.distinct().count());
+
+ // anyMatch()
+ stream = Stream.of("a", "b", "c", "d");
+ System.out.println(stream.anyMatch(e -> e.contains("a")));
+
+ // filter()
+ stream = Stream.of("a", "b", "c", "d");
+ stream.filter(e -> e.contains("b")).forEach(e -> System.out.println(e));
+
+ // map()
+ stream = Stream.of("a", "b", "c", "d");
+ stream.map(e -> e.toUpperCase()).forEach(e -> System.out.println(e));
+
+ // flatMap()
+ stream = getBigList().stream().flatMap(lst -> lst.stream());
+ stream.forEach(e -> System.out.println(e));
+
+ //[any|all|none]Match()
+ System.out.println(Stream.of("a", "b", "c", "d").allMatch( e -> (e.length() == 1)));
+ System.out.println(Stream.of("a", "b", "c", "d").noneMatch(e -> (e.length() == 2)));
+ System.out.println(Stream.of("a", "b", "c", "d").anyMatch( e -> e.equals("a") ));
+
+ //reduce()
+ stream = Stream.of("a", "b", "c", "d");
+ System.out.println(stream.reduce("", (x,y) -> apply(x,y)));
+
+ //collect(Collectors)
+ stream = Stream.of("a", "b", "c", "d");
+ System.out.println(stream.collect(Collectors.toList()));
+
+
+ }
+
+ private static String apply(String a, String b){
+ System.out.println(a+"->"+b);
+ return a+b;
+ }
+
+ private static List> getBigList(){
+
+ List> bigList = new LinkedList>();
+
+ List list1 = new LinkedList();
+ list1.add("a");
+ list1.add("b");
+
+ List list2 = new LinkedList();
+ list2.add("c");
+ list2.add("d");
+
+ bigList.add(list1);
+ bigList.add(list2);
+
+ return bigList;
+ }
+
+
+}