1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
/*
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;
public class Exc extends RuntimeException {
private final String stack;
private Exc_msg[] msgs_ary = new Exc_msg[8]; private int msgs_len = 8, msgs_idx = 0;
public Exc(String stack, String type, String msg, Object... args) {
Msgs_add(type, msg, args);
this.stack = stack;
}
public int Stack_erase() {return stack_erase;} public Exc Stack_erase_1_() {stack_erase = 1; return this;} private int stack_erase = 0;
public Exc Args_add(Object... args) {msgs_ary[msgs_idx - 1].Args_add(args); return this;} // i - 1 to get current
@gplx.Internal protected boolean Type_match(String type) {
for (int i = 0; i < msgs_len; ++i) {
if (String_.Eq(type, msgs_ary[i].Type())) return true;
}
return false;
}
@gplx.Internal protected void Msgs_add(String type, String msg, Object[] args) {
if (msgs_idx == msgs_len) {
int new_len = msgs_len * 2;
Exc_msg[] new_ary = new Exc_msg[new_len];
Array_.CopyTo(msgs_ary, new_ary, 0);
this.msgs_ary = new_ary;
this.msgs_len = new_len;
}
msgs_ary[msgs_idx] = new Exc_msg(type, msg, args);
++msgs_idx;
}
public String To_str_all() {
String rv = "";
for (int i = msgs_idx - 1; i > -1; --i) {
rv += "[err " + Int_.Xto_str(i) + "] " + msgs_ary[i].To_str() + "\n";
}
rv += "[stack]\n " + Stack_to_str(this, stack);
return rv;
}
@Override public String getMessage() {return To_str_all();}
private static String Stack_to_str(Exception e, String stack) {
String rv = stack;
if (rv == Exc_.Stack_null) { // occurs for thrown gplx exceptions; EX: throw Exc_.new_unimplemented
rv = ""; // set to "" b/c String concat below;
String[] lines = String_.Split_lang(Exc_.Stack_lang(e), '\n');
int len = lines.length;
for (int i = 0; i < len; ++i) {
String line = lines[i];
if (String_.Has_at_bgn(line, "gplx.Exc_.new")) continue; // ignore stack frames with "gplx.Exc_.new"; EX: throw Exc_.new_unimplemented
if (String_.Len(rv) > 0) rv += "\n";
rv += line;
}
}
rv = String_.Replace(rv, "\n", "\n "); // " " is to indent stack stack
return rv;
}
}

View File

@@ -0,0 +1,67 @@
/*
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;
public class Exc_ {
private static String Type__none = ""; @gplx.Internal protected static String Stack_null = null;
public static final String Type__op_canceled = "gplx.op_canceled";
public static void Noop(Exception e) {}
public static Exc as_(Object obj) {return obj instanceof Exc ? (Exc)obj : null;}
public static Exc new_(String msg, Object... args) {return new Exc(Stack_null, Type__none, msg, args);}
public static Exc new_w_type(String type, String msg, Object... args) {return new Exc(Stack_null, type, msg, args);}
public static Exc new_exc(Exception e, String type, String msg, Object... args) {
Exc rv = ClassAdp_.Eq_typeSafe(e, Exc.class) ? (Exc)e : new Exc(Exc_.Stack_lang(e), Type__none, Exc_.Message_lang(e));
rv.Msgs_add(type, msg, args);
return rv;
}
public static Exc new_unhandled(Object val) {return new Exc(Stack_null, Type__none, "val is not in switch/if", "val", val);}
public static Exc new_unimplemented() {return new Exc(Stack_null, Type__none, "method not implemented");}
public static Exc new_unimplemented_w_msg(String msg, Object... args) {return new Exc(Stack_null, Type__none, msg, args);}
public static Exc new_deprecated(String s) {return new Exc(Stack_null, Type__none, "deprecated", "method", s);}
public static Exc new_parse_type(Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw);}
public static Exc new_parse_exc(Exception e, Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw).Args_add("e", Err_.Message_lang(e));}
public static Exc new_parse(String type, String raw) {return new Exc(Stack_null, Type__none, "parse failed", "type", type, "raw", raw);}
public static Exc new_null(String s) {return new Exc(Stack_null, Type__none, "null obj", "obj", s);}
public static Exc new_missing_idx(int idx, int len) {return new Exc(Stack_null, Type__none, "index is out of bounds", "idx", idx, "len", len);}
public static Exc new_missing_key(String key) {return new Exc(Stack_null, Type__none, "key not found", "key", key);}
public static Exc new_invalid_op(String msg) {return new Exc(Stack_null, Type__none, msg);}
public static Exc new_op_canceled() {return new Exc(Stack_null, Type__op_canceled, "canceled by usr");}
public static Exc new_type_mismatch_w_exc(Exception ignore, Class<?> t, Object o) {return new_type_mismatch(t, o);}
public static Exc new_type_mismatch(Class<?> t, Object o) {return new Exc(Stack_null, Type__none, "type mismatch", "expdType", ClassAdp_.FullNameOf_type(t), "actlType", ClassAdp_.NameOf_obj(o), "actlObj", Object_.Xto_str_strict_or_null_mark(o));}
public static Exc new_cast(Exception ignore, Class<?> t, Object o) {
String o_str = "";
try {o_str = Object_.Xto_str_strict_or_null_mark(o);}
catch (Exception e) {o_str = "<ERROR>"; Exc_.Noop(e);}
return new Exc(Stack_null, Type__none, "cast failed", "type", ClassAdp_.NameOf_type(t), "obj", o_str);
}
public static String Message_lang(Exception e) {return e.getMessage();}
public static String Stack_lang(Exception e) {
String rv = "";
StackTraceElement[] ary = e.getStackTrace();
int len = ary.length;
for (int i = 0; i < len; i++) {
if (i != 0) rv += "\n";
rv += ary[i].toString();
}
return rv;
}
public static boolean Type_match(Exception e, String type) {
Exc exc = Exc_.as_(e);
return exc == null ? false : exc.Type_match(type);
}
}

View File

@@ -0,0 +1,44 @@
/*
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;
class Exc_msg {
private final String msg; private Object[] args;
public Exc_msg(String type, String msg, Object[] args) {
this.type = type;
this.msg = msg;
this.args = args;
}
public String Type() {return type;} private final String type;
public void Args_add(Object[] add) {
this.args = (Object[])Array_.Resize_add(args, add);
}
public String To_str() {
String rv = String_.Len_eq_0(type) ? "" : "[" + type + "] ";
rv += msg;
int len = args.length;
if (len > 0) {
rv += ":";
for (int i = 0; i < len; i += 2) {
Object key = args[i];
Object val = i < len ? args[i + 1] : "MISSING_VAL";
rv += " " + Object_.Xto_str_strict_or_null_mark(key) + "=" + Object_.Xto_str_strict_or_null_mark(val);
}
}
return rv;
}
}

View File

@@ -0,0 +1,155 @@
/*
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.core.brys; import gplx.*; import gplx.core.*;
public class Bry_rdr {
public byte[] Src() {return src;} protected byte[] src;
public int Src_len() {return src_len;} protected int src_len;
public void Init(byte[] src) {this.Init(src, 0);}
public void Init(byte[] src, int pos) {
this.src = src; this.src_len = src.length; this.pos = pos;
}
public int Pos() {return pos;} public Bry_rdr Pos_(int v) {this.pos = v; return this;} protected int pos;
public void Pos_add(int v) {pos += v;}
public boolean Pos_is_eos() {return pos == src_len;}
public void Pos_add_one() {++pos;}
public int Or_int() {return or_int;} public void Or_int_(int v) {or_int = v;} private int or_int = Int_.MinValue;
public byte[] Or_bry() {return or_bry;} public void Or_bry_(byte[] v) {or_bry = v;} private byte[] or_bry;
public int Find_fwd(byte find) {return Bry_finder.Find_fwd(src, find, pos);}
public int Find_fwd_ws() {return Bry_finder.Find_fwd_until_ws(src, pos, src_len);}
public int Find_fwd__pos_at_lhs(byte[] find_bry) {return Find_fwd__pos_at(find_bry, Bool_.N);}
public int Find_fwd__pos_at_rhs(byte[] find_bry) {return Find_fwd__pos_at(find_bry, Bool_.Y);}
public int Find_fwd__pos_at(byte[] find_bry, boolean pos_at_rhs) {
int find_pos = Bry_finder.Find_fwd(src, find_bry, pos, src_len);
if (pos_at_rhs) find_pos += find_bry.length;
if (find_pos != Bry_finder.Not_found) pos = find_pos;
return find_pos;
}
public int Read_int_to_semic() {return Read_int_to(Byte_ascii.Semic);}
public int Read_int_to_comma() {return Read_int_to(Byte_ascii.Comma);}
public int Read_int_to_pipe() {return Read_int_to(Byte_ascii.Pipe);}
public int Read_int_to_nl() {return Read_int_to(Byte_ascii.Nl);}
public int Read_int_to_quote() {return Read_int_to(Byte_ascii.Quote);}
public int Read_int_to_non_num(){return Read_int_to(Byte_ascii.Nil);}
public int Read_int_to(byte to_char) {
int bgn = pos;
int rv = 0;
int negative = 1;
while (pos < src_len) {
byte b = src[pos++];
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 = (rv * 10) + (b - Byte_ascii.Num_0);
break;
case Byte_ascii.Dash:
if (negative == -1) // 2nd negative
return or_int; // return or_int
else // 1st negative
negative = -1; // flag negative
break;
default: {
boolean match = b == to_char;
if (to_char == Byte_ascii.Nil) {// hack for Read_int_to_non_num
--pos;
match = true;
}
return match ? rv * negative : or_int;
}
}
}
return bgn == pos ? or_int : rv * negative;
}
public byte[] Read_bry_to_nl() {return Read_bry_to(Byte_ascii.Nl);}
public byte[] Read_bry_to_semic() {return Read_bry_to(Byte_ascii.Semic);}
public byte[] Read_bry_to_pipe() {return Read_bry_to(Byte_ascii.Pipe);}
public byte[] Read_bry_to_quote() {return Read_bry_to(Byte_ascii.Quote);}
public byte[] Read_bry_to_apos() {return Read_bry_to(Byte_ascii.Apos);}
public byte[] Read_bry_to(byte to_char) {
int bgn = pos;
while (pos < src_len) {
byte b = src[pos];
if (b == to_char)
return Bry_.Mid(src, bgn, pos++);
else
++pos;
}
return bgn == pos ? or_bry : Bry_.Mid(src, bgn, src_len);
}
public boolean Read_yn_to_pipe() {return Read_byte_to_pipe() == Byte_ascii.Ltr_y;}
public byte Read_byte_to_pipe() {
byte rv = src[pos];
pos += 2; // 1 for byte; 1 for pipe;
return rv;
}
public double Read_double_to_pipe() {return Read_double_to(Byte_ascii.Pipe);}
public double Read_double_to(byte to_char) {
byte[] double_bry = Read_bry_to(to_char);
return Double_.parse_(String_.new_a7(double_bry)); // double will never have utf8
}
@gplx.Virtual public Bry_rdr Skip_ws() {
while (pos < src_len) {
switch (src[pos]) {
case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr: case Byte_ascii.Space:
++pos;
break;
default:
return this;
}
}
return this;
}
public Bry_rdr Skip_alpha_num_under() {
while (pos < src_len) {
switch (src[pos]) {
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:
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:
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:
case Byte_ascii.Underline:
++pos;
break;
default:
return this;
}
}
return this;
}
public void Chk_bry_or_fail(byte[] bry) {
int bry_len = bry.length;
boolean match = Bry_.Match(src, pos, pos + bry_len, bry);
if (match) pos += bry_len;
else throw Exc_.new_("bry.rdr:chk failed", "bry", bry, "pos", pos);
}
public void Chk_byte_or_fail(byte b) {
boolean match = pos < src_len ? src[pos] == b : false;
if (match) ++pos;
else throw Exc_.new_("bry.rdr:chk failed", "byte", b, "pos", pos);
}
public byte[] Mid_by_len_safe(int len) {
int end = pos + len; if (end > src_len) end = src_len;
return Bry_.Mid(src, pos, end);
}
}

View File

@@ -0,0 +1,88 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import gplx.core.primitives.*;
public class Btrie_bwd_mgr {
public int Match_pos() {return match_pos;} private int match_pos;
public Object Match_exact(byte[] src, int bgn_pos, int end_pos) {
Object rv = Match(src[bgn_pos], src, bgn_pos, end_pos);
return rv == null ? null : match_pos - bgn_pos == end_pos - bgn_pos ? rv : null;
}
public Object Match_bgn(byte[] src, int bgn_pos, int end_pos) {return Match(src[bgn_pos], src, bgn_pos, end_pos);}
public Object Match(byte b, byte[] src, int bgn_pos, int end_pos) {
// NOTE: bgn, end follows same semantics as fwd where bgn >= & end < except reversed: bgn <= & end >; EX: "abcde" should pass 5, -1
Object rv = null; int cur_pos = match_pos = bgn_pos;
Btrie_slim_itm cur = root;
while (true) {
Btrie_slim_itm nxt = cur.Ary_find(b); if (nxt == null) return rv; // nxt does not hav b; return rv;
--cur_pos;
if (nxt.Ary_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();
if (nxt_val != null) {match_pos = cur_pos; rv = nxt_val;} // nxt is node; cache rv (in case of false match)
if (cur_pos == end_pos) return rv; // increment cur_pos and exit if src_len
b = src[cur_pos];
cur = nxt;
}
}
public Btrie_bwd_mgr Add_str_byte(String key, byte val) {return Add(Bry_.new_u8(key), Byte_obj_val.new_(val));}
public Btrie_bwd_mgr Add_byteVal_strAry(byte val, String... ary) {
int ary_len = ary.length;
Byte_obj_val byteVal = Byte_obj_val.new_(val);
for (int i = 0; i < ary_len; i++) {
String itm = ary[i];
Add(Bry_.new_u8(itm), byteVal);
}
return this;
}
public Btrie_bwd_mgr Add(String key, Object val) {return Add(Bry_.new_u8(key), val);}
public Btrie_bwd_mgr Add(byte[] key, Object val) {
if (val == null) throw Exc_.new_("null objects cannot be registered", "key", String_.new_u8(key));
int key_len = key.length;
Btrie_slim_itm cur = root;
for (int i = key_len - 1; i > -1; i--) {
byte b = key[i];
if (root.Case_any() && (b > 64 && b < 91)) b += 32;
Btrie_slim_itm nxt = cur.Ary_find(b);
if (nxt == null)
nxt = cur.Ary_add(b, null);
if (i == 0)
nxt.Val_set(val);
cur = nxt;
}
count++; // FUTURE: do not increment if replacing value
return this;
}
public int Count() {return count;} private int count;
public void Del(byte[] key) {
int key_len = key.length;
Btrie_slim_itm cur = root;
for (int i = 0; i < key_len; i++) {
byte b = key[i];
cur = cur.Ary_find(b);
if (cur == null) break;
cur.Ary_del(b);
}
count--; // FUTURE: do not decrement if not found
}
public void Clear() {root.Clear(); count = 0;}
public static Btrie_bwd_mgr cs_() {return new Btrie_bwd_mgr(false);}
public static Btrie_bwd_mgr ci_() {return new Btrie_bwd_mgr(true);}
public Btrie_bwd_mgr(boolean caseAny) {
root = new Btrie_slim_itm(Byte_.Zero, null, caseAny);
} private Btrie_slim_itm root;
}

View 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.core.btries; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Btrie_bwd_mgr_tst {
@Before public void init() {} private Btrie_bwd_mgr trie;
private void ini_setup1() {
trie = new Btrie_bwd_mgr(false);
run_Add("c" , 1);
run_Add("abc" , 123);
}
@Test public void Get_by() {
ini_setup1();
tst_MatchAtCur("c" , 1);
tst_MatchAtCur("abc" , 123);
tst_MatchAtCur("bc" , 1);
tst_MatchAtCur("yzabc" , 123);
tst_MatchAtCur("ab" , null);
}
@Test public void Fetch_intl() {
trie = new Btrie_bwd_mgr(false);
run_Add("a<EFBFBD>", 1);
tst_MatchAtCur("a<EFBFBD>" , 1);
tst_MatchAtCur("<EFBFBD>" , null);
}
@Test public void Eos() {
ini_setup1();
tst_Match("ab", Byte_ascii.Ltr_c, 2, 123);
}
@Test public void Match_exact() {
ini_setup1();
tst_MatchAtCurExact("c", 1);
tst_MatchAtCurExact("bc", null);
tst_MatchAtCurExact("abc", 123);
}
private void ini_setup2() {
trie = new Btrie_bwd_mgr(false);
run_Add("a" , 1);
run_Add("b" , 2);
}
@Test public void Match_2() {
ini_setup2();
tst_MatchAtCur("a", 1);
tst_MatchAtCur("b", 2);
}
private void ini_setup_caseAny() {
trie = Btrie_bwd_mgr.ci_();
run_Add("a" , 1);
run_Add("b" , 2);
}
@Test public void CaseAny() {
ini_setup_caseAny();
tst_MatchAtCur("a", 1);
tst_MatchAtCur("A", 1);
}
private void run_Add(String k, int val) {trie.Add(Bry_.new_u8(k), val);}
private void tst_Match(String srcStr, byte b, int bgn_pos, int expd) {
byte[] src = Bry_.new_u8(srcStr);
Object actl = trie.Match(b, src, bgn_pos, -1);
Tfds.Eq(expd, actl);
}
private void tst_MatchAtCur(String srcStr, Object expd) {
byte[] src = Bry_.new_u8(srcStr);
Object actl = trie.Match(src[src.length - 1], src, src.length - 1, -1);
Tfds.Eq(expd, actl);
}
private void tst_MatchAtCurExact(String srcStr, Object expd) {
byte[] src = Bry_.new_u8(srcStr);
Object actl = trie.Match_exact(src, src.length - 1, -1);
Tfds.Eq(expd, actl);
}
}

View File

@@ -0,0 +1,156 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import gplx.core.primitives.*;
public class Btrie_fast_mgr {
private ByteTrieItm_fast root;
public boolean CaseAny() {return root.CaseAny();} public Btrie_fast_mgr CaseAny_(boolean v) {root.CaseAny_(v); return this;}
public int Match_pos() {return match_pos;} private int match_pos;
public Object Match_exact(byte[] src, int bgn_pos, int end_pos) {
Object rv = Match_bgn_w_byte(src[bgn_pos], src, bgn_pos, end_pos);
return rv == null ? null : match_pos - bgn_pos == end_pos - bgn_pos ? rv : null;
}
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 src_len) {
match_pos = bgn_pos;
ByteTrieItm_fast nxt = root.Ary_find(b); if (nxt == null) return null; // nxt does not have b; return rv;
Object rv = null; int cur_pos = bgn_pos + 1;
ByteTrieItm_fast cur = root;
while (true) {
if (nxt.Ary_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();
if (nxt_val != null) {match_pos = cur_pos; rv = nxt_val;} // nxt is node; cache rv (in case of false match)
if (cur_pos == src_len) return rv; // eos; exit
b = src[cur_pos];
cur = nxt;
nxt = cur.Ary_find(b); if (nxt == null) return rv;
++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_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);}
public Btrie_fast_mgr Add(byte[] key, Object val) {
if (val == null) throw Exc_.new_("null objects cannot be registered", "key", String_.new_u8(key));
int key_len = key.length; int key_end = key_len - 1;
ByteTrieItm_fast cur = root;
for (int i = 0; i < key_len; i++) {
byte b = key[i];
ByteTrieItm_fast nxt = cur.Ary_find(b);
if (nxt == null)
nxt = cur.Ary_add(b, null);
if (i == key_end)
nxt.Val_set(val);
cur = nxt;
}
return this;
}
public Btrie_fast_mgr Add_stub(byte tid, String s) {
byte[] bry = Bry_.new_u8(s);
Btrie_itm_stub stub = new Btrie_itm_stub(tid, bry);
return Add(bry, stub);
}
public void Del(byte[] key) {
int key_len = key.length;
ByteTrieItm_fast cur = root;
for (int i = 0; i < key_len; i++) {
byte b = key[i];
Object itm_obj = cur.Ary_find(b);
if (itm_obj == null) break; // b not found; no match; exit;
ByteTrieItm_fast itm = (ByteTrieItm_fast)itm_obj;
if (i == key_len - 1) { // last char
if (itm.Val() == null) break; // itm does not have val; EX: trie with "abc", and "ab" deleted
if (itm.Ary_is_empty())
cur.Ary_del(b);
else
itm.Val_set(null);
}
else { // mid char; set itm as cur and continue
cur = itm;
}
}
}
public void Clear() {root.Clear();}
public byte[] Replace(Bry_bfr tmp_bfr, byte[] src, int bgn, int end) {
int pos = bgn;
boolean dirty = false;
while (pos < end) {
byte b = src[pos];
Object o = this.Match_bgn_w_byte(b, src, pos, end);
if (o == null) {
if (dirty)
tmp_bfr.Add_byte(b);
pos++;
}
else {
if (!dirty) {
tmp_bfr.Add_mid(src, bgn, pos);
dirty = true;
}
tmp_bfr.Add((byte[])o);
pos = match_pos;
}
}
return dirty ? tmp_bfr.Xto_bry_and_clear() : src;
}
public static Btrie_fast_mgr cs_() {return new Btrie_fast_mgr(false);}
public static Btrie_fast_mgr ci_ascii_() {return new Btrie_fast_mgr(true);}
public static Btrie_fast_mgr new_(boolean case_any) {return new Btrie_fast_mgr(case_any);}
Btrie_fast_mgr(boolean caseAny) {
root = new ByteTrieItm_fast(Byte_.Zero, null, caseAny);
}
}
class ByteTrieItm_fast {
private ByteTrieItm_fast[] ary = new ByteTrieItm_fast[256];
public byte Key_byte() {return key_byte;} private byte key_byte;
public Object Val() {return val;} public void Val_set(Object val) {this.val = val;} Object val;
public boolean Ary_is_empty() {return ary_is_empty;} private boolean ary_is_empty;
public boolean CaseAny() {return caseAny;} public ByteTrieItm_fast CaseAny_(boolean v) {caseAny = v; return this;} private boolean caseAny;
public void Clear() {
val = null;
for (int i = 0; i < 256; i++) {
if (ary[i] != null) {
ary[i].Clear();
ary[i] = null;
}
}
ary_len = 0;
ary_is_empty = true;
}
public ByteTrieItm_fast Ary_find(byte b) {
int key_byte = (caseAny && (b > 64 && b < 91) ? b + 32 : b) & 0xff;// PATCH.JAVA:need to convert to unsigned byte
return ary[key_byte];
}
public ByteTrieItm_fast Ary_add(byte b, Object val) {
int key_byte = (caseAny && (b > 64 && b < 91) ? b + 32 : b) & 0xff;// PATCH.JAVA:need to convert to unsigned byte
ByteTrieItm_fast rv = new ByteTrieItm_fast(b, val, caseAny);
ary[key_byte] = rv;
++ary_len;
ary_is_empty = false;
return rv;
}
public void Ary_del(byte b) {
int key_byte = (caseAny && (b > 64 && b < 91) ? b + 32 : b) & 0xff;// PATCH.JAVA:need to convert to unsigned byte
ary[key_byte] = null;
--ary_len;
ary_is_empty = ary_len == 0;
} int ary_len = 0;
public ByteTrieItm_fast(byte key_byte, Object val, boolean caseAny) {this.key_byte = key_byte; this.val = val; this.caseAny = caseAny;}
}

View File

@@ -0,0 +1,85 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Btrie_fast_mgr_tst {
private Btrie_fast_mgr_fxt fxt = new Btrie_fast_mgr_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Get_by() {
fxt.Test_matchAtCur("a" , 1);
fxt.Test_matchAtCur("abc" , 123);
fxt.Test_matchAtCur("ab" , 1);
fxt.Test_matchAtCur("abcde" , 123);
fxt.Test_matchAtCur(" a" , null);
}
@Test public void Bos() {
fxt.Test_match("bc", Byte_ascii.Ltr_a, -1, 123);
}
@Test public void Match_exact() {
fxt.Test_matchAtCurExact("a", 1);
fxt.Test_matchAtCurExact("ab", null);
fxt.Test_matchAtCurExact("abc", 123);
}
@Test public void Del_noop__no_match() {
fxt.Exec_del("d");
fxt.Test_matchAtCurExact("a" , 1);
fxt.Test_matchAtCurExact("abc" , 123);
}
@Test public void Del_noop__partial_match() {
fxt.Exec_del("ab");
fxt.Test_matchAtCurExact("a" , 1);
fxt.Test_matchAtCurExact("abc" , 123);
}
@Test public void Del_match__long() {
fxt.Exec_del("abc");
fxt.Test_matchAtCurExact("a" , 1);
fxt.Test_matchAtCurExact("abc" , null);
}
@Test public void Del_match__short() {
fxt.Exec_del("a");
fxt.Test_matchAtCurExact("a" , null);
fxt.Test_matchAtCurExact("abc" , 123);
}
}
class Btrie_fast_mgr_fxt {
private Btrie_fast_mgr trie;
public void Clear() {
trie = Btrie_fast_mgr.cs_();
Init_add( 1 , Byte_ascii.Ltr_a);
Init_add(123 , Byte_ascii.Ltr_a, Byte_ascii.Ltr_b, Byte_ascii.Ltr_c);
}
public void Init_add(int val, byte... ary) {trie.Add(ary, val);}
public void Test_match(String src_str, byte b, int bgn_pos, int expd) {
byte[] src = Bry_.new_a7(src_str);
Object actl = trie.Match_bgn_w_byte(b, src, bgn_pos, src.length);
Tfds.Eq(expd, actl);
}
public void Test_matchAtCur(String src_str, Object expd) {
byte[] src = Bry_.new_a7(src_str);
Object actl = trie.Match_bgn(src, 0, src.length);
Tfds.Eq(expd, actl);
}
public void Test_matchAtCurExact(String src_str, Object expd) {
byte[] src = Bry_.new_a7(src_str);
Object actl = trie.Match_exact(src, 0, src.length);
Tfds.Eq(expd, actl);
}
public void Exec_del(String src_str) {
trie.Del(Bry_.new_u8(src_str));
}
}

View File

@@ -0,0 +1,23 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
public class Btrie_itm_stub {
public Btrie_itm_stub(byte tid, byte[] val) {this.tid = tid; this.val = val;}
public byte Tid() {return tid;} private byte tid;
public byte[] Val() {return val;} private byte[] val;
}

View File

@@ -0,0 +1,24 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
public interface Btrie_mgr {
int Match_pos();
Object Match_bgn(byte[] src, int bgn_pos, int end_pos);
Btrie_mgr Add_obj(String key, Object val);
Btrie_mgr Add_obj(byte[] key, Object val);
}

View File

@@ -0,0 +1,130 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
public class Btrie_slim_itm {
private Btrie_slim_itm[] ary = Btrie_slim_itm.Ary_empty;
public Btrie_slim_itm(byte key_byte, Object val, boolean case_any) {this.key_byte = key_byte; this.val = val; this.case_any = case_any;}
public byte Key_byte() {return key_byte;} private byte key_byte;
public Object Val() {return val;} public void Val_set(Object val) {this.val = val;} private Object val;
public boolean Case_any() {return case_any;} private boolean case_any;
public boolean Ary_is_empty() {return ary == Btrie_slim_itm.Ary_empty;}
public void Clear() {
val = null;
for (int i = 0; i < ary_len; i++)
ary[i].Clear();
ary = Btrie_slim_itm.Ary_empty;
ary_len = ary_max = 0;
}
public Btrie_slim_itm Ary_find(byte b) {
int find_val = (case_any && (b > 64 && b < 91) ? b + 32 : b) & 0xff;// PATCH.JAVA:need to convert to unsigned byte
int key_val = 0;
switch (ary_len) {
case 0: return null;
case 1:
Btrie_slim_itm rv = ary[0];
key_val = rv.Key_byte() & 0xff;// PATCH.JAVA:need to convert to unsigned byte;
key_val = (case_any && (key_val > 64 && key_val < 91) ? key_val + 32 : key_val);
return key_val == find_val ? rv : null;
default:
int adj = 1;
int prv_pos = 0;
int prv_len = ary_len;
int cur_len = 0;
int cur_idx = 0;
Btrie_slim_itm itm = null;
while (true) {
cur_len = prv_len / 2;
if (prv_len % 2 == 1) ++cur_len;
cur_idx = prv_pos + (cur_len * adj);
if (cur_idx < 0) cur_idx = 0;
else if (cur_idx >= ary_len) cur_idx = ary_len - 1;
itm = ary[cur_idx];
key_val = itm.Key_byte() & 0xff; // PATCH.JAVA:need to convert to unsigned byte;
key_val = (case_any && (key_val > 64 && key_val < 91) ? key_val + 32 : key_val);
if (find_val < key_val) adj = -1;
else if (find_val > key_val) adj = 1;
else /*(find_val == cur_val)*/ return itm;
if (cur_len == 1) {
cur_idx += adj;
if (cur_idx < 0 || cur_idx >= ary_len) return null;
itm = ary[cur_idx];
return (itm.Key_byte() & 0xff) == find_val ? itm : null; // PATCH.JAVA:need to convert to unsigned byte;
}
prv_len = cur_len;
prv_pos = cur_idx;
}
}
}
public Btrie_slim_itm Ary_add(byte b, Object val) {
int new_len = ary_len + 1;
if (new_len > ary_max) {
ary_max += 4;
ary = (Btrie_slim_itm[])Array_.Resize(ary, ary_max);
}
Btrie_slim_itm rv = new Btrie_slim_itm(b, val, case_any);
ary[ary_len] = rv;
ary_len = new_len;
ByteHashItm_sorter._.Sort(ary, ary_len);
return rv;
}
public void Ary_del(byte b) {
boolean found = false;
for (int i = 0; i < ary_len; i++) {
if (found) {
if (i < ary_len - 1)
ary[i] = ary[i + 1];
}
else {
if (b == ary[i].Key_byte()) found = true;
}
}
if (found) --ary_len;
}
public static final Btrie_slim_itm[] Ary_empty = new Btrie_slim_itm[0]; int ary_len = 0, ary_max = 0;
}
class ByteHashItm_sorter {// quicksort
Btrie_slim_itm[] ary; int ary_len;
public void Sort(Btrie_slim_itm[] ary, int ary_len) {
if (ary == null || ary_len < 2) return;
this.ary = ary;
this.ary_len = ary_len;
Sort_recurse(0, ary_len - 1);
}
private void Sort_recurse(int lo, int hi) {
int i = lo, j = hi;
int mid = ary[lo + (hi-lo)/2].Key_byte()& 0xFF; // get mid itm
while (i <= j) { // divide into two lists
while ((ary[i].Key_byte() & 0xFF) < mid) // if lhs.cur < mid, then get next from lhs
i++;
while ((ary[j].Key_byte() & 0xFF) > mid) // if rhs.cur > mid, then get next from rhs
j--;
// lhs.cur > mid && rhs.cur < mid; switch lhs.cur and rhs.cur; increase i and j
if (i <= j) {
Btrie_slim_itm tmp = ary[i];
ary[i] = ary[j];
ary[j] = tmp;
i++;
j--;
}
}
if (lo < j) Sort_recurse(lo, j);
if (i < hi) Sort_recurse(i, hi);
}
public static final ByteHashItm_sorter _ = new ByteHashItm_sorter(); ByteHashItm_sorter() {}
}

