Skip to content

Commit c0f050f

Browse files
committed
Re-Initial Commit
0 parents  commit c0f050f

File tree

186 files changed

+21241
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+21241
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
/src/jokrey/utilities/simple/data_structure/queue/LFQueue.java

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 jokrey
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# utility-algorithms-java
2+
3+
Multi purpose utility algorithms.
4+
The algorithms and classes are all each cool (duh),
5+
but each not big enough for their own repository.
6+
7+
Some of the algorithms here may be implementation of protocols that exist in other "utility-algorithms-*lang*" repos.
8+
9+
10+
11+
12+
13+
### Installing
14+
15+
* Clone the repository with the 'git clone' command for now. (may turn this into a maven repo at some point)
16+
17+
## Features
18+
19+
* ubae - universal byte array encoder (also: *rust*) - NoSQL database system using 'tags' as data identifier. Allows storing any kind of data. Works in memory, on disk, remote and remote with multiple users. (downside: Search is currently in O(n))
20+
* use - universal string encoder (also: *python*, *go*) - Multiple sets of utf8 data? Only one String to store it all? Use this.
21+
* mcnp - multi chunk network protocol (also: *go*, *rust*) - protocol for simple, low level cross programming language network communication. Uses TCP-Sockets. (downside: currently lacks async and timeout functionality)
22+
* bitsandbytes - certain helper functionality concerning lower level operations
23+
* date_time - (yeah yeah never build a date api yourself) - a very simplistic date and time api for when dealing with timezones and complex stuff is overkill
24+
* timediffmarker(also: *rust*) and callcounter - fairly dumb debugging tool. Only slightly better than Sys.out's
25+
* asap_queue - fairly dumb 'defer call until possible' queue (uses exceptions for flow control, so use with a strong stomach only)
26+
27+
## Usage
28+
29+
See the java doc commentary on the api's themselves
30+
31+
## License
32+
33+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

