1   package ixa.kaflib;
2   
3   import java.io.Serializable;
4   import java.util.*;
5   
6   public class Span<T> implements Serializable {
7   	@Override
8   	public String toString() {
9   		return "Span{" +
10  				"targets=" + targets +
11  				'}';
12  	}
13  
14  	//private List<String> targets;
15  	private List<T> targets;
16  	private Set<T> heads;
17  
18  	Span() {
19  		this.targets = new ArrayList<T>();
20  		this.heads = new HashSet<>();
21  	}
22  
23  	Span(List<T> targets) {
24  		this.targets = targets;
25  		this.heads = new HashSet<>();
26  	}
27  
28  	Span(List<T> targets, T head) {
29  		this.targets = targets;
30  		this.heads = new HashSet<>();
31  		heads.add(head);
32  	}
33  
34  	public boolean isEmpty() {
35  		return (this.targets.size() <= 0);
36  	}
37  
38  	public String getStr() {
39  		StringBuilder builder = new StringBuilder();
40  		for (Object term : targets) {
41  			if (builder.length() != 0) {
42  				builder.append(' ');
43  			}
44  			if (term instanceof Term) {
45  				builder.append(((Term) term).getStr());
46  			}
47  			else if (term instanceof WF) {
48  				builder.append(((WF) term).getForm());
49  			}
50  			else {
51  				builder.append(term.toString());
52  			}
53  		}
54  		return builder.toString();
55  	}
56  
57  	public List<T> getTargets() {
58  		return this.targets;
59  	}
60  
61  	public T getFirstTarget() {
62  		return this.targets.get(0);
63  	}
64  
65  	public boolean hasHead() {
66  		return (this.heads.size() > 0);
67  	}
68  
69  	@Deprecated
70  	public T getHead() {
71  		for (T term : this.heads) {
72  			return term;
73  		}
74  
75  		return null;
76  	}
77  
78  	public Set<T> getHeads() {
79  		return heads;
80  	}
81  
82  	public boolean isHead(T target) {
83  		return (this.heads.contains(target));
84  	}
85  
86  	public void setHead(T head) {
87  		this.heads.add(head);
88  	}
89  
90  	public void deleteHead(T head) {
91  		this.heads.remove(head);
92  	}
93  
94  	public void clearHeads() {
95  		this.heads = new HashSet<>();
96  	}
97  
98  	public void addTarget(T target) {
99  		this.targets.add(target);
100 	}
101 
102 	public void addTarget(T target, boolean isHead) {
103 		this.targets.add(target);
104 		if (isHead) {
105 			this.heads.add(target);
106 		}
107 	}
108 
109 	public void addTargets(List<T> targets) {
110 		this.targets.addAll(targets);
111 	}
112 
113 	public boolean hasTarget(T target) {
114 		for (T t : targets) {
115 			if (t == target) {
116 				return true;
117 			}
118 		}
119 		return false;
120 	}
121 
122 	public int size() {
123 		return this.targets.size();
124 	}
125 	
126 
127 	@Override
128 	public boolean equals(Object object) {
129 	    if (object == this) {
130 	        return true;
131 	    }
132 	    if (!(object instanceof Span<?>)) {
133 	        return false;
134 	    }
135 	    Span<?> other = (Span<?>) object;
136 	    return other.targets.equals(targets) && other.heads.equals(heads);
137 	}
138 
139     @Override
140     public int hashCode() {
141         return Objects.hash(targets, heads);
142     }
143 
144 }