Skip to main content

toad_jni/java/net/
protocol_family.rs

1use crate::java::{self, NoUpcast, Object};
2
3/// `java.net.ProtocolFamily`
4pub struct ProtocolFamily(java::lang::Object);
5java::object_newtype!(ProtocolFamily);
6impl java::Class for ProtocolFamily {
7  const PATH: &'static str = "java/net/ProtocolFamily";
8}
9
10/// `java.net.StandardProtocolFamily`
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub enum StandardProtocolFamily {
13  /// `java.net.StandardProtocolFamily.INET`
14  INet,
15  /// `java.net.StandardProtocolFamily.INET6`
16  INet6,
17  /// `java.net.StandardProtocolFamily.UNIX`
18  Unix,
19}
20
21impl StandardProtocolFamily {
22  /// cast a `StandardProtocolFamily` to [`ProtocolFamily`]
23  pub fn into_protocol_family(self, e: &mut java::Env) -> ProtocolFamily {
24    ProtocolFamily(self.downcast(e))
25  }
26}
27
28impl java::Object for StandardProtocolFamily {
29  fn upcast(e: &mut java::Env, jobj: java::lang::Object) -> Self {
30    let (inet, inet6, unix) =
31      (Self::INet.downcast(e), Self::INet6.downcast(e), Self::Unix.downcast(e));
32    if jobj.equals(e, &inet) {
33      Self::INet
34    } else if jobj.equals(e, &inet6) {
35      Self::INet6
36    } else if jobj.equals(e, &unix) {
37      Self::Unix
38    } else {
39      panic!("not StandardProtocolFamily: {}", jobj.to_string(e));
40    }
41  }
42
43  fn downcast(self, e: &mut java::Env) -> java::lang::Object {
44    static INET: java::StaticField<StandardProtocolFamily, NoUpcast<StandardProtocolFamily>> =
45      java::StaticField::new("INET");
46    static INET6: java::StaticField<StandardProtocolFamily, NoUpcast<StandardProtocolFamily>> =
47      java::StaticField::new("INET6");
48    static UNIX: java::StaticField<StandardProtocolFamily, NoUpcast<StandardProtocolFamily>> =
49      java::StaticField::new("UNIX");
50
51    match self {
52      | Self::INet => INET.get(e).downcast(e),
53      | Self::INet6 => INET6.get(e).downcast(e),
54      | Self::Unix => UNIX.get(e).downcast(e),
55    }
56  }
57
58  fn downcast_ref(&self, e: &mut java::Env) -> java::lang::Object {
59    self.downcast(e)
60  }
61}
62
63impl java::Class for StandardProtocolFamily {
64  const PATH: &'static str = "java/net/StandardProtocolFamily";
65}