UtilityAlgorithms.iml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
import java.util.*;
4+
5+
/**
6+
* "Queue" that stores calls that may be executable at a later time.
7+
* Requires a later call to one of {@link #tryCallAllAsap()}, {@link #callAndRemoveAll_orCancel()}, {@link #callAndRemoveAll_orIgnore()}.
8+
* Has it's applications.
9+
*/
10+
public class ASAP_Queue {
11+
private final HashMap<Integer, PredeterminableCall> once_calls = new HashMap<>();
12+
private final Queue<PredeterminableCall> call_queue = new LinkedList<>();
13+
14+
/**
15+
* @return whether any calls are in the queue
16+
*/
17+
public boolean hasCalls() {
18+
return call_queue.isEmpty() && once_calls.isEmpty();
19+
}
20+
21+
/**
22+
* Will go through each call and attempt to call them.
23+
* If a call cannot be called then it goes back into the queue.
24+
* Unless they indicate that they can never be called.
25+
*/
26+
public void tryCallAllAsap() {
27+
PredeterminableCall call;
28+
while((call = call_queue.poll()) != null) {
29+
callAsap(call);//would re-add on fail, unless can never be called
30+
}
31+
32+
Iterator<Map.Entry<Integer, PredeterminableCall>> once_iter = once_calls.entrySet().iterator();
33+
while(once_iter.hasNext()) {
34+
Map.Entry<Integer, PredeterminableCall> once_call = once_iter.next();
35+
once_iter.remove();
36+
callOnceAsap(once_call.getKey(), once_call.getValue());
37+
}
38+
}
39+
40+
/**
41+
* Will remove every call and attempt each of them one last time.
42+
*/
43+
public void callAndRemoveAll_orIgnore() {
44+
PredeterminableCall call;
45+
while((call = call_queue.poll()) != null) {
46+
tryCall_ignore(call);
47+
}
48+
49+
Iterator<Map.Entry<Integer, PredeterminableCall>> once_iter = once_calls.entrySet().iterator();
50+
while(once_iter.hasNext()) {
51+
Map.Entry<Integer, PredeterminableCall> once_call = once_iter.next();
52+
once_iter.remove();
53+
tryCall_ignore(once_call.getValue());
54+
}
55+
}
56+
57+
/**
58+
* Will remove every call and attempt each of them one last time.
59+
* If a call fails it will throw an exception and not be removed.
60+
* @throws CannotBeExecutedException if a call cannot be executed
61+
*/
62+
public void callAndRemoveAll_orCancel() throws CannotBeExecutedException {
63+
Iterator<PredeterminableCall> queue_iter = call_queue.iterator();
64+
while(queue_iter.hasNext()) {
65+
PredeterminableCall call = queue_iter.next();
66+
tryCall(call);
67+
queue_iter.remove();
68+
}
69+
70+
Iterator<Map.Entry<Integer, PredeterminableCall>> once_iter = once_calls.entrySet().iterator();
71+
while(once_iter.hasNext()) {
72+
Map.Entry<Integer, PredeterminableCall> once_call = once_iter.next();
73+
tryCall(once_call.getValue());
74+
once_iter.remove();
75+
}
76+
}
77+
78+
79+
public void callPostponed(Call call) {
80+
callPostponed(PredeterminableCall.fromCall(call));
81+
}
82+
public void callPostponed(PredeterminableCall call) {
83+
call_queue.add(call);
84+
}
85+
86+
public boolean callAsap(Call call) {
87+
return callAsap(PredeterminableCall.fromCall(call));
88+
}
89+
public boolean callAsap(PredeterminableCall call) {
90+
try {
91+
if(!call.canBeCalled()) {
92+
callPostponed(call);
93+
return false;
94+
} else if(!call.canEverBeCalled()) {
95+
return false;
96+
} else {
97+
call.call();
98+
return true;
99+
}
100+
} catch (CannotBeExecutedYetException e) {
101+
callPostponed(call);
102+
return false;
103+
} catch (CanNeverBeExecutedException e) {
104+
return false;
105+
}
106+
}
107+
108+
public void callOncePostponed(int uid, Call call) {
109+
callOncePostponed(new Integer(uid), PredeterminableCall.fromCall(call));
110+
}
111+
public void callOncePostponed(int uid, PredeterminableCall call) {
112+
callOncePostponed(new Integer(uid), call);
113+
}
114+
private void callOncePostponed(Integer uid, PredeterminableCall call) {
115+
once_calls.put(uid, call);
116+
}
117+
118+
public boolean callOnceAsap(int uid, Call call) {
119+
return callOnceAsap(new Integer(uid), PredeterminableCall.fromCall(call));
120+
}
121+
public boolean callOnceAsap(int uid, PredeterminableCall call) {
122+
return callOnceAsap(new Integer(uid), call);
123+
}
124+
private boolean callOnceAsap(Integer uid, PredeterminableCall call) {
125+
try {
126+
if(!call.canBeCalled()) {
127+
callOncePostponed(uid, call);
128+
return false;
129+
} else if(!call.canEverBeCalled()) {
130+
return false;
131+
} else {
132+
call.call();
133+
return true;
134+
}
135+
} catch (CannotBeExecutedYetException e) {
136+
callOncePostponed(uid, call);
137+
return false;
138+
} catch (CanNeverBeExecutedException e) {
139+
return false;
140+
}
141+
}
142+
143+
private void tryCall(PredeterminableCall call) throws CannotBeExecutedYetException, CanNeverBeExecutedException {
144+
if(!call.canBeCalled()) {
145+
throw new CannotBeExecutedYetException();
146+
} else if(!call.canEverBeCalled()) {
147+
throw new CanNeverBeExecutedException();
148+
} else {
149+
call.call();
150+
}
151+
}
152+
private void tryCall_ignore(PredeterminableCall call) {
153+
if(call.canBeCalled() && call.canEverBeCalled()) {
154+
try {
155+
call.call();
156+
} catch (CannotBeExecutedYetException | CanNeverBeExecutedException ignored) {}
157+
}
158+
}
159+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
/**
4+
* A call
5+
* @author jokrey
6+
*/
7+
@FunctionalInterface
8+
public interface Call {
9+
/**
10+
* Execute the call
11+
* @throws CannotBeExecutedYetException if the call is impossible at this time
12+
* @throws CanNeverBeExecutedException if the call can never be executed - will remove the call from the queue
13+
*/
14+
void call() throws CannotBeExecutedYetException, CanNeverBeExecutedException;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
/**
4+
* Used as flow control when the call can never be executed and retrying is futile.
5+
* The call will therefore be removed from the queue, if this exception was thrown.
6+
*
7+
* (yeah, yeah i know: Exceptions as flow control is bad.. But there is an option not to use exceptions in {@link PredeterminableCall}).
8+
* @author jokrey
9+
*/
10+
public class CanNeverBeExecutedException extends CannotBeExecutedException {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
/**
4+
* Used as flow control when the call can't be executed at this point for any reason
5+
*
6+
* (yeah, yeah i know: Exceptions as flow control is bad.. But there is an option not to use exceptions in {@link PredeterminableCall}).
7+
* @author jokrey
8+
*/
9+
public class CannotBeExecutedException extends Exception {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
/**
4+
* Used as flow control when the call can't be executed yet, but might be executable at a later time.
5+
*
6+
* (yeah, yeah i know: Exceptions as flow control is bad.. But there is an option not to use exceptions in {@link PredeterminableCall}).
7+
* @author jokrey
8+
*/
9+
public class CannotBeExecutedYetException extends CannotBeExecutedException {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package jokrey.utilities.asap_queue;
2+
3+
/**
4+
* Non exceptions as flow control version of {@link Call}.
5+
*
6+
* @author jokrey
7+
*/
8+
9+
public interface PredeterminableCall extends Call {
10+
/**
11+
* If it is previously determinable whether or not a call is possible, then return false here.
12+
*
13+
* (inverted pre query version of {@link CannotBeExecutedYetException}
14+
* @return whether the call is possible
15+
*/
16+
boolean canBeCalled();
17+
18+
/**
19+
* If it is previously determinable whether or not a call is never possible, then return false here.
20+
* If false is returned then the call will be removed from the queue.
21+
*
22+
* (inverted pre query version of {@link CanNeverBeExecutedException}
23+
* @return whether the call will ever be possible
24+
*/
25+
boolean canEverBeCalled();
26+
27+
static PredeterminableCall fromCall(Call call) {
28+
return new PredeterminableCall() {
29+
@Override public void call() throws CannotBeExecutedYetException, CanNeverBeExecutedException {
30+
call.call();
31+
}
32+
@Override public boolean canBeCalled() { return true; }
33+
@Override public boolean canEverBeCalled() {
34+
return true;
35+
}
36+
};
37+
}
38+
}

0 commit comments

Comments
 (0)