Skip to content

Commit 2d18c8b

Browse files
committed
feat: using hash set
1 parent 1242566 commit 2d18c8b

File tree

5 files changed

+15
-59
lines changed

5 files changed

+15
-59
lines changed

dart_native/android/src/main/java/com/dartnative/dart_native/ArrayListConverter.java

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.dartnative.dart_native;
22

3-
import io.flutter.Log;
3+
import android.util.Log;
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.Collections;
7+
import java.util.HashSet;
78
import java.util.List;
89
import java.util.Set;
910

@@ -142,54 +143,11 @@ public List arrayToList(Object array) {
142143

143144
/************************set to list***************************************/
144145

145-
public List setToList(Set set) {
146-
Log.d("HUIZZ", "set start");
147-
Object ele = set.iterator().next();
148-
List arrayList;
149-
if (ele instanceof Integer) {
150-
arrayList = new ArrayList<Integer>();
151-
for (Object element : set) {
152-
arrayList.add(element);
153-
}
154-
} else if (ele instanceof Short) {
155-
arrayList = new ArrayList<Short>();
156-
for (Object element : set) {
157-
arrayList.add(element);
158-
}
159-
} else if (ele instanceof Byte) {
160-
arrayList = new ArrayList<Byte>();
161-
for (Object element : set) {
162-
arrayList.add(element);
163-
}
164-
} else if (ele instanceof Long) {
165-
arrayList = new ArrayList<Long>();
166-
for (Object element : set) {
167-
arrayList.add(element);
168-
}
169-
} else if (ele instanceof Boolean) {
170-
arrayList = new ArrayList<Boolean>();
171-
for (Object element : set) {
172-
arrayList.add(element);
173-
}
174-
} else if (ele instanceof Float) {
175-
arrayList = new ArrayList<Float>();
176-
for (Object element : set) {
177-
arrayList.add(element);
178-
}
179-
} else if (ele instanceof Double) {
180-
arrayList = new ArrayList<Double>();
181-
for (Object element : set) {
182-
arrayList.add(element);
183-
}
184-
} else if (ele instanceof Character) {
185-
arrayList = new ArrayList<Character>();
186-
for (Object element : set) {
187-
arrayList.add(element);
188-
}
189-
} else {
190-
arrayList = Arrays.asList(set.toArray());
146+
public List setToList(HashSet<Object> set) {
147+
if (set == null || set.size() == 0) {
148+
return new ArrayList();
191149
}
192-
Log.d("HUIZZ", "set success");
193-
return arrayList;
150+
151+
return new ArrayList<Object>(set);
194152
}
195153
}

dart_native/android/src/main/jni/dart_native.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ void *invokeNativeMethodNeo(void *classPtr, char *methodName, void **args, char
220220
fillArgs(args, argTypes, argValues, argCount);
221221
}
222222
char *methodSignature = spliceChar(signature, returnType);
223+
NSLog("call method %s %s", methodName, methodSignature);
223224
jmethodID method = getEnv()->GetMethodID(cls, methodName, methodSignature);
224225

225226
if (strlen(returnType) > 1) {

dart_native/example/android/app/src/main/java/com/dartnative/dart_native_example/RuntimeStub.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Iterator;
1212
import java.util.List;
1313
import java.util.Set;
14+
import java.util.HashSet;
1415

1516
public class RuntimeStub {
1617
private final String TAG = "dart_java";
@@ -159,7 +160,7 @@ public byte[] getByteArray(byte[] bytes) {
159160
return new byte[]{1, 2, 10};
160161
}
161162

162-
public Set<Integer> getIntSet(Set<Integer> set) {
163+
public Set<Integer> getIntSet(HashSet<Integer> set) {
163164
for(Integer element : set) {
164165
Log.d(TAG, "Set element is " + element);
165166
}
@@ -171,7 +172,7 @@ public Set<Integer> getIntSet(Set<Integer> set) {
171172
return backSet;
172173
}
173174

174-
public Set<Float> getFloatSet(Set<Float> set) {
175+
public Set<Float> getFloatSet(HashSet<Float> set) {
175176
for(Float element : set) {
176177
Log.d(TAG, "Set element is " + element);
177178
}

dart_native/example/lib/android/unit_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ testAndroid(RuntimeStub stub) {
8686
// print("item $item");
8787
// }
8888
//
89-
// list = stub.getByteList([byte(1), byte(2), byte(3), byte(4)]);
89+
// List list = stub.getByteList([byte(1), byte(2), byte(3), byte(4)]);
9090
// for (int item in list) {
9191
// print("item $item");
9292
// }

dart_native/lib/src/android/foundation/collection/jset.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import 'package:dart_native/src/android/runtime/jsubclass.dart';
55
import 'package:ffi/ffi.dart';
66

77
/// Stands for `Set` in Android.
8-
const String CLS_SET = "java/util/Set";
98
const String CLS_HASH_SET = "java/util/HashSet";
109

1110
class JSet extends JSubclass<Set> {
12-
JSet(Set value, {String clsName: CLS_SET, InitSubclass init: _new}) : super(value, _new, clsName) {
11+
JSet(Set value, {String clsName: CLS_HASH_SET, InitSubclass init: _new}) : super(value, _new, clsName) {
1312
value = Set.of(value);
1413
}
1514

16-
JSet.fromPointer(Pointer<Void> ptr, {String clsName: CLS_SET}) : super.fromPointer(ptr, clsName) {
15+
JSet.fromPointer(Pointer<Void> ptr, {String clsName: CLS_HASH_SET}) : super.fromPointer(ptr, clsName) {
1716
JObject converter = JObject("com/dartnative/dart_native/ArrayListConverter");
18-
List list = JList.fromPointer(converter.invoke("setToList", [JObject("java/util/Set", pointer: ptr)], "Ljava/util/List;")).raw;
17+
List list = JList.fromPointer(converter.invoke("setToList", [JObject("java/util/HashSet", pointer: ptr)], "Ljava/util/List;")).raw;
1918
raw = list.toSet();
2019
}
2120
}
@@ -25,9 +24,6 @@ Pointer<Utf8> _argSignature = Utf8.toUtf8("Ljava/lang/Object;");
2524
/// New native 'Set'.
2625
Pointer<Void> _new(dynamic value, String clsName) {
2726
if (value is Set) {
28-
///'Set' default implementation 'HashSet'.
29-
if (clsName == CLS_SET) clsName = CLS_HASH_SET;
30-
3127
JObject nativeSet = JObject(clsName);
3228
if (value == null) {
3329
return nativeSet.pointer;

0 commit comments

Comments
 (0)