Skip to content

Commit 23be711

Browse files
committed
🔖 泛型示例
1 parent 0c6b7c7 commit 23be711

6 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.dunwu.javase.util;
2+
3+
import io.github.dunwu.javase.util.tuple.FiveTuple;
4+
import io.github.dunwu.javase.util.tuple.FourTuple;
5+
import io.github.dunwu.javase.util.tuple.ThreeTuple;
6+
import io.github.dunwu.javase.util.tuple.TwoTuple;
7+
8+
/**
9+
* 元祖工具类
10+
*/
11+
public class TupleUtil {
12+
public static <A, B> TwoTuple<A, B> tuple(A a, B b) {
13+
return new TwoTuple<A, B>(a, b);
14+
}
15+
16+
public static <A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c) {
17+
return new ThreeTuple<A, B, C>(a, b, c);
18+
}
19+
20+
public static <A, B, C, D> FourTuple<A, B, C, D> tuple(A a, B b, C c, D d) {
21+
return new FourTuple<A, B, C, D>(a, b, c, d);
22+
}
23+
24+
public static <A, B, C, D, E> FiveTuple<A, B, C, D, E> tuple(A a, B b, C c, D d, E e) {
25+
return new FiveTuple<A, B, C, D, E>(a, b, c, d, e);
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//: net/mindview/util/FiveTuple.java
2+
package io.github.dunwu.javase.util.tuple;
3+
4+
public class FiveTuple<A,B,C,D,E>
5+
extends FourTuple<A,B,C,D> {
6+
public final E fifth;
7+
public FiveTuple(A a, B b, C c, D d, E e) {
8+
super(a, b, c, d);
9+
fifth = e;
10+
}
11+
@Override
12+
public String toString() {
13+
return "(" + first + ", " + second + ", " +
14+
third + ", " + fourth + ", " + fifth + ")";
15+
}
16+
} ///:~
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//: net/mindview/util/FourTuple.java
2+
package io.github.dunwu.javase.util.tuple;
3+
4+
public class FourTuple<A,B,C,D> extends ThreeTuple<A,B,C> {
5+
public final D fourth;
6+
public FourTuple(A a, B b, C c, D d) {
7+
super(a, b, c);
8+
fourth = d;
9+
}
10+
@Override
11+
public String toString() {
12+
return "(" + first + ", " + second + ", " +
13+
third + ", " + fourth + ")";
14+
}
15+
} ///:~
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//: net/mindview/util/ThreeTuple.java
2+
package io.github.dunwu.javase.util.tuple;
3+
4+
public class ThreeTuple<A,B,C> extends TwoTuple<A,B> {
5+
public final C third;
6+
public ThreeTuple(A a, B b, C c) {
7+
super(a, b);
8+
third = c;
9+
}
10+
@Override
11+
public String toString() {
12+
return "(" + first + ", " + second + ", " + third +")";
13+
}
14+
} ///:~
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.dunwu.javase.util.tuple;
2+
3+
public class TwoTuple<A, B> {
4+
public final A first;
5+
public final B second;
6+
7+
public TwoTuple(A a, B b) {
8+
first = a;
9+
second = b;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return "(" + first + ", " + second + ")";
15+
}
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.dunwu.javase.util;
2+
3+
import io.github.dunwu.javase.util.tuple.FiveTuple;
4+
import io.github.dunwu.javase.util.tuple.FourTuple;
5+
import io.github.dunwu.javase.util.tuple.ThreeTuple;
6+
import io.github.dunwu.javase.util.tuple.TwoTuple;
7+
import org.junit.Test;
8+
9+
import java.util.ArrayList;
10+
11+
/**
12+
* @author Zhang Peng
13+
*/
14+
public class TupleUtilTest {
15+
static TwoTuple<String, Integer> f() {
16+
return TupleUtil.tuple("hi", 47);
17+
}
18+
19+
static ThreeTuple<Amphibian, String, Integer> g() {
20+
return TupleUtil.tuple(new Amphibian(), "hi", 47);
21+
}
22+
23+
static FourTuple<Vehicle, Amphibian, String, Integer> h() {
24+
return TupleUtil.tuple(new Vehicle(), new Amphibian(), "hi", 47);
25+
}
26+
27+
static FiveTuple<Vehicle, Amphibian, String, Integer, Double> k() {
28+
return TupleUtil.tuple(new Vehicle(), new Amphibian(), "hi", 47, 11.1);
29+
}
30+
31+
static class TupleList<A, B, C, D> extends ArrayList<FourTuple<A, B, C, D>> {}
32+
33+
static class Amphibian {}
34+
35+
static class Vehicle {}
36+
37+
@Test
38+
public void test() {
39+
TwoTuple<String, Integer> ttsi = f();
40+
System.out.println(ttsi);
41+
// ttsi.first = "there"; // Compile error: final
42+
System.out.println(g());
43+
System.out.println(h());
44+
System.out.println(k());
45+
}
46+
47+
@Test
48+
public void testList() {
49+
TupleList<Vehicle, Amphibian, String, Integer> tl = new TupleList<>();
50+
tl.add(h());
51+
tl.add(h());
52+
for (FourTuple<Vehicle, Amphibian, String, Integer> i : tl)
53+
System.out.println(i);
54+
}
55+
}

0 commit comments

Comments
 (0)