Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.index.config;
2

    
3
import java.util.*;
4

    
5
import com.google.common.base.Predicate;
6
import com.google.common.base.Predicates;
7
import com.google.common.collect.Lists;
8
import com.google.common.collect.Sets;
9
import eu.dnetlib.data.mapreduce.util.RelDescriptor;
10
import eu.dnetlib.data.proto.TypeProtos;
11
import eu.dnetlib.data.proto.TypeProtos.Type;
12
import org.apache.commons.collections.CollectionUtils;
13

    
14
public class EntityConfigTable extends HashMap<TypeProtos.Type, EntityConfig> {
15

    
16
	private static final long serialVersionUID = 6087987206844928698L;
17

    
18
	public Collection<LinkDescriptor> getDescriptors(final Type source) {
19
		final EntityConfig entityConfig = super.get(source);
20
		if (entityConfig == null) return Lists.newArrayList();
21
		return Lists.newArrayList(entityConfig.getLinks().values());
22
	}
23

    
24
	public LinkDescriptor getDescriptor(final Type type, final RelDescriptor relDescriptor) {
25
		return super.get(type).getLinks().get(relDescriptor);
26
	}
27

    
28
	public Set<String> getFilter(final Type type, final RelDescriptor relDescriptor) {
29
		final LinkDescriptor ld = getDescriptor(type, relDescriptor);
30
		return ld != null ? Sets.newHashSet(ld.getFields()) : new HashSet<>();
31
	}
32

    
33
	public boolean includeDuplicates(final Type type) {
34
		return super.get(type).getIndexDuplicates();
35
	}
36

    
37
	public boolean hasIncludeFields(final Type type){
38
		final EntityConfig config = super.get(type);
39
		if (config == null) {
40
			throw new IllegalArgumentException("the configuration table does not contain type: " + type.toString());
41
		}
42
		return CollectionUtils.isNotEmpty(config.getIncludeFields());
43
	}
44

    
45
	public boolean hasExcludeFields(final Type type){
46
		return CollectionUtils.isNotEmpty(super.get(type).getExcludeFields());
47
	}
48

    
49
	public List<String> getIncludeFields(final Type type){
50
		return super.get(type).getIncludeFields();
51
	}
52

    
53
	public List<String> getExcludeFields(final Type type){
54
		return super.get(type).getExcludeFields();
55
	}
56

    
57
	public Predicate<String> getIncludeFilter(final Type type, final RelDescriptor relDescriptor){
58
		final Set<String> filter = getFilter(type, relDescriptor);
59
		return fieldName -> filter.contains(fieldName);
60
	}
61

    
62
	public Predicate<String> getFilter(final Type type){
63
		if(hasIncludeFields(type)){
64
			return getIncludeFilter(type);
65
		}
66
		if(hasExcludeFields(type)){
67
			return getExcludeFilter(type);
68
		}
69
		return Predicates.alwaysTrue();
70
	}
71

    
72
	private Predicate<String> getIncludeFilter(final Type type) {
73
		return fieldName -> {
74
			if(getIncludeFields(type) == null || getIncludeFields(type).isEmpty()) return false;
75
			return getIncludeFields(type).contains(fieldName);
76
		};
77
	}
78

    
79
	private Predicate<String> getExcludeFilter(final Type type) {
80
		return fieldName -> {
81
			if(getExcludeFields(type) == null || getExcludeFields(type).isEmpty()) return true;
82
			return !getExcludeFields(type).contains(fieldName);
83
		};
84
	}
85

    
86

    
87
}
(4-4/8)