-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fjn
committed
Sep 12, 2022
1 parent
97196b6
commit ab58010
Showing
14 changed files
with
259 additions
and
39 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
langx-java/src/main/java/com/jn/langx/lifecycle/DestroyableCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.lifecycle; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
import com.jn.langx.util.io.close.AbstractCloser; | ||
|
||
import java.util.List; | ||
|
||
public class DestroyableCloser extends AbstractCloser<Destroyable> { | ||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Destroyable.class); | ||
} | ||
|
||
@Override | ||
protected void doClose(Destroyable destroyable) throws Throwable { | ||
destroyable.destroy(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
langx-java/src/main/java/com/jn/langx/lifecycle/LifecycleCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.lifecycle; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
import com.jn.langx.util.io.close.AbstractCloser; | ||
|
||
import java.util.List; | ||
|
||
public class LifecycleCloser extends AbstractCloser<Lifecycle> { | ||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Lifecycle.class); | ||
} | ||
|
||
@Override | ||
protected void doClose(Lifecycle lifecycle) throws Throwable { | ||
lifecycle.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.jn.langx.util.io; | ||
|
||
import java.util.List; | ||
|
||
public interface Closer<I> { | ||
void close(I i); | ||
List<Class> applyTo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
langx-java/src/main/java/com/jn/langx/util/io/close/AbstractCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.annotation.NonNull; | ||
import com.jn.langx.util.io.Closer; | ||
import com.jn.langx.util.logging.Loggers; | ||
import com.jn.langx.util.reflect.Reflects; | ||
|
||
|
||
public abstract class AbstractCloser<I> implements Closer<I> { | ||
|
||
@Override | ||
public void close(I i) { | ||
if (i != null) { | ||
try { | ||
doClose(i); | ||
} catch (Throwable e) { | ||
Loggers.getLogger(getClass()).warn("error occur when close {}, error: {}", Reflects.getFQNClassName(i.getClass()), e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void doClose(@NonNull I i) throws Throwable; | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
langx-java/src/main/java/com/jn/langx/util/io/close/CloseableCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
|
||
import com.jn.langx.util.collection.Collects; | ||
|
||
import java.io.Closeable; | ||
import java.util.List; | ||
|
||
public class CloseableCloser extends AbstractCloser<Closeable> { | ||
@Override | ||
protected void doClose(Closeable closeable) throws Throwable{ | ||
closeable.close(); | ||
} | ||
|
||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Closeable.class); | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
langx-java/src/main/java/com/jn/langx/util/io/close/ConnectionCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
|
||
import java.io.Closeable; | ||
import java.sql.Connection; | ||
import java.util.List; | ||
|
||
public class ConnectionCloser extends AbstractCloser<Connection> { | ||
@Override | ||
protected void doClose(Connection connection) throws Throwable { | ||
connection.close(); | ||
} | ||
|
||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Connection.class); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
langx-java/src/main/java/com/jn/langx/util/io/close/ForceCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
import com.jn.langx.util.reflect.Reflects; | ||
|
||
import java.util.List; | ||
|
||
public class ForceCloser extends AbstractCloser<Object> { | ||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Object.class); | ||
} | ||
|
||
@Override | ||
protected void doClose(Object o) throws Throwable { | ||
Reflects.invokeAnyMethodForcedIfPresent(o, "close", null, null); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
langx-java/src/main/java/com/jn/langx/util/io/close/ObjectCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.util.Preconditions; | ||
import com.jn.langx.util.collection.Collects; | ||
import com.jn.langx.util.function.Consumer; | ||
import com.jn.langx.util.function.Predicate; | ||
import com.jn.langx.util.io.Closer; | ||
import com.jn.langx.util.reflect.Reflects; | ||
|
||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
|
||
public class ObjectCloser { | ||
private static Map<Class, Closer> closerMap = Collects.emptyHashMap(true); | ||
private static ForceCloser forceCloser = new ForceCloser(); | ||
|
||
static { | ||
Collects.forEach(ServiceLoader.load(Closer.class), new Consumer<Closer>() { | ||
@Override | ||
public void accept(final Closer closer) { | ||
register(closer); | ||
} | ||
}); | ||
} | ||
|
||
public static void register(final Closer closer) { | ||
if (closer != null) { | ||
Collects.forEach(closer.applyTo(), new Consumer<Class>() { | ||
@Override | ||
public void accept(Class aClass) { | ||
closerMap.put(aClass, closer); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static void close(Object obj) { | ||
if (obj == null) { | ||
return; | ||
} | ||
Class type = obj.getClass(); | ||
findCloser(type).close(obj); | ||
} | ||
|
||
private static Closer findCloser(final Class type) { | ||
Preconditions.checkNotNull(type); | ||
Closer closer = closerMap.get(type); | ||
if (closer == null) { | ||
Class t = Collects.findFirst(closerMap.keySet(), new Predicate<Class>() { | ||
@Override | ||
public boolean test(Class expectClass) { | ||
return Reflects.isSubClassOrEquals(expectClass, type); | ||
} | ||
}); | ||
|
||
if (t != null) { | ||
closer = closerMap.get(t); | ||
} | ||
} | ||
|
||
if (closer == null) { | ||
closer = forceCloser; | ||
} | ||
|
||
return closer; | ||
} | ||
|
||
|
||
} |
18 changes: 18 additions & 0 deletions
18
langx-java/src/main/java/com/jn/langx/util/io/close/ResultSetCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
|
||
import java.sql.ResultSet; | ||
import java.util.List; | ||
|
||
public class ResultSetCloser extends AbstractCloser<ResultSet> { | ||
@Override | ||
protected void doClose(ResultSet resultSet) throws Throwable { | ||
resultSet.close(); | ||
} | ||
|
||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(ResultSet.class); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
langx-java/src/main/java/com/jn/langx/util/io/close/StatementCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.util.io.close; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
|
||
import java.sql.Statement; | ||
import java.util.List; | ||
|
||
public class StatementCloser extends AbstractCloser<Statement>{ | ||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(Statement.class); | ||
} | ||
|
||
@Override | ||
protected void doClose(Statement statement) throws Throwable { | ||
statement.close(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
langx-java/src/main/resources/META-INF/services/com.jn.langx.util.io.Closer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
com.jn.langx.util.io.close.CloseableCloser | ||
com.jn.langx.lifecycle.LifecycleCloser | ||
com.jn.langx.lifecycle.DestroyableCloser | ||
com.jn.langx.util.io.close.ConnectionCloser | ||
com.jn.langx.util.io.close.ResultSetCloser | ||
com.jn.langx.util.io.close.StatementCloser |
18 changes: 18 additions & 0 deletions
18
langx-java8/src/main/java/com/jn/langx/java8/util/io/AutoCloseableCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jn.langx.java8.util.io; | ||
|
||
import com.jn.langx.util.collection.Collects; | ||
import com.jn.langx.util.io.close.AbstractCloser; | ||
|
||
import java.util.List; | ||
|
||
public class AutoCloseableCloser extends AbstractCloser<AutoCloseable> { | ||
@Override | ||
public List<Class> applyTo() { | ||
return Collects.<Class>newArrayList(AutoCloseable.class); | ||
} | ||
|
||
@Override | ||
protected void doClose(AutoCloseable autoCloseable) throws Throwable { | ||
autoCloseable.close(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
langx-java8/src/main/resources/META-INF/services/com.jn.langx.util.io.Closer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.jn.langx.java8.util.io.AutoCloseableCloser |