ejni/abstractions/
set.rs

1use crate::abstractions::iterator::Iterator;
2use crate::class::Class;
3use crate::object::Object;
4use jni::errors::Result;
5use jni::objects::JValue;
6use jni::sys::_jobject;
7use jni::JNIEnv;
8
9/// Wrapper around `java.util.Set`
10pub struct Set<'a> {
11    /// The Set itself
12    pub inner: Object<'a>,
13
14    /// The Class contained in the Set
15    pub class: Class<'a>,
16
17    env: &'a JNIEnv<'a>,
18}
19
20#[allow(clippy::from_over_into)]
21impl<'a> Into<*mut _jobject> for Set<'a> {
22    fn into(self) -> *mut _jobject {
23        self.inner.inner.into_inner()
24    }
25}
26
27impl<'a> Drop for Set<'a> {
28    fn drop(&mut self) {
29        let _ = self.env.delete_local_ref(self.inner.inner);
30    }
31}
32
33impl<'a> Set<'a> {
34    /// Create a new Set. The caller must guarantee that the passed in Object implements Set and is not null.
35    pub fn new(env: &'a JNIEnv<'a>, object: Object<'a>, class: Class<'a>) -> Self {
36        Self {
37            inner: object,
38            class,
39            env,
40        }
41    }
42
43    /// Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
44    pub fn hashset(env: &'a JNIEnv<'a>, v_class: Class<'a>) -> Result<Self> {
45        let hashset = env.new_object("java/util/HashSet", "()V", &[])?;
46        Ok(Self {
47            inner: Object::new(env, hashset, Class::HashSet(env)?),
48            class: v_class,
49            env,
50        })
51    }
52
53    /// Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
54    pub fn hashset_with_capacity(
55        env: &'a JNIEnv<'a>,
56        v_class: Class<'a>,
57        initial_capacity: i32,
58    ) -> Result<Self> {
59        let hashset = env.new_object(
60            "java/util/HashSet",
61            "(I)V",
62            &[JValue::Int(initial_capacity)],
63        )?;
64        Ok(Self {
65            inner: Object::new(env, hashset, Class::HashSet(env)?),
66            class: v_class,
67            env,
68        })
69    }
70
71    /// Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
72    pub fn hashset_with_capacity_and_load_factor(
73        env: &'a JNIEnv<'a>,
74        v_class: Class<'a>,
75        initial_capacity: i32,
76        load_factor: f32,
77    ) -> Result<Self> {
78        let hashset = env.new_object(
79            "java/util/HashSet",
80            "(IF)V",
81            &[JValue::Int(initial_capacity), JValue::Float(load_factor)],
82        )?;
83        Ok(Self {
84            inner: Object::new(env, hashset, Class::HashSet(env)?),
85            class: v_class,
86            env,
87        })
88    }
89
90    /// Returns the number of elements in this set (its cardinality).
91    pub fn size(&self) -> Result<i32> {
92        let size = self.env.call_method(self.inner.inner, "size", "()I", &[])?;
93        size.i()
94    }
95
96    /// Returns an iterator over the elements in this set.
97    pub fn iterator(&self) -> Result<Iterator<'a>> {
98        let iterator =
99            self.env
100                .call_method(self.inner.inner, "iterator", "()Ljava/util/Iterator;", &[])?;
101        let object = Object::new(self.env, iterator.l()?, Class::Iterator(self.env)?);
102        let iterator = Iterator::new(self.env, object, self.class.clone());
103        Ok(iterator)
104    }
105
106    /// Convert the java.util.Set to a Vec
107    pub fn to_vec(&self) -> Result<Vec<Object<'a>>> {
108        let mut vec = Vec::new();
109        let iter = self.iterator()?;
110        while let Some(i) = iter.next()? {
111            vec.push(i);
112        }
113
114        Ok(vec)
115    }
116}