upgrade Try.java

dev
ehlxr 2021-02-04 15:39:36 +08:00
parent b23ae1c75e
commit f8cd382ba9
3 changed files with 272 additions and 83 deletions

View File

@ -0,0 +1,93 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* @author ehlxr
* @since 2021-01-28 16:03.
*/
public class Main {
public static void main(String[] args) {
// 旧 API 转新 API
// Date -> Instant:
Instant ins1 = new Date().toInstant();
System.out.println(ins1);
ZonedDateTime zonedDateTime = ins1.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println(localDateTime);
// Calendar -> Instant -> ZonedDateTime:
Calendar calendar = Calendar.getInstance();
Instant ins2 = calendar.toInstant();
ZonedDateTime zdt = ins2.atZone(calendar.getTimeZone().toZoneId());
System.out.println(zdt);
// 新 API 转旧 API
// ZonedDateTime -> long:
ZonedDateTime zdt1 = ZonedDateTime.now();
long ts = zdt1.toEpochSecond() * 1000;
// long -> Date:
Date date = new Date(ts);
// long -> Calendar:
Calendar calendar1 = Calendar.getInstance();
calendar1.clear();
calendar1.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId()));
calendar1.setTimeInMillis(zdt.toEpochSecond() * 1000);
// 在数据库中存储时间戳时,尽量使用 long 型时间戳,它具有省空间,效率高,不依赖数据库的优点
ts = 1574208900000L;
System.out.println(timestampToString(ts, Locale.CHINA, "Asia/Shanghai"));
System.out.println(timestampToString(ts, Locale.US, "America/New_York"));
}
/**
*
*/
static String timestampToString(long epochMilli, Locale lo, String zoneId) {
Instant ins = Instant.ofEpochMilli(epochMilli);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT)
// 按照 Locale 默认习惯格式化
.withLocale(lo);
return f.withLocale(lo).format(ZonedDateTime.ofInstant(ins, ZoneId.of(zoneId)));
}
}

View File

@ -0,0 +1,96 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @author ehlxr
* @since 2021-02-04 14:40.
*/
public class Builder<T> {
private final Supplier<T> instantiator;
private final List<Consumer<T>> modifiers = new ArrayList<>();
public Builder(Supplier<T> instantiator) {
this.instantiator = instantiator;
}
public static <T> Builder<T> of(Supplier<T> instantiator) {
return new Builder<>(instantiator);
}
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
return this;
}
public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
modifiers.add(c);
return this;
}
public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
modifiers.add(c);
return this;
}
public T build() {
T value = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
/**
* 1 Consumer
*/
@FunctionalInterface
public interface Consumer1<T, P1> {
void accept(T t, P1 p1);
}
/**
* 2 Consumer
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
void accept(T t, P1 p1, P2 p2);
}
/**
* 3 Consumer
*/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
void accept(T t, P1 p1, P2 p2, P3 p3);
}
}

View File

