Skip to content

Commit b46d26f

Browse files
author
caizhihao
committed
feat(NewAPI): add other
1 parent f3ba175 commit b46d26f

File tree

8 files changed

+206
-1
lines changed

8 files changed

+206
-1
lines changed

src/main/java/io/github/burukeyou/dataframe/iframe/OperationIFrame.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.burukeyou.dataframe.iframe;
22

33

4+
import io.github.burukeyou.dataframe.iframe.function.CompareTwo;
5+
46
import java.util.Collection;
57
import java.util.Comparator;
68

@@ -78,6 +80,12 @@ public interface OperationIFrame<T> {
7880
*/
7981
IFrame<T> retainAll(Collection<T> other,Comparator<T> comparator);
8082

83+
/**
84+
* Retains only the elements in this list that are contained in the specified collection
85+
* @return other collection
86+
* @param comparator repetitive judgment comparator
87+
*/
88+
<K> IFrame<T> retainAllOther(Collection<K> other, CompareTwo<T,K> comparator);
8189

8290
/**
8391
* intersection other frame
@@ -131,7 +139,6 @@ public interface OperationIFrame<T> {
131139
*/
132140
IFrame<T> different(Collection<T> other);
133141

134-
135142
/**
136143
* different other collection
137144
* Elements that are not within the other frame
@@ -140,6 +147,14 @@ public interface OperationIFrame<T> {
140147
*/
141148
IFrame<T> different(Collection<T> other,Comparator<T> comparator);
142149

150+
/**
151+
* different other collection
152+
* Elements that are not within the other frame
153+
* @return other collection
154+
* @param comparator repetitive judgment comparator
155+
*/
156+
<K> IFrame<T> differentOther(Collection<K> other, CompareTwo<T,K> comparator);
157+
143158
/**
144159
* subtract other
145160
* @return other

src/main/java/io/github/burukeyou/dataframe/iframe/OperationJDFrame.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.burukeyou.dataframe.iframe;
22

33

4+
import io.github.burukeyou.dataframe.iframe.function.CompareTwo;
5+
46
import java.util.Collection;
57
import java.util.Comparator;
68

@@ -75,6 +77,12 @@ public interface OperationJDFrame<T> extends OperationIFrame<T> {
7577
*/
7678
JDFrame<T> retainAll(Collection<T> other,Comparator<T> comparator);
7779

80+
/**
81+
* Retains only the elements in this list that are contained in the specified collection
82+
* @return other collection
83+
* @param comparator repetitive judgment comparator
84+
*/
85+
<K> JDFrame<T> retainAllOther(Collection<K> other, CompareTwo<T,K> comparator);
7886

7987
/**
8088
* intersection other frame
@@ -137,6 +145,14 @@ public interface OperationJDFrame<T> extends OperationIFrame<T> {
137145
*/
138146
JDFrame<T> different(Collection<T> other,Comparator<T> comparator);
139147

148+
/**
149+
* different other collection
150+
* Elements that are not within the other frame
151+
* @return other collection
152+
* @param comparator repetitive judgment comparator
153+
*/
154+
<K> JDFrame<T> differentOther(Collection<K> other, CompareTwo<T,K> comparator);
155+
140156
/**
141157
* subtract other
142158
* @return other

src/main/java/io/github/burukeyou/dataframe/iframe/OperationSDFrame.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.burukeyou.dataframe.iframe;
22

33

4+
import io.github.burukeyou.dataframe.iframe.function.CompareTwo;
5+
46
import java.util.Collection;
57
import java.util.Comparator;
68

@@ -77,6 +79,12 @@ public interface OperationSDFrame<T> extends OperationIFrame<T> {
7779
*/
7880
SDFrame<T> retainAll(Collection<T> other,Comparator<T> comparator);
7981

82+
/**
83+
* Retains only the elements in this list that are contained in the specified collection
84+
* @return other collection
85+
* @param comparator repetitive judgment comparator
86+
*/
87+
<K> SDFrame<T> retainAllOther(Collection<K> other, CompareTwo<T,K> comparator);
8088

8189
/**
8290
* intersection other frame
@@ -139,6 +147,14 @@ public interface OperationSDFrame<T> extends OperationIFrame<T> {
139147
*/
140148
SDFrame<T> different(Collection<T> other,Comparator<T> comparator);
141149

150+
/**
151+
* different other collection
152+
* Elements that are not within the other frame
153+
* @return other collection
154+
* @param comparator repetitive judgment comparator
155+
*/
156+
<K> SDFrame<T> differentOther(Collection<K> other, CompareTwo<T,K> comparator);
157+
142158
/**
143159
* subtract other
144160
* @return other
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.github.burukeyou.dataframe.iframe.function;
2+
3+
import java.io.Serializable;
4+
import java.util.function.Function;
5+
6+
/**
7+
* support compare two different element
8+
* support null value to compare , if null indicates lower priority
9+
*
10+
* @param <L> left element
11+
* @param <R> right element
12+
*
13+
* @author caizhihao
14+
*/
15+
public interface CompareTwo<L,R> {
16+
17+
/**
18+
* compare two different element
19+
* @param left left element
20+
* @param right right element
21+
*/
22+
int compare(L left, R right);
23+
24+
/**
25+
* Build JoinOn based on fields
26+
* @param leftField left element field
27+
* @param rightField right element field
28+
*/
29+
static <L,R, U extends Comparable<? super U>> CompareTwo<L,R> on(Function<L,U> leftField, Function<R,U> rightField){
30+
return (t,k) -> {{
31+
if (t == null && k == null){
32+
return 0;
33+
}
34+
if (t == null){
35+
return -1;
36+
}
37+
if (k == null){
38+
return 1;
39+
}
40+
U leftFieldValue = leftField.apply(t);
41+
U rightFieldValue = rightField.apply(k);
42+
43+
if (leftFieldValue == null && rightFieldValue == null){
44+
return 0;
45+
}
46+
if (leftFieldValue == null){
47+
return -1;
48+
}
49+
if (rightFieldValue == null){
50+
return 1;
51+
}
52+
return leftFieldValue.compareTo(rightFieldValue);
53+
}};
54+
}
55+
56+
/**
57+
* when equal compare other field
58+
* @param leftField left element field
59+
* @param rightField left element field
60+
*/
61+
default <U extends Comparable<? super U>> CompareTwo<L,R> thenOn(Function<L,U> leftField, Function<R,U> rightField){
62+
CompareTwo<L, R> other = on(leftField, rightField);
63+
return (CompareTwo<L,R> & Serializable) (c1, c2) -> {
64+
int res = compare(c1, c2);
65+
return (res != 0) ? res : other.compare(c1, c2);
66+
};
67+
}
68+
69+
/**
70+
* when equal compare other field
71+
* @param other other compare
72+
*/
73+
default CompareTwo<L,R> thenCompare(CompareTwo<L,R> other) {
74+
return (CompareTwo<L,R> & Serializable) (c1, c2) -> {
75+
int res = compare(c1, c2);
76+
return (res != 0) ? res : other.compare(c1, c2);
77+
};
78+
}
79+
}

src/main/java/io/github/burukeyou/dataframe/iframe/impl/AbstractDataFrameImpl.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alibaba.fastjson.JSON;
55
import com.alibaba.fastjson.JSONArray;
66
import io.github.burukeyou.dataframe.iframe.IFrame;
7+
import io.github.burukeyou.dataframe.iframe.function.CompareTwo;
78
import io.github.burukeyou.dataframe.iframe.function.ListToOneFunction;
89
import io.github.burukeyou.dataframe.iframe.function.ReplenishFunction;
910
import io.github.burukeyou.dataframe.iframe.function.SetFunction;
@@ -750,6 +751,20 @@ protected List<T> retainAllList(List<T> leftList, Collection<T> rightList, Compa
750751
return leftList.stream().filter(set::contains).collect(toList());
751752
}
752753

754+
protected <K> List<T> retainAllOtherList(List<T> leftList, Collection<K> rightList, CompareTwo<T,K> comparator){
755+
if (ListUtils.isEmpty(rightList)){
756+
return Collections.emptyList();
757+
}
758+
return leftList.stream().filter(e -> {
759+
for (K k : rightList) {
760+
if (comparator.compare(e,k) == 0){
761+
return true;
762+
}
763+
}
764+
return false;
765+
}).collect(toList());
766+
}
767+
753768
protected List<T> intersectionList(List<T> leftList, Collection<T> rightList){
754769
if (ListUtils.isEmpty(leftList) || ListUtils.isEmpty(rightList)){
755770
return Collections.emptyList();
@@ -793,6 +808,24 @@ protected List<T> differentList(List<T> leftList, Collection<T> rightList, Compa
793808
return leftList;
794809
}
795810

811+
protected <K> List<T> differentOtherList(List<T> leftList, Collection<K> rightList, CompareTwo<T,K> comparator) {
812+
if (ListUtils.isEmpty(leftList)){
813+
return leftList;
814+
}
815+
if (ListUtils.isEmpty(rightList)){
816+
return leftList;
817+
}
818+
return leftList.stream().filter(e -> {
819+
// todo optimization get (e,k) compare result
820+
for (K k : rightList) {
821+
if (comparator.compare(e,k) == 0){
822+
return false;
823+
}
824+
}
825+
return true;
826+
}).collect(toList());
827+
}
828+
796829
protected List<T> subtractList(List<T> leftList, Collection<T> rightList){
797830
if (ListUtils.isEmpty(rightList)){
798831
return leftList;

src/main/java/io/github/burukeyou/dataframe/iframe/impl/JDFrameImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,10 @@ public JDFrameImpl<T> retainAll(Collection<T> other, Comparator<T> comparator) {
11231123
return returnDF(retainAllList(viewList(),other,comparator));
11241124
}
11251125

1126+
@Override
1127+
public <K> JDFrameImpl<T> retainAllOther(Collection<K> other, CompareTwo<T, K> comparator) {
1128+
return returnDF(retainAllOtherList(viewList(),other,comparator));
1129+
}
11261130
@Override
11271131
public JDFrameImpl<T> intersection(IFrame<T> other) {
11281132
return returnDF(intersectionList(viewList(),other.toLists()));
@@ -1163,6 +1167,11 @@ public JDFrameImpl<T> different(Collection<T> other, Comparator<T> comparator) {
11631167
return returnDF(differentList(viewList(),other,comparator));
11641168
}
11651169

1170+
@Override
1171+
public <K> JDFrameImpl<T> differentOther(Collection<K> other, CompareTwo<T, K> comparator) {
1172+
return returnDF(differentOtherList(viewList(),other,comparator));
1173+
}
1174+
11661175
@Override
11671176
public JDFrameImpl<T> subtract(IFrame<T> other) {
11681177
List<T> ts = other.toLists();

src/main/java/io/github/burukeyou/dataframe/iframe/impl/SDFrameImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import io.github.burukeyou.dataframe.iframe.IFrame;
5+
import io.github.burukeyou.dataframe.iframe.JDFrame;
56
import io.github.burukeyou.dataframe.iframe.SDFrame;
67
import io.github.burukeyou.dataframe.iframe.WindowSDFrame;
78
import io.github.burukeyou.dataframe.iframe.function.*;
@@ -1130,6 +1131,11 @@ public SDFrameImpl<T> retainAll(Collection<T> other) {
11301131
public SDFrameImpl<T> retainAll(Collection<T> other, Comparator<T> comparator) {
11311132
return returnDF(retainAllList(viewList(),other,comparator));
11321133
}
1134+
1135+
@Override
1136+
public <K> SDFrame<T> retainAllOther(Collection<K> other, CompareTwo<T, K> comparator) {
1137+
return returnDF(retainAllOtherList(viewList(),other,comparator));
1138+
}
11331139
@Override
11341140
public SDFrameImpl<T> intersection(IFrame<T> other) {
11351141
return returnDF(intersectionList(viewList(),other.toLists()));
@@ -1169,6 +1175,11 @@ public SDFrameImpl<T> different(Collection<T> other, Comparator<T> comparator) {
11691175
return returnDF(differentList(viewList(),other,comparator));
11701176
}
11711177

1178+
@Override
1179+
public <K> SDFrameImpl<T> differentOther(Collection<K> other, CompareTwo<T, K> comparator) {
1180+
return returnDF(differentOtherList(viewList(),other,comparator));
1181+
}
1182+
11721183
@Override
11731184
public SDFrameImpl<T> subtract(IFrame<T> other) {
11741185
return returnDF(subtractList(viewList(),other.toLists()));

src/test/java/io/github/burukeyou/JDFrameTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.burukeyou.data.UserInfo;
55
import io.github.burukeyou.dataframe.iframe.JDFrame;
66
import io.github.burukeyou.dataframe.iframe.SDFrame;
7+
import io.github.burukeyou.dataframe.iframe.function.CompareTwo;
78
import io.github.burukeyou.dataframe.iframe.item.FI2;
89
import io.github.burukeyou.dataframe.iframe.item.FI3;
910
import io.github.burukeyou.dataframe.iframe.item.FI4;
@@ -582,4 +583,29 @@ public void testOper2(){
582583

583584
System.out.println();
584585
}
586+
587+
@Test
588+
public void testOper3(){
589+
List<UserInfo> us1 = Arrays.asList(new UserInfo("a", 1,"99"), new UserInfo("a",11,"99"),new UserInfo("c",3,"88"), new UserInfo("b", 4,"77"));
590+
591+
System.out.println("---- retainAllOther-1 -----");
592+
SDFrame.read(studentList)
593+
.retainAllOther(us1, CompareTwo.on(Student::getName,UserInfo::getKey1))
594+
.show();
595+
596+
System.out.println("---- retainAllOther-2 -----");
597+
SDFrame.read(studentList)
598+
.retainAllOther(us1, CompareTwo.on(Student::getName,UserInfo::getKey1).thenOn(Student::getAge,UserInfo::getKey2))
599+
.show();
600+
601+
System.out.println("---- differentOther-1 -----");
602+
SDFrame.read(studentList)
603+
.differentOther(us1, CompareTwo.on(Student::getName,UserInfo::getKey1))
604+
.show();
605+
606+
System.out.println("---- differentOther-2 -----");
607+
SDFrame.read(studentList)
608+
.differentOther(us1, CompareTwo.on(Student::getName,UserInfo::getKey1).thenOn(Student::getAge,UserInfo::getKey2))
609+
.show();
610+
}
585611
}

0 commit comments

Comments
 (0)