pull/617/head
laoyuyu 5 years ago
parent a524fd3e5a
commit c9eb57132b
  1. 29
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/ConstantIntercept.java
  2. 29
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoader.java
  3. 41
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/ILoaderInterceptor.java
  4. 75
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderChain.java
  5. 47
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/LoaderIntercept.java
  6. 39
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/RecordInterceptor.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;
}
}

@ -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();
}

@ -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();
}
}

@ -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<ILoaderInterceptor> interceptors;
public LoaderChain(List<ILoaderInterceptor> 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;
}
}

@ -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;
}
}

@ -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();
}
}
Loading…
Cancel
Save