Skip to content

Commit e75fd0e

Browse files
committed
Merge branch 'master' of https://github.com/byhieg/JavaTutorial
2 parents c41dde8 + 3c1aee6 commit e75fd0e

9 files changed

Lines changed: 427 additions & 13 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cn.byhieg.designpatterntutorial.builder;
2+
3+
/**
4+
* Created by shiqifeng on 2017/5/7.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class Person {
8+
9+
private int age;
10+
private String name;
11+
private int height;
12+
private int weight;
13+
14+
public int getAge() {
15+
return age;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public int getHeight() {
23+
return height;
24+
}
25+
26+
public int getWeight() {
27+
return weight;
28+
}
29+
30+
public Person(Builder builder) {
31+
age = builder.age;
32+
name = builder.name;
33+
height = builder.height;
34+
weight = builder.weight;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "age = " + age + " name = " + name + " height = " + height + " weight = " + weight;
40+
}
41+
42+
public static class Builder{
43+
44+
private int age;
45+
private String name;
46+
private int height;
47+
private int weight;
48+
49+
public Builder setAge(int age) {
50+
this.age = age;
51+
return this;
52+
}
53+
54+
public Builder setName(String name) {
55+
this.name = name;
56+
return this;
57+
}
58+
59+
public Builder setHeight(int height) {
60+
this.height = height;
61+
return this;
62+
}
63+
64+
public Builder setWeight(int weight) {
65+
this.weight = weight;
66+
return this;
67+
}
68+
69+
public Person build() {
70+
return new Person(this);
71+
}
72+
}
73+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cn.byhieg.designpatterntutorial.builder;
2+
3+
/**
4+
* Created by shiqifeng on 2017/5/7.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class SimpleDialog {
8+
9+
10+
public SimpleDialogController controller;
11+
12+
public SimpleDialog(){
13+
controller = new SimpleDialogController();
14+
}
15+
16+
public void setIcon(String icon) {
17+
controller.setIcon(icon);
18+
}
19+
20+
public void setTitle(String title) {
21+
controller.setTitle(title);
22+
}
23+
24+
public void setMessage(String message) {
25+
controller.setMessage(message);
26+
}
27+
28+
public void setPositiveButton(String positiveButton) {
29+
controller.setPositiveButton(positiveButton);
30+
}
31+
32+
public void setNegativeButton(String negativeButton) {
33+
controller.setNegativeButton(negativeButton);
34+
}
35+
36+
37+
public static class Builder{
38+
SimpleDialogController.DialogParams P;
39+
40+
public Builder(){
41+
P = new SimpleDialogController.DialogParams();
42+
}
43+
44+
public Builder setIcon(String icon){
45+
P.icon = icon;
46+
return this;
47+
}
48+
49+
public Builder setTitle(String title) {
50+
P.title = title;
51+
return this;
52+
}
53+
54+
public Builder setMessage(String message) {
55+
P.message = message;
56+
return this;
57+
}
58+
59+
public Builder setPositiveButton(String positiveButton) {
60+
P.positiveButton = positiveButton;
61+
return this;
62+
}
63+
64+
public Builder setNegativeButton(String negativeButton) {
65+
P.negativeButton = negativeButton;
66+
return this;
67+
}
68+
69+
public SimpleDialog create(){
70+
SimpleDialog dialog = new SimpleDialog();
71+
P.apply(dialog.controller);
72+
System.out.println(" ICON = " + P.icon + " MESSAGE = " + P.message + " positiveButton = " + P.positiveButton + " negativeButton" + P.negativeButton);
73+
return dialog;
74+
}
75+
}
76+
77+
78+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cn.byhieg.designpatterntutorial.builder;
2+
3+
/**
4+
* Created by shiqifeng on 2017/5/7.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class SimpleDialogController {
8+
9+
private String icon;
10+
private String title;
11+
private String message;
12+
private String positiveButton;
13+
private String negativeButton;
14+
15+
public void setIcon(String icon) {
16+
this.icon = icon;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
23+
public void setMessage(String message) {
24+
this.message = message;
25+
}
26+
27+
public void setPositiveButton(String positiveButton) {
28+
this.positiveButton = positiveButton;
29+
}
30+
31+
public void setNegativeButton(String negativeButton) {
32+
this.negativeButton = negativeButton;
33+
}
34+
35+
public String getIcon() {
36+
return icon;
37+
}
38+
39+
public String getTitle() {
40+
return title;
41+
}
42+
43+
public String getMessage() {
44+
return message;
45+
}
46+
47+
public String getPositiveButton() {
48+
return positiveButton;
49+
}
50+
51+
public String getNegativeButton() {
52+
return negativeButton;
53+
}
54+
55+
public static class DialogParams{
56+
public String icon;
57+
public String title;
58+
public String message;
59+
public String positiveButton;
60+
public String negativeButton;
61+
62+
public void apply(SimpleDialogController controller) {
63+
if (icon != null) {
64+
controller.setIcon(icon);
65+
}
66+
if (title != null) {
67+
controller.setTitle(title);
68+
}
69+
if (message != null) {
70+
controller.setMessage(message);
71+
}
72+
if (positiveButton != null) {
73+
controller.setPositiveButton(positiveButton);
74+
}
75+
if (negativeButton != null) {
76+
controller.setNegativeButton(negativeButton);
77+
}
78+
}
79+
}
80+
81+
}

src/main/java/cn/byhieg/niotutorial/NioTest.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.RandomAccessFile;
77
import java.nio.ByteBuffer;
88
import java.nio.channels.FileChannel;
9+
import java.util.ArrayList;
10+
import java.util.List;
911

1012
/**
1113
* Created by shiqifeng on 2017/4/10.
@@ -14,22 +16,38 @@
1416
public class NioTest {
1517

1618
public static void main(String[] args) throws IOException {
17-
String s = File.separator;
18-
RandomAccessFile aFile = new RandomAccessFile("D:" + s + "read_file.txt","rw");
19-
FileChannel inChannel = aFile.getChannel();
20-
21-
ByteBuffer buf = ByteBuffer.allocate(48);
22-
int bytesRead = inChannel.read(buf);
23-
while (bytesRead != -1) {
24-
buf.flip();
25-
while (buf.hasRemaining()) {
26-
System.out.print((char) buf.get());
19+
for (int i = 2; i <= 100; i++) {
20+
if(i == 2 || i == 5 || i == 7 || i == 3){
21+
System.out.print(i + " ");
22+
continue;
23+
}
24+
if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0) {
25+
continue;
2726
}
2827

29-
buf.clear();
30-
bytesRead = inChannel.read(buf);
28+
System.out.print(i + " ");
29+
}
30+
System.out.println();
31+
for (int i = 2 ; i <= 100;i++){
32+
if (isPrime(i)){
33+
System.out.print(i + " ");
34+
}
3135
}
36+
}
37+
38+
39+
public static boolean isPrime(int n){
40+
if(n < 2){
41+
return false;
42+
}
43+
for (int i = 2; i <= Math.sqrt(n);i++){
44+
if (n % i == 0){
45+
return false;
46+
}
47+
}
48+
return true;
3249

33-
aFile.close();
3450
}
51+
52+
3553
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.byhieg.threadtutorial.concurrent.atom;
2+
3+
import java.util.concurrent.atomic.*;
4+
5+
/**
6+
* Created by shiqifeng on 2017/5/5.
7+
* Mail byhieg@gmail.com
8+
*/
9+
public class AtomFactory {
10+
11+
private static final AtomFactory atomFactory = new AtomFactory();
12+
13+
private AtomFactory(){
14+
15+
}
16+
17+
public static AtomFactory getInstance(){
18+
return atomFactory;
19+
}
20+
21+
public AtomicInteger createAtomInt(int a){
22+
return new AtomicInteger(a);
23+
}
24+
25+
public AtomicIntegerArray createAtomArray(int[] a) {
26+
return new AtomicIntegerArray(a);
27+
}
28+
29+
public AtomicReference<MyObject> createAtomReference(MyObject object){
30+
return new AtomicReference<>();
31+
}
32+
33+
public AtomicIntegerFieldUpdater<MyObject> createAtomIntegerUpdate(String fieldName) {
34+
return AtomicIntegerFieldUpdater.newUpdater(MyObject.class, fieldName);
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.byhieg.threadtutorial.concurrent.atom;
2+
3+
/**
4+
* Created by shiqifeng on 2017/5/5.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class MyObject {
8+
9+
public String name = "byhieg";
10+
public int age = 24;
11+
public volatile int id = 1;
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.byhieg.designpatterntutorialtest;
2+
3+
import cn.byhieg.designpatterntutorial.builder.Person;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2017/5/7.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class BuilderTest extends TestCase {
11+
public void testBuild() throws Exception {
12+
Person person = new Person.Builder().setAge(24).setHeight(178).setName("byhieg").setWeight(80).build();
13+
System.out.println(person.toString());
14+
}
15+
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.byhieg.designpatterntutorialtest;
2+
3+
import cn.byhieg.designpatterntutorial.builder.SimpleDialog;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2017/5/7.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class SimpleDialogTest extends TestCase {
11+
12+
public void testBuilder() throws Exception {
13+
SimpleDialog dialog = new SimpleDialog.Builder().setIcon("图标").setMessage("这是Dialog").setPositiveButton("确认").setNegativeButton("否定").create();
14+
15+
16+
}
17+
18+
}

0 commit comments

Comments
 (0)