Some simple and lightweight tools and utilities in Java.
<dependency>
<groupId>com.eroelf</groupId>
<artifactId>javaxsx</artifactId>
<version>${version}</version>
</dependency>
This package provides some basic definition for an event happened at a specified time, say, History
.
If a History
is related to a subjective action, it becomes a Behavior
. As many websites are concerned, users is a basic dimension, so there comes UserBehavior
.
Behaviors
class defines some basic methods to deal with behaviors.
Captcha interfaces and a simple implementation.
Captcha captcha=new SimpleCaptcha(captchaLength, captchaImageWidth, captchaImageHeight);
String text=captcha.generateText();
BufferedImage bi=captcha.generateImage(text);
Concurrent utilities.
Iterator<Product> iterator;// The producer.
int queueCapacity;// The capacity of the queue holding products that waiting to be consumed.
int nThreads;// Number of consumers.
long timeout;// Time to wait for the threads to join.
TimeUnit unit;// The unit of timeout.
Consumer<Product> handler=p -> System.out.println(p.toString());// Define how do consumers process those products.
ProducerConsumer pc=new ProducerConsumer();
pc.consume(iterator, queueCapacity, nThreads, timeout, unit, handler);
Provides some simple APIs for database requests. There is NO support for connection pools and standing connections.
For example:
DoDb doDb=new DoDb();
doDb.setConnection(connection);
doDb.executeQuery(true, "select ...");
doDb.executeUpdate(false, "insert into ...", obj1, obj2, ...);
doDb.close();
doDb.prepareStatement(true, "insert into person (id, name, age, learning_subject) values (?, ?, ?, ?)");
while(...)
{
...
String id=...
String name=...
int age=...
String learningSubject=...
doDb.addBatch(id, name, age, learningSubject);
...
}
doDb.executeBatch();
public class Person
{
public String id;
public String name;
public int age;
public String learningSubject;
}
List<Person> list=doDb.fromQuery(Person.class, true, true, "select id, name, age, learning_subject from person");
A simple and lightweight database connection pool. Support transactions.
DataSource dataSource=new DataSource(...);
dataSource.executeQuery(...);
dataSource.executeUpdate(...);
List<...> list=dataSource.fromQuery(...);
dataSource.executeTransaction(conn -> {
try
{
conn.executeUpdate(...);
conn.executeUpdate(...);
}
catch(SQLException e)
{
throw new RuntimeException(e);
}
});
Provide APIs that deal with necessary working flows to update information of a database.
DbUpdater<Person> dbUpdater1=new DbInserter {
@Override
public Iterable<Object[]> process(Person data)
{
return new Collection.singleton(new Object[]{data.name, data.age});
}
};
DbUpdater<Person> dbUpdater2=new DbUpdater {
//...
};
String inputFileName;
Gson gson=new Gson();
UpdateTask task=new UpdateTask();
task.consume(Iterators.transform(new FileIterator(inputFileName), line -> gson.fromJson(line, Person.class)), dbUpdater1, dbUpdater2);
Provides some simple API for geography.
System.out.println(CoordinateConverter.fromWGS84ToGCJ02(39.9671454379, 116.3281085168));
System.out.println(CoordinateConverter.fromGCJ02ToWGS84(CoordinateConverter.fromWGS84ToGCJ02(39.9671454379, 116.3281085168)));
System.out.println(CoordinateConverter.fromWGS84ToBD09(39.9684488626, 116.3342346822));
System.out.println(CoordinateConverter.fromBD09ToGCJ02(39.97411842293451, 116.34085726246536));
Output:
\> (39.96845939748989, 116.3342501022455, NaN)
\> (39.9671336261829, 116.32809425470623, NaN)
\> (39.975428838226456, 116.34701964033485, NaN)
\> (39.968449623723544, 116.33423475270888, NaN)
System.out.println(GeoUtil.distance(39.2635, 116.8098, 39.6572, 116.3422));
Output:
\> 59414.14058762891
This package provides APIs for grouping some certain set of objects. Includes methods for configuring, updating from some source, and schedule auto-updating.
Simple usage
GroupingUtil.configFacet("GROUPING_TEST", "_grouping_test", "A,B,C,Z".split(","), "0.25,0.75,1".split(","), (id) -> true, new DigestHashGetter("md5", 2));
Group group=GroupingUtil.getGroupFromIdentifier("GROUPING_TEST", "390QSJPOSFN0543XF0WJ");
System.out.println(group.getFacetName());
System.out.println(group.getGroupName());
Output:
\> GROUPING_TEST
\> A
One may like to delegate a Group
object to apply a special strategy on objects in this group. Just assign a GroupTask
functional interface to the Group
object.
Classes in package updater are helpful for update grouping configures from a specified source, e.g., a database.
Gson Type Adapters.
APIs for a simple form to index a bunch of objects, implemented by using HashMap
.
String fileName="data.gz";
new FileReader(fileName).lines();
FileReaderPlus.readAFile(fileName, (line) -> {
...
return true;
});
new FileIterator(fileName).lines();
new DirFileIterator("/").lines();
Support text, gzip, bzip2, zstd, and zip format. Extend InputHelper
class and override getCompressType
and convert
methods to modify compress type judgment and to support more file types.
The Compressor
class provides methods to compress and decompress data. Override CompressorBuilder::setCompression
method to support more compress types.
Some mathematics related methods. Such as mapping, combination, and statistics.
-
Examples of using class
DataPreProcessor
:-
Basic statistics and scaling
DataPreProcessor dataPreProcessor=new DataPreProcessor(new double[]{2, 4, 4, 6}, false); System.out.println(dataPreProcessor.getOriginalMean()); System.out.println(dataPreProcessor.getOriginalStd()); System.out.println(dataPreProcessor.getOriginalSum()); System.out.println(dataPreProcessor.getOriginalMax()); System.out.println(dataPreProcessor.getOriginalMin()); System.out.println("--------------------------------"); dataPreProcessor.scale(2, 1); dataPreProcessor.toUniform(); System.out.println(dataPreProcessor.getProcessedMean()); System.out.println(dataPreProcessor.getProcessedStd()); System.out.println(dataPreProcessor.getProcessedSum()); System.out.println(dataPreProcessor.getProcessedMax()); System.out.println(dataPreProcessor.getProcessedMin()); System.out.println("--------------------------------"); dataPreProcessor.reScale(2, 1); dataPreProcessor.reScale(-0.5, -1); System.out.println(dataPreProcessor.getProcessedMean()); System.out.println(dataPreProcessor.getProcessedStd()); System.out.println(dataPreProcessor.getProcessedSum()); System.out.println(dataPreProcessor.getProcessedMax()); System.out.println(dataPreProcessor.getProcessedMin());
Output:
\> 4.0 \> 1.632993161855452 \> 16.0 \> 6.0 \> 2.0 \> -------------------------------- \> 0.5 \> 0.30618621784789724 \> 2.0 \> 0.875 \> 0.125 \> -------------------------------- \> -2.0 \> 0.30618621784789724 \> -8.0 \> -1.625 \> -2.375
-
Histogram for console
DataPreProcessor dataPreProcessor=new DataPreProcessor(new double[]{2, 4, 4, 6}, false); HistInfo histInfo=dataPreProcessor.getOriginalHistogram(new double[]{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5}, true); String histStr=DataPreProcessor.formatHistogram(histInfo, 100); System.out.println(histStr);
Output:
\> [0.5, 1.5)|0.00000000| \> [1.5, 2.5)|0.25000000|>>>>>>>>>>>>>>>>>>>>>>>>> \> [2.5, 3.5)|0.00000000| \> [3.5, 4.5)|0.50000000|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \> [4.5, 5.5)|0.00000000| \> [5.5, 6.5)|0.25000000|>>>>>>>>>>>>>>>>>>>>>>>>> \> others|0.00000000|
-
This package is an abstract core working flow of any certain recall, recommender, estimating, scoring, and ranking system.
There is a demo for a simple usage.
-
feature
packageFor
Item
feature storing and scoring.-
model
packageA
Modelable
object can be "modeled", say, filling features, by one or moreModeler
objects. That means theModelable
object contains features of a specified thing while thoseModeler
objects can be anything like strategies or descriptors to describe theModelable
object. -
score
packageA
Scoreable
object can be "scored" by aScorer
object and hold the final score until next modification.Scoreable
objects areComparable
by their scores in descending order, which implies the higher the score the "better" theScoreable
object is. This is consistent to general ranking and recommender projects. -
strategy
packageA
Strategy
object is aModeler
object but with method to recall candidates under its definition. -
Item
classAn
Item
object is both aModelable
and aScoreable
object. This means anItem
object is very suitable to represent a specified "product" in a well defined ranking and recommender system.
-
-
flow
packageThis package defines general working flows for ranking and recommender systems in both enumerable candidate situation and innumerable candidate situation. Note that innumerable candidate situation can also be used in enumerable candidate situation.
-
controller
packageThis package defines many detailed behaviors of both the two working flow mentioned above.
-
convert
packageProvide a
Converter
class for converting a back-end product data structure (Item
) list to a front-end product data structure (Info
) list, as well as logging necessary information for eachInfo
object to a logger data structure (InfoLog
). -
estimate
packageThe top-most interface for both the enumerable and the innumerable candidate working flow. Call the
generate
method to get the scored but unordered product (Item
) list. UsingCollections.sort
to sort the list if necessary.Please note that in most cases the
generate
method need NOT to be overridden. -
Item
,Info
, andInfoLog
As described above, objects of all the three classes are "products" in a specified ranking and recommender system.
Item
class is used for back-end data,Info
class is used for front-end data, andInfoLog
class is used for logging necessary information forInfo
objects.
-
General data structure for a server monitoring.
Provide APIs for constructing, parsing, and requesting URLs.
Help to repeatedly construct URLs for a specified domain and pre-defined parameters with different values. See the Javadoc for detail.
Help to construct and parse URLs.
Help to request URLs. There is NO support for connection pools and standing connections.
Implemented an HTTP requester connection pool client.
Java reflection utilities.
Wrap runtime objects to make it possible to call their methods by method name strings.
This is useful when you have a bunch of objects containing methods with identical signatures but with no common interface.
ClassWrapper wrappedObject=new ClassWrapper(obj);
wrappedObject.invoke('methodName', ...);
Implemented HashMapTrie
and DoubleArrayTrie
, and a simple trie division algorithm in IndexedTrie
.
- ZHONG Weikun
This project is released under the Apache license 2.0.
Copyright 2018 ZHONG Weikun.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.