From c9eb57132bf091072c16ade56082256df2194a67 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Mon, 23 Dec 2019 20:27:42 +0800 Subject: [PATCH] intercept --- .../aria/core/loader/ConstantIntercept.java | 29 +++++++ .../com/arialyy/aria/core/loader/ILoader.java | 29 +++++++ .../aria/core/loader/ILoaderInterceptor.java | 41 ++++++++++ .../arialyy/aria/core/loader/LoaderChain.java | 75 +++++++++++++++++++ .../aria/core/loader/LoaderIntercept.java | 47 ++++++++++++ .../aria/core/loader/RecordInterceptor.java | 39 ++++++++++ 6 files changed, 260 insertions(+) create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/ConstantIntercept.java create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoader.java create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoaderInterceptor.java create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderChain.java create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderIntercept.java create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/RecordInterceptor.java diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ConstantIntercept.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ConstantIntercept.java new file mode 100644 index 00000000..16df241a --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ConstantIntercept.java @@ -0,0 +1,29 @@ + +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +/** + * 用于初始化一些常用的常量 + */ +public final class ConstantIntercept implements ILoaderInterceptor { + + + + @Override public ILoader intercept(Chain chain) { + return null; + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoader.java new file mode 100644 index 00000000..d6bfda0f --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoader.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +public interface ILoader { + + void start(); + + void stop(); + + void isBreak(); + + String getKey(); + + long getCurrentProgress(); +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoaderInterceptor.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoaderInterceptor.java new file mode 100644 index 00000000..a6a4adfc --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoaderInterceptor.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.wrapper.ITaskWrapper; + +/** + * 拦截器 + */ +public interface ILoaderInterceptor { + + ILoader intercept(Chain chain); + + interface Chain { + + void updateRecord(TaskRecord record); + + TaskRecord getRecord(); + + IEventListener getListener(); + + ITaskWrapper getWrapper(); + + ILoader proceed(); + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderChain.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderChain.java new file mode 100644 index 00000000..5956e73e --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderChain.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import java.util.List; + +/** + * 责任链 + */ +public final class LoaderChain implements ILoaderInterceptor.Chain { + private ITaskWrapper wrapper; + private IEventListener listener; + private TaskRecord taskRecord; + private int index; + private List interceptors; + + public LoaderChain(List interceptors, ITaskWrapper wrapper, + IEventListener listener, TaskRecord taskRecord, + int index) { + this.interceptors = interceptors; + this.wrapper = wrapper; + this.listener = listener; + this.taskRecord = taskRecord; + this.index = index; + } + + @Override public void updateRecord(TaskRecord record) { + this.taskRecord = record; + } + + @Override public TaskRecord getRecord() { + return taskRecord; + } + + @Override public IEventListener getListener() { + return listener; + } + + @Override public ITaskWrapper getWrapper() { + return wrapper; + } + + @Override public ILoader proceed() { + int index = this.index + 1; + if (index >= interceptors.size()) { + throw new AssertionError(); + } + + LoaderChain next = new LoaderChain(interceptors, wrapper, listener, taskRecord, index); + ILoaderInterceptor interceptor = interceptors.get(index); + ILoader loader = interceptor.intercept(next); + + if (loader == null) { + throw new NullPointerException("Loader为空"); + } + + return loader; + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderIntercept.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderIntercept.java new file mode 100644 index 00000000..dc8582e4 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderIntercept.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +public class LoaderIntercept implements ILoaderInterceptor, ILoader { + + + + @Override public ILoader intercept(Chain chain) { + + + return this; + } + + @Override public void start() { + + } + + @Override public void stop() { + + } + + @Override public void isBreak() { + + } + + @Override public String getKey() { + return null; + } + + @Override public long getCurrentProgress() { + return 0; + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/RecordInterceptor.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/RecordInterceptor.java new file mode 100644 index 00000000..98c0f0bd --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/RecordInterceptor.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.loader; + +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.inf.IRecordHandlerAdapter; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; + +/** + * 任务记录拦截器,用于处理任务记录 + */ +public final class RecordInterceptor implements ILoaderInterceptor { + private IRecordHandlerAdapter adapter; + + public RecordInterceptor(IRecordHandlerAdapter adapter) { + this.adapter = adapter; + } + + @Override public ILoader intercept(Chain chain) { + RecordHandler recordHandler = new RecordHandler((AbsTaskWrapper) chain.getWrapper()); + recordHandler.setAdapter(adapter); + chain.updateRecord(recordHandler.getRecord()); + + return chain.proceed(); + } +}