View File

@@ -0,0 +1,49 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Btrie_slim_itm_tst {
private Btrie_slim_itm itm = new Btrie_slim_itm(Byte_.Zero, null, false);
@Before public void init() {itm.Clear();}
@Test public void Find_nil() {
tst_Find(Byte_ascii.Ltr_a, null);
}
@Test public void Add_one() {
run_Add(Byte_ascii.Ltr_a);
tst_Find(Byte_ascii.Ltr_a, "a");
}
@Test public void Add_many() {
run_Add(Byte_ascii.Bang, Byte_ascii.Num_0, Byte_ascii.Ltr_a, Byte_ascii.Ltr_B);
tst_Find(Byte_ascii.Ltr_a, "a");
}
@Test public void Del() {
run_Add(Byte_ascii.Bang, Byte_ascii.Num_0, Byte_ascii.Ltr_a, Byte_ascii.Ltr_B);
tst_Find(Byte_ascii.Ltr_a, "a");
run_Del(Byte_ascii.Ltr_a);
tst_Find(Byte_ascii.Ltr_a, null);
tst_Find(Byte_ascii.Num_0, "0");
tst_Find(Byte_ascii.Ltr_B, "B");
}
private void tst_Find(byte b, String expd) {
Btrie_slim_itm actl_itm = itm.Ary_find(b);
Object actl = actl_itm == null ? null : actl_itm.Val();
Tfds.Eq(expd, actl);
}
private void run_Add(byte... ary) {for (byte b : ary) itm.Ary_add(b, Char_.XtoStr((char)b));}
private void run_Del(byte... ary) {for (byte b : ary) itm.Ary_del(b);}
}

