Skip to content

Commit 1009c8d

Browse files
committed
feat(pattern-iterator): add User model and file handling classes for user data processing
- fix pattern-decorator project version
1 parent 21649fc commit 1009c8d

9 files changed

Lines changed: 359 additions & 1 deletion

File tree

pattern-design/pattern-decorator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.helltractor.demo</groupId>
99
<artifactId>pattern-design</artifactId>
10-
<version>1.0-SNAPSHOT</version>
10+
<version>${revision}</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.helltractor.demo</groupId>
9+
<artifactId>pattern-design</artifactId>
10+
<version>${revision}</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>pattern-iterator</artifactId>
15+
16+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.helltractor.demo;
2+
3+
import com.helltractor.demo.model.User;
4+
import com.helltractor.demo.user.UserFile;
5+
import com.helltractor.demo.user.UserFileBatch;
6+
import com.helltractor.demo.user.UserFileBatchRead;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.function.Consumer;
14+
15+
public class FileRead {
16+
17+
public static void main(String[] args) throws IOException {
18+
List<User> userList = new ArrayList<>();
19+
20+
readUser((user) -> {
21+
System.out.println(user.toString());
22+
userList.add(user);
23+
});
24+
25+
File file = new File("user.txt");
26+
UserFile userFile = new UserFile(file);
27+
for (User user : userFile) {
28+
System.out.println(user.toString());
29+
}
30+
31+
UserFileBatch userFileBatch = new UserFileBatch(file);
32+
for (List<User> batch : userFileBatch) {
33+
for (User user : batch) {
34+
System.out.println(user.toString());
35+
}
36+
}
37+
38+
UserFileBatchRead userFileBatchRead = new UserFileBatchRead(file);
39+
for (User user : userFileBatchRead) {
40+
System.out.println(user.toString());
41+
}
42+
}
43+
44+
private static void readUser(Consumer<User> consumer) throws IOException {
45+
List<String> lines = Files.readAllLines(new File("user.txt").toPath());
46+
for (String line : lines) {
47+
String midString = line.substring(1, line.length() - 1);
48+
String[] parts = midString.split(",");
49+
String name = parts[0].trim();
50+
int age = Integer.parseInt(parts[1].trim());
51+
User user = new User(name, age);
52+
consumer.accept(user);
53+
}
54+
}
55+
56+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.helltractor.demo.model;
2+
3+
public class User {
4+
5+
private final String name;
6+
7+
private final int age;
8+
9+
public User(String name, int age) {
10+
this.name = name;
11+
this.age = age;
12+
}
13+
14+
public String toString() {
15+
return "User{name='" + name + "', age=" + age + '}';
16+
}
17+
18+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.helltractor.demo.user;
2+
3+
import com.helltractor.demo.model.User;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.util.ArrayList;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
import java.util.NoSuchElementException;
12+
import java.util.stream.Collectors;
13+
14+
public class UserFile implements Iterable<User> {
15+
16+
private final File file;
17+
18+
public UserFile(File file) {
19+
this.file = file;
20+
}
21+
22+
@Override
23+
public Iterator<User> iterator() {
24+
return new UserFileIterator();
25+
}
26+
27+
private class UserFileIterator implements Iterator<User> {
28+
29+
private int index = 0;
30+
private List<User> userList = new ArrayList<>();
31+
32+
UserFileIterator() {
33+
try {
34+
userList = readUsersFromFile();
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
private List<User> readUsersFromFile() throws IOException {
41+
try {
42+
return Files.readAllLines(file.toPath()).stream().
43+
map(line -> {
44+
String midString = line.substring(1, line.length() - 1);
45+
String[] parts = midString.split(",");
46+
String name = parts[0].trim();
47+
int age = Integer.parseInt(parts[1].trim());
48+
return new User(name, age);
49+
}).collect(Collectors.toList());
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
return new ArrayList<>();
53+
}
54+
}
55+
56+
@Override
57+
public boolean hasNext() {
58+
return index < userList.size();
59+
}
60+
61+
@Override
62+
public User next() {
63+
if (!hasNext()) {
64+
throw new NoSuchElementException("No more users available");
65+
}
66+
return userList.get(index++);
67+
}
68+
}
69+
70+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.helltractor.demo.user;
2+
3+
import com.helltractor.demo.model.User;
4+
5+
import java.io.*;
6+
import java.util.ArrayList;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.NoSuchElementException;
10+
11+
public class UserFileBatch implements Iterable<List<User>> {
12+
13+
private final File file;
14+
private int batchSize = 10;
15+
16+
public UserFileBatch(File file) {
17+
this.file = file;
18+
}
19+
20+
public UserFileBatch(File file, int batchSize) {
21+
this.file = file;
22+
this.batchSize = batchSize;
23+
}
24+
25+
@Override
26+
public Iterator<List<User>> iterator() {
27+
return new UserFileBatchIterator();
28+
}
29+
30+
private class UserFileBatchIterator implements Iterator<List<User>> {
31+
32+
private boolean finished = false;
33+
private final BufferedReader reader;
34+
private List<User> batchUserList = new ArrayList<>();
35+
36+
UserFileBatchIterator() {
37+
try {
38+
this.reader = new BufferedReader(new FileReader(file));
39+
} catch (FileNotFoundException e) {
40+
throw new RuntimeException("File not found: " + file.getAbsolutePath(), e);
41+
}
42+
}
43+
44+
private void readUsersFromFile() throws IOException {
45+
batchUserList.clear();
46+
try {
47+
String line;
48+
while ((line = reader.readLine()) != null) {
49+
String midString = line.substring(1, line.length() - 1);
50+
String[] parts = midString.split(",");
51+
String name = parts[0].trim();
52+
int age = Integer.parseInt(parts[1].trim());
53+
batchUserList.add(new User(name, age));
54+
55+
if (batchUserList.size() == batchSize) {
56+
break;
57+
}
58+
}
59+
if (batchUserList.isEmpty()) {
60+
reader.close();
61+
finished = true;
62+
}
63+
} catch (IOException e) {
64+
finished = true;
65+
try {
66+
reader.close();
67+
} catch (IOException closeException) {
68+
throw new RuntimeException("Error closing reader", closeException);
69+
}
70+
}
71+
}
72+
73+
@Override
74+
public boolean hasNext() {
75+
return !finished;
76+
}
77+
78+
@Override
79+
public List<User> next() {
80+
if (!hasNext()) {
81+
throw new NoSuchElementException("No more users available");
82+
}
83+
try {
84+
readUsersFromFile();
85+
} catch (IOException e) {
86+
throw new RuntimeException("Error reading users from file", e);
87+
}
88+
return batchUserList;
89+
}
90+
}
91+
92+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.helltractor.demo.user;
2+
3+
import com.helltractor.demo.model.User;
4+
5+
import java.io.*;
6+
import java.util.ArrayList;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.NoSuchElementException;
10+
11+
public class UserFileBatchRead implements Iterable<User> {
12+
13+
private final File file;
14+
private int batchSize = 10;
15+
16+
public UserFileBatchRead(File file) {
17+
this.file = file;
18+
}
19+
20+
public UserFileBatchRead(File file, int batchSize) {
21+
this.file = file;
22+
this.batchSize = batchSize;
23+
}
24+
25+
@Override
26+
public Iterator<User> iterator() {
27+
return new UserFileReadBatchIterator();
28+
}
29+
30+
private class UserFileReadBatchIterator implements Iterator<User> {
31+
32+
private final BufferedReader reader;
33+
private int index = 0;
34+
private boolean finished = false;
35+
private final List<User> batchUserList = new ArrayList<>();
36+
37+
UserFileReadBatchIterator() {
38+
try {
39+
this.reader = new BufferedReader(new FileReader(file));
40+
} catch (FileNotFoundException e) {
41+
throw new RuntimeException("File not found: " + file.getAbsolutePath(), e);
42+
}
43+
}
44+
45+
private void readUsersFromFile() {
46+
batchUserList.clear();
47+
index = 0;
48+
try {
49+
String line;
50+
while ((line = reader.readLine()) != null) {
51+
String midString = line.substring(1, line.length() - 1);
52+
String[] parts = midString.split(",");
53+
String name = parts[0].trim();
54+
int age = Integer.parseInt(parts[1].trim());
55+
batchUserList.add(new User(name, age));
56+
if (batchUserList.size() == batchSize) {
57+
break;
58+
}
59+
}
60+
if (batchUserList.isEmpty()) {
61+
reader.close();
62+
finished = true;
63+
}
64+
} catch (IOException e) {
65+
finished = true;
66+
try {
67+
reader.close();
68+
} catch (IOException closeException) {
69+
throw new RuntimeException("Error closing reader", closeException);
70+
}
71+
}
72+
}
73+
74+
@Override
75+
public boolean hasNext() {
76+
if (batchUserList == null || index >= batchUserList.size() && !finished) {
77+
try {
78+
readUsersFromFile();
79+
} catch (Exception e) {
80+
throw new RuntimeException("Error reading users from file", e);
81+
}
82+
}
83+
return index < batchUserList.size();
84+
}
85+
86+
@Override
87+
public User next() {
88+
if (!hasNext()) {
89+
throw new NoSuchElementException("No more users available");
90+
}
91+
return batchUserList.get(index++);
92+
}
93+
}
94+
95+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
['user1',1]
2+
['user2',2]
3+
['user3',3]
4+
['user4',4]
5+
['user5',5]
6+
['user6',6]
7+
['user7',7]
8+
['user8',8]
9+
['user9',9]
10+
['user10',10]

pattern-design/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>pattern-command</module>
1515
<module>pattern-memento</module>
1616
<module>pattern-decorator</module>
17+
<module>pattern-iterator</module>
1718
</modules>
1819

1920
<properties>

0 commit comments

Comments
 (0)