summaryrefslogtreecommitdiff
path: root/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SimpleDimension.java
blob: 52250faf011971f68ab3d64e389f171f498a5363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package net.israfil.foundation.collections.nspace;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author cgruber
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class SimpleDimension implements Dimension {

	private final String name;
	private final Set<String> positions = new HashSet<String>();
	private final Set<NSpace> spaces = new HashSet<NSpace>();
	
	public SimpleDimension() {
		name = null;
	}
	
	public SimpleDimension(String name, String ordinal) {
		this.name = name;
		positions.add(ordinal);
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#getName()
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @see java.util.Collection#size()
	 */
	public int size() {
		return this.positions.size();
	}

	/**
	 * @see java.util.Collection#isEmpty()
	 */
	public boolean isEmpty() {
		throw new UnsupportedOperationException("Dimension cannot have no positions.  Remove the dimension.");
	}

	/**
	 * @see java.util.Collection#contains(java.lang.Object)
	 */
	public boolean contains(Object positions) {
		return this.positions.contains(positions);
	}

	/**
	 * @see java.util.Collection#iterator()
	 */
	public Iterator<String> iterator() {
		return this.positions.iterator();
	}

	/**
	 * @see java.util.Collection#toArray()
	 */
	public String[] toArray() {
		return this.positions.toArray(new String[size()]);
	}

	/**
	 * @see java.util.Collection#toArray(java.lang.Object)
	 */
	public <T> T[] toArray(T[] array) {
		return this.positions.toArray(array);
	}
	
	/**
	 * @see java.util.Collection#add(java.lang.Object)
	 */
	public boolean add(String
			position) {
		boolean result = this.positions.add(position);
		notifyAddPosition((String)position);
		return result;
	}

	/**
	 * @see java.util.Collection#remove(java.lang.Object)
	 */
	public boolean remove(Object position) {
		boolean result = this.positions.remove(position);
		notifyRemovePosition((String)position);
		return result;
	}

	/**
	 * @see java.util.Collection#containsAll(java.util.Collection)
	 */
	public boolean containsAll(Collection<?> positions) {
		return this.positions.containsAll(positions);
	}

	/**
	 * @see java.util.Collection#addAll(java.util.Collection)
	 */
	public boolean addAll(Collection<? extends String> positions) {
		return this.positions.addAll(positions);
	}

	/** 
	 * @see java.util.Collection#retainAll(java.util.Collection)
	 */
	public boolean retainAll(Collection<?> positions) {
		return this.positions.retainAll(positions);
	}

	/**
	 * @see java.util.Collection#removeAll(java.util.Collection)
	 */
	public boolean removeAll(Collection<?> positions) {
		return this.positions.removeAll(positions);
	}

	/**
	 * @see java.util.Collection#clear()
	 */
	public void clear() {
		throw new UnsupportedOperationException("Dimensions must always have one ordinal");
	}



	/**
	 * @see org.frugenplat.framework.nspace.Dimension#abandonNSpace(org.frugenplat.framework.nspace.NSpace)
	 */
	public void abandonNSpace(NSpace n) {
		this.spaces.remove(n);
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#getWithNSpace(org.frugenplat.framework.nspace.NSpace)
	 */
	public void getWithNSpace(NSpace n) {
		this.spaces.add(n);
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#abandonNSpace(org.frugenplat.framework.nspace.NSpace)
	 */
	public void abandonNSpaces(Collection<NSpace> spaces) {
		this.spaces.removeAll(spaces);
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#getWithNSpace(org.frugenplat.framework.nspace.NSpace)
	 */
	public void getWithNSpaces(Collection<NSpace> spaces) {
		this.spaces.addAll(spaces);
	}
	
	private void notifyAddPosition(String position) {
		for (NSpace space : spaces) {
			space.dimensionAddedPosition(this,position);
		}
	}
	
	private void notifyRemovePosition(String position) {
		for (NSpace space : spaces) {
			space.dimensionRemovedPosition(this,position);
		}
	}
	
	@SuppressWarnings("unused")
    private void notifyAddPositions(Collection<String> positions) {
		
	}

	@SuppressWarnings("unused")
    private void notifyRemovePositions(Collection<String> positions) {

	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return name.hashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.toString());
		sb.append(" [");
		sb.append(name);
		sb.append("] {");
		for (String s : positions) sb.append(s);
		sb.append("}");
		return sb.toString();
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#addPosition(java.lang.String)
	 */
	public Dimension addPosition(String s) {
		add(s);
		return this;
	}

	/**
	 * @see org.frugenplat.framework.nspace.Dimension#deletePosition(java.lang.String)
	 */
	public Dimension deletePosition(String s) {
		remove(s);
		return this;
	}



}