View File

@@ -0,0 +1,128 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import gplx.core.primitives.*;
public class Btrie_slim_mgr implements Btrie_mgr {
Btrie_slim_mgr(boolean case_match) {root = new Btrie_slim_itm(Byte_.Zero, null, !case_match);} private Btrie_slim_itm root;
public int Count() {return count;} private int count;
public int Match_pos() {return match_pos;} private int match_pos;
public Object Match_exact(byte[] src, int bgn_pos, int end_pos) {
Object rv = Match_bgn_w_byte(src[bgn_pos], src, bgn_pos, end_pos);
return rv == null ? null : match_pos - bgn_pos == end_pos - bgn_pos ? rv : null;
}
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 src_len) {
Object rv = null; int cur_pos = match_pos = bgn_pos;
Btrie_slim_itm cur = root;
while (true) {
Btrie_slim_itm nxt = cur.Ary_find(b); if (nxt == null) return rv; // nxt does not hav b; return rv;
++cur_pos;
if (nxt.Ary_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();
if (nxt_val != null) {match_pos = cur_pos; rv = nxt_val;} // nxt is node; cache rv (in case of false match)
if (cur_pos == src_len) return rv; // increment cur_pos and exit if src_len
b = src[cur_pos];
cur = nxt;
}
}
public Btrie_slim_mgr Add_bry_tid(byte[] bry, byte tid) {return (Btrie_slim_mgr)Add_obj(bry, Byte_obj_val.new_(tid));}
public Btrie_slim_mgr Add_str_byte(String key, byte val) {return (Btrie_slim_mgr)Add_obj(Bry_.new_u8(key), Byte_obj_val.new_(val));}
public Btrie_slim_mgr Add_str_int(String key, int val) {return (Btrie_slim_mgr)Add_obj(Bry_.new_u8(key), Int_obj_val.new_(val));}
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_str_byte__many(byte val, String... ary) {
int ary_len = ary.length;
Byte_obj_val bval = Byte_obj_val.new_(val);
for (int i = 0; i < ary_len; i++)
Add_obj(Bry_.new_u8(ary[i]), bval);
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) {
for (byte i = 0; i < ary_len; i++) {
byte[] bry = ary[i];
Add_obj(bry, new Btrie_itm_stub(i, bry));
}
return this;
}
public Btrie_mgr Add_obj(String key, Object val) {return Add_obj(Bry_.new_u8(key), val);}
public Btrie_mgr Add_obj(byte[] key, Object val) {
if (val == null) throw Exc_.new_("null objects cannot be registered", "key", String_.new_u8(key));
int key_len = key.length; int key_end = key_len - 1;
Btrie_slim_itm cur = root;
for (int i = 0; i < key_len; i++) {
byte b = key[i];
if (root.Case_any() && (b > 64 && b < 91)) b += 32;
Btrie_slim_itm nxt = cur.Ary_find(b);
if (nxt == null)
nxt = cur.Ary_add(b, null);
if (i == key_end)
nxt.Val_set(val);
cur = nxt;
}
count++; // FUTURE: do not increment if replacing value
return this;
}
public void Del(byte[] key) {
int key_len = key.length;
Btrie_slim_itm cur = root;
for (int i = 0; i < key_len; i++) {
byte b = key[i];
Btrie_slim_itm nxt = cur.Ary_find(b);
if (nxt == null) break;
Object nxt_val = nxt.Val();
if (nxt_val == null) // cur is end of chain; remove entry; EX: Abc and at c
cur.Ary_del(b);
else // cur is mid of chain; null out entry
nxt.Val_set(null);
cur = nxt;
}
count--; // FUTURE: do not decrement if not found
}
public byte[] Replace(Bry_bfr tmp_bfr, byte[] src, int bgn, int end) {
int pos = bgn;
boolean dirty = false;
while (pos < end) {
byte b = src[pos];
Object o = this.Match_bgn_w_byte(b, src, pos, end);
if (o == null) {
if (dirty)
tmp_bfr.Add_byte(b);
pos++;
}
else {
if (!dirty) {
tmp_bfr.Add_mid(src, bgn, pos);
dirty = true;
}
tmp_bfr.Add((byte[])o);
pos = match_pos;
}
}
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 new_(boolean v) {return new Btrie_slim_mgr(v);}
}

View File

@@ -0,0 +1,92 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Btrie_slim_mgr_tst {
@Before public void init() {
} private Btrie_slim_mgr trie;
private void ini_setup1() {
trie = Btrie_slim_mgr.cs_();
run_Add("a" , 1);
run_Add("abc" , 123);
}
@Test public void Get_by() {
ini_setup1();
tst_MatchAtCur("a" , 1);
tst_MatchAtCur("abc" , 123);
tst_MatchAtCur("ab" , 1);
tst_MatchAtCur("abcde" , 123);
tst_MatchAtCur(" a" , null);
}
@Test public void Bos() {
ini_setup1();
tst_Match("bc", Byte_ascii.Ltr_a, -1, 123);
}
@Test public void Match_exact() {
ini_setup1();
tst_MatchAtCurExact("a", 1);
tst_MatchAtCurExact("ab", null);
tst_MatchAtCurExact("abc", 123);
}
private void ini_setup2() {
trie = Btrie_slim_mgr.cs_();
run_Add("a" , 1);
run_Add("b" , 2);
}
@Test public void Match_2() {
ini_setup2();
tst_MatchAtCur("a", 1);
tst_MatchAtCur("b", 2);
}
private void ini_setup_caseAny() {
trie = Btrie_slim_mgr.ci_ascii_(); // NOTE:ci.ascii:test
run_Add("a" , 1);
run_Add("b" , 2);
}
@Test public void CaseAny() {
ini_setup_caseAny();
tst_MatchAtCur("a", 1);
tst_MatchAtCur("A", 1);
}
@Test public void Del() {
ini_setup1();
trie.Del(Bry_.new_a7("a")); // delete "a"; "abc" still remains;
tst_MatchAtCur("a" , null);
tst_MatchAtCur("abc" , 123);
trie.Del(Bry_.new_a7("abc"));
tst_MatchAtCur("abc" , null);
}
private void run_Add(String k, int val) {trie.Add_obj(Bry_.new_a7(k), val);}
private void tst_Match(String srcStr, byte b, int bgn_pos, int expd) {
byte[] src = Bry_.new_a7(srcStr);
Object actl = trie.Match_bgn_w_byte(b, src, bgn_pos, src.length);
Tfds.Eq(expd, actl);
}
private void tst_MatchAtCur(String srcStr, Object expd) {
byte[] src = Bry_.new_a7(srcStr);
Object actl = trie.Match_bgn_w_byte(src[0], src, 0, src.length);
Tfds.Eq(expd, actl);
}
private void tst_MatchAtCurExact(String srcStr, Object expd) {
byte[] src = Bry_.new_a7(srcStr);
Object actl = trie.Match_exact(src, 0, src.length);
Tfds.Eq(expd, actl);
}
}

View File

@@ -0,0 +1,68 @@
/*
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.core.btries; import gplx.*; import gplx.core.*;
import gplx.intl.*;
class Btrie_utf8_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 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;}
public void Clear() {
val = null;
nxts.Clear();
nxts = null;
}
public Btrie_utf8_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;
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;
else { // itm has asymmetric_bry; EX: "İ" was added to trie, must match "İ" and "i";
if (called_by_match) { // called by mgr.Match
return
( Bry_.Eq(rv.key, src, c_bgn, c_end) // key matches src; EX: "aİ"
|| Bry_.Eq(rv.asymmetric_bry, src, c_bgn, c_end) // asymmetric_bry matches src; EX: "ai"; note that "aI" won't match
)
? rv : null;
}
else { // called by mgr.Add; this means that an asymmetric_itm was already added; happens when "İ" added first and then "I" added next
rv.asymmetric_bry = null; // always null out asymmetric_bry; note that this noops non-asymmetric itms, while making an asymmetric_itm case-insenstivie (matches İ,i,I); see tests
return rv;
}
}
}
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);
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 "_"
byte[] asymmetric_bry = case_itm.Asymmetric_bry();
if (asymmetric_bry != null) { // case_itm has asymmetry_bry; only itms in Xol_case_itm_ that are created with Tid_upper and Tid_lower will be non-null
rv.asymmetric_bry = asymmetric_bry; // set itm to asymmetric_bry; EX: for İ, asymmetric_bry = i
nxts.Add_bry_obj(asymmetric_bry, rv); // add the asymmetric_bry to the hash; in above example, this allows "i" to match "İ"
}
}
return rv;
}
}

View File

@@ -0,0 +1,68 @@
/*
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.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) {
this.case_mgr = case_mgr;
this.root = new Btrie_utf8_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;
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;
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();
if (nxt_val != null) {match_pos = cur_pos; rv = nxt_val;} // nxt is node; cache rv (in case of false match)
if (cur_pos == end_pos) return rv; // increment cur_pos and exit if end
b = src[cur_pos];
cur = nxt;
}
}
public void Clear() {root.Clear(); count = 0;}
public Btrie_mgr Add_obj(String key, Object val) {return Add_obj(Bry_.new_u8(key), val);}
public Btrie_mgr Add_obj(byte[] key, Object val) {
if (val == null) throw Exc_.new_("null objects cannot be registered", "key", String_.new_u8(key));
int key_len = key.length;
Btrie_utf8_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);
if (nxt == null)
nxt = cur.Nxts_add(case_mgr, Bry_.Mid(key, c_bgn, c_end), null);
c_bgn = c_end;
if (c_bgn == key_len)
nxt.Val_set(val);
cur = nxt;
}
++count;
return this;
}
public static Btrie_utf8_mgr new_(Gfo_case_mgr case_mgr) {return new Btrie_utf8_mgr(case_mgr);}
}

View File

@@ -0,0 +1,24 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public interface Criteria extends XtoStrAble {
byte Tid();
boolean Matches(Object obj);
void Val_from_args(Hash_adp args);
void Val_as_obj_(Object obj);
}

View File

@@ -0,0 +1,53 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
import gplx.texts.*; /*RegxPatn_cls_like*/
public class Criteria_ {
public static final Criteria All = new Criteria_const(true);
public static final Criteria None = new Criteria_const(false);
public static Criteria Not(Criteria arg) {return new Criteria_not(arg);}
public static Criteria And(Criteria lhs, Criteria rhs) {return new Criteria_and(lhs, rhs);}
public static Criteria And_many(Criteria... ary) {
int len = Array_.Len(ary); if (len == 0) throw Exc_.new_("cannot AND 0 criterias;");
Criteria rv = ary[0];
for (int i = 1; i < len; i++)
rv = And(rv, ary[i]);
return rv;
}
public static Criteria Or(Criteria lhs, Criteria rhs) {return new Criteria_or(lhs, rhs);}
public static Criteria Or_many(Criteria... ary) {
int len = Array_.Len(ary); if (len == 0) throw Exc_.new_("cannot OR 0 criterias;");
Criteria rv = ary[0];
for (int i = 1; i < len; i++)
rv = Or(rv, ary[i]);
return rv;
}
public static Criteria eq_(Object arg) {return new Criteria_eq(false, arg);}
public static Criteria eqn_(Object arg) {return new Criteria_eq(true, arg);}
public static Criteria in_(Object... array) {return new Criteria_in(false, array);}
public static Criteria inn_(Object... array) {return new Criteria_in(true, array);}
public static Criteria lt_(Comparable val) {return new Criteria_comp(CompareAble_.Less, val);}
public static Criteria lte_(Comparable val) {return new Criteria_comp(CompareAble_.LessOrSame, val);}
public static Criteria mt_(Comparable val) {return new Criteria_comp(CompareAble_.More, val);}
public static Criteria mte_(Comparable val) {return new Criteria_comp(CompareAble_.MoreOrSame, val);}
public static Criteria between_(Comparable lhs, Comparable rhs) {return new Criteria_between(false, lhs, rhs);}
public static Criteria between_(boolean negated, Comparable lhs, Comparable rhs) {return new Criteria_between(negated, lhs, rhs);}
public static Criteria like_(String pattern) {return new Criteria_like(false, RegxPatn_cls_like_.parse_(pattern, RegxPatn_cls_like.EscapeDefault));}
public static Criteria liken_(String pattern) {return new Criteria_like(true, RegxPatn_cls_like_.parse_(pattern, RegxPatn_cls_like.EscapeDefault));}
public static final byte Tid_custom = 0, Tid_const = 1, Tid_not = 2, Tid_and = 3, Tid_or = 4, Tid_eq = 5, Tid_between = 6, Tid_in = 7, Tid_like = 8, Tid_comp = 9, Tid_wrapper = 10, Tid_iomatch = 11, Tid_db_obj_ary = 12;
}

View File

@@ -0,0 +1,40 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public class Criteria_between implements Criteria {
public Criteria_between(boolean negate, Comparable lhs, Comparable rhs) {this.negate = negate; this.lhs = lhs; this.rhs = rhs;}
public byte Tid() {return Criteria_.Tid_between;}
public boolean Negated() {return negate;} private final boolean negate;
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public void Val_as_obj_(Object v) {
Object[] ary = (Object[])v;
lhs = (Comparable)ary[0];
rhs = (Comparable)ary[1];
}
public Comparable Lhs() {return lhs;} private Comparable lhs;
public Comparable Rhs() {return rhs;} private Comparable rhs;
public String XtoStr() {return String_.Concat_any("BETWEEN ", lhs, " AND ", rhs);}
public boolean Matches(Object compObj) {
Comparable comp = CompareAble_.as_(compObj);
int lhsResult = CompareAble_.CompareComparables(lhs, comp);
int rhsResult = CompareAble_.CompareComparables(rhs, comp);
boolean rv = (lhsResult * rhsResult) != 1;
return negate ? !rv : rv;
}
public static Criteria_between as_(Object obj) {return obj instanceof Criteria_between ? (Criteria_between)obj : null;}
}

View 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.core.criterias; import gplx.*; import gplx.core.*;
public abstract class Criteria_bool_base implements Criteria {
@gplx.Internal protected void Ctor(String op_literal, Criteria lhs, Criteria rhs) {this.op_literal = op_literal; this.lhs = lhs; this.rhs = rhs;}
public abstract byte Tid();
public abstract boolean Matches(Object curVal);
public void Val_from_args(Hash_adp args) {lhs.Val_from_args(args); rhs.Val_from_args(args);}
public void Val_as_obj_(Object v) {throw Exc_.new_unimplemented();}
public String XtoStr() {return String_.Concat(lhs.XtoStr(), " ", this.op_literal, " ", rhs.XtoStr());}
public String Op_literal() {return op_literal;} private String op_literal;
public Criteria Lhs() {return lhs;} private Criteria lhs;
public Criteria Rhs() {return rhs;} private Criteria rhs;
public static Criteria_bool_base as_(Object obj) {return obj instanceof Criteria_bool_base ? (Criteria_bool_base)obj : null;}
}
class Criteria_and extends Criteria_bool_base {
public Criteria_and(Criteria lhs, Criteria rhs) {this.Ctor("AND", lhs, rhs);}
@Override public byte Tid() {return Criteria_.Tid_not;}
@Override public boolean Matches(Object curVal) {return this.Lhs().Matches(curVal) && this.Rhs().Matches(curVal);}
}
class Criteria_or extends Criteria_bool_base {
public Criteria_or(Criteria lhs, Criteria rhs) {this.Ctor("OR", lhs, rhs);}
@Override public byte Tid() {return Criteria_.Tid_or;}
@Override public boolean Matches(Object curVal) {return this.Lhs().Matches(curVal) || this.Rhs().Matches(curVal);}
}
class Criteria_const implements Criteria {
public Criteria_const(boolean val) {this.val = val;}
public byte Tid() {return Criteria_.Tid_const;}
public boolean Matches(Object comp) {return val;} private final boolean val;
public void Val_from_args(Hash_adp args) {;}
public void Val_as_obj_(Object v) {throw Exc_.new_unimplemented();}
public String XtoStr() {return String_.Concat(" IS ", Bool_.Xto_str_lower(val));}
}
class Criteria_not implements Criteria {
private final Criteria criteria;
public Criteria_not(Criteria v) {this.criteria = v;}
public byte Tid() {return Criteria_.Tid_not;}
public boolean Matches(Object obj) {return !criteria.Matches(obj);}
public void Val_from_args(Hash_adp args) {criteria.Val_from_args(args);}
public void Val_as_obj_(Object v) {criteria.Val_as_obj_(v);}
public String XtoStr() {return String_.Concat_any(" NOT ", criteria.XtoStr());}
}

View File

@@ -0,0 +1,37 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public class Criteria_comp implements Criteria {
private final int comp_mode;
@gplx.Internal protected Criteria_comp(int comp_mode, Comparable val) {this.comp_mode = comp_mode; this.val = val;}
public byte Tid() {return Criteria_.Tid_comp;}
public Comparable Val() {return val;} private Comparable val;
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public void Val_as_obj_(Object v) {val = (Comparable)v;}
public boolean Matches(Object compObj) {
Comparable comp = CompareAble_.as_(compObj);
return CompareAble_.Is(comp_mode, comp, val);
}
public String XtoStr() {return String_.Concat_any(XtoSymbol(), " ", val);}
public String XtoSymbol() {
String comp_sym = comp_mode < CompareAble_.Same ? "<" : ">";
String eq_sym = comp_mode % 2 == CompareAble_.Same ? "=" : "";
return comp_sym + eq_sym;
}
public static Criteria_comp as_(Object obj) {return obj instanceof Criteria_comp ? (Criteria_comp)obj : null;}
}

View File

@@ -0,0 +1,34 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public class Criteria_eq implements Criteria {
@gplx.Internal protected Criteria_eq(boolean negated, Object val) {this.negated = negated; this.val = val;}
public byte Tid() {return Criteria_.Tid_eq;}
public boolean Negated() {return negated;} private final boolean negated;
public Object Val() {return val;} private Object val;
public void Val_as_obj_(Object v) {this.val = v;}
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public boolean Matches(Object comp) {
Class<?> val_type = ClassAdp_.ClassOf_obj(val);
if (!ClassAdp_.Eq_typeSafe(comp, val_type)) throw Exc_.new_type_mismatch(val_type, comp);
boolean rv = Object_.Eq(val, comp);
return negated ? !rv : rv;
}
public String XtoStr() {return String_.Concat_any("= ", val);}
public static Criteria_eq as_(Object obj) {return obj instanceof Criteria_eq ? (Criteria_eq)obj : null;}
}

View File

@@ -0,0 +1,67 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public class Criteria_fld implements Criteria {
Criteria_fld(String key, Criteria crt) {this.key = key; this.crt = crt;}
public byte Tid() {return Criteria_.Tid_wrapper;}
public String Key() {return key;} private final String key;
public Criteria Crt() {return crt;} private final Criteria crt;
public void Val_as_obj_(Object v) {throw Exc_.new_unimplemented();}
public void Val_from_args(Hash_adp args) {
List_adp list = (List_adp)args.Get_by(key); if (list == null) throw Exc_.new_("criteria.fld key not found", "key", key);
Object o = Fill_val(key, crt.Tid(), list);
crt.Val_as_obj_(o);
}
public boolean Matches(Object invkObj) {
GfoInvkAble invk = (GfoInvkAble)invkObj;
if (key == Criteria_fld.Key_null) return crt.Matches(invkObj);
Object comp = GfoInvkAble_.InvkCmd(invk, key);
return crt.Matches(comp);
}
public String XtoStr() {return String_.Concat(key, " ", crt.XtoStr());}
public static final String Key_null = null;
public static Criteria_fld as_(Object obj) {return obj instanceof Criteria_fld ? (Criteria_fld)obj : null;}
public static Criteria_fld new_(String key, Criteria crt) {return new Criteria_fld(key, crt);}
public static Object Fill_val(String key, byte tid, List_adp list) {
int len = list.Count();
switch (tid) {
case Criteria_.Tid_eq:
case Criteria_.Tid_comp:
case Criteria_.Tid_like:
case Criteria_.Tid_iomatch:
if (len != 1) throw Exc_.new_("list.len should be 1", "key", key, "tid", tid, "len", len);
return list.Get_at(0);
case Criteria_.Tid_between:
if (len != 2) throw Exc_.new_("list.len should be 2", "key", key, "tid", tid, "len", len);
return new Object[] {list.Get_at(0), list.Get_at(1)};
case Criteria_.Tid_in:
if (len == 0) throw Exc_.new_("list.len should be > 0", "key", key, "tid", tid, "len", len);
return list.To_obj_ary();
case Criteria_.Tid_const:
case Criteria_.Tid_not:
case Criteria_.Tid_and:
case Criteria_.Tid_or:
if (len != 0) throw Exc_.new_("list.len should be 0", "key", key, "tid", tid, "len", len);
return key; // no values to fill in; return back key
case Criteria_.Tid_wrapper: // not recursive
case Criteria_.Tid_db_obj_ary: // unsupported
case Criteria_.Tid_custom:
default: throw Exc_.new_unhandled(tid);
}
}
}

View File

@@ -0,0 +1,46 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
public class Criteria_in implements Criteria {
public Criteria_in(boolean negated, Object[] ary) {this.negated = negated; Val_as_obj_ary_(ary);}
public byte Tid() {return Criteria_.Tid_in;}
public boolean Negated() {return negated;} private final boolean negated;
public Object[] Val_as_obj_ary() {return ary;} private Object[] ary; private Class<?> ary_type; private int ary_len;
private void Val_as_obj_ary_(Object[] v) {
this.ary = v;
ary_len = Array_.Len(ary);
ary_type = ary_len == 0 ? Object.class : ClassAdp_.ClassOf_obj(ary[0]);
}
public void Val_as_obj_(Object v) {Val_as_obj_ary_((Object[])v);}
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public boolean Matches(Object comp) {
if (ary_len == 0) return false; // empty array never matches
if (!ClassAdp_.Eq_typeSafe(comp, ary_type)) throw Exc_.new_type_mismatch(ary_type, comp);
boolean rv = false;
for (int i = 0; i < ary_len; i++) {
Object val = ary[i];
if (Object_.Eq(val, comp)) {
rv = true;
break;
}
}
return negated ? !rv : rv;
}
public String XtoStr() {return String_.Concat_any("IN ", String_.Concat_any(ary));}
public static Criteria_in as_(Object obj) {return obj instanceof Criteria_in ? (Criteria_in)obj : null;}
}

View File

@@ -0,0 +1,50 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
import org.junit.*;
import gplx.ios.*;
public class Criteria_ioItm_tst {
IoItmFil fil; Criteria crt; IoItm_fxt fx = IoItm_fxt.new_();
@Test public void IoType() {
crt = crt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmFil.Type_Fil));
tst_Match(true, crt, fx.fil_wnt_("C:\\fil.txt"));
tst_Match(false, crt, fx.dir_wnt_("C:\\dir"));
}
@Test public void Ext() {
crt = crt_(IoItm_base_.Prop_Ext, Criteria_.eq_(".txt"));
tst_Match(true, crt, fx.fil_wnt_("C:\\fil.txt"));
tst_Match(false, crt, fx.fil_wnt_("C:\\fil.xml"), fx.fil_wnt_("C:\\fil.txt1"), fx.fil_wnt_("C:\\fil1.txt.xml"), fx.dir_wnt_("C:\\.txt"));
}
@Test public void Modified() {
fil = fx.fil_wnt_("C:\\fil.txt");
crt = crt_(IoItmFil_.Prop_Modified, Criteria_.mte_(DateAdp_.parse_gplx("2001-01-01")));
tst_Match(true, crt, fil.ModifiedTime_(DateAdp_.parse_gplx("2001-01-02")), fil.ModifiedTime_(DateAdp_.parse_gplx("2001-01-01")));
tst_Match(false, crt, fil.ModifiedTime_(DateAdp_.parse_gplx("2000-12-31")));
}
@Test public void IoMatch() {
Criteria crt = Criteria_ioMatch.parse_(true, "*.txt", false);
CriteriaFxt fx_crt = new CriteriaFxt();
fx_crt.tst_Matches(crt, Io_url_.new_any_("file.txt"));
fx_crt.tst_MatchesNot(crt, Io_url_.new_any_("file.xml"));
}
Criteria crt_(String fld, Criteria crt) {return Criteria_fld.new_(fld, crt);}
void tst_Match(boolean expt, Criteria fieldCrt, IoItm_base... ary) {
for (IoItm_base itm : ary)
Tfds.Eq(expt, fieldCrt.Matches(itm));
}
}

