You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
package com.fr.env.detect.impl; |
|
|
|
import com.fr.env.detect.base.ExceptionDetector; |
|
import com.fr.env.detect.bean.DetectorResult; |
|
import com.fr.env.detect.bean.DetectorStatus; |
|
import org.jetbrains.annotations.Nullable; |
|
|
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.List; |
|
import java.util.stream.Collectors; |
|
|
|
/** |
|
* 检测链 |
|
* 责任链模式, 有先后顺序,哪一个先获得,哪一个先终止。 |
|
* |
|
* created by Harrison on 2022/06/16 |
|
**/ |
|
public class DetectorChain { |
|
|
|
private List<ExceptionDetector> detectors = new ArrayList<>(); |
|
|
|
public static DetectorChain construct(ExceptionDetector... detectors) { |
|
|
|
DetectorChain detectorChain = new DetectorChain(); |
|
detectorChain.detectors = Arrays.stream(detectors).collect(Collectors.toList()); |
|
return detectorChain; |
|
} |
|
|
|
@Nullable |
|
public DetectorResult detect() { |
|
|
|
for (ExceptionDetector detector : detectors) { |
|
DetectorResult result = detector.detect(); |
|
if (result != null && result.getStatus() == DetectorStatus.EXCEPTION) { |
|
return result; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
public List<ExceptionDetector> getDetectors() { |
|
|
|
return this.detectors; |
|
} |
|
}
|
|
|