mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Refactor: @Test whitespace clean-up and other corelib changes
This commit is contained in:
10
baselib/src/gplx/objects/GfoKeyVal.java
Normal file
10
baselib/src/gplx/objects/GfoKeyVal.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package gplx.objects;
|
||||
|
||||
public class GfoKeyVal<K, V> {
|
||||
public GfoKeyVal(K key, V val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
public K Key() {return key;} private final K key;
|
||||
public V Val() {return val;} public void ValSet(V v) {this.val = v;} private V val;
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.errs; import gplx.*; import gplx.objects.*;
|
||||
import gplx.objects.strings.*;
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.errs; import gplx.objects.Object_;
|
||||
import gplx.objects.strings.String_;
|
||||
public class Err_ {
|
||||
public static void Noop(Exception e) {}
|
||||
public static Err New_fmt(String fmt, Object... args) {return new Err(String_.Format(fmt, args));}
|
||||
@@ -27,14 +27,15 @@ public class Err_ {
|
||||
public static Err New_unhandled_default(Object o) {
|
||||
return new Err("val is not in switch; val=" + Object_.To_str(o));
|
||||
}
|
||||
public static Err New_unimplemented() {return new Err("Method is not implemented");}
|
||||
|
||||
public static String Message_lang(Exception e) {
|
||||
public static String Message_lang(Exception e) {
|
||||
return Error.class.isAssignableFrom(e.getClass())
|
||||
? e.toString() // java.lang.Error returns null for "getMessage()"; return "toString()" instead
|
||||
: e.getMessage();
|
||||
}
|
||||
|
||||
public static String Trace_lang(Throwable e) {
|
||||
public static String Trace_lang(Throwable e) {
|
||||
StackTraceElement[] ary = e.getStackTrace();
|
||||
String rv = "";
|
||||
for (int i = 0; i < ary.length; i++) {
|
||||
@@ -43,4 +44,4 @@ public class Err_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
baselib/src/gplx/objects/events/GfoEvent.java
Normal file
23
baselib/src/gplx/objects/events/GfoEvent.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package gplx.objects.events;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GfoEvent<A> {
|
||||
private final List<GfoHandler<A>> handlers = new ArrayList<>();
|
||||
private final String name;
|
||||
private final GfoEventOwner owner;
|
||||
public GfoEvent(String name, GfoEventOwner owner) {
|
||||
this.name = name;
|
||||
this.owner = owner;
|
||||
}
|
||||
public void Add(GfoHandler<A> handler) {
|
||||
owner.EventsEnabledSet(true);
|
||||
handlers.add(handler);
|
||||
}
|
||||
public void Run(A args) {
|
||||
for (GfoHandler<A> handler : handlers) {
|
||||
handler.Run(owner, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
baselib/src/gplx/objects/events/GfoEventOwner.java
Normal file
5
baselib/src/gplx/objects/events/GfoEventOwner.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package gplx.objects.events;
|
||||
|
||||
public interface GfoEventOwner {
|
||||
boolean EventsEnabled(); void EventsEnabledSet(boolean v);
|
||||
}
|
||||
5
baselib/src/gplx/objects/events/GfoHandler.java
Normal file
5
baselib/src/gplx/objects/events/GfoHandler.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package gplx.objects.events;
|
||||
|
||||
public interface GfoHandler<A> {
|
||||
void Run(GfoEventOwner sender, A args);
|
||||
}
|
||||
4
baselib/src/gplx/objects/lists/GfoComparator.java
Normal file
4
baselib/src/gplx/objects/lists/GfoComparator.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
public interface GfoComparator<T> extends java.util.Comparator<T> {
|
||||
}
|
||||
49
baselib/src/gplx/objects/lists/GfoHashBase.java
Normal file
49
baselib/src/gplx/objects/lists/GfoHashBase.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import gplx.objects.errs.Err_;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class GfoHashBase<K, V> implements Iterable<Map.Entry<K, V>> {
|
||||
protected final Map<K, V> map = new HashMap<>();
|
||||
|
||||
public int Len() {return map.size();}
|
||||
public void Clear() {map.clear();}
|
||||
public void Add(GfoHashKeyFunc<K> keyFunc, V v) {Add(keyFunc.ToHashKey(), v);}
|
||||
public void Add(K k, V v) {
|
||||
if (k == null) {
|
||||
throw Err_.New_fmt("key cannot be null; val={0}", v);
|
||||
}
|
||||
if (map.containsKey(k)) {
|
||||
throw Err_.New_fmt("key already exists: key={0} val={1}", k, v);
|
||||
}
|
||||
map.put(k, v);
|
||||
}
|
||||
public void Del(K k) {map.remove(k);}
|
||||
public void Set(K k, V v) {map.put(k, v);}
|
||||
public boolean Has(K k) {return map.containsKey(k);}
|
||||
public V GetByOrNull(K k) {return map.get(k);}
|
||||
public V GetByOr(K k, V or) {
|
||||
V v = map.get(k);
|
||||
if (v == null) {
|
||||
v = or;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
public V GetByOrFail(GfoHashKeyFunc<K> func) {return GetByOrFail(func.ToHashKey());}
|
||||
public V GetByOrFail(K k) {
|
||||
V v = map.get(k);
|
||||
if (v == null) {
|
||||
throw Err_.New_fmt("val not found; key={0}", k);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override public Iterator<Map.Entry<K, V>> iterator() {return map.entrySet().iterator();}
|
||||
@Override public void forEach(Consumer<? super Map.Entry<K, V>> action) {map.entrySet().forEach(action);}
|
||||
@Override public Spliterator<Map.Entry<K, V>> spliterator() {return map.entrySet().spliterator();}
|
||||
}
|
||||
5
baselib/src/gplx/objects/lists/GfoHashKeyFunc.java
Normal file
5
baselib/src/gplx/objects/lists/GfoHashKeyFunc.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
public interface GfoHashKeyFunc<K> {
|
||||
K ToHashKey();
|
||||
}
|
||||
127
baselib/src/gplx/objects/lists/GfoIndexedList.java
Normal file
127
baselib/src/gplx/objects/lists/GfoIndexedList.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import gplx.objects.GfoKeyVal;
|
||||
import gplx.objects.errs.Err_;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class GfoIndexedList<K, V> implements Iterable<V> {
|
||||
private final GfoListBase<V> list = new GfoListBase<>();
|
||||
private final GfoHashBase<K, GfoIndexedListEntry<K, V>> hash = new GfoHashBase<>();
|
||||
public boolean Has(K key) {
|
||||
return hash.Has(key);
|
||||
}
|
||||
public int Len() {return hash.Len();}
|
||||
public V GetAt(int key) {
|
||||
return list.GetAt(key);
|
||||
}
|
||||
public V GetByOrFail(GfoHashKeyFunc<K> func) {return GetByOrFail(func.ToHashKey());}
|
||||
public V GetByOrFail(K key) {
|
||||
GfoIndexedListEntry<K, V> val = hash.GetByOrNull(key);
|
||||
if (val == null) {
|
||||
throw Err_.New_fmt("Unknown key: {0}", key);
|
||||
}
|
||||
return val.Val();
|
||||
}
|
||||
public V GetByOr(K key, V or) {
|
||||
GfoIndexedListEntry<K, V> val = hash.GetByOrNull(key);
|
||||
return val == null ? or : val.Val();
|
||||
}
|
||||
public Optional<V> GetBy(K key) {
|
||||
GfoIndexedListEntry<K, V> val = hash.GetByOrNull(key);
|
||||
return val == null ? Optional.empty() : Optional.of(val.Val());
|
||||
}
|
||||
public V GetByOrNull(K key) {
|
||||
GfoIndexedListEntry<K, V> val = hash.GetByOrNull(key);
|
||||
return val == null ? null : val.Val();
|
||||
}
|
||||
public void Sort(GfoComparator<V> comparator) {
|
||||
list.Sort(comparator);
|
||||
}
|
||||
public void AddAt(int i, K key, V val) {
|
||||
for (Map.Entry<K, GfoIndexedListEntry<K, V>> mapEntry : hash) {
|
||||
GfoIndexedListEntry<K, V> mapEntryItem = mapEntry.getValue();
|
||||
if (mapEntryItem.ListIdx() >= i) {
|
||||
mapEntryItem.ListIdxSet(mapEntryItem.ListIdx() + 1);
|
||||
}
|
||||
}
|
||||
hash.Add(key, new GfoIndexedListEntry<>(key, val, i));
|
||||
list.AddAt(i, val);
|
||||
}
|
||||
public void DelAt(int i, K key) {
|
||||
for (Map.Entry<K, GfoIndexedListEntry<K, V>> mapEntry : hash) {
|
||||
GfoIndexedListEntry<K, V> mapEntryItem = mapEntry.getValue();
|
||||
if (mapEntryItem.ListIdx() > i) {
|
||||
mapEntryItem.ListIdxSet(mapEntryItem.ListIdx() - 1);
|
||||
}
|
||||
}
|
||||
hash.Del(key);
|
||||
list.DelAt(i);
|
||||
}
|
||||
public void Add(GfoHashKeyFunc<K> func, V val) {Add(func.ToHashKey(), val);}
|
||||
public void Add(K key, V val) {
|
||||
hash.Add(key, new GfoIndexedListEntry<>(key, val, list.Len()));
|
||||
list.Add(val);
|
||||
}
|
||||
public void Set(K key, V val) {
|
||||
GfoIndexedListEntry<K, V> entry = hash.GetByOrNull(key);
|
||||
if (entry == null) {
|
||||
this.Add(key, val);
|
||||
}
|
||||
else {
|
||||
entry.ValSet(val);
|
||||
list.Set(entry.ListIdx(), val);
|
||||
}
|
||||
}
|
||||
public void DelBy(K key) {
|
||||
GfoIndexedListEntry<K, V> entry = hash.GetByOrNull(key);
|
||||
if (entry == null) {
|
||||
return; // if key is unknown, do nothing; matches behavior of java.util.Hashtable
|
||||
}
|
||||
hash.Del(key);
|
||||
int listIdx = entry.ListIdx();
|
||||
list.DelAt(listIdx);
|
||||
for (Map.Entry<K, GfoIndexedListEntry<K, V>> mapEntry : hash) {
|
||||
GfoIndexedListEntry<K, V> mapEntryItem = mapEntry.getValue();
|
||||
if (mapEntryItem.ListIdx() > listIdx) {
|
||||
mapEntryItem.ListIdxSet(mapEntryItem.ListIdx() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Clear() {
|
||||
list.Clear();
|
||||
hash.Clear();
|
||||
}
|
||||
public V[] ToAry(Class<?> clz) {return list.ToAry(clz);}
|
||||
public GfoKeyVal<K, V>[] ToKvAry() {
|
||||
int len = this.Len();
|
||||
GfoKeyVal<K, V>[] ary = new GfoKeyVal[len];
|
||||
int idx = 0;
|
||||
for (Map.Entry<K, GfoIndexedListEntry<K, V>> mapEntry : hash) {
|
||||
GfoIndexedListEntry<K, V> mapEntryItem = mapEntry.getValue();
|
||||
ary[idx++] = new GfoKeyVal<>(mapEntryItem.Key(), mapEntryItem.Val());
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
|
||||
@Override public Iterator<V> iterator() {return list.iterator();}
|
||||
@Override public void forEach(Consumer<? super V> action) {list.forEach(action);}
|
||||
@Override public Spliterator<V> spliterator() {return list.spliterator();}
|
||||
}
|
||||
class GfoIndexedListEntry<K, V> {
|
||||
private final K key;
|
||||
private V val;
|
||||
private int listIdx;
|
||||
public GfoIndexedListEntry(K key, V val, int listIdx) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
this.listIdx = listIdx;
|
||||
}
|
||||
public K Key() {return key;}
|
||||
public V Val() {return val;} public void ValSet(V v) {this.val = v;}
|
||||
public int ListIdx() {return listIdx;} public void ListIdxSet(int v) {listIdx = v;}
|
||||
}
|
||||
157
baselib/src/gplx/objects/lists/GfoListBase.java
Normal file
157
baselib/src/gplx/objects/lists/GfoListBase.java
Normal file
@@ -0,0 +1,157 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import gplx.objects.errs.Err_;
|
||||
import gplx.objects.events.GfoEvent;
|
||||
import gplx.objects.events.GfoEventOwner;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class GfoListBase<E> implements Iterable<E>, GfoEventOwner {
|
||||
protected final List<E> list = new ArrayList<>();
|
||||
private boolean eventsEnabled;
|
||||
|
||||
public GfoListBase() {
|
||||
this.itemsChanged = new GfoEvent<>("Add", this);
|
||||
}
|
||||
|
||||
public int Len() {return list.size();}
|
||||
public E GetAt(int i) {return list.get(i);}
|
||||
public E GetAtBgn() {return list.get(0);}
|
||||
public E GetAtEnd() {return list.get(list.size() - 1);}
|
||||
|
||||
@Override
|
||||
public boolean EventsEnabled() {
|
||||
return eventsEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void EventsEnabledSet(boolean v) {
|
||||
eventsEnabled = v;
|
||||
}
|
||||
public GfoEvent<ItemsChangedArg<E>> ItemsChanged() {return itemsChanged;} private final GfoEvent<ItemsChangedArg<E>> itemsChanged;
|
||||
public GfoListBase<E> Add(E itm) {
|
||||
list.add(itm);
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Add, GfoListBase.NewFromArray(itm)));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public GfoListBase<E> AddMany(E... ary) {
|
||||
for (E itm : ary) {
|
||||
this.Add(itm);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public GfoListBase<E> AddMany(GfoListBase<E> list) {
|
||||
for (E itm : list) {
|
||||
this.Add(itm);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public void AddAt(int i, E itm) {
|
||||
list.add(i, itm);
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Add, GfoListBase.NewFromArray(itm)));
|
||||
}
|
||||
}
|
||||
public void Set(int i, E itm) {
|
||||
list.set(i, itm);
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Set, GfoListBase.NewFromArray(itm)));
|
||||
}
|
||||
}
|
||||
public void DelAt(int i) {
|
||||
list.remove(i);
|
||||
}
|
||||
public void DelBetween(int i) {DelBetween(i, list.size() - 1);}
|
||||
public void DelBetween(int bgn, int end) {
|
||||
// put items in array for event
|
||||
int len = end - bgn;
|
||||
GfoListBase<E> old = new GfoListBase<>();
|
||||
for (int i = 0; i < len; i++) {
|
||||
old.Add(list.get(i + bgn));
|
||||
}
|
||||
|
||||
// actually delete
|
||||
for (int i = bgn; i < end; i++) {
|
||||
list.remove(bgn);
|
||||
}
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Del, old));
|
||||
}
|
||||
}
|
||||
|
||||
public void DelAtEnd() {
|
||||
int idx = list.size() - 1;
|
||||
E itm = list.get(idx);
|
||||
list.remove(idx);
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Del, GfoListBase.NewFromArray(itm)));
|
||||
}
|
||||
}
|
||||
public void Clear() {
|
||||
GfoListBase<E> old = new GfoListBase<>();
|
||||
for (E itm : list)
|
||||
old.Add(itm);
|
||||
list.clear();
|
||||
if (eventsEnabled) {
|
||||
itemsChanged.Run(new ItemsChangedArg<>(ItemsChangedType.Clear, old));
|
||||
}
|
||||
}
|
||||
public void Sort(GfoComparator<E> comparator) {
|
||||
list.sort(comparator);
|
||||
}
|
||||
public E[] ToAry(Class<?> clz) {
|
||||
int len = list.size();
|
||||
E[] rv = (E[])Array.newInstance(clz, len);
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = list.get(i);
|
||||
return rv;
|
||||
}
|
||||
public String[] ToStringAry() {
|
||||
int len = list.size();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = list.get(i).toString();
|
||||
return rv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new GfoListBaseIterator(list);
|
||||
}
|
||||
|
||||
class GfoListBaseIterator implements Iterator<E> {
|
||||
private final List<E> list;
|
||||
private int curIdx;
|
||||
private int len;
|
||||
public GfoListBaseIterator(List<E> list) {
|
||||
this.list = list;
|
||||
this.len = list.size();
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return curIdx < len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return (E)list.get(curIdx++);
|
||||
}
|
||||
@Override
|
||||
public void remove() {
|
||||
throw Err_.New_unimplemented();
|
||||
}
|
||||
}
|
||||
|
||||
public static <E> GfoListBase<E> NewFromArray(E... array) {
|
||||
GfoListBase<E> rv = new GfoListBase<>();
|
||||
for (E itm : array) {
|
||||
rv.Add(itm);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
39
baselib/src/gplx/objects/lists/GfoSetBase.java
Normal file
39
baselib/src/gplx/objects/lists/GfoSetBase.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import gplx.objects.errs.Err_;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class GfoSetBase<I> implements Iterable<I> {
|
||||
protected final Set<I> hash = new HashSet<>();
|
||||
|
||||
public int Len() {return hash.size();}
|
||||
public void Clear() {hash.clear();}
|
||||
public GfoSetBase<I> AddMany(I... ary) {
|
||||
for (I itm : ary) {
|
||||
Add(itm);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public void Add(GfoHashKeyFunc<I> keyFunc) {Add(keyFunc.ToHashKey());}
|
||||
public GfoSetBase<I> Add(I itm) {
|
||||
if (itm == null) {
|
||||
throw Err_.New_fmt("key cannot be null; val={0}", itm);
|
||||
}
|
||||
if (hash.contains(itm)) {
|
||||
throw Err_.New_fmt("key already exists: key={0} val={1}", itm);
|
||||
}
|
||||
hash.add(itm);
|
||||
return this;
|
||||
}
|
||||
public void Del(I itm) {hash.remove(itm);}
|
||||
public boolean Has(I itm) {return hash.contains(itm);}
|
||||
|
||||
@Override public Iterator<I> iterator() {return hash.iterator();}
|
||||
@Override public void forEach(Consumer<? super I> action) {hash.forEach(action);}
|
||||
@Override public Spliterator<I> spliterator() {return hash.spliterator();}
|
||||
}
|
||||
11
baselib/src/gplx/objects/lists/ItemsChangedArg.java
Normal file
11
baselib/src/gplx/objects/lists/ItemsChangedArg.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
public class ItemsChangedArg<T> {
|
||||
public ItemsChangedArg(ItemsChangedType type, GfoListBase<T> itms) {
|
||||
this.type = type;
|
||||
this.itms = itms;
|
||||
}
|
||||
public ItemsChangedType Type() {return type;} private final ItemsChangedType type;
|
||||
public GfoListBase<T> Itms() {return itms;} private final GfoListBase<T> itms;
|
||||
public T Itm() {return itms.GetAt(0);}
|
||||
}
|
||||
8
baselib/src/gplx/objects/lists/ItemsChangedType.java
Normal file
8
baselib/src/gplx/objects/lists/ItemsChangedType.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
public enum ItemsChangedType {
|
||||
Add,
|
||||
Del,
|
||||
Set,
|
||||
Clear,
|
||||
}
|
||||
14
baselib/src/gplx/objects/lists/ListUtil.java
Normal file
14
baselib/src/gplx/objects/lists/ListUtil.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ListUtil {
|
||||
public static byte[] ToArray(List<Byte> list) {
|
||||
int len = list.size();
|
||||
byte[] rv = new byte[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rv[i] = list.get(i);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives; import gplx.*; import gplx.objects.*;
|
||||
import org.junit.*; import gplx.tests.*;
|
||||
public class Int__tst {
|
||||
private final Int__fxt fxt = new Int__fxt();
|
||||
@Test public void Parse_or() {
|
||||
@Test public void Parse_or() {
|
||||
fxt.Test__Parse_or("123", 123); // basic
|
||||
fxt.Test__Parse_or_min_value(null); // null
|
||||
fxt.Test__Parse_or_min_value(""); // empty
|
||||
@@ -26,20 +26,20 @@ public class Int__tst {
|
||||
fxt.Test__Parse_or("-123", -123); // negative
|
||||
fxt.Test__Parse_or_min_value("1-23"); // negative at invalid position
|
||||
}
|
||||
@Test public void Between() {
|
||||
@Test public void Between() {
|
||||
fxt.Test__Between(1, 0, 2, true); // simple true
|
||||
fxt.Test__Between(3, 0, 2, false); // simple false
|
||||
fxt.Test__Between(0, 0, 2, true); // bgn true
|
||||
fxt.Test__Between(2, 0, 2, true); // end true
|
||||
}
|
||||
@Test public void Count_digits() {
|
||||
@Test public void Count_digits() {
|
||||
fxt.Test__Count_digits( 0, 1);
|
||||
fxt.Test__Count_digits( 9, 1);
|
||||
fxt.Test__Count_digits( 100, 3);
|
||||
fxt.Test__Count_digits( -1, 2);
|
||||
fxt.Test__Count_digits(-100, 4);
|
||||
}
|
||||
@Test public void Log10() {
|
||||
@Test public void Log10() {
|
||||
fxt.Test__Log10( 0, 0);
|
||||
fxt.Test__Log10( 1, 0);
|
||||
fxt.Test__Log10( 2, 0);
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.strings; import gplx.*; import gplx.objects.*;
|
||||
import org.junit.*; import gplx.tests.*;
|
||||
public class String__tst {
|
||||
private final String__fxt fxt = new String__fxt();
|
||||
@Test public void Len() {
|
||||
@Test public void Len() {
|
||||
fxt.Test__Len("" , 0);
|
||||
fxt.Test__Len("abc", 3);
|
||||
}
|
||||
@Test public void Format() {
|
||||
@Test public void Format() {
|
||||
fxt.Test__Format("" , ""); // empty fmt
|
||||
fxt.Test__Format("" , "", "a"); // empty fmt w/ args
|
||||
fxt.Test__Format("a" , "a"); // no args
|
||||
|
||||
35
baselib/src/gplx/objects/strings/bfrs/GfoStringBuilder.java
Normal file
35
baselib/src/gplx/objects/strings/bfrs/GfoStringBuilder.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package gplx.objects.strings.bfrs;
|
||||
|
||||
import gplx.objects.strings.String_;
|
||||
|
||||
public class GfoStringBuilder {
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
public int Len() {return sb.length();}
|
||||
public GfoStringBuilder Clear() {
|
||||
sb.delete(0, sb.length());
|
||||
return this;
|
||||
}
|
||||
public GfoStringBuilder AddNl() {return Add("\n");}
|
||||
public GfoStringBuilder Add(int v) {sb.append(v); return this;}
|
||||
public GfoStringBuilder Add(String v) {sb.append(v); return this;}
|
||||
public GfoStringBuilder Add(char v) {sb.append(v); return this;}
|
||||
public GfoStringBuilder AddObj(Object v) {sb.append(v); return this;}
|
||||
public GfoStringBuilder AddMid(String s, int bgn) {sb.append(s, bgn, s.length()); return this;}
|
||||
public GfoStringBuilder AddMid(String s, int bgn, int end) {sb.append(s, bgn, end); return this;}
|
||||
public GfoStringBuilder AddFmt(String fmt, Object... args) {
|
||||
sb.append(String_.Format(fmt, args));
|
||||
return this;
|
||||
}
|
||||
public GfoStringBuilder AddRepeat(char c, int repeat) {
|
||||
sb.ensureCapacity(this.Len() + repeat);
|
||||
for (int i = 0; i < repeat; i++)
|
||||
sb.append(c);
|
||||
return this;
|
||||
}
|
||||
public String ToStr() {return sb.toString();}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +1,52 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.strings.unicodes; import gplx.*; import gplx.objects.*; import gplx.objects.strings.*;
|
||||
import org.junit.*; import gplx.tests.*;
|
||||
import gplx.objects.errs.*;
|
||||
public class Ustring_tst {
|
||||
private final Ustring_fxt fxt = new Ustring_fxt();
|
||||
@Test public void Empty() {
|
||||
@Test public void Empty() {
|
||||
fxt.Init("");
|
||||
fxt.Test__Len(0, 0);
|
||||
}
|
||||
@Test public void Blank() {
|
||||
@Test public void Blank() {
|
||||
fxt.Init("");
|
||||
fxt.Test__Len(0, 0);
|
||||
}
|
||||
@Test public void Single() {
|
||||
@Test public void Single() {
|
||||
fxt.Init("Abc");
|
||||
fxt.Test__Len(3, 3);
|
||||
fxt.Test__Get_code(65, 98, 99);
|
||||
fxt.Test__Map_code_to_char(0, 1, 2, 3);
|
||||
fxt.Test__Map_char_to_code(0, 1, 2, 3);
|
||||
}
|
||||
@Test public void Multi() {
|
||||
@Test public void Multi() {
|
||||
fxt.Init("a¢€𤭢b");
|
||||
fxt.Test__Len(5, 6);
|
||||
fxt.Test__Get_code(97, 162, 8364, 150370, 98);
|
||||
fxt.Test__Map_code_to_char(0, 1, 2, 3, 5, 6);
|
||||
fxt.Test__Map_char_to_code(0, 1, 2, 3, -1, 4, 5);
|
||||
}
|
||||
@Test public void Index_of() {
|
||||
@Test public void Index_of() {
|
||||
fxt.Test__Index_of("abc", "b", 0, 1); // basic
|
||||
fxt.Test__Index_of("ab", "bc", 0, -1); // out-of-bounds
|
||||
fxt.Test__Index_of("a¢e", "¢", 0, 1); // check UTF-8 strings still match at byte-level
|
||||
}
|
||||
|
||||
@Test public void Substring() {
|
||||
@Test public void Substring() {
|
||||
fxt.Test__Substring("abc", 1, 2, "b"); // basic
|
||||
fxt.Test__Substring("¢bc", 1, 2, "b"); // check UTF-8 strings don't get lopped off
|
||||
}
|
||||
|
||||
70
baselib/tst/gplx/objects/lists/GfoIndexedListEntryTest.java
Normal file
70
baselib/tst/gplx/objects/lists/GfoIndexedListEntryTest.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package gplx.objects.lists;
|
||||
|
||||
import gplx.tests.Gftest_fxt;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GfoIndexedListEntryTest {
|
||||
private GfoIndexedList<String, String> list = new GfoIndexedList<>();
|
||||
@Test public void Add() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
testGetAt(0, "a");
|
||||
testGetAt(1, "b");
|
||||
testGetAt(2, "c");
|
||||
testGetByOrFail("A", "a");
|
||||
testGetByOrFail("B", "b");
|
||||
testGetByOrFail("C", "c");
|
||||
testIterate("a", "b", "c");
|
||||
}
|
||||
@Test public void DelBy() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
list.DelBy("A");
|
||||
|
||||
testIterate("b", "c");
|
||||
|
||||
list.DelBy("B");
|
||||
|
||||
testIterate("c");
|
||||
|
||||
list.DelBy("C");
|
||||
|
||||
testIterate();
|
||||
}
|
||||
@Test public void DelBy_SameVal() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "a");
|
||||
|
||||
list.DelBy("C");
|
||||
|
||||
testIterate("a", "b"); // fails if "b", "a"
|
||||
}
|
||||
@Test public void Set() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
list.Set("B", "bb");
|
||||
testGetByOrFail("B", "bb");
|
||||
testIterate("a", "bb", "c");
|
||||
}
|
||||
private void testGetByOrFail(String key, String expd) {
|
||||
Gftest_fxt.Eq__str(expd, list.GetByOrFail(key));
|
||||
}
|
||||
private void testGetAt(int idx, String expd) {
|
||||
Gftest_fxt.Eq__str(expd, list.GetAt(idx));
|
||||
}
|
||||
private void testIterate(String... expd) {
|
||||
String[] actl = new String[expd.length];
|
||||
int i = 0;
|
||||
for (String itm : list) {
|
||||
actl[i++] = itm;
|
||||
}
|
||||
Gftest_fxt.Eq__ary(expd, actl);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user