View File

@@ -0,0 +1,37 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
import gplx.texts.*;
public class Criteria_ioMatch implements Criteria { // EX: url IOMATCH '*.xml|*.txt'
public Criteria_ioMatch(boolean match, RegxPatn_cls_ioMatch pattern) {this.match = match; this.pattern = pattern;}
public byte Tid() {return Criteria_.Tid_iomatch;}
public boolean Negated() {return !match;} private final boolean match;
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public void Val_as_obj_(Object v) {this.pattern = (RegxPatn_cls_ioMatch)v;}
public RegxPatn_cls_ioMatch Pattern() {return pattern;} private RegxPatn_cls_ioMatch pattern;
public boolean Matches(Object compObj) {
Io_url comp = (Io_url)compObj;
boolean rv = pattern.Matches(comp.XtoCaseNormalized());
return match ? rv : !rv;
}
public String XtoStr() {return String_.Concat_any("IOMATCH ", pattern);}
public static final String TokenName = "IOMATCH";
public static Criteria_ioMatch as_(Object obj) {return obj instanceof Criteria_ioMatch ? (Criteria_ioMatch)obj : null;}
public static Criteria_ioMatch parse_(boolean match, String raw, boolean caseSensitive) {return new Criteria_ioMatch(match, RegxPatn_cls_ioMatch_.parse_(raw, caseSensitive));}
}

