2017-02-07 03:14:55 +00:00
|
|
|
/*
|
|
|
|
XOWA: the XOWA Offline Wiki Application
|
2017-02-21 20:45:17 +00:00
|
|
|
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
2017-02-07 03:14:55 +00:00
|
|
|
|
2017-02-21 20:45:17 +00:00
|
|
|
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
|
|
|
or alternatively under the terms of the Apache License Version 2.0.
|
2017-02-07 03:14:55 +00:00
|
|
|
|
2017-02-21 20:45:17 +00:00
|
|
|
You may use XOWA according to either of these licenses as is most appropriate
|
|
|
|
for your project on a case-by-case basis.
|
2017-02-07 03:14:55 +00:00
|
|
|
|
2017-02-21 20:45:17 +00:00
|
|
|
The terms of each license can be found in the source code repository:
|
|
|
|
|
|
|
|
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
|
|
|
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
2017-02-07 03:14:55 +00:00
|
|
|
*/
|
2017-10-24 00:50:50 +00:00
|
|
|
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
2019-07-12 03:17:06 +00:00
|
|
|
import gplx.core.brys.*;
|
|
|
|
public class XophpArray implements Bry_bfr_able {
|
|
|
|
private final Ordered_hash hash = Ordered_hash_.New();
|
|
|
|
private int nxt_idx;
|
|
|
|
public int Len() {return hash.Len();}
|
2019-07-25 02:17:21 +00:00
|
|
|
public int Count() {return hash.Len();}
|
2019-07-12 03:17:06 +00:00
|
|
|
public void Clear() {
|
|
|
|
hash.Clear();
|
|
|
|
nxt_idx = 0;
|
|
|
|
}
|
|
|
|
public XophpArray Add(Object val) {
|
|
|
|
int key = nxt_idx++;
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_int(key, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
public XophpArray Add(int key, Object val) {
|
|
|
|
nxt_idx = key + 1;
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_int(key, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
public XophpArray Add(double key, Object val) {
|
|
|
|
int key_as_int = (int)key;
|
|
|
|
nxt_idx = key_as_int + 1;
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_int(key_as_int, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
public XophpArray Add(boolean key, Object val) {
|
|
|
|
int key_as_int = key ? 1 : 0;
|
|
|
|
nxt_idx = key_as_int + 1;
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_int(key_as_int, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
public XophpArray Add(String key, Object val) {
|
|
|
|
int key_as_int = Int_.Parse_or(key, Int_.Min_value);
|
|
|
|
if (key_as_int == Int_.Min_value) {
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_str(key, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-07-22 02:09:21 +00:00
|
|
|
Set(XophpArrayItm.New_int(key_as_int, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
nxt_idx = key_as_int + 1;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2019-07-22 02:09:21 +00:00
|
|
|
public XophpArray Get_at_ary(int i) {return (XophpArray)Get_at(i);}
|
|
|
|
public String Get_at_str(int i) {return (String)Get_at(i);}
|
|
|
|
public Object Get_at(int i) {
|
|
|
|
if (i < 0 || i >= hash.Len()) return null;
|
|
|
|
XophpArrayItm itm = (XophpArrayItm)hash.Get_at(i);
|
|
|
|
return itm == null ? null : itm.Val();
|
2019-07-12 03:17:06 +00:00
|
|
|
}
|
|
|
|
public void Del_at(int i) {
|
|
|
|
XophpArrayItm itm = (XophpArrayItm)hash.Get_at(i);
|
|
|
|
if (itm != null) {
|
2019-07-22 02:09:21 +00:00
|
|
|
hash.Del(itm.Key());
|
2019-07-12 03:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public XophpArray Add_many(Object... val) {
|
|
|
|
for (Object itm : val) {
|
|
|
|
Add(itm);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2019-07-22 02:09:21 +00:00
|
|
|
public Object Get_by_obj(Object key) {return Get_by(Object_.Xto_str_strict_or_null(key));}
|
|
|
|
public Object Get_by(int key) {return Get_by(Int_.To_str(key));}
|
|
|
|
public Object Get_by(String key) {
|
2019-07-12 03:17:06 +00:00
|
|
|
XophpArrayItm itm = (XophpArrayItm)hash.Get_by(key);
|
|
|
|
return itm.Val();
|
|
|
|
}
|
|
|
|
public void Set(int key, Object val) {
|
2019-07-22 02:09:21 +00:00
|
|
|
this.Set(XophpArrayItm.New_int(key, val));
|
2019-07-12 03:17:06 +00:00
|
|
|
}
|
2019-07-22 02:09:21 +00:00
|
|
|
public void Unset(int key) {Unset(Int_.To_str(key));}
|
|
|
|
public void Unset(String key) {
|
2019-07-12 03:17:06 +00:00
|
|
|
hash.Del(key);
|
|
|
|
}
|
2019-07-22 02:09:21 +00:00
|
|
|
public boolean Has_obj(Object key) {return Has(Object_.Xto_str_strict_or_null(key));}
|
|
|
|
public boolean Has(String key) {
|
2019-07-12 03:17:06 +00:00
|
|
|
return hash.Has(key);
|
|
|
|
}
|
|
|
|
public XophpArrayItm[] To_ary() {
|
|
|
|
return (XophpArrayItm[])hash.To_ary(XophpArrayItm.class);
|
|
|
|
}
|
|
|
|
public String To_str() {
|
|
|
|
Bry_bfr bfr = Bry_bfr_.New();
|
|
|
|
To_bfr(bfr);
|
|
|
|
return bfr.To_str_and_clear();
|
|
|
|
}
|
|
|
|
public void To_bfr(Bry_bfr bfr) {
|
|
|
|
XophpArrayItm[] itms = To_ary();
|
|
|
|
for (XophpArrayItm itm : itms) {
|
|
|
|
itm.To_bfr(bfr);
|
|
|
|
}
|
|
|
|
}
|
2019-07-22 02:09:21 +00:00
|
|
|
private void Set(XophpArrayItm itm) {
|
|
|
|
String key = itm.Key();
|
|
|
|
XophpArrayItm cur = (XophpArrayItm)hash.Get_by(key);
|
|
|
|
if (cur == null) {
|
|
|
|
hash.Add(key, itm);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cur.Val_(itm.Val());
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 03:17:06 +00:00
|
|
|
public static XophpArray New(Object... vals) {
|
|
|
|
XophpArray rv = new XophpArray();
|
|
|
|
for (Object val : vals)
|
|
|
|
rv.Add(val);
|
|
|
|
return rv;
|
2017-10-24 00:50:50 +00:00
|
|
|
}
|
|
|
|
}
|