下面列出了java.util.concurrent.CopyOnWriteArrayList#isEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData,
final NgramContext ngramContext, final long proximityInfoHandle,
final SettingsValuesForSuggestion settingsValuesForSuggestion,
final int sessionId, final float weightForLocale,
final float[] inOutWeightOfLangModelVsSpatialModel) {
final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first
// dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composedData,
ngramContext, proximityInfoHandle, settingsValuesForSuggestion, sessionId,
weightForLocale, inOutWeightOfLangModelVsSpatialModel);
if (null == suggestions) suggestions = new ArrayList<>();
final int length = dictionaries.size();
for (int i = 1; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(
composedData, ngramContext, proximityInfoHandle, settingsValuesForSuggestion,
sessionId, weightForLocale, inOutWeightOfLangModelVsSpatialModel);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
}
public boolean hasSubscriberForEvent(Class<?> eventClass) {
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
if (eventTypes != null) {
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(clazz);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
return true;
}
}
}
return false;
}
/**
* 测试抓取imooc的文章列表。按照抓取的文章条数控制程序停止,并且手动指定待抓取的连接条数
*/
@Test
public void imoocTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.IMOOC);
// 设置用户的id
config.setUid("1175248")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.URL_COUNT)
// 根据ExitWay设置,当ExitWay = URL_COUNT时, count表示待抓取的链接个数;当ExitWay = DURATION时, count表示爬虫运行的时间,理想状态时1s抓取一条,受实际网速影响;当ExitWay = default时,程序不做限制,抓取所有匹配到的文章,“慎用”
// 如果不手动设置该值, 则取ExitWayEnum中默认的数量,URL_COUNT(10),DURATION(60)
.setCount(2);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取csdn的文章列表。按照程序运行的时间(s)控制程序停止,并且手动指定程序运行的时间
*/
@Test
public void csdnTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.CSDN);
// 设置用户的id
config.setUid("u011197448")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.DURATION)
// 根据ExitWay设置,当ExitWay = URL_COUNT时, count表示待抓取的链接个数;当ExitWay = DURATION时, count表示爬虫运行的时间,理想状态时1s抓取一条,受实际网速影响;当ExitWay = default时,程序不做限制,抓取所有匹配到的文章,“慎用”
// 如果不手动设置该值, 则取ExitWayEnum中默认的数量,URL_COUNT(10),DURATION(60)
.setCount(10);
HunterProcessor hunter = new BlogHunterProcessor(config);
System.out.println("程序开始执行:" + new Date());
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
System.out.println("程序执行完毕:" + new Date());
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取iteye的文章列表。按照抓取的文章条数控制程序停止,并使用默认的条数(10条)
*/
@Test
public void iteyeTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.ITEYE);
// 设置用户的id
config.setUid("843977358")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.URL_COUNT);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取cnblogs的文章列表。按照程序运行的时间(s)控制程序停止,并使用默认的时间(60s)
*/
@Test
public void cnblogsTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.CNBLOGS);
// 设置用户的id
config.setUid("zhangyadong")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.DURATION);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取掘金的文章列表
*/
@Test
public void juejinTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.JUEJIN);
// 设置用户的id
config.setUid("5b90662de51d450e8b1370f6")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.URL_COUNT)
.setCount(5);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取v2ex的文章列表,自定义抓取规则
*/
@Test
public void v2exTest2() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.V2EX);
config.setEntryUrls("https://www.v2ex.com/member/Evernote")
.addEntryUrl("https://www.v2ex.com/member/ityouknow")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.DURATION)
// 设定抓取120秒, 如果所有文章都被抓取过了,则会提前停止
.setCount(120);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取oschina的文章列表
*/
@Test
public void oschinaTest() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.OSCHINA);
config.setUid("haitaohu")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.URL_COUNT)
.setCount(5);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
/**
* 测试抓取oschina的文章列表,自定义抓取规则
*/
@Test
public void oschinaTest2() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.V2EX);
config.setEntryUrls("https://my.oschina.net/haitaohu")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.DURATION)
// 设定抓取120秒, 如果所有文章都被抓取过了,则会提前停止
.setCount(120);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData,
final NgramContext ngramContext, final long proximityInfoHandle,
final SettingsValuesForSuggestion settingsValuesForSuggestion,
final int sessionId, final float weightForLocale,
final float[] inOutWeightOfLangModelVsSpatialModel) {
final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first
// dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composedData,
ngramContext, proximityInfoHandle, settingsValuesForSuggestion, sessionId,
weightForLocale, inOutWeightOfLangModelVsSpatialModel);
if (null == suggestions) suggestions = new ArrayList<>();
final int length = dictionaries.size();
for (int i = 1; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(
composedData, ngramContext, proximityInfoHandle, settingsValuesForSuggestion,
sessionId, weightForLocale, inOutWeightOfLangModelVsSpatialModel);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
}
public boolean hasSubscriberForEvent(Class<?> eventClass) {
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
if (eventTypes != null) {
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(clazz);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
return true;
}
}
}
return false;
}
public void unsubscribeTopic (String topicStr, MQTT_PV pv) throws Exception
{
if (!connect())
{
PV.logger.log(Level.WARNING, "Could not unsubscribe to mqtt topic \"" + topicStr
+ "\" due to no broker connection");
throw new Exception("MQTT unsubscribe failed: no broker connection");
}
final CopyOnWriteArrayList<MQTT_PV> pvs = subscribers.get(topicStr);
if (pvs == null)
{
PV.logger.log(Level.WARNING, "Could not unsubscribe to mqtt topic \"" + topicStr
+ "\" due to no internal record of topic");
throw new Exception("MQTT unsubscribe failed: no topic record");
}
pvs.remove(pv);
if (pvs.isEmpty())
{
subscribers.remove(topicStr);
myClient.unsubscribe(topicStr);
if (subscribers.isEmpty())
disconnect();
}
}
public void receiveConfigInfo(final ConfigureInfomation configureInfomation) {
String dataId = configureInfomation.getDataId();
String group = configureInfomation.getGroup();
if (null == dataId) {
dataLog.error("[receiveConfigInfo] dataId is null");
return;
}
String key = makeKey(dataId, group);
CopyOnWriteArrayList<ManagerListener> listeners = allListeners.get(key);
if (listeners == null || listeners.isEmpty()) {
dataLog.warn("[notify-listener] no listener for dataId=" + dataId + ", group=" + group);
return;
}
for (ManagerListener listener : listeners) {
try {
notifyListener(configureInfomation, listener);
}
catch (Throwable t) {
dataLog.error("call listener error, dataId=" + dataId + ", group=" + group, t);
}
}
}
/**
* @hide
*/
public final void dispatchOnEnterAnimationComplete() {
// NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
// perform the dispatching. The iterator is a safe guard against listeners that
// could mutate the list by calling the various add/remove methods. This prevents
// the array from being modified while we iterate it.
final CopyOnWriteArrayList<OnEnterAnimationCompleteListener> listeners =
mOnEnterAnimationCompleteListeners;
if (listeners != null && !listeners.isEmpty()) {
for (OnEnterAnimationCompleteListener listener : listeners) {
listener.onEnterAnimationComplete();
}
}
}
/**
* 高级使用
*/
@Test
public void other() {
HunterConfig config = HunterConfigContext.getHunterConfig(Platform.IMOOC);
// set会重置,add会追加
config.setEntryUrls("https://www.imooc.com/u/1175248/articles")
.addEntryUrl("https://www.imooc.com/u/4321686/articles")
// 设置程序退出的方式
.setExitWay(ExitWayEnum.URL_COUNT)
// 设定抓取120秒, 如果所有文章都被抓取过了,则会提前停止
.setCount(20)
// 每次抓取间隔的时间
.setSleepTime(100)
// 失败重试次数
.setRetryTimes(3)
// 针对抓取失败的链接 循环重试次数
.setCycleRetryTimes(3)
// 开启的线程数
.setThreadCount(5)
// 开启图片转存
.setConvertImg(true);
HunterProcessor hunter = new BlogHunterProcessor(config);
CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
if (null == list || list.isEmpty()) {
System.out.println("没获取到数据");
} else {
this.check(list);
}
}
@Override
public void onActionComplete(PluginLiteInfo info, int resultCode) throws RemoteException {
String pkgName = info.packageName;
PluginDebugLog.installFormatLog(TAG, "onActionComplete with %s, resultCode: %d", pkgName, resultCode);
if (sActionMap.containsKey(pkgName)) {
final CopyOnWriteArrayList<Action> actions = sActionMap.get(pkgName);
if (actions == null) {
return;
}
synchronized (actions) { // Action列表加锁同步
PluginDebugLog.installFormatLog(TAG, "%s has %d action in list!", pkgName, actions.size());
if (actions.size() > 0) {
Action finishedAction = actions.remove(0);
if (finishedAction != null) {
PluginDebugLog.installFormatLog(TAG,
"get and remove first action:%s ", finishedAction.toString());
}
if (actions.isEmpty()) {
PluginDebugLog.installFormatLog(TAG,
"onActionComplete remove empty action list of %s", pkgName);
sActionMap.remove(pkgName);
} else {
// 执行下一个Action操作,不能同步,否则容易出现栈溢出
executeNextAction(actions, pkgName);
}
}
}
}
}
private boolean postSingleEventForEventType(String tag, Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
if (TextUtils.isEmpty(tag)) tag = DEFAULT_TAG;
if (tag.equals(subscription.subscriberMethod.tag)) {
postToSubscription(subscription, event, postingState.isMainThread);
} else {
Log.d("EventBus", "当前指定的Tag未找到");
}
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
private boolean postSingleEventForEventType(String tag, Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
if (TextUtils.isEmpty(tag)) tag = DEFAULT_TAG;
if (tag.equals(subscription.subscriberMethod.tag)) {
postToSubscription(subscription, event, postingState.isMainThread);
} else {
Log.d("EventBus", "当前指定的Tag未找到");
}
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
private void run(List<String> args)
{
try
{
furnace = FurnaceFactory.getInstance();
furnace.setServerMode(true);
CopyOnWriteArrayList<Command> commands = new CopyOnWriteArrayList<>(processArguments(args));
if (!executePhase(CommandPhase.PRE_CONFIGURATION, commands))
return;
if (!executePhase(CommandPhase.CONFIGURATION, commands))
return;
if (commands.isEmpty())
{
// no commands are available, just print the help and exit
new DisplayHelpCommand().execute();
return;
}
if (!containsMutableRepository(furnace.getRepositories()))
{
furnace.addRepository(AddonRepositoryMode.MUTABLE, getUserAddonsDir());
}
if (!executePhase(CommandPhase.POST_CONFIGURATION, commands) || commands.isEmpty())
return;
furnace.addContainerLifecycleListener(containerStatusListener);
try
{
startFurnace();
}
catch (Exception e)
{
System.out.println("Failed to start "+ Util.WINDUP_BRAND_NAME_ACRONYM+"!");
if (e.getMessage() != null)
System.out.println("Failure reason: " + e.getMessage());
e.printStackTrace();
}
if (!executePhase(CommandPhase.PRE_EXECUTION, commands) || commands.isEmpty())
return;
furnace.addContainerLifecycleListener(new GreetingListener());
// Now see if there are any server SPIs that need to run
Imported<WindupServerProvider> serverProviders = furnace.getAddonRegistry().getServices(WindupServerProvider.class);
for (WindupServerProvider serverProvider : serverProviders)
{
String expectedArgName = serverProvider.getName();
boolean matches = args.stream().anyMatch(arg ->
arg.equals(expectedArgName) || arg.equals("--" + expectedArgName)
);
if (matches)
{
serverProvider.runServer(args.toArray(new String[args.size()]));
return;
}
}
if (!executePhase(CommandPhase.EXECUTION, commands) || commands.isEmpty())
return;
if (!executePhase(CommandPhase.POST_EXECUTION, commands) || commands.isEmpty())
return;
}
catch (Throwable t)
{
System.err.println(Util.WINDUP_BRAND_NAME_ACRONYM +" execution failed due to: " + t.getMessage());
t.printStackTrace();
}
}