1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-10-27 20:34:16 +00:00
gnosygnu_xowa/100_core/src/gplx/Yn.java

78 lines
2.8 KiB
Java

/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx;
import gplx.core.stores.*;
import gplx.objects.primitives.BoolUtl;
public class Yn {
public static final String Y = "y", N = "n";
public static boolean parse_by_char_or(String v, boolean or) {
if (String_.Eq(v, Y)) return true;
else if (String_.Eq(v, N)) return false;
else return or;
}
public static boolean parse_or_n_(String v) {return parse_or(v, false);}
public static int parse_as_int(String v) {
if (v == null) return BoolUtl.NullInt;
else if (String_.Eq(v, "y")) return BoolUtl.YInt;
else if (String_.Eq(v, "n")) return BoolUtl.NInt;
else return BoolUtl.NullInt;
}
public static boolean parse_or(String v, boolean or) {
int v_int = parse_as_int(v);
switch (v_int) {
case BoolUtl.NInt: return false;
case BoolUtl.YInt: return true;
case BoolUtl.NullInt: return or;
default: throw Err_.new_unhandled(v_int);
}
}
public static boolean parse(String v) {
int v_int = parse_as_int(v);
if (v_int == BoolUtl.NullInt) Err_.new_unhandled(v);
return v_int == BoolUtl.YInt;
}
public static String To_str(boolean v) {return v ? "y" : "n";}
public static String To_nullable_str(byte v) {
switch (v) {
case BoolUtl.YByte: return "y";
case BoolUtl.NByte: return "n";
case BoolUtl.NullByte: return "?";
default: throw Err_.new_unhandled(v);
}
}
public static byte To_nullable_byte(String v) {
if (v != null && String_.Len(v) == 1) {
char c = String_.CharAt(v, 0);
switch (c) {
case 'y': return BoolUtl.YByte;
case 'n': return BoolUtl.NByte;
case '?': return BoolUtl.NullByte;
}
}
throw Err_.new_unhandled(v);
}
public static boolean store_bool_or(SrlMgr mgr, String key, boolean or) {
String v = mgr.SrlStrOr(key, "");
return mgr.Type_rdr() ? parse_or(v, or) : or;
}
public static boolean coerce_(Object o) {String s = String_.as_(o); return s != null ? parse_or(s, false) : BoolUtl.Cast(o);}
public static boolean readOrFalse_(DataRdr rdr, String key) {return read_(rdr, key, false);}
public static boolean readOrTrue_(DataRdr rdr, String key) {return read_(rdr, key, true);}
static boolean read_(DataRdr rdr, String key, boolean or) {
String v = rdr.ReadStrOr(key, null);
return parse_or(v, or);
}
}