@ -40,10 +40,10 @@ public interface Try {
* Tryable
*
* @param consumer {@link ThrowableConsumer}
* @param <T>
* @param <P>
* @return {@link TryConsumer}
*/
static <T> TryConsumer<T> of(ThrowableConsumer<? super T> consumer) {
static <P> TryConsumer<P> of(ThrowableConsumer<? super P> consumer) {
return new TryConsumer<>(consumer);
}
@ -62,11 +62,11 @@ public interface Try {
* Tryable
*
* @param function {@link ThrowableFunction}
* @param <T>
* @param <P>
* @param <R>
* @return {@link TryFunction}
*/
static <T, R> TryFunction<T, R> of(ThrowableFunction<? super T, ? extends R> function) {
static <P, R> TryFunction<P, R> of(ThrowableFunction<? super P, ? extends R> function) {
return new TryFunction<>(function);
}
@ -160,32 +160,15 @@ public interface Try {
}
}
class TryConsumer<T> extends Tryable<TryConsumer<T>> {
private final ThrowableConsumer<? super T> consumer;
protected TryConsumer(ThrowableConsumer<? super T> consumer) {
Objects.requireNonNull(consumer, "No consumer present");
this.consumer = consumer;
super.c = this;
}
@FunctionalInterface
interface ThrowableConsumer<P> {
/**
*
* Performs this operation on the given argument.
*
* @param t
* @param p the input argument
* @throws Throwable throwable
*/
public void accept(T t) {
try {
Objects.requireNonNull(t, "No accept t present");
consumer.accept(t);
} catch (final Throwable e) {
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
} finally {
doFinally();
}
}
void accept(P p) throws Throwable;
}
class TrySupplier<R> extends Tryable<TrySupplier<R>> {
@ -232,11 +215,72 @@ public interface Try {
}
}
class TryFunction<T, R> extends Tryable<TryFunction<T, R>> {
private final ThrowableFunction<? super T, ? extends R> function;
private T t;
@FunctionalInterface
interface ThrowableSupplier<P> {
/**
* Gets a result.
*
* @return a result
* @throws Throwable throwable
*/
P get() throws Throwable;
}
protected TryFunction(ThrowableFunction<? super T, ? extends R> function) {
@FunctionalInterface
interface ThrowableRunnable {
/**
* Performs this operation
*
* @throws Throwable throwable
*/
void run() throws Throwable;
}
@FunctionalInterface
interface ThrowableFunction<P, R> {
/**
* Applies this function to the given argument.
*
* @param p the function argument
* @return the function result
* @throws Throwable throwable
*/
R apply(P p) throws Throwable;
}
class TryConsumer<P> extends Tryable<TryConsumer<P>> {
private final ThrowableConsumer<? super P> consumer;
protected TryConsumer(ThrowableConsumer<? super P> consumer) {
Objects.requireNonNull(consumer, "No consumer present");
this.consumer = consumer;
super.c = this;
}
/**
*
*
* @param p
*/
public void accept(P p) {
try {
Objects.requireNonNull(p, "No accept p present");
consumer.accept(p);
} catch (final Throwable e) {
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
} finally {
doFinally();
}
}
}
class TryFunction<P, R> extends Tryable<TryFunction<P, R>> {
private final ThrowableFunction<? super P, ? extends R> function;
private P p;
protected TryFunction(ThrowableFunction<? super P, ? extends R> function) {
Objects.requireNonNull(function, "No function present");
this.function = function;
@ -246,13 +290,13 @@ public interface Try {
/**
*
*
* @param t
* @param p
* @return {@link TryFunction}
*/
public TryFunction<T, R> apply(T t) {
Objects.requireNonNull(t, "Apply t should not null");
public TryFunction<P, R> apply(P p) {
Objects.requireNonNull(p, "Apply p should not null");
this.t = t;
this.p = p;
return this;
}
@ -264,9 +308,9 @@ public interface Try {
*/
public R get(R r) {
try {
Objects.requireNonNull(function, "No apply t present");
Objects.requireNonNull(function, "No apply p present");
return function.apply(t);
return function.apply(p);
} catch (final Throwable e) {
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
return r;
@ -282,9 +326,9 @@ public interface Try {
*/
public R get() {
try {
Objects.requireNonNull(t, "No apply t present");
Objects.requireNonNull(p, "No apply p present");
return function.apply(t);
return function.apply(p);
} catch (final Throwable e) {
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
return null;
@ -294,50 +338,6 @@ public interface Try {
}
}
@FunctionalInterface
interface ThrowableRunnable {
/**
* Performs this operation
*
* @throws Throwable throwable
*/
void run() throws Throwable;
}
@FunctionalInterface
interface ThrowableConsumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
* @throws Throwable throwable
*/
void accept(T t) throws Throwable;
}
@FunctionalInterface
interface ThrowableSupplier<T> {
/**
* Gets a result.
*
* @return a result
* @throws Throwable throwable
*/
T get() throws Throwable;
}
@FunctionalInterface
interface ThrowableFunction<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
* @throws Throwable throwable
*/
R apply(T t) throws Throwable;
}
@SuppressWarnings({"ConstantConditions", "Convert2MethodRef"})
static void main(String[] args) {
// 有返回值,无入参