2019-11-07 12:47:12 +01:00
|
|
|
package eu.dnetlib.support;
|
2019-08-06 12:09:34 +02:00
|
|
|
|
|
|
|
import java.io.Serializable;
|
2023-04-17 11:06:27 +02:00
|
|
|
import java.util.*;
|
2019-08-06 12:09:34 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-06-11 10:46:46 +02:00
|
|
|
import java.util.stream.Stream;
|
2019-08-06 12:09:34 +02:00
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
|
|
import eu.dnetlib.pace.model.MapDocument;
|
2023-04-17 11:06:27 +02:00
|
|
|
import org.codehaus.jackson.annotate.JsonIgnore;
|
2020-06-11 10:46:46 +02:00
|
|
|
|
2019-08-06 12:09:34 +02:00
|
|
|
public class Block implements Serializable {
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
private String key;
|
2019-08-06 12:09:34 +02:00
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
private List<MapDocument> documents;
|
|
|
|
|
|
|
|
public Block() {
|
|
|
|
super();
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 11:06:27 +02:00
|
|
|
public Block(String key, List<MapDocument> documents) {
|
|
|
|
this.key = key;
|
|
|
|
this.documents = documents;
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
public Block(String key, Iterable<MapDocument> documents) {
|
2019-08-06 12:09:34 +02:00
|
|
|
this.key = key;
|
2020-06-11 10:46:46 +02:00
|
|
|
this.documents = Lists.newArrayList(documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Block from(String key, MapDocument doc) {
|
|
|
|
Block block = new Block();
|
|
|
|
block.setKey(key);
|
|
|
|
block.setDocuments(Lists.newArrayList(doc));
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Block from(String key, Iterator<Block> blocks, String orderField, int maxSize) {
|
|
|
|
Block block = new Block();
|
|
|
|
block.setKey(key);
|
|
|
|
|
|
|
|
Iterable<Block> it = () -> blocks;
|
|
|
|
|
|
|
|
block
|
|
|
|
.setDocuments(
|
|
|
|
StreamSupport
|
|
|
|
.stream(it.spliterator(), false)
|
|
|
|
.flatMap(b -> b.getDocuments().stream())
|
|
|
|
.sorted(Comparator.comparing(a -> a.getFieldMap().get(orderField).stringValue()))
|
|
|
|
.limit(maxSize)
|
|
|
|
.collect(Collectors.toCollection(ArrayList::new)));
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Block from(Block b1, Block b2, String orderField, int maxSize) {
|
|
|
|
Block block = new Block();
|
|
|
|
block.setKey(b1.getKey());
|
|
|
|
block
|
|
|
|
.setDocuments(
|
|
|
|
Stream
|
|
|
|
.concat(b1.getDocuments().stream(), b2.getDocuments().stream())
|
|
|
|
.sorted(Comparator.comparing(a -> a.getFieldMap().get(orderField).stringValue()))
|
|
|
|
.limit(maxSize)
|
|
|
|
.collect(Collectors.toCollection(ArrayList::new)));
|
|
|
|
return block;
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKey(String key) {
|
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
public List<MapDocument> getDocuments() {
|
|
|
|
return documents;
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
public void setDocuments(List<MapDocument> documents) {
|
|
|
|
this.documents = documents;
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
public int comparisons() {
|
|
|
|
return (documents.size()*(documents.size()-1))/2;
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:46:46 +02:00
|
|
|
public int elements() {
|
|
|
|
return documents.size();
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
2019-08-06 17:06:05 +02:00
|
|
|
|
2020-09-29 12:01:25 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Block{" +
|
|
|
|
"key='" + key + '\'' +
|
|
|
|
", size=" + documents.size() + '\'' +
|
|
|
|
", names=" + documents.stream().map(d -> d.getFieldMap().get("country").stringValue()).collect(Collectors.toList()) + '\'' +
|
|
|
|
'}';
|
|
|
|
}
|
2019-08-06 12:09:34 +02:00
|
|
|
}
|
2020-06-11 10:46:46 +02:00
|
|
|
|