forked from plusone/plusone-commons
Compare commits
2 Commits
feature/ne
...
feature/se
Author | SHA1 | Date | |
---|---|---|---|
dc86c59d91 | |||
eba31a93f3 |
1
pom.xml
1
pom.xml
@@ -10,6 +10,7 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>8</java.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<jackson.version>2.13.5</jackson.version>
|
||||
|
@@ -31,7 +31,8 @@ import xyz.zhouxy.plusone.commons.util.RegexUtil;
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class ValidatableStringRecord {
|
||||
public abstract class ValidatableStringRecord
|
||||
implements Comparable<ValidatableStringRecord> {
|
||||
private final String value;
|
||||
|
||||
protected ValidatableStringRecord(String value, Pattern pattern) {
|
||||
@@ -51,6 +52,11 @@ public abstract class ValidatableStringRecord {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ValidatableStringRecord o) {
|
||||
return this.value.compareTo(o.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value();
|
||||
|
@@ -1,58 +0,0 @@
|
||||
package xyz.zhouxy.plusone.commons.net;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
@Beta
|
||||
public class MoreInetAddresses {
|
||||
|
||||
public static NetworkInterfaceInfo getNetworkInterfaceInfo() throws SocketException {
|
||||
final Enumeration<NetworkInterface> all = NetworkInterface.getNetworkInterfaces();
|
||||
NetworkInterface networkInterface;
|
||||
while (all.hasMoreElements()) {
|
||||
networkInterface = all.nextElement();
|
||||
if (networkInterface.isLoopback() || networkInterface.isVirtual()) {
|
||||
continue;
|
||||
}
|
||||
final byte[] mac = networkInterface.getHardwareAddress();
|
||||
if (mac != null) {
|
||||
return new NetworkInterfaceInfo(networkInterface, mac);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No available network interface found");
|
||||
}
|
||||
|
||||
public static Inet4Address getIpv4(NetworkInterface networkInterface) {
|
||||
final Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
|
||||
InetAddress ip;
|
||||
while (ips.hasMoreElements()) {
|
||||
ip = ips.nextElement();
|
||||
if (!ip.isLoopbackAddress() && (ip instanceof Inet4Address)) {
|
||||
return (Inet4Address) ip;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No available address found");
|
||||
}
|
||||
|
||||
public static Inet6Address getIpv6(NetworkInterface networkInterface) {
|
||||
final Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
|
||||
InetAddress ip;
|
||||
while (ips.hasMoreElements()) {
|
||||
ip = ips.nextElement();
|
||||
if (!ip.isLoopbackAddress() && (ip instanceof Inet6Address)) {
|
||||
return (Inet6Address) ip;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No available address found");
|
||||
}
|
||||
|
||||
private MoreInetAddresses() {
|
||||
throw new UnsupportedOperationException("Utility class");
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
package xyz.zhouxy.plusone.commons.net;
|
||||
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class NetworkInterfaceInfo {
|
||||
private final NetworkInterface networkInterface;
|
||||
private final byte[] mac;
|
||||
|
||||
NetworkInterfaceInfo(NetworkInterface networkInterface, byte[] mac) {
|
||||
this.networkInterface = Objects.requireNonNull(networkInterface);
|
||||
this.mac = Objects.requireNonNull(mac);
|
||||
}
|
||||
|
||||
public NetworkInterface getNetworkInterface() {
|
||||
return networkInterface;
|
||||
}
|
||||
|
||||
public byte[] getMac() {
|
||||
return Arrays.copyOf(this.mac, this.mac.length);
|
||||
}
|
||||
|
||||
public String getMacStr() {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
String s;
|
||||
for (int i = 0; i < this.mac.length; i++) {
|
||||
if (i != 0) {
|
||||
result.append("-");
|
||||
}
|
||||
s = Integer.toHexString(this.mac[i] & 0xFF);
|
||||
if (s.length() == 1) {
|
||||
result.append("0");
|
||||
}
|
||||
result.append(s);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
133
src/main/java/xyz/zhouxy/plusone/commons/seq/Seq.java
Normal file
133
src/main/java/xyz/zhouxy/plusone/commons/seq/Seq.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package xyz.zhouxy.plusone.commons.seq;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
@Beta
|
||||
@FunctionalInterface
|
||||
public interface Seq<T> extends Consumer<Consumer<T>> {
|
||||
// 这个方法和 forEach 是完全等价的
|
||||
void accept(Consumer<T> consumer);
|
||||
|
||||
static <T> Seq<T> from(Collection<T> collection) {
|
||||
return collection::forEach;
|
||||
}
|
||||
|
||||
static <T> Seq<T> unit(T t) {
|
||||
return c -> c.accept(t);
|
||||
}
|
||||
|
||||
default <E> Seq<E> map(Function<T, E> func) {
|
||||
Seq<T> srcSeq = this;
|
||||
return c -> srcSeq.accept((T t) -> c.accept(func.apply(t)));
|
||||
}
|
||||
|
||||
default <E> Seq<E> flatMap(Function<T, Seq<E>> function) {
|
||||
Seq<T> srcSeq = this;
|
||||
return c -> srcSeq.accept(t -> function.apply(t).accept(c));
|
||||
}
|
||||
|
||||
default Seq<T> filter(Predicate<T> predicate) {
|
||||
Seq<T> srcSeq = this;
|
||||
return (Consumer<T> c) -> srcSeq.accept((T t) -> {
|
||||
if (predicate.test(t)) {
|
||||
c.accept(t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void stop() {
|
||||
throw StopException.INSTANCE;
|
||||
}
|
||||
|
||||
default void consumeTillStop(Consumer<T> consumer) {
|
||||
try {
|
||||
accept(consumer);
|
||||
} catch (StopException ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
default Seq<T> take(int n) {
|
||||
return c -> {
|
||||
int[] i = { n };
|
||||
consumeTillStop(t -> {
|
||||
if (i[0]-- > 0) {
|
||||
c.accept(t);
|
||||
} else {
|
||||
stop();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
default Seq<T> drop(int n) {
|
||||
return c -> {
|
||||
int[] a = { n - 1 };
|
||||
accept(t -> {
|
||||
if (a[0] < 0) {
|
||||
c.accept(t);
|
||||
} else {
|
||||
a[0]--;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
default Seq<T> onEach(Consumer<T> consumer) {
|
||||
Seq<T> srcSeq = this;
|
||||
return c -> srcSeq.accept(consumer.andThen(c));
|
||||
}
|
||||
|
||||
default <E, R> Seq<R> zip(Iterable<E> iterable, BiFunction<T, E, R> function) {
|
||||
return c -> {
|
||||
Iterator<E> iterator = iterable.iterator();
|
||||
consumeTillStop(t -> {
|
||||
if (iterator.hasNext()) {
|
||||
c.accept(function.apply(t, iterator.next()));
|
||||
} else {
|
||||
stop();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
default String join(String sep) {
|
||||
StringJoiner joiner = new StringJoiner(sep);
|
||||
accept(t -> joiner.add(t.toString()));
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
default List<T> toList() {
|
||||
ImmutableList.Builder<T> builder = ImmutableList.builder();
|
||||
accept(builder::add);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
default Set<T> toSet() {
|
||||
ImmutableSet.Builder<T> set = ImmutableSet.builder();
|
||||
accept(set::add);
|
||||
return set.build();
|
||||
}
|
||||
|
||||
static class StopException extends RuntimeException {
|
||||
private static final long serialVersionUID = -8975705275260458012L;
|
||||
private static final StopException INSTANCE = new StopException();
|
||||
|
||||
@Override
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,6 +10,11 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
class ValidatableStringRecordTests {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ValidatableStringRecordTests.class);
|
||||
@@ -21,6 +26,16 @@ class ValidatableStringRecordTests {
|
||||
String usernameStr = username.value();
|
||||
assertNotNull(usernameStr);
|
||||
log.info("usernameStr: {}", usernameStr);
|
||||
|
||||
List<Username> usernames = Arrays.asList(
|
||||
Username.of("ZhouXY108"),
|
||||
Username.of("code_108"),
|
||||
Username.of("Luquan"),
|
||||
Username.of("Code108")
|
||||
);
|
||||
log.info("{}", Collections.max(usernames));
|
||||
log.info("{}", Collections.max(usernames,
|
||||
Comparator.<Username, String>comparing(o -> o.value().toLowerCase())));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,28 +0,0 @@
|
||||
package xyz.zhouxy.plusone.commons.net;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
class MoreInetAddressesTests {
|
||||
|
||||
@Test
|
||||
void testGetNetworkInterfaceInfo() throws SocketException {
|
||||
NetworkInterfaceInfo networkInterfaceInfo = MoreInetAddresses.getNetworkInterfaceInfo();
|
||||
String macStr = networkInterfaceInfo.getMacStr();
|
||||
log.info("mac: {}", macStr);
|
||||
assertEquals(NetUtil.getLocalMacAddress(), macStr);
|
||||
NetworkInterface networkInterface = networkInterfaceInfo.getNetworkInterface();
|
||||
String ipv4 = MoreInetAddresses.getIpv4(networkInterface).getHostAddress();
|
||||
log.info("ipv4: {}", ipv4);
|
||||
String ipv6 = MoreInetAddresses.getIpv6(networkInterface).getHostAddress();
|
||||
log.info("ipv6: {}", ipv6);
|
||||
}
|
||||
}
|
56
src/test/java/xyz/zhouxy/plusone/commons/seq/SeqTests.java
Normal file
56
src/test/java/xyz/zhouxy/plusone/commons/seq/SeqTests.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package xyz.zhouxy.plusone.commons.seq;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
class SeqTests {
|
||||
|
||||
@Test
|
||||
void testSeq() {
|
||||
List<Integer> list = ImmutableList.of(108, 2333, 666, 555, 1, 2, 3, 23, 78, 65, 77, 22, 108);
|
||||
List<String> result = new ArrayList<>();
|
||||
Seq<String> targetSeq = new Seq<String>() {
|
||||
@Override
|
||||
public void accept(Consumer<String> c) {
|
||||
list.forEach(t -> {
|
||||
c.accept(String.valueOf(t + 250));
|
||||
});
|
||||
}
|
||||
};
|
||||
targetSeq.accept(result::add);
|
||||
// list.forEach(t -> {
|
||||
// result.add(t + 250 + "");
|
||||
// });
|
||||
log.info("{}", result);
|
||||
|
||||
for (Integer i : produceEvenNumbers(9)) {
|
||||
System.out.print(i);
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
Set<String> set = new TreeSet<>();
|
||||
Seq<String> s = result::forEach;
|
||||
s.accept(set::add);
|
||||
log.info("{}", set);
|
||||
}
|
||||
|
||||
Iterable<Integer> produceEvenNumbers(int upto) {
|
||||
Seq<Integer> seq = c -> {
|
||||
for (int i = 0; i <= upto; i += 2) {
|
||||
c.accept(i);
|
||||
}
|
||||
};
|
||||
return seq.toList();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user