mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.8.1.1
This commit is contained in:
@@ -44,19 +44,27 @@ public class Err extends RuntimeException {
|
||||
msgs_ary[msgs_idx] = new Err_msg(type, msg, args);
|
||||
++msgs_idx;
|
||||
}
|
||||
public String To_str__full() {return To_str(Bool_.N);}
|
||||
public String To_str__log() {return To_str(Bool_.Y);}
|
||||
private String To_str(boolean called_by_log) {
|
||||
public String To_str__full() {return To_str(Bool_.N, Bool_.Y);}
|
||||
public String To_str__log() {return To_str(Bool_.Y, Bool_.Y);}
|
||||
public String To_str__msg_only(){
|
||||
return msgs_idx == 0 ? "<<MISSING ERROR MESSAGE>>" : msgs_ary[0].To_str(); // take 1st message only
|
||||
}
|
||||
public String To_str__top_wo_args() {
|
||||
return msgs_idx == 0 ? "<<MISSING ERROR MESSAGE>>" : msgs_ary[0].To_str_wo_args();
|
||||
}
|
||||
private String To_str(boolean called_by_log, boolean include_trace) {
|
||||
String nl_str = called_by_log ? "\t" : "\n";
|
||||
String rv = ""; //nl_str + "----------------------------------------------------------------------" + nl_str;
|
||||
for (int i = 0; i < msgs_idx; ++i) {
|
||||
rv += "[err " + Int_.Xto_str(i) + "] " + msgs_ary[i].To_str() + nl_str;
|
||||
}
|
||||
rv += "[trace]:" + Trace_to_str(is_gplx, called_by_log, trace_ignore, trace == null ? Err_.Trace_lang(this) : trace);
|
||||
if (include_trace)
|
||||
rv += "[trace]:" + Trace_to_str(is_gplx, called_by_log, trace_ignore, trace == null ? Err_.Trace_lang(this) : trace);
|
||||
return rv;
|
||||
}
|
||||
@Override public String getMessage() {return To_str__full();}
|
||||
@Override public String getMessage() {return To_str__msg_only();}
|
||||
public static String Trace_to_str(boolean is_gplx, boolean called_by_log, int ignore_lines, String trace) {
|
||||
if (trace == null) return ""; // WORKAROUND:.NET: StackTrace is only available when error is thrown; can't do "Console.Write(new Exception().StackTrace);
|
||||
String[] lines = String_.Split_lang(trace, '\n'); int lines_len = lines.length;
|
||||
int line_bgn = 0;
|
||||
if (is_gplx) { // remove Err_.new_wo_type lines from trace for gplx exceptions
|
||||
|
||||
@@ -66,6 +66,6 @@ public class Err_ {
|
||||
}
|
||||
public static String Message_gplx_full(Exception e) {return cast_or_make(e).To_str__full();}
|
||||
public static String Message_gplx_log(Exception e) {return cast_or_make(e).To_str__log();}
|
||||
private static Err cast_or_make(Exception e) {return ClassAdp_.Eq_typeSafe(e, Err.class) ? (Err)e : new Err(Bool_.N, Err_.Trace_lang(e), ClassAdp_.NameOf_obj(e), Err_.Message_lang(e));}
|
||||
public static Err cast_or_make(Exception e) {return ClassAdp_.Eq_typeSafe(e, Err.class) ? (Err)e : new Err(Bool_.N, Err_.Trace_lang(e), ClassAdp_.NameOf_obj(e), Err_.Message_lang(e));}
|
||||
public static final String Type__op_canceled = "gplx.op_canceled";
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ public class Err_msg {
|
||||
public void Args_add(Object[] add) {
|
||||
this.args = (Object[])Array_.Resize_add(args, add);
|
||||
}
|
||||
public String To_str() {return To_str_w_type(type, msg, args);}
|
||||
public String To_str() {return To_str_w_type(type, msg, args);}
|
||||
public String To_str_wo_type() {return To_str(msg, args);}
|
||||
public String To_str_wo_args() {return To_str(msg);}
|
||||
public static String To_str(String msg, Object... args) {return To_str_w_type(null, msg, args);}
|
||||
public static String To_str_w_type(String type, String msg, Object... args) {
|
||||
String rv = String_.Len_eq_0(type) ? "" : "<" + type + "> ";
|
||||
|
||||
@@ -42,8 +42,8 @@ public class Btrie_fast_mgr {
|
||||
++cur_pos;
|
||||
}
|
||||
}
|
||||
public Btrie_fast_mgr Add_bry_bval(byte key, byte val) {return Add(new byte[] {key}, Byte_obj_val.new_(val));}
|
||||
public Btrie_fast_mgr Add_bry_bval(byte[] key, byte val) {return Add(key, Byte_obj_val.new_(val));}
|
||||
public Btrie_fast_mgr Add_bry_byte(byte key, byte val) {return Add(new byte[] {key}, Byte_obj_val.new_(val));}
|
||||
public Btrie_fast_mgr Add_bry_byte(byte[] key, byte val) {return Add(key, Byte_obj_val.new_(val));}
|
||||
public Btrie_fast_mgr Add_str_byte(String key, byte val) {return Add(Bry_.new_u8(key), Byte_obj_val.new_(val));}
|
||||
public Btrie_fast_mgr Add(byte key, Object val) {return Add(new byte[] {key}, val);}
|
||||
public Btrie_fast_mgr Add(String key, Object val) {return Add(Bry_.new_u8(key), val);}
|
||||
|
||||
@@ -46,8 +46,8 @@ public class Btrie_slim_mgr implements Btrie_mgr {
|
||||
public Btrie_slim_mgr Add_bry(String key, String val) {return (Btrie_slim_mgr)Add_obj(Bry_.new_u8(key), Bry_.new_u8(val));}
|
||||
public Btrie_slim_mgr Add_bry(String key, byte[] val) {return (Btrie_slim_mgr)Add_obj(Bry_.new_u8(key), val);}
|
||||
public Btrie_slim_mgr Add_bry(byte[] v) {return (Btrie_slim_mgr)Add_obj(v, v);}
|
||||
public Btrie_slim_mgr Add_bry_bval(byte b, byte val) {return (Btrie_slim_mgr)Add_obj(new byte[] {b}, Byte_obj_val.new_(val));}
|
||||
public Btrie_slim_mgr Add_bry_bval(byte[] bry, byte val) {return (Btrie_slim_mgr)Add_obj(bry, Byte_obj_val.new_(val));}
|
||||
public Btrie_slim_mgr Add_bry_byte(byte b, byte val) {return (Btrie_slim_mgr)Add_obj(new byte[] {b}, Byte_obj_val.new_(val));}
|
||||
public Btrie_slim_mgr Add_bry_byte(byte[] bry, byte val) {return (Btrie_slim_mgr)Add_obj(bry, Byte_obj_val.new_(val));}
|
||||
public Btrie_slim_mgr Add_str_byte__many(byte val, String... ary) {
|
||||
int ary_len = ary.length;
|
||||
Byte_obj_val bval = Byte_obj_val.new_(val);
|
||||
@@ -55,6 +55,13 @@ public class Btrie_slim_mgr implements Btrie_mgr {
|
||||
Add_obj(Bry_.new_u8(ary[i]), bval);
|
||||
return this;
|
||||
}
|
||||
public Btrie_slim_mgr Add_many_int(int val, String... ary) {
|
||||
int len = ary.length;
|
||||
Int_obj_val obj = Int_obj_val.new_(val);
|
||||
for (int i = 0; i < len; i++)
|
||||
Add_obj(Bry_.new_u8(ary[i]), obj);
|
||||
return this;
|
||||
}
|
||||
public Btrie_slim_mgr Add_stub(String key, byte val) {byte[] bry = Bry_.new_u8(key); return (Btrie_slim_mgr)Add_obj(bry, new Btrie_itm_stub(val, bry));}
|
||||
public Btrie_slim_mgr Add_stubs(byte[][] ary) {return Add_stubs(ary, ary.length);}
|
||||
public Btrie_slim_mgr Add_stubs(byte[][] ary, int ary_len) {
|
||||
@@ -121,8 +128,8 @@ public class Btrie_slim_mgr implements Btrie_mgr {
|
||||
return dirty ? tmp_bfr.Xto_bry_and_clear() : src;
|
||||
}
|
||||
public void Clear() {root.Clear(); count = 0;}
|
||||
public static Btrie_slim_mgr cs_() {return new Btrie_slim_mgr(true);}
|
||||
public static Btrie_slim_mgr ci_ascii_() {return new Btrie_slim_mgr(false);}
|
||||
public static Btrie_slim_mgr ci_utf_8_() {return new Btrie_slim_mgr(false);}
|
||||
public static Btrie_slim_mgr cs() {return new Btrie_slim_mgr(Bool_.Y);}
|
||||
public static Btrie_slim_mgr ci_a7() {return new Btrie_slim_mgr(Bool_.N);}
|
||||
public static Btrie_slim_mgr ci_u8() {return new Btrie_slim_mgr(Bool_.N);}
|
||||
public static Btrie_slim_mgr new_(boolean v) {return new Btrie_slim_mgr(v);}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public class Btrie_slim_mgr_tst {
|
||||
@Before public void init() {
|
||||
} private Btrie_slim_mgr trie;
|
||||
private void ini_setup1() {
|
||||
trie = Btrie_slim_mgr.cs_();
|
||||
trie = Btrie_slim_mgr.cs();
|
||||
run_Add("a" , 1);
|
||||
run_Add("abc" , 123);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class Btrie_slim_mgr_tst {
|
||||
tst_MatchAtCurExact("abc", 123);
|
||||
}
|
||||
private void ini_setup2() {
|
||||
trie = Btrie_slim_mgr.cs_();
|
||||
trie = Btrie_slim_mgr.cs();
|
||||
run_Add("a" , 1);
|
||||
run_Add("b" , 2);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public class Btrie_slim_mgr_tst {
|
||||
tst_MatchAtCur("b", 2);
|
||||
}
|
||||
private void ini_setup_caseAny() {
|
||||
trie = Btrie_slim_mgr.ci_ascii_(); // NOTE:ci.ascii:test
|
||||
trie = Btrie_slim_mgr.ci_a7(); // NOTE:ci.ascii:test
|
||||
run_Add("a" , 1);
|
||||
run_Add("b" , 2);
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.btries; import gplx.*; import gplx.core.*;
|
||||
import gplx.intl.*;
|
||||
class Btrie_utf8_itm {
|
||||
class Btrie_u8_itm {
|
||||
private Hash_adp_bry nxts;
|
||||
private byte[] asymmetric_bry;
|
||||
public Btrie_utf8_itm(byte[] key, Object val) {this.key = key; this.val = val;}
|
||||
public Btrie_u8_itm(byte[] key, Object val) {this.key = key; this.val = val;}
|
||||
public byte[] Key() {return key;} private byte[] key;
|
||||
public Object Val() {return val;} public void Val_set(Object val) {this.val = val;} private Object val;
|
||||
public boolean Nxts_is_empty() {return nxts == null;}
|
||||
@@ -29,11 +29,11 @@ class Btrie_utf8_itm {
|
||||
nxts.Clear();
|
||||
nxts = null;
|
||||
}
|
||||
public Btrie_utf8_itm Nxts_find(byte[] src, int c_bgn, int c_end, boolean called_by_match) {
|
||||
public Btrie_u8_itm Nxts_find(byte[] src, int c_bgn, int c_end, boolean called_by_match) {
|
||||
if (nxts == null) return null;
|
||||
Object rv_obj = nxts.Get_by_mid(src, c_bgn, c_end);
|
||||
if (rv_obj == null) return null;
|
||||
Btrie_utf8_itm rv = (Btrie_utf8_itm)rv_obj;
|
||||
Btrie_u8_itm rv = (Btrie_u8_itm)rv_obj;
|
||||
byte[] asymmetric_bry = rv.asymmetric_bry;
|
||||
if (asymmetric_bry == null) // itm doesn't have asymmetric_bry; note that this is the case for most items
|
||||
return rv;
|
||||
@@ -51,9 +51,9 @@ class Btrie_utf8_itm {
|
||||
}
|
||||
}
|
||||
}
|
||||
public Btrie_utf8_itm Nxts_add(Gfo_case_mgr case_mgr, byte[] key, Object val) {
|
||||
Btrie_utf8_itm rv = new Btrie_utf8_itm(key, val);
|
||||
if (nxts == null) nxts = Hash_adp_bry.ci_utf8_(case_mgr);
|
||||
public Btrie_u8_itm Nxts_add(Gfo_case_mgr case_mgr, byte[] key, Object val) {
|
||||
Btrie_u8_itm rv = new Btrie_u8_itm(key, val);
|
||||
if (nxts == null) nxts = Hash_adp_bry.ci_u8(case_mgr);
|
||||
nxts.Add_bry_obj(key, rv);
|
||||
Gfo_case_itm case_itm = case_mgr.Get_or_null(key[0], key, 0, key.length); // get case_item
|
||||
if (case_itm != null) { // note that case_itm may be null; EX: "__TOC__" and "_"
|
||||
@@ -17,22 +17,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.btries; import gplx.*; import gplx.core.*;
|
||||
import gplx.intl.*;
|
||||
public class Btrie_utf8_mgr implements Btrie_mgr {
|
||||
private Btrie_utf8_itm root; private Gfo_case_mgr case_mgr;
|
||||
Btrie_utf8_mgr(Gfo_case_mgr case_mgr) {
|
||||
public class Btrie_u8_mgr implements Btrie_mgr {
|
||||
private Btrie_u8_itm root; private Gfo_case_mgr case_mgr;
|
||||
Btrie_u8_mgr(Gfo_case_mgr case_mgr) {
|
||||
this.case_mgr = case_mgr;
|
||||
this.root = new Btrie_utf8_itm(Bry_.Empty, null);
|
||||
this.root = new Btrie_u8_itm(Bry_.Empty, null);
|
||||
}
|
||||
public int Count() {return count;} private int count;
|
||||
public int Match_pos() {return match_pos;} private int match_pos;
|
||||
public Object Match_bgn(byte[] src, int bgn_pos, int end_pos) {return Match_bgn_w_byte(src[bgn_pos], src, bgn_pos, end_pos);}
|
||||
public Object Match_bgn_w_byte(byte b, byte[] src, int bgn_pos, int end_pos) {
|
||||
Object rv = null; int cur_pos = match_pos = bgn_pos;
|
||||
Btrie_utf8_itm cur = root;
|
||||
Btrie_u8_itm cur = root;
|
||||
while (true) {
|
||||
int c_len = Utf8_.Len_of_char_by_1st_byte(b);
|
||||
int c_end = cur_pos + c_len;
|
||||
Btrie_utf8_itm nxt = cur.Nxts_find(src, cur_pos, c_end, true); if (nxt == null) return rv; // nxts does not have key; return rv;
|
||||
Btrie_u8_itm nxt = cur.Nxts_find(src, cur_pos, c_end, true); if (nxt == null) return rv; // nxts does not have key; return rv;
|
||||
cur_pos = c_end;
|
||||
if (nxt.Nxts_is_empty()) {match_pos = cur_pos; return nxt.Val();} // nxt is leaf; return nxt.Val() (which should be non-null)
|
||||
Object nxt_val = nxt.Val();
|
||||
@@ -47,13 +47,13 @@ public class Btrie_utf8_mgr implements Btrie_mgr {
|
||||
public Btrie_mgr Add_obj(byte[] key, Object val) {
|
||||
if (val == null) throw Err_.new_wo_type("null objects cannot be registered", "key", String_.new_u8(key));
|
||||
int key_len = key.length;
|
||||
Btrie_utf8_itm cur = root;
|
||||
Btrie_u8_itm cur = root;
|
||||
int c_bgn = 0;
|
||||
while (c_bgn < key_len) {
|
||||
byte c = key[c_bgn];
|
||||
int c_len = Utf8_.Len_of_char_by_1st_byte(c);
|
||||
int c_end = c_bgn + c_len;
|
||||
Btrie_utf8_itm nxt = cur.Nxts_find(key, c_bgn, c_end, false);
|
||||
Btrie_u8_itm nxt = cur.Nxts_find(key, c_bgn, c_end, false);
|
||||
if (nxt == null)
|
||||
nxt = cur.Nxts_add(case_mgr, Bry_.Mid(key, c_bgn, c_end), null);
|
||||
c_bgn = c_end;
|
||||
@@ -64,5 +64,5 @@ public class Btrie_utf8_mgr implements Btrie_mgr {
|
||||
++count;
|
||||
return this;
|
||||
}
|
||||
public static Btrie_utf8_mgr new_(Gfo_case_mgr case_mgr) {return new Btrie_utf8_mgr(case_mgr);}
|
||||
public static Btrie_u8_mgr new_(Gfo_case_mgr case_mgr) {return new Btrie_u8_mgr(case_mgr);}
|
||||
}
|
||||
@@ -177,31 +177,6 @@ public class Bry_ {
|
||||
for (int i = 0; i < src_len; i++)
|
||||
trg[i + trg_bgn] = src[i + src_bgn];
|
||||
}
|
||||
public static byte[][] XtoByteAryAry(String... strAry) {
|
||||
int strAryLen = strAry.length;
|
||||
byte[][] rv = new byte[strAryLen][];
|
||||
for (int i = 0; i < strAryLen; i++)
|
||||
rv[i] = Bry_.new_u8(strAry[i]);
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Xto_str_lower(byte[] src, int bgn, int end) {
|
||||
int len = end - bgn;
|
||||
byte[] rv = new byte[len];
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Ltr_A: case Byte_ascii.Ltr_B: case Byte_ascii.Ltr_C: case Byte_ascii.Ltr_D: case Byte_ascii.Ltr_E:
|
||||
case Byte_ascii.Ltr_F: case Byte_ascii.Ltr_G: case Byte_ascii.Ltr_H: case Byte_ascii.Ltr_I: case Byte_ascii.Ltr_J:
|
||||
case Byte_ascii.Ltr_K: case Byte_ascii.Ltr_L: case Byte_ascii.Ltr_M: case Byte_ascii.Ltr_N: case Byte_ascii.Ltr_O:
|
||||
case Byte_ascii.Ltr_P: case Byte_ascii.Ltr_Q: case Byte_ascii.Ltr_R: case Byte_ascii.Ltr_S: case Byte_ascii.Ltr_T:
|
||||
case Byte_ascii.Ltr_U: case Byte_ascii.Ltr_V: case Byte_ascii.Ltr_W: case Byte_ascii.Ltr_X: case Byte_ascii.Ltr_Y: case Byte_ascii.Ltr_Z:
|
||||
b += 32;
|
||||
break;
|
||||
}
|
||||
rv[i - bgn] = b;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Replace_one(byte[] src, byte[] find, byte[] repl) {
|
||||
int src_len = src.length;
|
||||
int findPos = Bry_finder.Find(src, find, 0, src_len, true); if (findPos == Bry_.NotFound) return src;
|
||||
@@ -325,7 +300,7 @@ public class Bry_ {
|
||||
}
|
||||
public static byte[] Mid_safe(byte[] src, int bgn, int end) {
|
||||
try {return Mid(src, bgn, end);}
|
||||
catch (Exception e) {Err_.Noop(e); return Bry_.Add_w_dlm(Byte_ascii.Space, Bry_.XbyInt(bgn), Bry_.XbyInt(end));}
|
||||
catch (Exception e) {Err_.Noop(e); return Bry_.Add_w_dlm(Byte_ascii.Space, Bry_.new_by_int(bgn), Bry_.new_by_int(end));}
|
||||
}
|
||||
public static byte[] Mid(byte[] src, int bgn) {return Mid(src, bgn, src.length);}
|
||||
public static byte[] Mid_or(byte[] src, int bgn, int end, byte[] or) {
|
||||
@@ -562,7 +537,7 @@ public class Bry_ {
|
||||
if (lhs[i] != rhs[i + rhs_bgn]) return false;
|
||||
return true;
|
||||
}
|
||||
public static boolean Eq_ci_ascii(byte[] lhs, byte[] rhs, int rhs_bgn, int rhs_end) {
|
||||
public static boolean Eq_ci_a7(byte[] lhs, byte[] rhs, int rhs_bgn, int rhs_end) {
|
||||
if (lhs == null && rhs == null) return true;
|
||||
else if (lhs == null || rhs == null) return false;
|
||||
int lhs_len = lhs.length;
|
||||
@@ -575,8 +550,22 @@ public class Bry_ {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static byte[] XtoStrBytesByInt(int val, int padLen) {return XtoStrBytesByInt(val, null, 0, padLen);}
|
||||
public static byte[] XtoStrBytesByInt(int val, byte[] ary, int aryPos, int padLen) {
|
||||
public static int To_int_by_a7(byte[] v) {
|
||||
int v_len = v.length;
|
||||
int mod = 8 * (v_len - 1);
|
||||
int rv = 0;
|
||||
for (int i = 0; i < v_len; i++) {
|
||||
rv |= v[i] << mod;
|
||||
mod -= 8;
|
||||
}
|
||||
return rv;
|
||||
// return ((0xFF & v[0]) << 24)
|
||||
// | ((0xFF & v[1]) << 16)
|
||||
// | ((0xFF & v[2]) << 8)
|
||||
// | (0xFF & v[3]);
|
||||
}
|
||||
public static byte[] To_a7_bry(int val, int pad_len) {return To_a7_bry(val, null, 0, pad_len);}
|
||||
public static byte[] To_a7_bry(int val, byte[] ary, int aryPos, int pad_len) {
|
||||
int neg = 0;
|
||||
if (val < 0) {
|
||||
val *= -1;
|
||||
@@ -584,47 +573,57 @@ public class Bry_ {
|
||||
}
|
||||
int digits = val == 0 ? 0 : Math_.Log10(val);
|
||||
digits += 1; // digits = log + 1; EX: Log(1-9) = 0, Log(10-99) = 1
|
||||
int aryLen = digits + neg, aryBgn = aryPos, pad = 0;
|
||||
if (aryLen < padLen) { // padding specified
|
||||
pad = padLen - aryLen;
|
||||
aryLen = padLen;
|
||||
int ary_len = digits + neg, aryBgn = aryPos, pad = 0;
|
||||
if (ary_len < pad_len) { // padding specified
|
||||
pad = pad_len - ary_len;
|
||||
ary_len = pad_len;
|
||||
}
|
||||
if (ary == null) ary = new byte[aryLen];
|
||||
if (ary == null) ary = new byte[ary_len];
|
||||
long factor = 1; // factor needs to be long to handle 1 billion (for which factor would be 10 billion)
|
||||
for (int i = 0; i < digits; i++) // calc maxFactor
|
||||
factor *= 10;
|
||||
if (neg == 1) ary[0] = Byte_NegSign;
|
||||
|
||||
for (int i = 0; i < pad; i++) // fill ary with pad
|
||||
ary[i + aryBgn] = XtoStrByte(0);
|
||||
ary[i + aryBgn] = Byte_ascii.To_a7_byte(0);
|
||||
aryBgn += pad; // advance aryBgn by pad
|
||||
for (int i = neg; i < aryLen - pad; i++) {
|
||||
for (int i = neg; i < ary_len - pad; i++) {
|
||||
int denominator = (int)(factor / 10); // cache denominator to check for divide by 0
|
||||
int digit = denominator == 0 ? 0 : (int)((val % factor) / denominator);
|
||||
ary[aryBgn + i] = XtoStrByte(digit);
|
||||
ary[aryBgn + i] = Byte_ascii.To_a7_byte(digit);
|
||||
factor /= 10;
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
public static byte Xto_byte_by_int(byte[] ary, int bgn, int end, byte or) {return (byte)Xto_int_or(ary, bgn, end, or);}
|
||||
public static int Xto_int(byte[] ary) {return Xto_int_or(ary, null, 0, ary.length, -1);}
|
||||
public static int Xto_int_or_fail(byte[] ary) {
|
||||
int rv = Xto_int_or(ary, null, 0, ary.length, Int_.MinValue);
|
||||
if (rv == Int_.MinValue) throw Err_.new_wo_type("could not parse to int", "val", String_.new_u8(ary));
|
||||
return rv;
|
||||
public static byte[] new_by_int(int v) {
|
||||
byte b0 = (byte)(v >> 24);
|
||||
byte b1 = (byte)(v >> 16);
|
||||
byte b2 = (byte)(v >> 8);
|
||||
byte b3 = (byte)(v);
|
||||
if (b0 != 0) return new byte[] {b0, b1, b2, b3};
|
||||
else if (b1 != 0) return new byte[] {b1, b2, b3};
|
||||
else if (b2 != 0) return new byte[] {b2, b3};
|
||||
else return new byte[] {b3};
|
||||
}
|
||||
public static boolean Xto_bool_by_int_or_fail(byte[] ary) {
|
||||
int rv = Xto_int_or(ary, null, 0, ary.length, Int_.MinValue);
|
||||
public static boolean To_bool_by_int(byte[] ary) {
|
||||
int rv = To_int_or(ary, null, 0, ary.length, Int_.MinValue);
|
||||
switch (rv) {
|
||||
case 0: return false;
|
||||
case 1: return true;
|
||||
default: throw Err_.new_wo_type("could not parse to boolean int", "val", String_.new_u8(ary));
|
||||
}
|
||||
}
|
||||
public static int Xto_int_or(byte[] ary, int or) {return Xto_int_or(ary, null, 0, ary.length, or);}
|
||||
public static int Xto_int_or(byte[] ary, int bgn, int end, int or) {return Xto_int_or(ary, null, bgn, end, or);}
|
||||
public static int Xto_int_or(byte[] ary, byte[] ignore_ary, int or) {return Xto_int_or(ary, ignore_ary, 0, ary.length, or);}
|
||||
public static int Xto_int_or(byte[] ary, byte[] ignore_ary, int bgn, int end, int or) {
|
||||
public static byte To_int_as_byte(byte[] ary, int bgn, int end, byte or) {return (byte)To_int_or(ary, bgn, end, or);}
|
||||
public static int To_int(byte[] ary) {
|
||||
int rv = To_int_or(ary, null, 0, ary.length, Int_.MinValue);
|
||||
if (rv == Int_.MinValue) throw Err_.new_wo_type("could not parse to int", "val", String_.new_u8(ary));
|
||||
return rv;
|
||||
}
|
||||
public static int To_int_or_neg1(byte[] ary) {return To_int_or(ary, null, 0, ary.length, -1);}
|
||||
public static int To_int_or(byte[] ary, int or) {return To_int_or(ary, null, 0, ary.length, or);}
|
||||
public static int To_int_or(byte[] ary, int bgn, int end, int or) {return To_int_or(ary, null, bgn, end, or);}
|
||||
public static int To_int_or(byte[] ary, byte[] ignore_ary, int or) {return To_int_or(ary, ignore_ary, 0, ary.length, or);}
|
||||
public static int To_int_or(byte[] ary, byte[] ignore_ary, int bgn, int end, int or) {
|
||||
if ( ary == null
|
||||
|| end == bgn // null-len
|
||||
) return or;
|
||||
@@ -658,7 +657,42 @@ public class Bry_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int Xto_int_or_trim(byte[] ary, int bgn, int end, int or) { // NOTE: same as Xto_int_or, except trims ws at bgn / end; DATE:2014-02-09
|
||||
public static long To_long_or(byte[] ary, long or) {return To_long_or(ary, null, 0, ary.length, or);}
|
||||
public static long To_long_or(byte[] ary, byte[] ignore_ary, int bgn, int end, long or) {
|
||||
if ( ary == null
|
||||
|| end == bgn // null-len
|
||||
) return or;
|
||||
long rv = 0, multiple = 1;
|
||||
for (int i = end - 1; i >= bgn; i--) { // -1 b/c end will always be next char; EX: {{{1}}}; bgn = 3, end = 4
|
||||
byte b = ary[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
rv += multiple * (b - Byte_ascii.Num_0);
|
||||
multiple *= 10;
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
return i == bgn ? rv * -1 : or;
|
||||
case Byte_ascii.Plus:
|
||||
return i == bgn ? rv : or;
|
||||
default:
|
||||
boolean invalid = true;
|
||||
if (ignore_ary != null) {
|
||||
int ignore_ary_len = ignore_ary.length;
|
||||
for (int j = 0; j < ignore_ary_len; j++) {
|
||||
if (b == ignore_ary[j]) {
|
||||
invalid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (invalid) return or;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int To_int_or__trim_ws(byte[] ary, int bgn, int end, int or) { // NOTE: same as To_int_or, except trims ws at bgn / end; DATE:2014-02-09
|
||||
if (end == bgn) return or; // null len
|
||||
int rv = 0, multiple = 1;
|
||||
boolean numbers_seen = false, ws_seen = false;
|
||||
@@ -684,7 +718,7 @@ public class Bry_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int Xto_int_or_lax(byte[] ary, int bgn, int end, int or) {
|
||||
public static int To_int_or__lax(byte[] ary, int bgn, int end, int or) {
|
||||
if (end == bgn) return or; // null-len
|
||||
int end_num = end;
|
||||
for (int i = bgn; i < end; i++) {
|
||||
@@ -705,14 +739,12 @@ public class Bry_ {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Xto_int_or(ary, bgn, end_num, or);
|
||||
return To_int_or(ary, bgn, end_num, or);
|
||||
}
|
||||
public static float XtoFloatByPos(byte[] ary, int bgn, int end) {return Float_.parse_(String_.new_u8(ary, bgn, end));}
|
||||
public static double Xto_double(byte[] bry) {return Double_.parse_(String_.new_u8(bry, 0, bry.length));}
|
||||
public static double Xto_double_or(byte[] bry, double or) {return Double_.parse_or(String_.new_u8(bry, 0, bry.length), or);}
|
||||
public static double XtoDoubleByPosOr(byte[] ary, int bgn, int end, double or) {return Double_.parse_or(String_.new_u8(ary, bgn, end), or);}
|
||||
public static double XtoDoubleByPos(byte[] ary, int bgn, int end) {return Double_.parse_(String_.new_u8(ary, bgn, end));}
|
||||
public static DecimalAdp XtoDecimalByPos(byte[] ary, int bgn, int end) {return DecimalAdp_.parse_(String_.new_u8(ary, bgn, end));}
|
||||
public static double To_double(byte[] ary, int bgn, int end) {return Double_.parse_(String_.new_u8(ary, bgn, end));}
|
||||
public static double To_double_or(byte[] bry, double or) {return Double_.parse_or(String_.new_u8(bry, 0, bry.length), or);}
|
||||
public static double To_double_or(byte[] ary, int bgn, int end, double or) {return Double_.parse_or(String_.new_u8(ary, bgn, end), or);}
|
||||
public static Decimal_adp To_decimal(byte[] ary, int bgn, int end) {return Decimal_adp_.parse_(String_.new_u8(ary, bgn, end));}
|
||||
public static final byte Dlm_fld = (byte)'|', Dlm_row = (byte)'\n', Dlm_quote = (byte)'"', Dlm_null = 0, Ascii_zero = 48;
|
||||
public static final String Fmt_csvDte = "yyyyMMdd HHmmss.fff";
|
||||
public static DateAdp ReadCsvDte(byte[] ary, Int_obj_ref posRef, byte lkp) {// ASSUME: fmt = yyyyMMdd HHmmss.fff
|
||||
@@ -783,7 +815,7 @@ public class Bry_ {
|
||||
int bgn = posRef.Val();
|
||||
int pos = Bry_finder.Find_fwd(ary, lkp, bgn, ary.length);
|
||||
if (pos == Bry_.NotFound) throw Err_.new_wo_type("lkp failed", "lkp", (char)lkp, "bgn", bgn);
|
||||
int rv = Bry_.Xto_int_or(ary, posRef.Val(), pos, -1);
|
||||
int rv = Bry_.To_int_or(ary, posRef.Val(), pos, -1);
|
||||
posRef.Val_(pos + 1); // +1 = lkp.Len
|
||||
return rv;
|
||||
}
|
||||
@@ -791,7 +823,7 @@ public class Bry_ {
|
||||
int bgn = posRef.Val();
|
||||
int pos = Bry_finder.Find_fwd(ary, lkp, bgn, ary.length);
|
||||
if (pos == Bry_.NotFound) throw Err_.new_wo_type("lkp failed", "lkp", (char)lkp, "bgn", bgn);
|
||||
double rv = Bry_.XtoDoubleByPos(ary, posRef.Val(), pos);
|
||||
double rv = Bry_.To_double(ary, posRef.Val(), pos);
|
||||
posRef.Val_(pos + 1); // +1 = lkp.Len
|
||||
return rv;
|
||||
}
|
||||
@@ -801,37 +833,6 @@ public class Bry_ {
|
||||
posRef.Val_(pos + 1); // +1 = lkp.Len
|
||||
}
|
||||
public static byte Byte_NegSign = (byte)'-';
|
||||
public static int XtoIntBy4Bytes(byte[] v) {
|
||||
int v_len = v.length;
|
||||
int mod = 8 * (v_len - 1);
|
||||
int rv = 0;
|
||||
for (int i = 0; i < v_len; i++) {
|
||||
rv |= v[i] << mod;
|
||||
mod -= 8;
|
||||
}
|
||||
return rv;
|
||||
// return ((0xFF & v[0]) << 24)
|
||||
// | ((0xFF & v[1]) << 16)
|
||||
// | ((0xFF & v[2]) << 8)
|
||||
// | (0xFF & v[3]);
|
||||
}
|
||||
public static byte[] XbyInt(int v) {
|
||||
byte b0 = (byte)(v >> 24);
|
||||
byte b1 = (byte)(v >> 16);
|
||||
byte b2 = (byte)(v >> 8);
|
||||
byte b3 = (byte)(v);
|
||||
if (b0 != 0) return new byte[] {b0, b1, b2, b3};
|
||||
else if (b1 != 0) return new byte[] {b1, b2, b3};
|
||||
else if (b2 != 0) return new byte[] {b2, b3};
|
||||
else return new byte[] {b3};
|
||||
}
|
||||
public static byte XtoStrByte(int digit) {
|
||||
switch (digit) {
|
||||
case 0: return Byte_ascii.Num_0; case 1: return Byte_ascii.Num_1; case 2: return Byte_ascii.Num_2; case 3: return Byte_ascii.Num_3; case 4: return Byte_ascii.Num_4;
|
||||
case 5: return Byte_ascii.Num_5; case 6: return Byte_ascii.Num_6; case 7: return Byte_ascii.Num_7; case 8: return Byte_ascii.Num_8; case 9: return Byte_ascii.Num_9;
|
||||
default: throw Err_.new_wo_type("unknown digit", "digit", digit);
|
||||
}
|
||||
}
|
||||
public static byte[][] Split(byte[] src, byte dlm) {return Split(src, dlm, false);}
|
||||
public static byte[][] Split(byte[] src, byte dlm, boolean trim) {
|
||||
if (Bry_.Len_eq_0(src)) return Bry_.Ary_empty;
|
||||
@@ -993,41 +994,43 @@ public class Bry_ {
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
public static byte[] Upper_1st(byte[] ary) {
|
||||
if (ary == null) return null;
|
||||
int len = ary.length;
|
||||
if (len == 0) return ary;
|
||||
byte b = ary[0];
|
||||
if (b > 96 && b < 123)
|
||||
ary[0] = (byte)(b - 32);
|
||||
return ary;
|
||||
}
|
||||
public static byte[] Upper_ascii(byte[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
byte b = ary[i];
|
||||
if (b > 96 && b < 123)
|
||||
ary[i] = (byte)(b - 32);
|
||||
public static byte[] Ucase__all(byte[] src) {return Xcase__all(Bool_.Y, src, 0, -1);}
|
||||
public static byte[] Lcase__all(byte[] src) {return Xcase__all(Bool_.N, src, 0, -1);}
|
||||
public static byte[] Lcase__all(byte[] src, int bgn, int end) {return Xcase__all(Bool_.N, src, bgn, end);}
|
||||
private static byte[] Xcase__all(boolean upper, byte[] src, int bgn, int end) {
|
||||
if (src == null) return null;
|
||||
int len = end == -1 ? src.length : end - bgn; if (len == 0) return src;
|
||||
byte[] rv = new byte[len];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte b = src[i + bgn];
|
||||
if (upper) {
|
||||
if (b > 96 && b < 123) b -= 32;
|
||||
}
|
||||
else {
|
||||
if (b > 64 && b < 91) b += 32;
|
||||
}
|
||||
rv[i] = b;
|
||||
}
|
||||
return ary;
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Lower_1st(byte[] ary) {
|
||||
if (ary == null) return null;
|
||||
int len = ary.length;
|
||||
if (len == 0) return ary;
|
||||
byte b = ary[0];
|
||||
if (b > 64 && b < 91)
|
||||
ary[0] = (byte)(b + 32);
|
||||
return ary;
|
||||
}
|
||||
public static byte[] Lower_ascii(byte[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
byte b = ary[i];
|
||||
if (b > 64 && b < 91)
|
||||
ary[i] = (byte)(b + 32);
|
||||
public static byte[] Ucase__1st(byte[] src) {return Xcase__1st(Bool_.Y, src);}
|
||||
public static byte[] Lcase__1st(byte[] src) {return Xcase__1st(Bool_.N, src);}
|
||||
private static byte[] Xcase__1st(boolean upper, byte[] src) {
|
||||
if (src == null) return null;
|
||||
int len = src.length; if (len == 0) return src;
|
||||
byte[] rv = new byte[len];
|
||||
byte b = src[0];
|
||||
if (upper) {
|
||||
if (b > 96 && b < 123) b -= 32;
|
||||
}
|
||||
return ary;
|
||||
else {
|
||||
if (b > 64 && b < 91) b += 32;
|
||||
}
|
||||
rv[0] = b;
|
||||
for (int i = 1; i < len; ++i) {
|
||||
rv[i] = src[i];
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Null_if_empty(byte[] v) {return Len_eq_0(v) ? null : v;}
|
||||
public static byte Get_at_end(byte[] v) {
|
||||
|
||||
@@ -43,9 +43,9 @@ public class Bry__tst {
|
||||
byte[] expd = new byte[expdAryAsInt.length];
|
||||
for (int i = 0; i < expd.length; i++) {
|
||||
int expdInt = expdAryAsInt[i];
|
||||
expd[i] = expdInt == Bry_.Byte_NegSign ? Bry_.Byte_NegSign : Bry_.XtoStrByte(expdAryAsInt[i]);
|
||||
expd[i] = expdInt == Bry_.Byte_NegSign ? Bry_.Byte_NegSign : Byte_ascii.To_a7_byte(expdAryAsInt[i]);
|
||||
}
|
||||
Tfds.Eq_ary(expd, Bry_.XtoStrBytesByInt(val, Int_.DigitCount(val)));
|
||||
Tfds.Eq_ary(expd, Bry_.To_a7_bry(val, Int_.DigitCount(val)));
|
||||
}
|
||||
@Test public void Has_at_end() {
|
||||
tst_HasAtEnd("a|bcd|e", "d" , 2, 5, true); // y_basic
|
||||
@@ -109,8 +109,8 @@ public class Bry__tst {
|
||||
tst_XtoInt("", -1);
|
||||
}
|
||||
void tst_XtoInt(String val, int expd) {tst_XtoInt(val, -1, expd);}
|
||||
void tst_XtoInt(String val, int or, int expd) {Tfds.Eq(expd, Bry_.Xto_int_or(Bry_.new_u8(val), or));}
|
||||
void tst_XtoIntBy4Bytes(int expd, byte... ary) {Tfds.Eq(expd, Bry_.XtoIntBy4Bytes(ary), "XtoInt"); Tfds.Eq_ary(ary, Bry_.XbyInt(expd), "XbyInt");}
|
||||
void tst_XtoInt(String val, int or, int expd) {Tfds.Eq(expd, Bry_.To_int_or(Bry_.new_u8(val), or));}
|
||||
void tst_XtoIntBy4Bytes(int expd, byte... ary) {Tfds.Eq(expd, Bry_.To_int_by_a7(ary), "XtoInt"); Tfds.Eq_ary(ary, Bry_.new_by_int(expd), "XbyInt");}
|
||||
void tst_ReadCsvStr(String raw, String expd) {tst_ReadCsvStr(raw, Int_obj_ref.zero_() , expd);}
|
||||
void tst_ReadCsvStr(String raw, int bgn, String expd) {tst_ReadCsvStr(raw, Int_obj_ref.new_(bgn), expd);}
|
||||
void tst_ReadCsvStr(String raw, Int_obj_ref bgnRef, String expd) {
|
||||
@@ -148,14 +148,14 @@ public class Bry__tst {
|
||||
tst_Xto_int_lax("a", 0);
|
||||
tst_Xto_int_lax("-1", -1);
|
||||
}
|
||||
private void tst_Xto_int_lax(String val, int expd) {Tfds.Eq(expd, Bry_.Xto_int_or_lax(Bry_.new_u8(val), 0, String_.Len(val), 0));}
|
||||
@Test public void Xto_int_or_trim() {
|
||||
private void tst_Xto_int_lax(String val, int expd) {Tfds.Eq(expd, Bry_.To_int_or__lax(Bry_.new_u8(val), 0, String_.Len(val), 0));}
|
||||
@Test public void To_int_or__trim_ws() {
|
||||
tst_Xto_int_trim("123 " , 123);
|
||||
tst_Xto_int_trim(" 123" , 123);
|
||||
tst_Xto_int_trim(" 123 " , 123);
|
||||
tst_Xto_int_trim(" 1 3 " , -1);
|
||||
}
|
||||
private void tst_Xto_int_trim(String val, int expd) {Tfds.Eq(expd, Bry_.Xto_int_or_trim(Bry_.new_u8(val), 0, String_.Len(val), -1));}
|
||||
private void tst_Xto_int_trim(String val, int expd) {Tfds.Eq(expd, Bry_.To_int_or__trim_ws(Bry_.new_u8(val), 0, String_.Len(val), -1));}
|
||||
@Test public void Compare() {
|
||||
tst_Compare("abcde", 0, 1, "abcde", 0, 1, CompareAble_.Same);
|
||||
tst_Compare("abcde", 0, 1, "abcde", 1, 2, CompareAble_.Less);
|
||||
|
||||
@@ -76,6 +76,13 @@ public class Byte_ascii {
|
||||
return b > Byte_ascii.Slash && b < Byte_ascii.Colon;
|
||||
}
|
||||
public static int Xto_digit(byte b) {return b - Byte_ascii.Num_0;}
|
||||
public static byte To_a7_byte(int digit) {
|
||||
switch (digit) {
|
||||
case 0: return Byte_ascii.Num_0; case 1: return Byte_ascii.Num_1; case 2: return Byte_ascii.Num_2; case 3: return Byte_ascii.Num_3; case 4: return Byte_ascii.Num_4;
|
||||
case 5: return Byte_ascii.Num_5; case 6: return Byte_ascii.Num_6; case 7: return Byte_ascii.Num_7; case 8: return Byte_ascii.Num_8; case 9: return Byte_ascii.Num_9;
|
||||
default: throw Err_.new_("Byte_ascii", "unknown digit", "digit", digit);
|
||||
}
|
||||
}
|
||||
public static byte Case_upper(byte b) {
|
||||
return b > 96 && b < 123
|
||||
? (byte)(b - 32)
|
||||
@@ -91,6 +98,8 @@ public class Byte_ascii {
|
||||
Tab_bry = new byte[] {Byte_ascii.Tab}
|
||||
, Nl_bry = new byte[] {Byte_ascii.Nl}
|
||||
, Bang_bry = new byte[] {Byte_ascii.Bang}
|
||||
, Quote_bry = new byte[] {Byte_ascii.Quote}
|
||||
, Hash_bry = new byte[] {Byte_ascii.Hash}
|
||||
, Dot_bry = new byte[] {Byte_ascii.Dot}
|
||||
, Comma_bry = new byte[] {Byte_ascii.Comma}
|
||||
, Colon_bry = new byte[] {Byte_ascii.Colon}
|
||||
@@ -100,7 +109,6 @@ public class Byte_ascii {
|
||||
, Brack_bgn_bry = new byte[] {Byte_ascii.Brack_bgn}
|
||||
, Brack_end_bry = new byte[] {Byte_ascii.Brack_end}
|
||||
, Apos_bry = new byte[] {Byte_ascii.Apos}
|
||||
, Quote_bry = new byte[] {Byte_ascii.Quote}
|
||||
, Pipe_bry = new byte[] {Byte_ascii.Pipe}
|
||||
, Underline_bry = new byte[] {Byte_ascii.Underline}
|
||||
, Slash_bry = new byte[] {Byte_ascii.Slash}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class Int_ary_ {
|
||||
|| pos_is_last
|
||||
) {
|
||||
if (num_bgn == -1) return or; // empty itm; EX: "1,"; "1,,2"
|
||||
int num = Bry_.Xto_int_or(src, num_bgn, num_end, Int_.MinValue);
|
||||
int num = Bry_.To_int_or(src, num_bgn, num_end, Int_.MinValue);
|
||||
if (num == Int_.MinValue) return or; // not a number; parse failed
|
||||
if (rv_len == 0) { // rv not init'd
|
||||
rv_len = (raw_len / 2) + 1; // default rv_len to len of String / 2; + 1 to avoid fraction rounding down
|
||||
@@ -77,7 +77,7 @@ public class Int_ary_ {
|
||||
case Byte_ascii.Dash:
|
||||
if (pos == raw_len -1) return or; // eos; EX: "1-"
|
||||
if (num_bgn == -1) return or; // no rng_bgn; EX: "-2"
|
||||
rng_bgn = Bry_.Xto_int_or(src, num_bgn, pos, Int_.MinValue);
|
||||
rng_bgn = Bry_.To_int_or(src, num_bgn, pos, Int_.MinValue);
|
||||
if (rng_bgn == Int_.MinValue) return or;
|
||||
num_bgn = -1;
|
||||
itm_is_rng = true;
|
||||
|
||||
@@ -30,17 +30,17 @@ public class Object_ {
|
||||
public static String Xto_str_strict_or_empty(Object v) {return v == null ? String_.Empty : ToString_lang(v);}
|
||||
private static String ToString_lang(Object v) {
|
||||
Class<?> c = v.getClass();
|
||||
if (ClassAdp_.Eq(c, String_.Cls_ref_type)) return (String)v;
|
||||
if (ClassAdp_.Eq(c, String_.Cls_ref_type)) return (String)v;
|
||||
else if (ClassAdp_.Eq(c, Bry_.Cls_ref_type)) return String_.new_u8((byte[])v);
|
||||
else return v.toString();
|
||||
else return v.toString();
|
||||
}
|
||||
public static String Xto_str_loose_or(Object v, String or) { // tries to pretty-print doubles; also standardizes true/false; DATE:2014-07-14
|
||||
if (v == null) return null;
|
||||
Class<?> c = ClassAdp_.ClassOf_obj(v);
|
||||
if (ClassAdp_.Eq(c, String_.Cls_ref_type)) return (String)v;
|
||||
else if (ClassAdp_.Eq(c, Bry_.Cls_ref_type)) return String_.new_u8((byte[])v);
|
||||
else if (ClassAdp_.Eq(c, Bry_.Cls_ref_type)) return String_.new_u8((byte[])v);
|
||||
else if (ClassAdp_.Eq(c, Bool_.Cls_ref_type)) return Bool_.cast_(v) ? Bool_.True_str : Bool_.False_str; // always return "true" / "false"
|
||||
else if (ClassAdp_.Eq(c, Double_.Cls_ref_type)) return Double_.Xto_str_loose(Double_.cast_(v));
|
||||
else return v.toString();
|
||||
else return v.toString();
|
||||
}
|
||||
}
|
||||
@@ -534,4 +534,15 @@ public class String_ implements GfoInvkAble {
|
||||
}
|
||||
return trg_ary;
|
||||
}
|
||||
public static String To_str__as_kv_ary(String... ary) {
|
||||
int len = ary.length;
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
for (int i = 0; i < len; i+=2) {
|
||||
bfr.Add_str_u8(ary[i]).Add_byte_eq();
|
||||
String val = i + 1 < len ? ary[i + 1] : null;
|
||||
if (val != null) bfr.Add_str_u8(val);
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
return bfr.Xto_str_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
public class DecimalAdp implements CompareAble {
|
||||
public int compareTo(Object obj) {DecimalAdp comp = (DecimalAdp)obj; return under.compareTo(comp.under);}
|
||||
|
||||
protected DecimalAdp(BigDecimal v) {this.under = v;} BigDecimal under;
|
||||
protected DecimalAdp(int v) {this.under = new BigDecimal(v);}
|
||||
public String Xto_str() {
|
||||
BigDecimal tmp = under;
|
||||
int tmp_scale = tmp.scale();
|
||||
if (tmp_scale <= -14) return tmp.toString(); // NOTE: if large number, call .toString which will return exponential notaion (1E##) instead of literal (1000....); 14 matches MW code; DATE:2015-04-10
|
||||
if (tmp_scale > 14)
|
||||
tmp = tmp.setScale(14, RoundingMode.DOWN); // NOTE: if small number, round down to remove excessive zeroes; 14 matches PHP/C# values more closely; RoundingMode.Down for same reason; see E, Pi tests
|
||||
return tmp .stripTrailingZeros() // NOTE: stripTrailingZeros for exp tests; EX: 120.0 -> 120; 0.01200000000000 -> .012
|
||||
.toPlainString(); // NOTE: toPlainString b/c stripTrailingZeros now converts 120 to 1.2E+2 (and any other value that is a multiple of 10)
|
||||
}
|
||||
public String Xto_str(String fmt) {return new DecimalFormat(fmt).format(under);}
|
||||
@Override public String toString() {return under.toString();}
|
||||
public boolean Eq(DecimalAdp v) {return v.under.doubleValue() == under.doubleValue();}
|
||||
public BigDecimal Xto_decimal() {return under;}
|
||||
public long Xto_long_mult_1000() {return under.movePointRight(3).longValue();}
|
||||
public int Fraction1000() {return (int)(under.movePointRight(3).floatValue() % 1000);}
|
||||
public double Xto_double() {return under.doubleValue();}
|
||||
public int Xto_int() {return (int)under.doubleValue();}
|
||||
public long Xto_long() {return (long)under.doubleValue();}
|
||||
public DecimalAdp Op_add(DecimalAdp v) {return new DecimalAdp(under.add(v.under, DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_subtract(DecimalAdp v) {return new DecimalAdp(under.subtract(v.under, DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_mult(DecimalAdp v) {return new DecimalAdp(under.multiply(v.under));}
|
||||
public DecimalAdp Op_mult(double v) {return new DecimalAdp(under.multiply(new BigDecimal(v, DecimalAdp_.Gplx_rounding_context)));}
|
||||
public DecimalAdp Op_mult(long v) {return new DecimalAdp(under.multiply(new BigDecimal(v)));}
|
||||
public DecimalAdp Op_divide(DecimalAdp v) {return new DecimalAdp(under.divide(v.under, DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_mod(DecimalAdp v) {return new DecimalAdp(under.remainder(v.under, DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_sqrt() {return new DecimalAdp(new BigDecimal(Math_.Sqrt(under.doubleValue())));}
|
||||
public DecimalAdp Op_abs() {return new DecimalAdp(under.abs(DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_pow(int v) {return new DecimalAdp(under.pow(v, DecimalAdp_.Gplx_rounding_context));}
|
||||
public DecimalAdp Op_truncate_decimal() {return new DecimalAdp(under.intValue());}
|
||||
public DecimalAdp Op_round(int v) {return new DecimalAdp(under.setScale(v, RoundingMode.HALF_UP));}
|
||||
public boolean Comp_gte(DecimalAdp v) {return under.doubleValue() >= v.under.doubleValue();}
|
||||
public boolean Comp_gte(int v) {return under.doubleValue() >= v;}
|
||||
public boolean Comp_lte(DecimalAdp v) {return under.doubleValue() <= v.under.doubleValue();}
|
||||
public boolean Comp_lte(int v) {return under.doubleValue() <= v;}
|
||||
public boolean Comp_gt(DecimalAdp v) {return under.doubleValue() > v.under.doubleValue();}
|
||||
public boolean Comp_gt(int v) {return under.doubleValue() > v;}
|
||||
public boolean Comp_lt(DecimalAdp v) {return under.doubleValue() < v.under.doubleValue();}
|
||||
public boolean Comp_lt(int v) {return under.doubleValue() < v;}
|
||||
public boolean Eq(int v) {return under.doubleValue() == v;}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
public class DecimalAdp_ {
|
||||
public static final Class<?> Cls_ref_type = DecimalAdp.class;
|
||||
public static DecimalAdp as_(Object obj) {return obj instanceof DecimalAdp ? (DecimalAdp)obj : null;}
|
||||
public static final DecimalAdp Zero = new DecimalAdp(0);
|
||||
public static final DecimalAdp One = new DecimalAdp(1);
|
||||
public static final DecimalAdp Neg1 = new DecimalAdp(-1);
|
||||
public static final DecimalAdp Const_e = DecimalAdp_.double_(Math_.E);
|
||||
public static final DecimalAdp Const_pi = DecimalAdp_.double_(Math_.Pi);
|
||||
public static DecimalAdp base1000_(long v) {return divide_(v, 1000);}
|
||||
public static DecimalAdp parts_1000_(long num, int frc) {return divide_((num * (1000)) + frc, 1000);}
|
||||
public static DecimalAdp parts_(long num, int frc) {
|
||||
// int log10 = frc == 0 ? 0 : (Math_.Log10(frc) + 1);
|
||||
// int pow10 = (int)Math_.Pow(10, log10);
|
||||
int pow10 = XtoPow10(frc);
|
||||
return divide_((num * (pow10)) + frc, pow10);
|
||||
}
|
||||
public static DecimalAdp cast_(Object obj) {return (DecimalAdp)obj;}
|
||||
static int XtoPow10(int v) {
|
||||
if (v > -1 && v < 10) return 10;
|
||||
else if (v > 9 && v < 100) return 100;
|
||||
else if (v > 99 && v < 1000) return 1000;
|
||||
else if (v > 999 && v < 10000) return 10000;
|
||||
else if (v > 9999 && v < 100000) return 100000;
|
||||
else if (v > 99999 && v < 1000000) return 1000000;
|
||||
else if (v > 999999 && v < 10000000) return 10000000;
|
||||
else if (v > 9999999 && v < 100000000) return 100000000;
|
||||
else if (v > 99999999 && v < 1000000000) return 1000000000;
|
||||
else throw Err_.new_wo_type("value must be between 0 and 1 billion", "v", v);
|
||||
}
|
||||
public static String CalcPctStr(long dividend, long divisor, String fmt) {
|
||||
if (divisor == 0) return "%ERR";
|
||||
return DecimalAdp_.float_(Float_.Div(dividend, divisor) * 100).Xto_str(fmt) + "%";
|
||||
}
|
||||
public static DecimalAdp divide_safe_(long lhs, long rhs) {return rhs == 0 ? Zero : divide_(lhs, rhs);}
|
||||
public static DecimalAdp divide_(long lhs, long rhs) {
|
||||
return new DecimalAdp(new BigDecimal(lhs).divide(new BigDecimal(rhs), Gplx_rounding_context));
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class DecimalAdp__tst {
|
||||
@Test public void divide_() {
|
||||
tst_divide_(1, 1000, "0.001");
|
||||
tst_divide_(1, 3, "0.33333333333333");
|
||||
tst_divide_(1, 7, "0.14285714285714");
|
||||
} void tst_divide_(int lhs, int rhs, String expd) {Tfds.Eq(expd, DecimalAdp_.divide_(lhs, rhs).Xto_str());}
|
||||
@Test public void base1000_() {
|
||||
tst_base1000_(1000, "1");
|
||||
tst_base1000_(1234, "1.234");
|
||||
tst_base1000_(123, "0.123");
|
||||
} void tst_base1000_(int val, String expd) {Tfds.Eq(expd, DecimalAdp_.base1000_(val).Xto_str());}
|
||||
@Test public void parts_() {
|
||||
tst_parts_(1, 0, "1");
|
||||
tst_parts_(1, 2, "1.2");
|
||||
tst_parts_(1, 23, "1.23");
|
||||
tst_parts_(123, 4567, "123.4567");
|
||||
} void tst_parts_(int num, int fracs, String expd) {Tfds.Eq(expd, DecimalAdp_.parts_(num, fracs).Xto_str());}
|
||||
@Test public void parse_() {
|
||||
tst_parse_("1", "1");
|
||||
tst_parse_("1.2", "1.2");
|
||||
tst_parse_("0.1", "0.1");
|
||||
} void tst_parse_(String raw, String expd) {Tfds.Eq(expd, DecimalAdp_.parse_(raw).Xto_str());}
|
||||
@Test public void Truncate_decimal() {
|
||||
tst_Truncate_decimal("1", "1");
|
||||
tst_Truncate_decimal("1.1", "1");
|
||||
tst_Truncate_decimal("1.9", "1");
|
||||
} void tst_Truncate_decimal(String raw, String expd) {Tfds.Eq(DecimalAdp_.parse_(expd).Xto_str(), DecimalAdp_.parse_(raw).Op_truncate_decimal().Xto_str());}
|
||||
@Test public void Fraction1000() {
|
||||
tst_Fraction1000(1, 1000, 1); // 0.001
|
||||
tst_Fraction1000(1, 3, 333); // 0.33333333
|
||||
tst_Fraction1000(1234, 1000, 234); // 1.234
|
||||
tst_Fraction1000(12345, 10000, 234); // 1.2345
|
||||
} void tst_Fraction1000(int lhs, int rhs, int expd) {Tfds.Eq(expd, DecimalAdp_.divide_(lhs, rhs).Fraction1000());}
|
||||
@Test public void Lt() {
|
||||
tst_Lt(1,123, 2, true);
|
||||
tst_Lt(1,99999999, 2, true);
|
||||
} void tst_Lt(int lhsNum, int lhsFrc, int rhs, boolean expd) {Tfds.Eq(expd, DecimalAdp_.parts_(lhsNum, lhsFrc).Comp_lt(rhs));}
|
||||
@Test public void XtoStr_fmt() {
|
||||
tst_XtoStr_fmt(1, 2, "0.0", "0.5");
|
||||
tst_XtoStr_fmt(1, 3, "0.0", "0.3");
|
||||
tst_XtoStr_fmt(10000, 7, "0,000.000", "1,428.571");
|
||||
} void tst_XtoStr_fmt(int l, int r, String fmt, String expd) {Tfds.Eq(expd, DecimalAdp_.divide_(l, r).Xto_str(fmt));}
|
||||
}
|
||||
91
100_core/src_120_basicDataType/gplx/Decimal_adp.java
Normal file
91
100_core/src_120_basicDataType/gplx/Decimal_adp.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
public class Decimal_adp implements CompareAble {
|
||||
public int compareTo(Object obj) {Decimal_adp comp = (Decimal_adp)obj; return under.compareTo(comp.under);}
|
||||
|
||||
protected Decimal_adp(BigDecimal v) {this.under = v;} private final BigDecimal under;
|
||||
protected Decimal_adp(int v) {this.under = new BigDecimal(v);}
|
||||
public Object Under() {return under;}
|
||||
public BigDecimal Under_as_native() {return under;}
|
||||
public int Precision() {return under.precision();}
|
||||
public int Frac_1000() {return (int)(under.movePointRight(3).floatValue() % 1000);}
|
||||
public boolean Eq(Decimal_adp v) {return v.under.doubleValue() == under.doubleValue();}
|
||||
public boolean Eq(int v) {return under.doubleValue() == v;}
|
||||
public String To_str() {
|
||||
BigDecimal tmp = under;
|
||||
int tmp_scale = tmp.scale();
|
||||
if (tmp_scale <= -14) return tmp.toString(); // NOTE: if large number, call .toString which will return exponential notaion (1E##) instead of literal (1000....); 14 matches MW code; DATE:2015-04-10
|
||||
if (tmp_scale > 14)
|
||||
tmp = tmp.setScale(14, RoundingMode.DOWN); // NOTE: if small number, round down to remove excessive zeroes; 14 matches PHP/C# values more closely; RoundingMode.Down for same reason; see E, Pi tests
|
||||
return tmp .stripTrailingZeros() // NOTE: stripTrailingZeros for exp tests; EX: 120.0 -> 120; 0.01200000000000 -> .012
|
||||
.toPlainString(); // NOTE: toPlainString b/c stripTrailingZeros now converts 120 to 1.2E+2 (and any other value that is a multiple of 10)
|
||||
}
|
||||
public String To_str(String fmt) {return new DecimalFormat(fmt).format(under);}
|
||||
@Override public String toString() {return under.toString();}
|
||||
public int To_int() {return (int)under.doubleValue();}
|
||||
public long To_long() {return (long)under.doubleValue();}
|
||||
public long To_long_mult_1000() {return under.movePointRight(3).longValue();}
|
||||
public double To_double() {return under.doubleValue();}
|
||||
public Decimal_adp Add(Decimal_adp v) {return new Decimal_adp(under.add(v.under, Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Subtract(Decimal_adp v) {return new Decimal_adp(under.subtract(v.under, Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Multiply(Decimal_adp v) {return new Decimal_adp(under.multiply(v.under));}
|
||||
public Decimal_adp Multiply(double v) {return new Decimal_adp(under.multiply(new BigDecimal(v, Decimal_adp_.Gplx_rounding_context)));}
|
||||
public Decimal_adp Multiply(long v) {return new Decimal_adp(under.multiply(new BigDecimal(v)));}
|
||||
public Decimal_adp Divide(Decimal_adp v) {return new Decimal_adp(under.divide(v.under, Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Mod(Decimal_adp v) {return new Decimal_adp(under.remainder(v.under, Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Abs() {return new Decimal_adp(under.abs(Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Pow(int v) {return new Decimal_adp(under.pow(v, Decimal_adp_.Gplx_rounding_context));}
|
||||
public Decimal_adp Sqrt() {return new Decimal_adp(new BigDecimal(Math_.Sqrt(under.doubleValue())));}
|
||||
public Decimal_adp Truncate() {return new Decimal_adp(under.intValue());}
|
||||
public Decimal_adp Round_old(int v) {return new Decimal_adp(under.setScale(v, RoundingMode.HALF_UP));}
|
||||
public Decimal_adp Round(int v) {
|
||||
BigDecimal new_val = null;
|
||||
if (v > 0) {
|
||||
new_val = under.setScale(v, RoundingMode.HALF_UP);
|
||||
}
|
||||
else {
|
||||
int actl_places = under.precision() - under.scale();
|
||||
int reqd_places = -v;
|
||||
if (reqd_places < actl_places)
|
||||
new_val = under.round(new java.math.MathContext(actl_places - reqd_places, RoundingMode.HALF_UP));
|
||||
else if (reqd_places == actl_places) {
|
||||
int base_10 = (int)Math_.Pow(10, reqd_places - 1);
|
||||
if (under.intValue() / base_10 < 5)
|
||||
new_val = BigDecimal.ZERO;
|
||||
else
|
||||
new_val = new BigDecimal(Math_.Pow(10, reqd_places));
|
||||
}
|
||||
else
|
||||
new_val = BigDecimal.ZERO;
|
||||
}
|
||||
return new Decimal_adp(new_val);
|
||||
}
|
||||
public boolean Comp_gte(Decimal_adp v) {return under.doubleValue() >= v.under.doubleValue();}
|
||||
public boolean Comp_gte(int v) {return under.doubleValue() >= v;}
|
||||
public boolean Comp_lte(Decimal_adp v) {return under.doubleValue() <= v.under.doubleValue();}
|
||||
public boolean Comp_lte(int v) {return under.doubleValue() <= v;}
|
||||
public boolean Comp_gt(Decimal_adp v) {return under.doubleValue() > v.under.doubleValue();}
|
||||
public boolean Comp_gt(int v) {return under.doubleValue() > v;}
|
||||
public boolean Comp_lt(Decimal_adp v) {return under.doubleValue() < v.under.doubleValue();}
|
||||
public boolean Comp_lt(int v) {return under.doubleValue() < v;}
|
||||
}
|
||||
57
100_core/src_120_basicDataType/gplx/Decimal_adp_.java
Normal file
57
100_core/src_120_basicDataType/gplx/Decimal_adp_.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
public class Decimal_adp_ {
|
||||
public static final Class<?> Cls_ref_type = Decimal_adp.class;
|
||||
public static Decimal_adp as_(Object obj) {return obj instanceof Decimal_adp ? (Decimal_adp)obj : null;}
|
||||
public static final Decimal_adp Zero = new Decimal_adp(0);
|
||||
public static final Decimal_adp One = new Decimal_adp(1);
|
||||
public static final Decimal_adp Neg1 = new Decimal_adp(-1);
|
||||
public static final Decimal_adp Const_e = Decimal_adp_.double_(Math_.E);
|
||||
public static final Decimal_adp Const_pi = Decimal_adp_.double_(Math_.Pi);
|
||||
public static Decimal_adp base1000_(long v) {return divide_(v, 1000);}
|
||||
public static Decimal_adp parts_1000_(long num, int frc) {return divide_((num * (1000)) + frc, 1000);}
|
||||
public static Decimal_adp parts_(long num, int frc) {
|
||||
// int log10 = frc == 0 ? 0 : (Math_.Log10(frc) + 1);
|
||||
// int pow10 = (int)Math_.Pow(10, log10);
|
||||
int pow10 = XtoPow10(frc);
|
||||
return divide_((num * (pow10)) + frc, pow10);
|
||||
}
|
||||
public static Decimal_adp cast_(Object obj) {return (Decimal_adp)obj;}
|
||||
static int XtoPow10(int v) {
|
||||
if (v > -1 && v < 10) return 10;
|
||||
else if (v > 9 && v < 100) return 100;
|
||||
else if (v > 99 && v < 1000) return 1000;
|
||||
else if (v > 999 && v < 10000) return 10000;
|
||||
else if (v > 9999 && v < 100000) return 100000;
|
||||
else if (v > 99999 && v < 1000000) return 1000000;
|
||||
else if (v > 999999 && v < 10000000) return 10000000;
|
||||
else if (v > 9999999 && v < 100000000) return 100000000;
|
||||
else if (v > 99999999 && v < 1000000000) return 1000000000;
|
||||
else throw Err_.new_wo_type("value must be between 0 and 1 billion", "v", v);
|
||||
}
|
||||
public static String CalcPctStr(long dividend, long divisor, String fmt) {
|
||||
if (divisor == 0) return "%ERR";
|
||||
return Decimal_adp_.float_(Float_.Div(dividend, divisor) * 100).To_str(fmt) + "%";
|
||||
}
|
||||
public static Decimal_adp divide_safe_(long lhs, long rhs) {return rhs == 0 ? Zero : divide_(lhs, rhs);}
|
||||
public static Decimal_adp divide_(long lhs, long rhs) {
|
||||
return new Decimal_adp(new BigDecimal(lhs).divide(new BigDecimal(rhs), Gplx_rounding_context));
|
||||
87
100_core/src_120_basicDataType/gplx/Decimal_adp__tst.java
Normal file
87
100_core/src_120_basicDataType/gplx/Decimal_adp__tst.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class Decimal_adp__tst {
|
||||
private final Decimal_adp__fxt fxt = new Decimal_adp__fxt();
|
||||
@Test public void divide_() {
|
||||
fxt.Test_divide(1, 1000, "0.001");
|
||||
fxt.Test_divide(1, 3, "0.33333333333333");
|
||||
fxt.Test_divide(1, 7, "0.14285714285714");
|
||||
}
|
||||
@Test public void base1000_() {
|
||||
fxt.Test_base_1000(1000, "1");
|
||||
fxt.Test_base_1000(1234, "1.234");
|
||||
fxt.Test_base_1000(123, "0.123");
|
||||
}
|
||||
@Test public void parts_() {
|
||||
fxt.Test_parts(1, 0, "1");
|
||||
fxt.Test_parts(1, 2, "1.2");
|
||||
fxt.Test_parts(1, 23, "1.23");
|
||||
fxt.Test_parts(123, 4567, "123.4567");
|
||||
}
|
||||
@Test public void parse_() {
|
||||
fxt.Test_parse("1", "1");
|
||||
fxt.Test_parse("1.2", "1.2");
|
||||
fxt.Test_parse("0.1", "0.1");
|
||||
}
|
||||
@Test public void Truncate_decimal() {
|
||||
fxt.Test_truncate_decimal("1", "1");
|
||||
fxt.Test_truncate_decimal("1.1", "1");
|
||||
fxt.Test_truncate_decimal("1.9", "1");
|
||||
}
|
||||
@Test public void Fraction1000() {
|
||||
fxt.Test_frac_1000(1, 1000, 1); // 0.001
|
||||
fxt.Test_frac_1000(1, 3, 333); // 0.33333333
|
||||
fxt.Test_frac_1000(1234, 1000, 234); // 1.234
|
||||
fxt.Test_frac_1000(12345, 10000, 234); // 1.2345
|
||||
}
|
||||
@Test public void Lt() {
|
||||
fxt.Test_comp_lt(1,123, 2, true);
|
||||
fxt.Test_comp_lt(1,99999999, 2, true);
|
||||
}
|
||||
@Test public void To_str_fmt() {
|
||||
fxt.Test_to_str_fmt(1, 2, "0.0", "0.5");
|
||||
fxt.Test_to_str_fmt(1, 3, "0.0", "0.3");
|
||||
fxt.Test_to_str_fmt(10000, 7, "0,000.000", "1,428.571");
|
||||
}
|
||||
@Test public void Round() {
|
||||
fxt.Test_round("123.456", 3, "123.456");
|
||||
fxt.Test_round("123.456", 2, "123.46");
|
||||
fxt.Test_round("123.456", 1, "123.5");
|
||||
fxt.Test_round("123.456", 0, "123");
|
||||
fxt.Test_round("123.456", -1, "120");
|
||||
fxt.Test_round("123.456", -2, "100");
|
||||
fxt.Test_round("123.456", -3, "0");
|
||||
|
||||
fxt.Test_round("6", -1, "10");
|
||||
fxt.Test_round("5", -1, "10");
|
||||
fxt.Test_round("6", -2, "0");
|
||||
}
|
||||
}
|
||||
class Decimal_adp__fxt {
|
||||
public void Test_divide(int lhs, int rhs, String expd) {Tfds.Eq(expd, Decimal_adp_.divide_(lhs, rhs).To_str());}
|
||||
public void Test_base_1000(int val, String expd) {Tfds.Eq(expd, Decimal_adp_.base1000_(val).To_str());}
|
||||
public void Test_parts(int num, int fracs, String expd) {Tfds.Eq(expd, Decimal_adp_.parts_(num, fracs).To_str());}
|
||||
public void Test_parse(String raw, String expd) {Tfds.Eq(expd, Decimal_adp_.parse_(raw).To_str());}
|
||||
public void Test_truncate_decimal(String raw, String expd) {Tfds.Eq(Decimal_adp_.parse_(expd).To_str(), Decimal_adp_.parse_(raw).Truncate().To_str());}
|
||||
public void Test_frac_1000(int lhs, int rhs, int expd) {Tfds.Eq(expd, Decimal_adp_.divide_(lhs, rhs).Frac_1000());}
|
||||
public void Test_comp_lt(int lhsNum, int lhsFrc, int rhs, boolean expd) {Tfds.Eq(expd, Decimal_adp_.parts_(lhsNum, lhsFrc).Comp_lt(rhs));}
|
||||
public void Test_to_str_fmt(int l, int r, String fmt, String expd) {Tfds.Eq(expd, Decimal_adp_.divide_(l, r).To_str(fmt));}
|
||||
public void Test_round(String raw, int places, String expd) {Tfds.Eq_str(expd, Decimal_adp_.parse_(raw).Round(places).To_str(), "round");}
|
||||
}
|
||||
@@ -19,11 +19,11 @@ package gplx;
|
||||
import gplx.core.strings.*;
|
||||
public class TimeSpanAdp implements CompareAble, EqAble {
|
||||
public long Fracs() {return fracs;} long fracs; public int FracsAsInt() {return (int)fracs;}
|
||||
public DecimalAdp TotalSecs() {
|
||||
return DecimalAdp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Sec]);
|
||||
public Decimal_adp TotalSecs() {
|
||||
return Decimal_adp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Sec]);
|
||||
}
|
||||
public DecimalAdp Total_days() {
|
||||
return DecimalAdp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Hour] * 24);
|
||||
public Decimal_adp Total_days() {
|
||||
return Decimal_adp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Hour] * 24);
|
||||
}
|
||||
public int[] Units() {return TimeSpanAdp_.Split_long(fracs, TimeSpanAdp_.Divisors);}
|
||||
public int Units_fracs() {
|
||||
|
||||
@@ -25,8 +25,8 @@ public class TimeSpanAdp_ {
|
||||
long fracs = (long)(seconds * Divisors[Idx_Sec]);
|
||||
return new TimeSpanAdp(fracs);
|
||||
}
|
||||
public static TimeSpanAdp decimal_(DecimalAdp seconds) {
|
||||
return new TimeSpanAdp(seconds.Xto_long_mult_1000());
|
||||
public static TimeSpanAdp decimal_(Decimal_adp seconds) {
|
||||
return new TimeSpanAdp(seconds.To_long_mult_1000());
|
||||
}
|
||||
public static TimeSpanAdp units_(int frc, int sec, int min, int hour) {
|
||||
int[] units = new int[] {frc, sec, min, hour};
|
||||
|
||||
@@ -25,7 +25,7 @@ public class TimeSpanAdp_basic_tst {
|
||||
}
|
||||
@Test public void TotalSecs() {
|
||||
TimeSpanAdp val = TimeSpanAdp_.fracs_(1987);
|
||||
Tfds.Eq_decimal(DecimalAdp_.parts_(1, 987), val.TotalSecs());
|
||||
Tfds.Eq_decimal(Decimal_adp_.parts_(1, 987), val.TotalSecs());
|
||||
}
|
||||
@Test public void Units() {
|
||||
tst_Units("01:02:03.987", 1, 2, 3, 987);
|
||||
|
||||
@@ -22,7 +22,7 @@ public class Hash_adp_bry extends gplx.lists.Hash_adp_base implements Hash_adp {
|
||||
private final Hash_adp_bry_itm_base proto, key_ref;
|
||||
Hash_adp_bry(Hash_adp_bry_itm_base proto) {
|
||||
this.proto = proto;
|
||||
key_ref = proto.New();
|
||||
this.key_ref = proto.New();
|
||||
}
|
||||
@Override protected Object Fetch_base(Object key) {return super.Fetch_base(key_ref.Init((byte[])key));}
|
||||
@Override protected void Del_base(Object key) {super.Del_base(key_ref.Init((byte[])key));}
|
||||
@@ -55,10 +55,10 @@ public class Hash_adp_bry extends gplx.lists.Hash_adp_base implements Hash_adp {
|
||||
key_itm.Init(key_bry, 0, key_bry.length);
|
||||
super.Add_base(key_itm, val);
|
||||
}
|
||||
public static Hash_adp_bry cs_() {return new Hash_adp_bry(Hash_adp_bry_itm_cs._);}
|
||||
public static Hash_adp_bry ci_ascii_() {return new Hash_adp_bry(Hash_adp_bry_itm_ci_ascii._);}
|
||||
public static Hash_adp_bry ci_utf8_(Gfo_case_mgr case_mgr) {return new Hash_adp_bry(Hash_adp_bry_itm_ci_utf8.get_or_new(case_mgr));}
|
||||
public static Hash_adp_bry c__utf8_(boolean case_match, Gfo_case_mgr case_mgr) {return case_match ? cs_() : ci_utf8_(case_mgr);}
|
||||
public static Hash_adp_bry cs() {return new Hash_adp_bry(Hash_adp_bry_itm_cs._);}
|
||||
public static Hash_adp_bry ci_a7() {return new Hash_adp_bry(Hash_adp_bry_itm_ci_a7._);}
|
||||
public static Hash_adp_bry ci_u8(Gfo_case_mgr case_mgr) {return new Hash_adp_bry(Hash_adp_bry_itm_ci_u8.get_or_new(case_mgr));}
|
||||
public static Hash_adp_bry c__u8(boolean case_match, Gfo_case_mgr case_mgr) {return case_match ? cs() : ci_u8(case_mgr);}
|
||||
}
|
||||
abstract class Hash_adp_bry_itm_base {
|
||||
public abstract Hash_adp_bry_itm_base New();
|
||||
@@ -92,9 +92,9 @@ class Hash_adp_bry_itm_cs extends Hash_adp_bry_itm_base {
|
||||
}
|
||||
public static final Hash_adp_bry_itm_cs _ = new Hash_adp_bry_itm_cs(); Hash_adp_bry_itm_cs() {}
|
||||
}
|
||||
class Hash_adp_bry_itm_ci_ascii extends Hash_adp_bry_itm_base {
|
||||
class Hash_adp_bry_itm_ci_a7 extends Hash_adp_bry_itm_base {
|
||||
private byte[] src; int src_bgn, src_end;
|
||||
@Override public Hash_adp_bry_itm_base New() {return new Hash_adp_bry_itm_ci_ascii();}
|
||||
@Override public Hash_adp_bry_itm_base New() {return new Hash_adp_bry_itm_ci_a7();}
|
||||
@Override public Hash_adp_bry_itm_base Init(byte[] src, int src_bgn, int src_end) {this.src = src; this.src_bgn = src_bgn; this.src_end = src_end; return this;}
|
||||
@Override public int hashCode() {
|
||||
int rv = 0;
|
||||
@@ -108,7 +108,7 @@ class Hash_adp_bry_itm_ci_ascii extends Hash_adp_bry_itm_base {
|
||||
}
|
||||
@Override public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
Hash_adp_bry_itm_ci_ascii comp = (Hash_adp_bry_itm_ci_ascii)obj;
|
||||
Hash_adp_bry_itm_ci_a7 comp = (Hash_adp_bry_itm_ci_a7)obj;
|
||||
byte[] comp_src = comp.src; int comp_bgn = comp.src_bgn, comp_end = comp.src_end;
|
||||
int comp_len = comp_end - comp_bgn, src_len = src_end - src_bgn;
|
||||
if (comp_len != src_len) return false;
|
||||
@@ -123,13 +123,13 @@ class Hash_adp_bry_itm_ci_ascii extends Hash_adp_bry_itm_base {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static final Hash_adp_bry_itm_ci_ascii _ = new Hash_adp_bry_itm_ci_ascii(); Hash_adp_bry_itm_ci_ascii() {}
|
||||
public static final Hash_adp_bry_itm_ci_a7 _ = new Hash_adp_bry_itm_ci_a7(); Hash_adp_bry_itm_ci_a7() {}
|
||||
}
|
||||
class Hash_adp_bry_itm_ci_utf8 extends Hash_adp_bry_itm_base {
|
||||
class Hash_adp_bry_itm_ci_u8 extends Hash_adp_bry_itm_base {
|
||||
private final Gfo_case_mgr case_mgr;
|
||||
Hash_adp_bry_itm_ci_utf8(Gfo_case_mgr case_mgr) {this.case_mgr = case_mgr;}
|
||||
Hash_adp_bry_itm_ci_u8(Gfo_case_mgr case_mgr) {this.case_mgr = case_mgr;}
|
||||
private byte[] src; int src_bgn, src_end;
|
||||
@Override public Hash_adp_bry_itm_base New() {return new Hash_adp_bry_itm_ci_utf8(case_mgr);}
|
||||
@Override public Hash_adp_bry_itm_base New() {return new Hash_adp_bry_itm_ci_u8(case_mgr);}
|
||||
@Override public Hash_adp_bry_itm_base Init(byte[] src, int src_bgn, int src_end) {this.src = src; this.src_bgn = src_bgn; this.src_end = src_end; return this;}
|
||||
@Override public int hashCode() {
|
||||
int rv = 0;
|
||||
@@ -149,7 +149,7 @@ class Hash_adp_bry_itm_ci_utf8 extends Hash_adp_bry_itm_base {
|
||||
}
|
||||
@Override public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
Hash_adp_bry_itm_ci_utf8 trg_itm = (Hash_adp_bry_itm_ci_utf8)obj;
|
||||
Hash_adp_bry_itm_ci_u8 trg_itm = (Hash_adp_bry_itm_ci_u8)obj;
|
||||
byte[] trg = trg_itm.src; int trg_bgn = trg_itm.src_bgn, trg_end = trg_itm.src_end;
|
||||
int src_c_bgn = src_bgn;
|
||||
int trg_c_bgn = trg_bgn;
|
||||
@@ -176,13 +176,13 @@ class Hash_adp_bry_itm_ci_utf8 extends Hash_adp_bry_itm_base {
|
||||
}
|
||||
return src_c_bgn == src_end && trg_c_bgn == trg_end; // only return true if both src and trg read to end of their brys, otherwise "a","ab" will match
|
||||
}
|
||||
public static Hash_adp_bry_itm_ci_utf8 get_or_new(Gfo_case_mgr case_mgr) {
|
||||
public static Hash_adp_bry_itm_ci_u8 get_or_new(Gfo_case_mgr case_mgr) {
|
||||
switch (case_mgr.Tid()) {
|
||||
case Gfo_case_mgr_.Tid_ascii: if (Itm_ascii == null) Itm_ascii = new Hash_adp_bry_itm_ci_utf8(case_mgr); return Itm_ascii;
|
||||
case Gfo_case_mgr_.Tid_utf8: if (Itm_utf8 == null) Itm_utf8 = new Hash_adp_bry_itm_ci_utf8(case_mgr); return Itm_utf8;
|
||||
case Gfo_case_mgr_.Tid_custom: return new Hash_adp_bry_itm_ci_utf8(case_mgr);
|
||||
case Gfo_case_mgr_.Tid_a7: if (Itm_a7 == null) Itm_a7 = new Hash_adp_bry_itm_ci_u8(case_mgr); return Itm_a7;
|
||||
case Gfo_case_mgr_.Tid_u8: if (Itm_u8 == null) Itm_u8 = new Hash_adp_bry_itm_ci_u8(case_mgr); return Itm_u8;
|
||||
case Gfo_case_mgr_.Tid_custom: return new Hash_adp_bry_itm_ci_u8(case_mgr);
|
||||
default: throw Err_.new_unhandled(case_mgr.Tid());
|
||||
}
|
||||
}
|
||||
private static Hash_adp_bry_itm_ci_utf8 Itm_ascii, Itm_utf8;
|
||||
private static Hash_adp_bry_itm_ci_u8 Itm_a7, Itm_u8;
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ public class Hash_adp_bry_tst {
|
||||
class Hash_adp_bry_fxt {
|
||||
Hash_adp_bry hash;
|
||||
public void Clear() {}
|
||||
public Hash_adp_bry_fxt New_cs() {hash = Hash_adp_bry.cs_(); return this;}
|
||||
public Hash_adp_bry_fxt New_ci() {hash = Hash_adp_bry.ci_ascii_(); return this;}
|
||||
public Hash_adp_bry_fxt New_cs() {hash = Hash_adp_bry.cs(); return this;}
|
||||
public Hash_adp_bry_fxt New_ci() {hash = Hash_adp_bry.ci_a7(); return this;}
|
||||
public Hash_adp_bry_fxt Add(String key) {byte[] key_bry = Bry_.new_u8(key); hash.Add(key_bry, key_bry); return this;}
|
||||
public Hash_adp_bry_fxt Count_tst(int expd) {Tfds.Eq(expd, hash.Count()); return this;}
|
||||
public Hash_adp_bry_fxt Get_bry_tst(String key) {return Get_bry_tst(key, key);}
|
||||
|
||||
@@ -17,5 +17,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.intl; import gplx.*;
|
||||
public class Gfo_case_mgr_ {
|
||||
public static final byte Tid_ascii = 0, Tid_utf8 = 1, Tid_custom = 2;
|
||||
public static final byte Tid_a7 = 0, Tid_u8 = 1, Tid_custom = 2;
|
||||
}
|
||||
|
||||
@@ -24,24 +24,24 @@ public class Io_size_ {
|
||||
pow++;
|
||||
}
|
||||
long div = (long)Math_.Pow((long)1024, (long)pow);
|
||||
DecimalAdp valDecimal = DecimalAdp_.divide_(val, div);
|
||||
Decimal_adp valDecimal = Decimal_adp_.divide_(val, div);
|
||||
String[] unit = Io_size_.Units[pow];
|
||||
return valDecimal.Xto_str("#,##0.000") + " " + String_.PadBgn(unit[0], 2, " ");
|
||||
return valDecimal.To_str("#,##0.000") + " " + String_.PadBgn(unit[0], 2, " ");
|
||||
}
|
||||
public static String To_str(long val, int exp_1024, String val_fmt, String unit_pad, boolean round_0_to_1) {
|
||||
long exp_val = (long)Math_.Pow(1024, exp_1024);
|
||||
DecimalAdp val_as_decimal = DecimalAdp_.divide_(val, exp_val);
|
||||
if (round_0_to_1 && val_as_decimal.Comp_lt(1)) val_as_decimal = DecimalAdp_.One;
|
||||
Decimal_adp val_as_decimal = Decimal_adp_.divide_(val, exp_val);
|
||||
if (round_0_to_1 && val_as_decimal.Comp_lt(1)) val_as_decimal = Decimal_adp_.One;
|
||||
String[] unit = Io_size_.Units[exp_1024];
|
||||
return val_as_decimal.Xto_str(val_fmt) + " " + String_.PadBgn(unit[0], 2, unit_pad);
|
||||
return val_as_decimal.To_str(val_fmt) + " " + String_.PadBgn(unit[0], 2, unit_pad);
|
||||
}
|
||||
public static long parse_or_(String raw, long or) {
|
||||
if (raw == null || raw == String_.Empty) return or;
|
||||
String[] terms = String_.Split(raw, " ");
|
||||
int termsLen = Array_.Len(terms); if (termsLen > 2) return or;
|
||||
|
||||
DecimalAdp val = null;
|
||||
try {val = DecimalAdp_.parse_(terms[0]);} catch (Exception exc) {Err_.Noop(exc); return or;}
|
||||
Decimal_adp val = null;
|
||||
try {val = Decimal_adp_.parse_(terms[0]);} catch (Exception exc) {Err_.Noop(exc); return or;}
|
||||
|
||||
int unitPow = 0;
|
||||
if (termsLen > 1) {
|
||||
@@ -50,13 +50,13 @@ public class Io_size_ {
|
||||
}
|
||||
int curPow = unitPow;
|
||||
while (curPow > 0) {
|
||||
val = val.Op_mult(1024);
|
||||
val = val.Multiply(1024);
|
||||
curPow--;
|
||||
}
|
||||
// DELETED:do not check for fractional bytes; EX: 10.7 GB DATE:2015-01-06
|
||||
// DecimalAdp comp = val.Op_truncate_decimal();
|
||||
// Decimal_adp comp = val.Op_truncate_decimal();
|
||||
// if (!val.Eq(comp)) return or;
|
||||
return val.Xto_long();
|
||||
return val.To_long();
|
||||
}
|
||||
private static int parse_unitPow_(String unitStr) {
|
||||
int unitLen = Array_.Len(Units);
|
||||
@@ -108,8 +108,8 @@ class Io_size_fmtr_arg implements Bry_fmtr_arg {
|
||||
pow++;
|
||||
}
|
||||
long div = (long)Math_.Pow((long)1024, (long)pow);
|
||||
DecimalAdp val_decimal = DecimalAdp_.divide_(val, div);
|
||||
bfr.Add_str(val_decimal.Xto_str("#,###.000")).Add_byte(Byte_ascii.Space).Add(gplx.ios.Io_size_.Units_bry[pow]);
|
||||
Decimal_adp val_decimal = Decimal_adp_.divide_(val, div);
|
||||
bfr.Add_str(val_decimal.To_str("#,###.000")).Add_byte(Byte_ascii.Space).Add(gplx.ios.Io_size_.Units_bry[pow]);
|
||||
if (suffix != null)
|
||||
bfr.Add(suffix);
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx;
|
||||
public class DecimalAdpClassXtn extends ClassXtn_base implements ClassXtn {
|
||||
public String Key() {return Key_const;} public static final String Key_const = "decimal"; // current dsv files reference "decimal"
|
||||
@Override public Class<?> UnderClass() {return DecimalAdp.class;}
|
||||
@Override public Class<?> UnderClass() {return Decimal_adp.class;}
|
||||
public Object DefaultValue() {return 0;}
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return DecimalAdp_.cast_(lhs).Eq(DecimalAdp_.cast_(rhs));} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object ParseOrNull(String raw) {return DecimalAdp_.parse_(raw);}
|
||||
@Override public String XtoUi(Object obj, String fmt) {return DecimalAdp_.cast_(obj).Xto_str();}
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return Decimal_adp_.cast_(lhs).Eq(Decimal_adp_.cast_(rhs));} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object ParseOrNull(String raw) {return Decimal_adp_.parse_(raw);}
|
||||
@Override public String XtoUi(Object obj, String fmt) {return Decimal_adp_.cast_(obj).To_str();}
|
||||
public static final DecimalAdpClassXtn _ = new DecimalAdpClassXtn(); DecimalAdpClassXtn() {} // added to ClassXtnPool by default
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ public interface GfoMsg {
|
||||
double ReadDoubleOr(String k, double or);
|
||||
DateAdp ReadDate(String k);
|
||||
DateAdp ReadDateOr(String k, DateAdp or);
|
||||
DecimalAdp ReadDecimal(String k);
|
||||
DecimalAdp ReadDecimalOr(String k, DecimalAdp or);
|
||||
Decimal_adp ReadDecimal(String k);
|
||||
Decimal_adp ReadDecimalOr(String k, Decimal_adp or);
|
||||
String ReadStr(String k);
|
||||
String ReadStrOr(String k, String or);
|
||||
Io_url ReadIoUrl(String k);
|
||||
|
||||
@@ -21,5 +21,5 @@ public class GfoMsgUtl {
|
||||
public static boolean SetBool(GfsCtx ctx, GfoMsg m, boolean cur) {return ctx.Deny() ? cur : m.ReadBoolOr("v", cur);}
|
||||
public static String SetStr(GfsCtx ctx, GfoMsg m, String cur) {return ctx.Deny() ? cur : m.ReadStrOr("v", cur);}
|
||||
public static Io_url SetIoUrl(GfsCtx ctx, GfoMsg m, Io_url cur) {return ctx.Deny() ? cur : m.ReadIoUrlOr("v", cur);}
|
||||
public static DecimalAdp SetDecimal(GfsCtx ctx, GfoMsg m, DecimalAdp cur) {return ctx.Deny() ? cur : m.ReadDecimalOr("v", cur);}
|
||||
public static Decimal_adp SetDecimal(GfsCtx ctx, GfoMsg m, Decimal_adp cur) {return ctx.Deny() ? cur : m.ReadDecimalOr("v", cur);}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ class GfoMsg_base implements GfoMsg {
|
||||
public long ReadLong(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Long_.parse_((String)rv) : Long_.cast_(rv);}
|
||||
public float ReadFloat(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Float_.parse_((String)rv) : Float_.cast_(rv);}
|
||||
public double ReadDouble(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Double_.parse_((String)rv) : Double_.cast_(rv);}
|
||||
public DecimalAdp ReadDecimal(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? DecimalAdp_.parse_((String)rv) : DecimalAdp_.cast_(rv);}
|
||||
public Decimal_adp ReadDecimal(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Decimal_adp_.parse_((String)rv) : Decimal_adp_.cast_(rv);}
|
||||
public String ReadStr(String k) {Object rv = ReadOr(k, null); if (rv == Nil) ThrowNotFound(k); return (String)rv;}
|
||||
public DateAdp ReadDate(String k) {Object rv = ReadOr(k, null); if (rv == Nil) ThrowNotFound(k); return parse ? DateAdp_.parse_gplx((String)rv) : DateAdp_.cast_(rv);}
|
||||
public Io_url ReadIoUrl(String k) {Object rv = ReadOr(k, null); if (rv == Nil) ThrowNotFound(k); return parse ? Io_url_.new_any_((String)rv) : Io_url_.cast_(rv);}
|
||||
@@ -148,7 +148,7 @@ class GfoMsg_base implements GfoMsg {
|
||||
public long ReadLongOr(String k, long or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Long_.parse_((String)rv) : Long_.cast_(rv);}
|
||||
public float ReadFloatOr(String k, float or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Float_.parse_((String)rv) : Float_.cast_(rv);}
|
||||
public double ReadDoubleOr(String k,double or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Double_.parse_((String)rv) : Double_.cast_(rv);}
|
||||
public DecimalAdp ReadDecimalOr(String k,DecimalAdp or) {Object rv = ReadOr(k, or); if (rv == Nil) return or ; return parse ? DecimalAdp_.parse_((String)rv) : DecimalAdp_.cast_(rv);}
|
||||
public Decimal_adp ReadDecimalOr(String k,Decimal_adp or) {Object rv = ReadOr(k, or); if (rv == Nil) return or ; return parse ? Decimal_adp_.parse_((String)rv) : Decimal_adp_.cast_(rv);}
|
||||
public String ReadStrOr(String k, String or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return (String)rv;}
|
||||
public DateAdp ReadDateOr(String k, DateAdp or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? DateAdp_.parse_gplx((String)rv) : DateAdp_.cast_(rv);}
|
||||
public Io_url ReadIoUrlOr(String k, Io_url or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Io_url_.new_any_((String)rv) : Io_url_.cast_(rv);}
|
||||
|
||||
@@ -39,7 +39,7 @@ public interface DataRdr extends SrlMgr, RlsAble {
|
||||
double ReadDouble(String key); double ReadDoubleOr(String key, double or);
|
||||
float ReadFloat(String key); float ReadFloatOr(String key, float or);
|
||||
byte ReadByte(String key); byte ReadByteOr(String key, byte or);
|
||||
DecimalAdp ReadDecimal(String key); DecimalAdp ReadDecimalOr(String key, DecimalAdp or);
|
||||
Decimal_adp ReadDecimal(String key); Decimal_adp ReadDecimalOr(String key, Decimal_adp or);
|
||||
DateAdp ReadDate(String key); DateAdp ReadDateOr(String key, DateAdp or);
|
||||
gplx.ios.Io_stream_rdr ReadRdr(String key);
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class DataRdr_null implements DataRdr {
|
||||
public double ReadDouble(String key) {return Double_.NaN;} public double ReadDoubleOr(String key, double or) {return or;}
|
||||
public float ReadFloat(String key) {return Float_.NaN;} public float ReadFloatOr(String key, float or) {return or;}
|
||||
public byte ReadByte(String key) {return Byte_.Min_value;} public byte ReadByteOr(String key, byte or) {return or;}
|
||||
public DecimalAdp ReadDecimal(String key) {return DecimalAdp_.Zero;}public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {return or;}
|
||||
public Decimal_adp ReadDecimal(String key) {return Decimal_adp_.Zero;}public Decimal_adp ReadDecimalOr(String key, Decimal_adp or) {return or;}
|
||||
public DateAdp ReadDate(String key) {return DateAdp_.MinValue;} public DateAdp ReadDateOr(String key, DateAdp or) {return or;}
|
||||
public gplx.ios.Io_stream_rdr ReadRdr(String key) {return gplx.ios.Io_stream_rdr_.Noop;}
|
||||
public boolean MoveNextPeer() {return false;}
|
||||
@@ -57,7 +57,7 @@ class DataRdr_null implements DataRdr {
|
||||
public long SrlLongOr(String key, long or) {return or;}
|
||||
public String SrlStrOr(String key, String or) {return or;}
|
||||
public DateAdp SrlDateOr(String key, DateAdp or) {return or;}
|
||||
public DecimalAdp SrlDecimalOr(String key, DecimalAdp or) {return or;}
|
||||
public Decimal_adp SrlDecimalOr(String key, Decimal_adp or) {return or;}
|
||||
public double SrlDoubleOr(String key, double or) {return or;}
|
||||
public Object SrlObjOr(String key, Object or) {return or;}
|
||||
public void SrlList(String key, List_adp list, SrlObj proto, String itmKey) {}
|
||||
|
||||
@@ -39,7 +39,7 @@ class DataWtr_null implements DataWtr {
|
||||
public long SrlLongOr(String key, long or) {return or;}
|
||||
public String SrlStrOr(String key, String or) {return or;}
|
||||
public DateAdp SrlDateOr(String key, DateAdp or) {return or;}
|
||||
public DecimalAdp SrlDecimalOr(String key, DecimalAdp or) {return or;}
|
||||
public Decimal_adp SrlDecimalOr(String key, Decimal_adp or) {return or;}
|
||||
public double SrlDoubleOr(String key, double or) {return or;}
|
||||
public Object SrlObjOr(String key, Object or) {return or;}
|
||||
public void SrlList(String key, List_adp list, SrlObj proto, String itmKey) {}
|
||||
|
||||
@@ -44,7 +44,7 @@ public abstract class DataWtr_base implements SrlMgr {
|
||||
public long SrlLongOr(String key, long or) {WriteData(key, or); return or;}
|
||||
public String SrlStrOr(String key, String or) {WriteData(key, or); return or;}
|
||||
public DateAdp SrlDateOr(String key, DateAdp or) {WriteData(key, or.XtoStr_gplx()); return or;}
|
||||
public DecimalAdp SrlDecimalOr(String key, DecimalAdp or) {WriteData(key, or.Xto_decimal()); return or;}
|
||||
public Decimal_adp SrlDecimalOr(String key, Decimal_adp or) {WriteData(key, or.Under()); return or;}
|
||||
public double SrlDoubleOr(String key, double or) {WriteData(key, or); return or;}
|
||||
public Object SrlObjOr(String key, Object or) {throw Err_.new_unimplemented();}
|
||||
public void TypeKey_(String v) {}
|
||||
|
||||
@@ -26,7 +26,7 @@ public interface SrlMgr {
|
||||
long SrlLongOr(String key, long v);
|
||||
String SrlStrOr(String key, String v);
|
||||
double SrlDoubleOr(String key, double v);
|
||||
DecimalAdp SrlDecimalOr(String key, DecimalAdp v);
|
||||
Decimal_adp SrlDecimalOr(String key, Decimal_adp v);
|
||||
DateAdp SrlDateOr(String key, DateAdp v);
|
||||
Object SrlObjOr(String key, Object v);
|
||||
void SrlList(String key, List_adp list, SrlObj proto, String itmKey);
|
||||
|
||||
@@ -131,27 +131,27 @@ public abstract class DataRdr_base implements SrlMgr {
|
||||
try {return (parse) ? DateAdp_.parse_gplx(String_.as_(val)) : (DateAdp)val;}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(DateAdp.class, key, val, exc);}
|
||||
}
|
||||
@gplx.Virtual public DecimalAdp ReadDecimal(String key) {
|
||||
@gplx.Virtual public Decimal_adp ReadDecimal(String key) {
|
||||
Object val = Read(key);
|
||||
try {
|
||||
if (parse) return DecimalAdp_.parse_(String_.as_(val));
|
||||
DecimalAdp rv = DecimalAdp_.as_(val);
|
||||
if (parse) return Decimal_adp_.parse_(String_.as_(val));
|
||||
Decimal_adp rv = Decimal_adp_.as_(val);
|
||||
return (rv == null)
|
||||
? DecimalAdp_.db_(val) // HACK: GfoNde_.rdr_ will call ReadAt(int i) on Db_data_rdr; since no Db_data_rdr knows about DecimalAdp, it will always return decimalType
|
||||
? Decimal_adp_.db_(val) // HACK: GfoNde_.rdr_ will call ReadAt(int i) on Db_data_rdr; since no Db_data_rdr knows about Decimal_adp, it will always return decimalType
|
||||
: rv;
|
||||
}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(DecimalAdp.class, key, val, exc);}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(Decimal_adp.class, key, val, exc);}
|
||||
}
|
||||
@gplx.Virtual public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {
|
||||
@gplx.Virtual public Decimal_adp ReadDecimalOr(String key, Decimal_adp or) {
|
||||
Object val = Read(key); if (val == null) return or;
|
||||
try {
|
||||
if (parse) return DecimalAdp_.parse_(String_.as_(val));
|
||||
DecimalAdp rv = DecimalAdp_.as_(val);
|
||||
if (parse) return Decimal_adp_.parse_(String_.as_(val));
|
||||
Decimal_adp rv = Decimal_adp_.as_(val);
|
||||
return (rv == null)
|
||||
? DecimalAdp_.db_(val) // HACK: GfoNde_.rdr_ will call ReadAt(int i) on Db_data_rdr; since no Db_data_rdr knows about DecimalAdp, it will always return decimalType
|
||||
? Decimal_adp_.db_(val) // HACK: GfoNde_.rdr_ will call ReadAt(int i) on Db_data_rdr; since no Db_data_rdr knows about Decimal_adp, it will always return decimalType
|
||||
: rv;
|
||||
}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(DecimalAdp.class, key, val, exc);}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(Decimal_adp.class, key, val, exc);}
|
||||
}
|
||||
public char ReadChar(String key) {
|
||||
Object val = Read(key);
|
||||
@@ -186,7 +186,7 @@ public abstract class DataRdr_base implements SrlMgr {
|
||||
public long SrlLongOr(String key, long or) {return ReadLongOr(key, or);}
|
||||
public String SrlStrOr(String key, String or) {return ReadStrOr(key, or);}
|
||||
public DateAdp SrlDateOr(String key, DateAdp or) {return ReadDateOr(key, or);}
|
||||
public DecimalAdp SrlDecimalOr(String key, DecimalAdp or) {return ReadDecimalOr(key, or);}
|
||||
public Decimal_adp SrlDecimalOr(String key, Decimal_adp or) {return ReadDecimalOr(key, or);}
|
||||
public double SrlDoubleOr(String key, double or) {return ReadDoubleOr(key, or);}
|
||||
public Object SrlObjOr(String key, Object or) {throw Err_.new_unimplemented();}
|
||||
public void XtoStr_gfml(String_bldr sb) {
|
||||
|
||||
@@ -19,11 +19,13 @@ package gplx;
|
||||
import gplx.core.strings.*; import gplx.core.consoles.*;
|
||||
public class Tfds { // URL:doc/gplx.tfds/Tfds.txt
|
||||
public static boolean SkipDb = false;
|
||||
public static void Eq_bool (boolean expd , boolean actl, String fmt, Object... args) {Eq_str(Bool_.Xto_str_lower(expd), Bool_.Xto_str_lower(actl), fmt, args);}
|
||||
public static void Eq_str (byte[] expd, String actl, String fmt, Object... args) {Eq_str(String_.new_u8(expd), actl, fmt, args);}
|
||||
public static void Eq_str (byte[] expd, byte[] actl, String fmt, Object... args) {Eq_str(String_.new_u8(expd), String_.new_u8(actl), fmt, args);}
|
||||
public static void Eq_str (String expd, byte[] actl, String fmt, Object... args) {Eq_str(expd, String_.new_u8(actl), fmt, args);}
|
||||
public static void Eq_str (String expd, String actl, String fmt, Object... args) {Eq_wkr(expd, actl, true, String_.Format(fmt, args));}
|
||||
public static void Eq_bool (boolean expd , boolean actl, String fmt, Object... args) {Eq_exec_y(expd, actl, fmt, args);}
|
||||
public static void Eq_byte (byte expd , byte actl, String fmt, Object... args) {Eq_exec_y(expd, actl, fmt, args);}
|
||||
public static void Eq_int (int expd , int actl, String fmt, Object... args) {Eq_exec_y(expd, actl, fmt, args);}
|
||||
public static void Eq_str (byte[] expd, byte[] actl, String fmt, Object... args) {Eq_exec_y(String_.new_u8(expd), String_.new_u8(actl), fmt, args);}
|
||||
public static void Eq_str (byte[] expd, String actl, String fmt, Object... args) {Eq_exec_y(String_.new_u8(expd), actl, fmt, args);}
|
||||
public static void Eq_str (String expd, byte[] actl, String fmt, Object... args) {Eq_exec_y(expd, String_.new_u8(actl), fmt, args);}
|
||||
public static void Eq_str (String expd, String actl, String fmt, Object... args) {Eq_exec_y(expd, actl, fmt, args);}
|
||||
|
||||
public static void Eq(Object expd, Object actl) {Eq_wkr(expd, actl, true, EmptyStr);}
|
||||
public static void Eq_able(EqAble expd, EqAble actl) {Eq_able_wkr(expd, actl, true, EmptyStr);}
|
||||
@@ -31,7 +33,7 @@ public class Tfds { // URL:doc/gplx.tfds/Tfds.txt
|
||||
public static void Eq_byte(byte expd, byte actl) {Eq_wkr(expd, actl, true, EmptyStr);}
|
||||
public static void Eq_long(long expd, long actl) {Eq_wkr(expd, actl, true, EmptyStr);}
|
||||
public static void Eq_float(float expd, float actl) {Eq_wkr(expd, actl, true, EmptyStr);}
|
||||
public static void Eq_decimal(DecimalAdp expd, DecimalAdp actl) {Eq_wkr(expd.Xto_double(), actl.Xto_double(), true, EmptyStr);}
|
||||
public static void Eq_decimal(Decimal_adp expd, Decimal_adp actl) {Eq_wkr(expd.To_double(), actl.To_double(), true, EmptyStr);}
|
||||
public static void Eq_date(DateAdp expd, DateAdp actl) {Eq_wkr(expd.XtoStr_gplx(), actl.XtoStr_gplx(), true, EmptyStr);}
|
||||
public static void Eq_date(DateAdp expd, DateAdp actl, String fmt, Object... args){Eq_wkr(expd.XtoStr_gplx(), actl.XtoStr_gplx(), true, String_.Format(fmt, args));}
|
||||
public static void Eq_url(Io_url expd, Io_url actl) {Eq_wkr(expd.Raw(), actl.Raw(), true, EmptyStr);}
|
||||
@@ -66,6 +68,11 @@ public class Tfds { // URL:doc/gplx.tfds/Tfds.txt
|
||||
public static void Eq_ary_str(Object lhs, Object rhs) {Eq_ary_wkr(lhs, rhs, false, EmptyStr);}
|
||||
public static void Eq_list(List_adp lhs, List_adp rhs) {Eq_list_wkr(lhs, rhs, TfdsEqListItmStr_cls_default._, EmptyStr);}
|
||||
public static void Eq_list(List_adp lhs, List_adp rhs, TfdsEqListItmStr xtoStr) {Eq_list_wkr(lhs, rhs, xtoStr, EmptyStr);}
|
||||
private static void Eq_exec_y(Object lhs, Object rhs, String fmt, Object[] args) {
|
||||
if (Object_.Eq(lhs, rhs)) return;
|
||||
String msg = msgBldr.Eq_xtoStr(lhs, rhs, String_.Format(fmt, args));
|
||||
throw Err_.new_wo_type(msg);
|
||||
}
|
||||
static void Eq_able_wkr(EqAble lhs, EqAble rhs, boolean expd, String customMsg) {
|
||||
boolean actl = false;
|
||||
if (lhs == null && rhs != null) actl = false;
|
||||
@@ -156,7 +163,6 @@ public class Tfds { // URL:doc/gplx.tfds/Tfds.txt
|
||||
private static final DateAdp time0 = DateAdp_.parse_gplx("2001-01-01 00:00:00.000");
|
||||
private static DateAdp nowTime; // NOTE: cannot set to time0 due to static initialization;
|
||||
public static void WriteText(String text) {Console_adp__sys.I.Write_str(text);}
|
||||
public static void Write_bry(byte[] ary) {Write(String_.new_u8(ary));}
|
||||
public static void Write() {Write("tmp");}
|
||||
public static void Write(Object... ary) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
|
||||
Reference in New Issue
Block a user