NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java

95 wiersze
2.5 KiB
Java
Czysty Zwykły widok Historia

2017-03-01 17:47:52 +00:00
package org.schabi.newpipe.extractor;
2017-12-15 21:12:32 +00:00
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2017-03-01 17:47:52 +00:00
2017-08-06 20:20:15 +00:00
import java.util.ArrayList;
import java.util.Collections;
2017-03-01 17:47:52 +00:00
import java.util.List;
/*
2017-03-01 17:47:52 +00:00
* Created by Christian Schabesberger on 12.02.17.
*
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
2018-02-24 21:20:50 +00:00
* InfoItemsCollector.java is part of NewPipe.
2017-03-01 17:47:52 +00:00
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
2018-02-24 21:20:50 +00:00
public abstract class InfoItemsCollector<I extends InfoItem, E> implements Collector<I,E> {
2017-03-01 17:47:52 +00:00
private final List<I> itemList = new ArrayList<>();
private final List<Throwable> errors = new ArrayList<>();
private final int serviceId;
/**
* Create a new collector
* @param serviceId the service id
*/
2018-02-24 21:20:50 +00:00
public InfoItemsCollector(int serviceId) {
2017-03-01 17:47:52 +00:00
this.serviceId = serviceId;
}
@Override
public List<I> getItems() {
return Collections.unmodifiableList(itemList);
2017-03-01 17:47:52 +00:00
}
@Override
2017-03-01 17:47:52 +00:00
public List<Throwable> getErrors() {
return Collections.unmodifiableList(errors);
2017-03-01 17:47:52 +00:00
}
@Override
public void reset() {
itemList.clear();
errors.clear();
2017-03-01 17:47:52 +00:00
}
/**
* Add an error
* @param error the error
*/
protected void addError(Exception error) {
errors.add(error);
2017-03-01 17:47:52 +00:00
}
/**
* Add an item
* @param item the item
*/
protected void addItem(I item) {
2017-03-01 17:47:52 +00:00
itemList.add(item);
}
/**
* Get the service id
* @return the service id
*/
public int getServiceId() {
2017-03-01 17:47:52 +00:00
return serviceId;
}
@Override
public void commit(E extractor) {
try {
addItem(extract(extractor));
2017-12-15 21:12:32 +00:00
} catch (FoundAdException ae) {
// found an ad. Maybe a debug line could be placed here
} catch (ParsingException e) {
addError(e);
}
}
2017-03-01 17:47:52 +00:00
}