Introduce commons-manifold

This commit is contained in:
Johannes Frohnmeyer 2022-11-24 14:55:39 +01:00
parent d1210e2f10
commit e87d46153f
Signed by: Johannes
GPG Key ID: E76429612C2929F4
11 changed files with 172 additions and 0 deletions

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# JfCommons
Common code for my java projects. Uses my common [build scripts](https://git.frohnmeyer-wds.de/Johannes/Scripts).
## Modules
- commons: Common java code without a major theme or external dependencies
- [muscript](https://git.frohnmeyer-wds.de/Johannes/java-commons/src/branch/master/muscript): A simple scripting language
- commons-slf4j: SLF4J bindings for the logging abstraction in commons. Supports using commons as a backend for slf and slf as a commons backend.
- commons-gson: Shades my fork of gson and provides some utility classes
- commons-manifold: Some common code using the features of manifold-ext. Mainly extension classes.

View File

@ -5,8 +5,10 @@ plugins {
repositories {
mavenCentral()
gradlePluginPortal()
maven("https://maven.frohnmeyer-wds.de/artifacts")
}
dependencies {
implementation("org.gradle.kotlin:gradle-kotlin-dsl-plugins:2.3.3")
implementation("io.gitlab.jfronny:convention:1.2-SNAPSHOT")
}

View File

@ -0,0 +1,19 @@
plugins {
id("commons.library")
id("jf.manifold")
}
dependencies {
implementation(project(":"))
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "io.gitlab.jfronny"
artifactId = "commons-manifold"
from(components["java"])
}
}
}

View File

@ -0,0 +1,14 @@
package commons.extensions.java.lang.Iterable;
import manifold.ext.rt.api.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@Extension
@Structural
public class IterableExt {
public static <T> Stream<T> stream(@This Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
}

View File

@ -0,0 +1,18 @@
package commons.extensions.java.util.Map;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import java.util.Iterator;
import java.util.Map;
@Extension
public abstract class MapExt<K, V> implements Iterable<Map.Entry<K, V>> {
public static <K, V> Iterator<Map.Entry<K,V>> iterator(@This Map<K, V> thiz) {
return thiz.entrySet().iterator();
}
public static <K, V> V set(@This Map<K, V> thiz, K key, V value) {
return thiz.put(key, value);
}
}

View File

@ -0,0 +1,7 @@
package commons.extensions.java.util.stream.BaseStream;
import manifold.ext.rt.api.Extension;
@Extension
public abstract class BaseStreamExt<T> implements Iterable<T> {
}

View File

@ -0,0 +1,44 @@
package commons.extensions.java.util.stream.Stream;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Extension
public class StreamExt {
public static <T> Set<T> toSet(@This Stream<T> thiz) {
return thiz.collect(LinkedHashSet::new, Set::add, Set::addAll);
}
public static <T, K, V> Map<K, V> toMap(@This Stream<T> thiz, Function<? super T, K> keyMapper, Function<? super T, V> valueMapper) {
return thiz.collect(Collectors.toMap(keyMapper, valueMapper));
}
public static <T, K> Map<K, T> toMap(@This Stream<T> thiz, Function<? super T, K> keyMapper) {
return thiz.toMap(keyMapper, Function.identity());
}
public static <T, V> Map<V, List<T>> groupingBy(@This Stream<T> thiz, Function<? super T, V> valueMapper) {
return thiz.collect(Collectors.groupingBy(valueMapper));
}
public static String join(@This Stream<String> thiz) {
return thiz.collect(Collectors.joining());
}
public static String join(@This Stream<String> thiz, String delimiter) {
return thiz.collect(Collectors.joining(delimiter));
}
public static String join(@This Stream<String> thiz, char delimiter) {
return thiz.join("" + delimiter);
}
public static <T> Stream<T> concat(@This Stream<T> thiz, Stream<T> other) {
return Stream.concat(thiz, other);
}
}

View File

@ -0,0 +1,14 @@
package commons.extensions.org.w3c.dom.Node;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import org.w3c.dom.Node;
@Extension
public class NodeExt {
public static boolean isWhitespace(@This Node thiz) {
if (thiz.nodeType == Node.TEXT_NODE && thiz.textContent.isBlank()) return true;
if (thiz.nodeType == Node.COMMENT_NODE) return true;
return false;
}
}

View File

@ -0,0 +1,35 @@
package commons.extensions.org.w3c.dom.NodeList;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Iterator;
import java.util.NoSuchElementException;
@Extension
public abstract class NodeListExt implements Iterable<Node> {
public static Iterator<Node> iterator(@This NodeList thiz) {
return new Iterator<>() {
private int index = 0;
@Override
public boolean hasNext() {
while (index < thiz.length && thiz[index].isWhitespace()) {
index++;
}
return index < thiz.length;
}
@Override
public Node next() {
if (!hasNext()) throw new NoSuchElementException();
return thiz[index++];
}
};
}
public static Node get(@This NodeList thiz, int index) {
return thiz.item(index);
}
}

View File

@ -0,0 +1,9 @@
package io.gitlab.jfronny.commons.manifold;
public class ManifoldTest {
public static void main(String[] args) {
for (String s : args.stream().stream()) {
System.out.println(s);
}
}
}

View File

@ -1,5 +1,6 @@
rootProject.name = "Commons"
include("commons-gson")
include("commons-manifold")
include("commons-slf4j")
include("muscript")