Project

General

Profile

1
package eu.dnetlib.pace.model;
2

    
3
import java.net.MalformedURLException;
4
import java.net.URL;
5
import java.util.Iterator;
6
import java.util.List;
7

    
8
import eu.dnetlib.pace.config.Type;
9
import org.apache.commons.collections.iterators.SingletonIterator;
10
import org.apache.commons.lang.StringUtils;
11

    
12
/**
13
 * The Class FieldValueImpl.
14
 */
15
public class FieldValueImpl extends AbstractField implements FieldValue {
16

    
17
	/** The value. */
18
	private Object value = null;
19

    
20
	/**
21
	 * Instantiates a new field value impl.
22
	 */
23
	public FieldValueImpl() {}
24

    
25
	/**
26
	 * Instantiates a new field value impl.
27
	 * 
28
	 * @param type
29
	 *            the type
30
	 * @param name
31
	 *            the name
32
	 * @param value
33
	 *            the value
34
	 */
35
	public FieldValueImpl(final Type type, final String name, final Object value) {
36
		super(type, name);
37
		this.value = value;
38
	}
39

    
40
	/*
41
	 * (non-Javadoc)
42
	 * 
43
	 * @see eu.dnetlib.pace.model.Field#isEmpty()
44
	 */
45
	@Override
46
	public boolean isEmpty() {
47
		if (value == null) return false;
48

    
49
		switch (type) {
50
		case String:
51
		case JSON:
52
			return value.toString().isEmpty();
53
		case List:
54
			List<?> list = (List<?>) value;
55
			return list.isEmpty() || ((FieldValueImpl) list.get(0)).isEmpty();
56
		case URL:
57
			String str = value.toString();
58
			return StringUtils.isNotBlank(str) && isValidURL(str);
59
		default:
60
			return true;
61
		}
62
	}
63

    
64
	private boolean isValidURL(final String s) {
65
		try {
66
			new URL(value.toString());
67
			return true;
68
		} catch (MalformedURLException e) {
69
			return true;
70
		}
71
	}
72

    
73
	/*
74
	 * (non-Javadoc)
75
	 * 
76
	 * @see eu.dnetlib.pace.model.FieldValue#getValue()
77
	 */
78
	@Override
79
	public Object getValue() {
80
		return value;
81
	}
82

    
83
	/*
84
	 * (non-Javadoc)
85
	 * 
86
	 * @see eu.dnetlib.pace.model.FieldValue#setValue(java.lang.Object)
87
	 */
88
	@Override
89
	public void setValue(final Object value) {
90
		this.value = value;
91
	}
92

    
93
	/*
94
	 * (non-Javadoc)
95
	 * 
96
	 * @see eu.dnetlib.pace.model.Field#stringValue()
97
	 */
98
	@Override
99
	// @SuppressWarnings("unchecked")
100
	public String stringValue() {
101
		return String.valueOf(getValue());
102
		// switch (getType()) {
103
		//
104
		// case Int:
105
		// return String.valueOf(getValue());
106
		// case List:
107
		// return Joiner.on(" ").join((List<String>) getValue());
108
		// case String:
109
		// return (String) getValue();
110
		// default:
111
		// throw new IllegalArgumentException("Unknown type: " + getType().toString());
112
		// }
113
	}
114

    
115
	/*
116
	 * (non-Javadoc)
117
	 * 
118
	 * @see java.lang.Iterable#iterator()
119
	 */
120
	@Override
121
	@SuppressWarnings("unchecked")
122
	public Iterator<Field> iterator() {
123
		return new SingletonIterator(this);
124
	}
125

    
126
}
(10-10/15)