Skip to content

Commit 862055f

Browse files
committed
Mapper 与 Array 接口新增 forEach 遍历方法
1 parent a50bc76 commit 862055f

4 files changed

Lines changed: 165 additions & 5 deletions

File tree

‎okhttps-test/src/main/java/com/ejlchina/okhttps/test/BaseTestCases.java‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ void testToMapper() {
3939
String json = getUserObjectStr();
4040
InputStream in = new ByteArrayInputStream(json.getBytes());
4141
Mapper mapper = msgConvertor.toMapper(in, StandardCharsets.UTF_8);
42+
43+
mapper.forEach((key, data) -> {
44+
if ("id".equals(key)) {
45+
Assert.assertEquals(1, data.toInt());
46+
}
47+
if ("name".equals(key)) {
48+
Assert.assertEquals("Jack", data.toString());
49+
}
50+
});
51+
4252
Set<String> keys = mapper.keySet();
4353
Assert.assertEquals(2, keys.size());
4454
Assert.assertTrue(keys.contains("id"));
@@ -55,15 +65,23 @@ void testToMapper() {
5565
void testToArray() {
5666
String json = getUserListStr();
5767
InputStream in = new ByteArrayInputStream(json.getBytes());
58-
Array jsonObj = msgConvertor.toArray(in, StandardCharsets.UTF_8);
59-
Assert.assertFalse(jsonObj.isEmpty());
60-
Assert.assertEquals(2, jsonObj.size());
61-
Mapper json1 = jsonObj.getMapper(0);
62-
Mapper json2 = jsonObj.getMapper(1);
68+
69+
Array array = msgConvertor.toArray(in, StandardCharsets.UTF_8);
70+
71+
array.forEach((index, data) -> {
72+
System.out.println("index = " + index);
73+
System.out.println("data = " + data.toMapper());
74+
});
75+
76+
Assert.assertFalse(array.isEmpty());
77+
Assert.assertEquals(2, array.size());
78+
Mapper json1 = array.getMapper(0);
79+
Mapper json2 = array.getMapper(1);
6380
Assert.assertEquals(1, json1.getInt("id"));
6481
Assert.assertEquals("Jack", json1.getString("name"));
6582
Assert.assertEquals(2, json2.getInt("id"));
6683
Assert.assertEquals("Tom", json2.getString("name"));
84+
6785
System.out.println("case 2 passed!");
6886
}
6987

‎okhttps/src/main/java/com/ejlchina/okhttps/Array.java‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,63 @@ public interface Array extends DataSet {
5757
*/
5858
String getString(int index);
5959

60+
/**
61+
* 遍历 Array
62+
* @since 2.5.1
63+
* @param consumer 消费者
64+
*/
65+
default void forEach(Consumer consumer) {
66+
for (int i = 0; i < size(); i++) {
67+
int index = i;
68+
consumer.accept(i, new Data() {
69+
@Override
70+
public Mapper toMapper() {
71+
return getMapper(index);
72+
}
73+
74+
@Override
75+
public Array toArray() {
76+
return getArray(index);
77+
}
78+
79+
@Override
80+
public boolean toBool() {
81+
return getBool(index);
82+
}
83+
84+
@Override
85+
public int toInt() {
86+
return getInt(index);
87+
}
88+
89+
@Override
90+
public long toLong() {
91+
return getLong(index);
92+
}
93+
94+
@Override
95+
public float toFloat() {
96+
return getFloat(index);
97+
}
98+
99+
@Override
100+
public double toDouble() {
101+
return getDouble(index);
102+
}
103+
104+
@Override
105+
public String toString() {
106+
return getString(index);
107+
}
108+
109+
});
110+
}
111+
}
112+
113+
interface Consumer {
114+
115+
void accept(int index, Data data);
116+
117+
}
118+
60119
}

‎okhttps/src/main/java/com/ejlchina/okhttps/DataSet.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,28 @@ public interface DataSet {
1616
*/
1717
boolean isEmpty();
1818

19+
/**
20+
* 只读数据
21+
* @since 2.5.1
22+
*/
23+
interface Data {
24+
25+
Mapper toMapper();
26+
27+
Array toArray();
28+
29+
boolean toBool();
30+
31+
int toInt();
32+
33+
long toLong();
34+
35+
float toFloat();
36+
37+
double toDouble();
38+
39+
String toString();
40+
41+
}
42+
1943
}

‎okhttps/src/main/java/com/ejlchina/okhttps/Mapper.java‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,63 @@ public interface Mapper extends DataSet {
7171
*/
7272
Set<String> keySet();
7373

74+
75+
/**
76+
* 遍历 Mapper
77+
* @since 2.5.1
78+
* @param consumer 消费者
79+
*/
80+
default void forEach(Consumer consumer) {
81+
for (String key: keySet()) {
82+
consumer.accept(key, new Data() {
83+
@Override
84+
public Mapper toMapper() {
85+
return getMapper(key);
86+
}
87+
88+
@Override
89+
public Array toArray() {
90+
return getArray(key);
91+
}
92+
93+
@Override
94+
public boolean toBool() {
95+
return getBool(key);
96+
}
97+
98+
@Override
99+
public int toInt() {
100+
return getInt(key);
101+
}
102+
103+
@Override
104+
public long toLong() {
105+
return getLong(key);
106+
}
107+
108+
@Override
109+
public float toFloat() {
110+
return getFloat(key);
111+
}
112+
113+
@Override
114+
public double toDouble() {
115+
return getDouble(key);
116+
}
117+
118+
@Override
119+
public String toString() {
120+
return getString(key);
121+
}
122+
123+
});
124+
}
125+
}
126+
127+
interface Consumer {
128+
129+
void accept(String key, Data data);
130+
131+
}
132+
74133
}

0 commit comments

Comments
 (0)