View File

@@ -0,0 +1,36 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
import gplx.texts.*; /*RegxPatn_cls_like*/
public class Criteria_like implements Criteria {
@gplx.Internal protected Criteria_like(boolean negated, RegxPatn_cls_like pattern) {
this.negated = negated; this.pattern = pattern;
}
public byte Tid() {return Criteria_.Tid_like;}
public boolean Negated() {return negated;} private final boolean negated;
public RegxPatn_cls_like Pattern() {return pattern;} private RegxPatn_cls_like pattern;
public void Val_from_args(Hash_adp args) {throw Exc_.new_unimplemented();}
public void Val_as_obj_(Object v) {this.pattern = (RegxPatn_cls_like)v;}
public boolean Matches(Object compObj) {
String comp = String_.as_(compObj); if (comp == null) throw Exc_.new_type_mismatch(String.class, compObj);
boolean rv = pattern.Matches(comp);
return negated ? !rv : rv;
}
public String XtoStr() {return String_.Concat_any("LIKE ", pattern);}
public static Criteria_like as_(Object obj) {return obj instanceof Criteria_like ? (Criteria_like)obj : null;}
}

View File

@@ -0,0 +1,92 @@
/*
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.core.criterias; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Criteria_tst {
@Test public void Equal() {
Criteria crt = Criteria_.eq_(true);
fx.tst_Matches(crt, true);
fx.tst_MatchesNot(crt, false);
fx.tst_MatchesFail(crt, "true");
fx.tst_Matches(Criteria_.eq_(1), 1);
fx.tst_Matches(Criteria_.eq_("equal"), "equal");
fx.tst_Matches(Criteria_.eq_(date), date);
}
@Test public void Not() {
Criteria crt = Criteria_.eqn_(true);
fx.tst_Matches(crt, false);
fx.tst_MatchesNot(crt, true);
fx.tst_MatchesFail(crt, "false");
fx.tst_Matches(Criteria_.eqn_(1), -1);
fx.tst_Matches(Criteria_.eqn_("equal"), "not equal");
fx.tst_Matches(Criteria_.eqn_(date), date.Add_minute(1));
}
@Test public void MoreThan() {
Criteria crt = Criteria_.mt_(0);
fx.tst_Matches(crt, 1, 2);
fx.tst_MatchesNot(crt, 0, -1);
fx.tst_MatchesFail(crt, "1");
fx.tst_Matches(Criteria_.mt_(0), 1);
fx.tst_Matches(Criteria_.mt_("a"), "b");
fx.tst_Matches(Criteria_.mt_(date), date.Add_minute(1));
fx.tst_Matches(Criteria_.mt_(false), true); // MISC: thus truth is greater than falsehood
}
@Test public void MoreThanEq() {
Criteria crt = Criteria_.mte_(0);
fx.tst_Matches(crt, 0);
}
@Test public void Less() {
Criteria crt = Criteria_.lt_(0);
fx.tst_Matches(crt, -1, -2);
fx.tst_MatchesNot(crt, 0, 1);
fx.tst_MatchesFail(crt, "-1");
}
@Test public void LessEq() {
Criteria crt = Criteria_.lte_(0);
fx.tst_Matches(crt, 0);
}
@Test public void Between() {
Criteria crt = Criteria_.between_(-1, 1);
fx.tst_Matches(crt, 0, 1, -1);
fx.tst_MatchesNot(crt, -2, 2);
fx.tst_MatchesFail(crt, "0");
fx.tst_Matches(Criteria_.between_(1, -1), 0); // reverse range
fx.tst_Matches(Criteria_.between_("a", "c"), "b");
}
@Test public void In() {
Criteria crt = Criteria_.in_(0, 1, 2);
fx.tst_Matches(crt, 0, 1, 2);
fx.tst_MatchesNot(crt, 3, -1);
fx.tst_MatchesFail(crt, "0");
}
CriteriaFxt fx = new CriteriaFxt();
DateAdp date = DateAdp_.parse_gplx("2001-01-01");
}
class CriteriaFxt {
public void tst_Matches(Criteria crt, Object... ary) {for (Object val : ary) Tfds.Eq(true, crt.Matches(val));}
public void tst_MatchesNot(Criteria crt, Object... ary) {for (Object val : ary) Tfds.Eq(false, crt.Matches(val));}
public void tst_MatchesFail(Criteria crt, Object val) {
try {crt.Matches(val);}
catch(Exception exc) {Exc_.Noop(exc); return;}
Tfds.Fail_expdError();
}
}

View File

@@ -0,0 +1,107 @@
/*
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.core.js; import gplx.*; import gplx.core.*;
public class Js_wtr {
private final Bry_bfr bfr = Bry_bfr.reset_(255);
private int arg_idx = 0, ary_idx = 0;
public byte Quote_char() {return quote_char;} public Js_wtr Quote_char_(byte v) {quote_char = v; return this;} private byte quote_char = Byte_ascii.Quote;
public void Clear() {bfr.Clear();}
public String To_str() {return bfr.Xto_str();}
public String To_str_and_clear() {return bfr.Xto_str_and_clear();}
public Js_wtr Func_init(String name) {return Func_init(Bry_.new_u8(name));}
public Js_wtr Func_init(byte[] name) {
bfr.Add(name).Add_byte(Byte_ascii.Paren_bgn);
arg_idx = 0;
return this;
}
public Js_wtr Func_term() {
bfr.Add_byte(Byte_ascii.Paren_end).Add_byte_semic();
return this;
}
public Js_wtr Prm_bry(byte[] bry) {
Prm_spr();
Write_val(bry);
return this;
}
public Js_wtr Prm_obj_ary(Object[] ary) {
int ary_len = ary.length;
for (int i = 0; i < ary_len; ++i) {
Object itm = ary[i];
if (i != 0) bfr.Add_byte(Byte_ascii.Comma);
boolean val_needs_quotes = true;
if ( ClassAdp_.Eq_typeSafe(itm, Bool_.Cls_ref_type)
|| ClassAdp_.Eq_typeSafe(itm, Int_.Cls_ref_type)
|| ClassAdp_.Eq_typeSafe(itm, Long_.Cls_ref_type)
) {
val_needs_quotes = false;
}
if (val_needs_quotes)
Write_val(Bry_.new_u8(Object_.Xto_str_strict_or_null_mark(itm)));
else
bfr.Add_obj_strict(itm);
}
return this;
}
public Js_wtr Ary_init() {
ary_idx = 0;
bfr.Add_byte(Byte_ascii.Brack_bgn);
return this;
}
public Js_wtr Ary_term() {
bfr.Add_byte(Byte_ascii.Brack_end);
return this;
}
public void Prm_spr() {
if (arg_idx != 0) bfr.Add_byte(Byte_ascii.Comma);
++arg_idx;
}
private void Ary_spr() {
if (ary_idx != 0) bfr.Add_byte(Byte_ascii.Comma);
++ary_idx;
}
public Js_wtr Ary_bry(byte[] bry) {
Ary_spr();
Write_val(bry);
return this;
}
private Js_wtr Write_keyword_return() {bfr.Add(Keyword_return); return this;}
public Js_wtr Write_statement_return_func(String func, Object... args) {
this.Write_keyword_return();
this.Func_init(func);
this.Prm_obj_ary(args);
this.Func_term();
return this;
}
public void Write_val(byte[] bry) {
bfr.Add_byte(quote_char);
int len = bry.length;
for (int i = 0; i < len; i++) {
byte b = bry[i];
switch (b) {
case Byte_ascii.Backslash: // "\" -> "\\"; needed else js will usurp \ as escape; EX: "\&" -> "&"; DATE:2014-06-24
case Byte_ascii.Quote:
case Byte_ascii.Apos: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(b); break;
case Byte_ascii.Nl: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(Byte_ascii.Ltr_n); break; // "\n" -> "\\n"
case Byte_ascii.Cr: break;// skip
default: bfr.Add_byte(b); break;
}
}
bfr.Add_byte(quote_char);
}
private static final byte[] Keyword_return = Bry_.new_a7("return ");
}

View File

@@ -0,0 +1,41 @@
/*
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.core.js; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Js_wtr_tst {
@Before public void Init() {fxt.Clear();} private Js_wtr_fxt fxt = new Js_wtr_fxt();
@Test public void Basic() {
fxt.Test_write_val_html("abc" , "'abc'");
fxt.Test_write_val_html("a'b" , "'a\\'b'");
fxt.Test_write_val_html("a\"b" , "'a\\\"b'");
fxt.Test_write_val_html("a\nb" , "'a\\nb'");
fxt.Test_write_val_html("a\rb" , "'ab'");
fxt.Test_write_val_html("a\\&b" , "'a\\\\&b'"); // PURPOSE: backslashes need to be escaped; need for MathJax and "\&"; PAGE:Electromagnetic_field_tensor; DATE:2014-06-24
}
}
class Js_wtr_fxt {
private Js_wtr wtr = new Js_wtr();
public void Clear() {
wtr.Clear();
wtr.Quote_char_(Byte_ascii.Apos);
}
public void Test_write_val_html(String raw, String expd) {
wtr.Write_val(Bry_.new_u8(raw));
Tfds.Eq(expd, wtr.To_str_and_clear());
}
}

View File

@@ -0,0 +1,36 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Bool_obj_ref {
public boolean Val() {return val;} private boolean val;
public boolean Val_y() {return val;}
public boolean Val_n() {return !val;}
public String Val_as_str_yn() {return Yn.Xto_str(val);}
public Bool_obj_ref Val_y_() {val = true; return this;}
public Bool_obj_ref Val_n_() {val = false; return this;}
public Bool_obj_ref Val_(boolean v) {val = v; return this;}
public Bool_obj_ref Val_toggle_() {val = !val; return this;}
@Override public String toString() {return Bool_.Xto_str_lower(val);}
public static Bool_obj_ref n_() {return new_(false);}
public static Bool_obj_ref y_() {return new_(true);}
public static Bool_obj_ref new_(boolean val) {
Bool_obj_ref rv = new Bool_obj_ref();
rv.val = val;
return rv;
} Bool_obj_ref() {}
}

View File

@@ -0,0 +1,34 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Bool_obj_val {
Bool_obj_val(int v) {val = v;} private final int val;
public boolean Val() {return val == 1;}
public static final Bool_obj_val
Null = new Bool_obj_val(-1)
, False = new Bool_obj_val(0)
, True = new Bool_obj_val(1)
;
public static Bool_obj_val read_(Object o) {String s = String_.as_(o); return s == null ? (Bool_obj_val)o : parse_(s);}
public static Bool_obj_val parse_(String raw) {
if (String_.Eq(raw, "y")) return Bool_obj_val.True;
else if (String_.Eq(raw, "n")) return Bool_obj_val.False;
else if (String_.Eq(raw, "")) return Bool_obj_val.Null;
else throw Exc_.new_parse_type(Bool_obj_val.class, raw);
}
}

View File

@@ -0,0 +1,35 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Bry_obj_ref {
public byte[] Val() {return val;} public Bry_obj_ref Val_(byte[] v) {val = v; return this;} private byte[] val;
@Override public int hashCode() {return CalcHashCode(val, 0, val.length);}
@Override public boolean equals(Object obj) {return obj == null ? false : Bry_.Eq(val, ((Bry_obj_ref)obj).Val());} // NOTE: strange, but null check needed; throws null error; EX.WP: File:Eug<75>ne Delacroix - La libert<72> guidant le peuple.jpg
public static int CalcHashCode(byte[] ary, int bgn, int end) {
int rv = 0;
for (int i = bgn; i < end; i++)
rv = (31 * rv) + ary[i];
return rv;
}
public static Bry_obj_ref null_() {return new_(null);}
public static Bry_obj_ref new_(byte[] val) {
Bry_obj_ref rv = new Bry_obj_ref();
rv.val = val;
return rv;
} private Bry_obj_ref() {}
}

View File

@@ -0,0 +1,31 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Byte_obj_ref {
public byte Val() {return val;} private byte val;
public Byte_obj_ref Val_(byte v) {val = v; return this;}
@Override public int hashCode() {return val;}
@Override public boolean equals(Object obj) {return obj == null ? false : val == ((Byte_obj_ref)obj).Val();}
@Override public String toString() {return Int_.Xto_str(val);}
public static Byte_obj_ref zero_() {return new_(Byte_.Zero);}
public static Byte_obj_ref new_(byte val) {
Byte_obj_ref rv = new Byte_obj_ref();
rv.val = val;
return rv;
} private Byte_obj_ref() {}
}

View File

@@ -0,0 +1,29 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Byte_obj_val {
public byte Val() {return val;} private byte val;
@Override public String toString() {return Int_.Xto_str(val);}
@Override public int hashCode() {return val;}
@Override public boolean equals(Object obj) {return obj == null ? false : val == ((Byte_obj_val)obj).Val();}
public static Byte_obj_val new_(byte val) {
Byte_obj_val rv = new Byte_obj_val();
rv.val = val;
return rv;
} private Byte_obj_val() {}
}

View File

@@ -0,0 +1,32 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Double_obj_val implements CompareAble {
public double Val() {return val;} double val;
@Override public String toString() {return Double_.Xto_str(val);}
@Override public int hashCode() {return (int)val;}
@Override public boolean equals(Object obj) {return obj == null ? false : val == ((Double_obj_val)obj).Val();}
public int compareTo(Object obj) {Double_obj_val comp = (Double_obj_val)obj; return Double_.Compare(val, comp.val);}
public static Double_obj_val neg1_() {return new_(-1);}
public static Double_obj_val zero_() {return new_(0);}
public static Double_obj_val new_(double val) {
Double_obj_val rv = new Double_obj_val();
rv.val = val;
return rv;
} Double_obj_val() {}
}

View File

@@ -0,0 +1,45 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Int_obj_ref {
public int Val() {return val;} public Int_obj_ref Val_(int v) {val = v; return this;} int val;
public int Val_add() {val++; return val;}
public int Val_add_post() {return val++;}
public int Val_add(int v) {val += v; return val;}
public Int_obj_ref Val_zero_() {val = 0; return this;}
public Int_obj_ref Val_neg1_() {val = -1; return this;}
public String Val_as_str() {return Int_.Xto_str(val);}
@Override public String toString() {return Int_.Xto_str(val);}
@Override public int hashCode() {return val;}
@Override public boolean equals(Object obj) {return val == ((Int_obj_ref)obj).Val();}
public static Int_obj_ref neg1_() {return new_(-1);}
public static Int_obj_ref zero_() {return new_(0);}
public static Int_obj_ref new_(int val) {
Int_obj_ref rv = new Int_obj_ref();
rv.val = val;
return rv;
} Int_obj_ref() {}
public static int[] Ary_xto_int_ary(Int_obj_ref[] ary) {
int len = ary.length;
int[] rv = new int[len];
for (int i = 0; i < len; ++i)
rv[i] = ary[i].val;
return rv;
}
}

View File

@@ -0,0 +1,32 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class Int_obj_val implements CompareAble {
public int Val() {return val;} int val;
@Override public String toString() {return Int_.Xto_str(val);}
@Override public int hashCode() {return val;}
@Override public boolean equals(Object obj) {return obj == null ? false : val == ((Int_obj_val)obj).Val();}
public int compareTo(Object obj) {Int_obj_val comp = (Int_obj_val)obj; return Int_.Compare(val, comp.val);}
public static Int_obj_val neg1_() {return new_(-1);}
public static Int_obj_val zero_() {return new_(0);}
public static Int_obj_val new_(int val) {
Int_obj_val rv = new Int_obj_val();
rv.val = val;
return rv;
} Int_obj_val() {}
}

View File

@@ -0,0 +1,30 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class String_obj_ref {
public String Val() {return val;} public String_obj_ref Val_(String v) {val = v; return this;} private String val;
public String_obj_ref Val_null_() {return Val_(null);}
@Override public String toString() {return val;}
public static String_obj_ref empty_() {return new_("");}
public static String_obj_ref null_() {return new_(null);}
public static String_obj_ref new_(String val) {
String_obj_ref rv = new String_obj_ref();
rv.val = val;
return rv;
} String_obj_ref() {}
}

View File

@@ -0,0 +1,27 @@
/*
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.core.primitives; import gplx.*; import gplx.core.*;
public class String_obj_val implements CompareAble {
public String Val() {return val;} private final String val;
@Override public String toString() {return val;}
public int compareTo(Object obj) {
String_obj_val comp = (String_obj_val)obj;
return String_.Compare_strict(val, comp.val);
}
public static String_obj_val new_(String val) {return new String_obj_val(val);} String_obj_val(String val) {this.val = val;}
}

View File

@@ -0,0 +1,115 @@
/*
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.core.strings; import gplx.*; import gplx.core.*;
public interface String_bldr {
boolean Has_none();
boolean Has_some();
String_bldr Add_many(String... array);
String_bldr Add_fmt(String format, Object... args);
String_bldr Add_fmt_line(String format, Object... args);
String_bldr Add_kv(String hdr, String val);
String_bldr Add_kv_obj(String k, Object v);
String_bldr Add_char_pipe();
String_bldr Add_char_nl();
String_bldr Add_char_crlf();
String_bldr Add_str_w_crlf(String v);
String_bldr Add_spr_unless_first(String s, String spr, int i);
String_bldr Clear();
String Xto_str_and_clear();
String XtoStr();
int Count();
String_bldr Add(byte[] v);
String_bldr Add(String s);
String_bldr Add(char c);
String_bldr Add(int i);
String_bldr Add_obj(Object o);
String_bldr Add_mid(char[] ary, int bgn, int count);
String_bldr Add_at(int idx, String s);
String_bldr Del(int bgn, int len);
}
abstract class String_bldr_base implements String_bldr {
public boolean Has_none() {return this.Count() == 0;}
public boolean Has_some() {return this.Count() > 0;}
public String_bldr Add_many(String... array) {for (String s : array) Add(s); return this;}
public String_bldr Add_fmt(String format, Object... args) {Add(String_.Format(format, args)); return this;}
public String_bldr Add_fmt_line(String format, Object... args) {Add_str_w_crlf(String_.Format(format, args)); return this;}
public String_bldr Add_kv_obj(String k, Object v) {
if (this.Count() != 0) this.Add(" ");
this.Add_fmt("{0}={1}", k, Object_.Xto_str_strict_or_null_mark(v));
return this;
}
public String_bldr Add_char_pipe() {return Add("|");}
public String_bldr Add_char_nl() {Add(Op_sys.Lnx.Nl_str()); return this;}
public String_bldr Add_char_crlf() {Add(Op_sys.Wnt.Nl_str()); return this;}
public String_bldr Add_str_w_crlf(String line) {Add(line); Add(String_.CrLf); return this;}
public String_bldr Add_spr_unless_first(String s, String spr, int i) {
if (i != 0) Add(spr);
Add(s);
return this;
}
public String_bldr Add_kv(String hdr, String val) {
if (String_.Len_eq_0(val)) return this;
if (this.Count() != 0) this.Add(' ');
this.Add(hdr);
this.Add(val);
return this;
}
public String_bldr Clear() {Del(0, Count()); return this;}
public String Xto_str_and_clear() {
String rv = XtoStr();
Clear();
return rv;
}
@Override public String toString() {return XtoStr();}
public abstract String XtoStr();
public abstract int Count();
public abstract String_bldr Add_at(int idx, String s);
public abstract String_bldr Add(byte[] v);
public abstract String_bldr Add(String s);
public abstract String_bldr Add(char c);
public abstract String_bldr Add(int i);
public abstract String_bldr Add_mid(char[] ary, int bgn, int count);
public abstract String_bldr Add_obj(Object o);
public abstract String_bldr Del(int bgn, int len);
}
class String_bldr_thread_single extends String_bldr_base {
private java.lang.StringBuilder sb = new java.lang.StringBuilder();
@Override public String XtoStr() {return sb.toString();}
@Override public int Count() {return sb.length();}
@Override public String_bldr Add_at(int idx, String s) {sb.insert(idx, s); return this;}
@Override public String_bldr Add(byte[] v) {sb.append(String_.new_u8(v)); return this;}
@Override public String_bldr Add(String s) {sb.append(s); return this;}
@Override public String_bldr Add(char c) {sb.append(c); return this;}
@Override public String_bldr Add(int i) {sb.append(i); return this;}
@Override public String_bldr Add_mid(char[] ary, int bgn, int count) {sb.append(ary, bgn, count); return this;}
@Override public String_bldr Add_obj(Object o) {sb.append(o); return this;}
@Override public String_bldr Del(int bgn, int len) {sb.delete(bgn, len); return this;}
}
class String_bldr_thread_multiple extends String_bldr_base {
private java.lang.StringBuffer sb = new java.lang.StringBuffer();
@Override public String XtoStr() {return sb.toString();}
@Override public int Count() {return sb.length();}
@Override public String_bldr Add_at(int idx, String s) {sb.insert(idx, s); return this;}
@Override public String_bldr Add(byte[] v) {sb.append(String_.new_u8(v)); return this;}
@Override public String_bldr Add(String s) {sb.append(s); return this;}
@Override public String_bldr Add(char c) {sb.append(c); return this;}
@Override public String_bldr Add(int i) {sb.append(i); return this;}
@Override public String_bldr Add_mid(char[] ary, int bgn, int count) {sb.append(ary, bgn, count); return this;}
@Override public String_bldr Add_obj(Object o) {sb.append(o); return this;}
@Override public String_bldr Del(int bgn, int len) {sb.delete(bgn, len); return this;}
}

View File

@@ -0,0 +1,22 @@
/*
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.core.strings; import gplx.*; import gplx.core.*;
public class String_bldr_ {
public static String_bldr new_() {return new String_bldr_thread_single();}
public static String_bldr new_thread() {return new String_bldr_thread_multiple();}
}

View File

@@ -0,0 +1,61 @@
/*
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.core.strings; import gplx.*; import gplx.core.*;
public class String_ring {
String[] ary = String_.Ary_empty;
public int Len() {return len;} int len;
public String_ring Max_(int v) {
if (v != max) {
ary = new String[v];
max = v;
}
return this;
} int max;
public void Clear() {
for (int i = 0; i < max; i++) {
ary[i] = null;
}
len = nxt = 0;
}
int nxt;
public void Push(String v) {
int idx = nxt++;
if (idx == max) {
idx = 0;
}
if (nxt == max) {
nxt = 0;
}
ary[idx] = v;
if (len < max)
++len;
}
public String[] Xto_str_ary() {
String[] rv = new String[len];
int ary_i = nxt - 1;
for (int rv_i = len - 1; rv_i > -1; rv_i--) {
if (ary_i == -1) {
ary_i = max - 1;
}
rv[rv_i] = ary[ary_i];
--ary_i;
}
return rv;
}
// public String Get_at(int i) {return }
}

View File

@@ -0,0 +1,46 @@
/*
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.core.strings; import gplx.*; import gplx.core.*;
import org.junit.*;
public class String_ring_tst {
String_ring_fxt fxt = new String_ring_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Basic() {
fxt.Clear().Max_(3).Push_many("a") .Expd("a");
fxt.Clear().Max_(3).Push_many("a", "b") .Expd("a", "b");
fxt.Clear().Max_(3).Push_many("a", "b", "c") .Expd("a", "b", "c");
fxt.Clear().Max_(3).Push_many("a", "b", "c", "d") .Expd("b", "c", "d");
fxt.Clear().Max_(3).Push_many("a", "b", "c", "d", "e") .Expd("c", "d", "e");
fxt.Clear().Max_(3).Push_many("a", "b", "c", "d", "e", "f") .Expd("d", "e", "f");
}
}
class String_ring_fxt {
String_ring ring = new String_ring();
public String_ring_fxt Clear() {ring.Clear(); return this;}
public String_ring_fxt Max_(int v) {ring.Max_(v); return this;}
public String_ring_fxt Push_many(String... ary) {
int ary_len = ary.length;
for (int i = 0; i < ary_len; i++)
ring.Push(ary[i]);
return this;
}
public String_ring_fxt Expd(String... expd) {
Tfds.Eq_ary_str(expd, ring.Xto_str_ary());
return this;
}
}

View File

@@ -0,0 +1,28 @@
/*
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.core.threads; import gplx.*; import gplx.core.*;
import java.util.concurrent.locks.*;
public class Gfo_lock {
private final ReentrantLock lock = new ReentrantLock(true);
public void Lock() {
lock.lock();
}
public void Unlock() {
lock.unlock();
}
}

View File

@@ -0,0 +1,45 @@
/*
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.core.threads; import gplx.*; import gplx.core.*;
import java.lang.*;
public class Thread_adp implements Runnable {
private String name; private GfoInvkAble invk; private String cmd; private GfoMsg msg;
@gplx.Internal protected Thread_adp(String name, GfoInvkAble invk, String cmd, GfoMsg msg) {
this.name = name; this.invk = invk; this.cmd = cmd; this.msg = msg;
this.ctor_ThreadAdp();
}
void ctor_ThreadAdp() {
this.thread = name == null ? new Thread(this) : new Thread(this, name);
}
public Thread Under_thread() {return thread;} private Thread thread;
public Thread_adp Start() {
thread.start();
return this;
}
public void Interrupt() {thread.interrupt();}
public void Join() {
try {thread.join();}
catch (Exception e) {Exc_.Noop(e);}
}
// public void Stop() {thread.stop();}
public boolean IsAlive() {return thread.isAlive();}
@Override public void run() {
invk.Invk(GfsCtx._, 0, cmd, msg);
}
public static final Thread_adp Null = new Thread_adp(Thread_adp_.Name_null, GfoInvkAble_.Null, "", GfoMsg_.Null);
}

View File

@@ -0,0 +1,36 @@
/*
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.core.threads; import gplx.*; import gplx.core.*;
public class Thread_adp_ {
public static void Sleep(int milliseconds) {
try {Thread.sleep(milliseconds);} catch (InterruptedException e) {throw Exc_.new_exc(e, "core", "thread interrupted", "milliseconds", milliseconds);}
}
public static void Notify_all(Object o) {o.notifyAll();}
public static void Wait(Object o) {
try {o.wait();}
catch (InterruptedException e) {throw Exc_.new_exc(e, "core", "thread wait");}
}
public static Thread_adp invk_(GfoInvkAble invk, String cmd) {return invk_(Name_null, invk, cmd);}
public static Thread_adp invk_(String name, GfoInvkAble invk, String cmd) {return new Thread_adp(name, invk, cmd, GfoMsg_.Null);}
public static Thread_adp invk_msg_(GfoInvkAble invk, GfoMsg msg) {return invk_msg_(Name_null, invk, msg);}
public static Thread_adp invk_msg_(String name, GfoInvkAble invk, GfoMsg msg) {return new Thread_adp(name, invk, msg.Key(), msg);}
public static void Run_invk_msg(String name, GfoInvkAble invk, GfoMsg m) {
Thread_adp_.invk_msg_(name, invk, m).Start();
}
public static final String Name_null = null;
}