mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.2.1
This commit is contained in:
28
400_xowa/src/gplx/xowa/langs/durations/Xol_duration_itm.java
Normal file
28
400_xowa/src/gplx/xowa/langs/durations/Xol_duration_itm.java
Normal 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.xowa.langs.durations; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
public class Xol_duration_itm {
|
||||
public Xol_duration_itm(byte tid, String name_str, long seconds) {
|
||||
this.tid = tid; this.seconds = seconds;
|
||||
this.name_str = name_str; this.name_bry = Bry_.new_a7(name_str);
|
||||
}
|
||||
public byte Tid() {return tid;} private byte tid;
|
||||
public byte[] Name_bry() {return name_bry;} private byte[] name_bry;
|
||||
public String Name_str() {return name_str;} private String name_str;
|
||||
public long Seconds() {return seconds;} private long seconds;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
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.xowa.langs.durations; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
public class Xol_duration_itm_ {
|
||||
private static final Hash_adp_bry regy = Hash_adp_bry.ci_ascii_(); // ASCII:MW.consts
|
||||
public static final byte
|
||||
Tid_millenia = 0
|
||||
, Tid_centuries = 1
|
||||
, Tid_decades = 2
|
||||
, Tid_years = 3
|
||||
, Tid_weeks = 4
|
||||
, Tid_days = 5
|
||||
, Tid_hours = 6
|
||||
, Tid_minutes = 7
|
||||
, Tid_seconds = 8
|
||||
;
|
||||
public static final Xol_duration_itm
|
||||
Itm_millenia = new_(Tid_millenia , "millenia" , 31556952000L)
|
||||
, Itm_centuries = new_(Tid_centuries , "centuries" , 3155695200L)
|
||||
, Itm_decades = new_(Tid_decades , "decades" , 315569520L)
|
||||
, Itm_years = new_(Tid_years , "years" , 31556952L) // 86400 * (365 + (24 * 3 + 25) / 400)
|
||||
, Itm_weeks = new_(Tid_weeks , "weeks" , 604800L)
|
||||
, Itm_days = new_(Tid_days , "days" , 86400L)
|
||||
, Itm_hours = new_(Tid_hours , "hours" , 3600L)
|
||||
, Itm_minutes = new_(Tid_minutes , "minutes" , 60L)
|
||||
, Itm_seconds = new_(Tid_seconds , "seconds" , 1L)
|
||||
;
|
||||
private static Xol_duration_itm new_(byte tid, String name, long factor) {
|
||||
Xol_duration_itm rv = new Xol_duration_itm(tid, name, factor);
|
||||
regy.Add(rv.Name_bry(), rv);
|
||||
return rv;
|
||||
}
|
||||
public static final Xol_duration_itm[] Ary_default = new Xol_duration_itm[]
|
||||
{ Itm_millenia
|
||||
, Itm_centuries
|
||||
, Itm_decades
|
||||
, Itm_years
|
||||
, Itm_weeks
|
||||
, Itm_days
|
||||
, Itm_hours
|
||||
, Itm_minutes
|
||||
, Itm_seconds
|
||||
};
|
||||
public static Xol_duration_itm[] Xto_itm_ary(KeyVal[] kv_ary) {
|
||||
if (kv_ary == null) return Xol_duration_itm_.Ary_default;
|
||||
List_adp rv = List_adp_.new_();
|
||||
int len = kv_ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
KeyVal kv = kv_ary[i];
|
||||
String name = kv.Val_to_str_or_empty();
|
||||
Xol_duration_itm itm = (Xol_duration_itm)regy.Get_by(Bry_.new_u8(name));
|
||||
if (itm != null)
|
||||
rv.Add(itm);
|
||||
}
|
||||
return (Xol_duration_itm[])rv.To_ary(Xol_duration_itm.class);
|
||||
}
|
||||
}
|
||||
class Xol_duration_itm_sorter implements gplx.lists.ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
Xol_duration_itm lhs = (Xol_duration_itm)lhsObj;
|
||||
Xol_duration_itm rhs = (Xol_duration_itm)rhsObj;
|
||||
return -Long_.Compare(lhs.Seconds(), rhs.Seconds()); // - to sort from largest to smallest
|
||||
}
|
||||
public static final Xol_duration_itm_sorter _ = new Xol_duration_itm_sorter(); Xol_duration_itm_sorter() {}
|
||||
}
|
||||
90
400_xowa/src/gplx/xowa/langs/durations/Xol_duration_mgr.java
Normal file
90
400_xowa/src/gplx/xowa/langs/durations/Xol_duration_mgr.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
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.xowa.langs.durations; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
public class Xol_duration_mgr {
|
||||
private Xol_msg_itm[] interval_msgs = null;
|
||||
public Xol_duration_mgr(Xol_lang lang) {this.lang = lang;} private Xol_lang lang;
|
||||
private Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
|
||||
public Xol_interval_itm[] Get_duration_intervals(long seconds, Xol_duration_itm[] intervals) {
|
||||
if (intervals == null) intervals = Xol_duration_itm_.Ary_default;
|
||||
Array_.Sort(intervals, Xol_duration_itm_sorter._);
|
||||
int intervals_len = intervals.length;
|
||||
long val = seconds;
|
||||
List_adp rv = List_adp_.new_();
|
||||
for (int i = 0; i < intervals_len; i++) {
|
||||
Xol_duration_itm itm = intervals[i];
|
||||
long itm_seconds = itm.Seconds();
|
||||
val = seconds / itm_seconds;
|
||||
if ( val > 0
|
||||
|| (i == intervals_len - 1 && rv.Count() == 0) // always add one seg; EX: 40 seconds, but minutes requested -> 0 minutes; DATE:2014-05-10
|
||||
) {
|
||||
seconds -= val * itm_seconds;
|
||||
rv.Add(new Xol_interval_itm(itm, val));
|
||||
}
|
||||
}
|
||||
return (Xol_interval_itm[])rv.To_ary(Xol_interval_itm.class);
|
||||
}
|
||||
public byte[] Format_durations(Xop_ctx ctx, long seconds, Xol_duration_itm[] ary) {
|
||||
if (interval_msgs == null) Format_durations_init();
|
||||
Xol_interval_itm[] intervals = Get_duration_intervals(seconds, ary);
|
||||
int intervals_len = intervals.length;
|
||||
byte[][] msgs_ary = new byte[intervals_len][];
|
||||
for (int i = 0; i < intervals_len; i++) {
|
||||
Xol_interval_itm interval = intervals[i];
|
||||
Xol_msg_itm msg_itm = interval_msgs[interval.Duration_itm().Tid()];
|
||||
byte[] msg_bry = msg_itm.Fmt(tmp_bfr, interval.Val());
|
||||
msg_bry = ctx.Wiki().Parser().Parse_text_to_html(ctx, msg_bry);
|
||||
msgs_ary[i] = msg_bry;
|
||||
}
|
||||
return List_to_str(msgs_ary);
|
||||
}
|
||||
private byte[] Msg_and, Msg_word_separator, Msg_comma_separator;
|
||||
private void Format_durations_init() {
|
||||
Xol_msg_mgr msg_mgr = lang.Msg_mgr();
|
||||
int len = Xol_duration_itm_.Ary_default.length;
|
||||
interval_msgs = new Xol_msg_itm[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_duration_itm itm = Xol_duration_itm_.Ary_default[i];
|
||||
byte[] msg_key = Bry_.Add(Bry_duration, itm.Name_bry());
|
||||
interval_msgs[i] = msg_mgr.Itm_by_key_or_new(msg_key);
|
||||
}
|
||||
} private static final byte[] Bry_duration = Bry_.new_a7("duration-");
|
||||
private void List_to_str_init() {
|
||||
Xol_msg_mgr msg_mgr = lang.Msg_mgr();
|
||||
Msg_and = msg_mgr.Val_by_str_or_empty("and");
|
||||
Msg_word_separator = msg_mgr.Val_by_str_or_empty("word-separator");
|
||||
Msg_comma_separator = msg_mgr.Val_by_str_or_empty("comma-separator");
|
||||
}
|
||||
|
||||
public byte[] List_to_str(byte[][] segs_ary) {
|
||||
int len = segs_ary.length;
|
||||
switch (len) {
|
||||
case 0: return Bry_.Empty;
|
||||
case 1: return segs_ary[0];
|
||||
default:
|
||||
if (Msg_and == null) List_to_str_init();
|
||||
int last_idx = len - 1;
|
||||
for (int i = 0; i < last_idx; i++) {
|
||||
if (i != 0) tmp_bfr.Add(Msg_comma_separator);
|
||||
tmp_bfr.Add(segs_ary[i]);
|
||||
}
|
||||
tmp_bfr.Add(Msg_and).Add(Msg_word_separator).Add(segs_ary[last_idx]);
|
||||
return tmp_bfr.Xto_bry_and_clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
400_xowa/src/gplx/xowa/langs/durations/Xol_interval_itm.java
Normal file
32
400_xowa/src/gplx/xowa/langs/durations/Xol_interval_itm.java
Normal 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.xowa.langs.durations; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
public class Xol_interval_itm {
|
||||
public Xol_interval_itm(Xol_duration_itm duration_itm, long val) {this.duration_itm = duration_itm; this.val = val;}
|
||||
public Xol_duration_itm Duration_itm() {return duration_itm;} private Xol_duration_itm duration_itm;
|
||||
public long Val() {return val;} private long val;
|
||||
public static KeyVal[] Xto_kv_ary(Xol_interval_itm[] ary) {
|
||||
int len = ary.length;
|
||||
KeyVal[] rv = new KeyVal[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_interval_itm itm = ary[i];
|
||||
rv[i] = KeyVal_.new_(itm.Duration_itm().Name_str(), (int)itm.Val()); // double for scribunto
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user