svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.api / src / main / java / org / gvsig / fmap / dal / AbstractStoresRepository.java @ 44304
History | View | Annotate | Download (5.44 KB)
1 |
package org.gvsig.fmap.dal; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.tools.util.UnmodifiableBasicSet; |
10 |
import org.gvsig.tools.util.UnmodifiableBasicSetChained; |
11 |
|
12 |
/**
|
13 |
*
|
14 |
* @author jjdelcerro
|
15 |
*/
|
16 |
@SuppressWarnings("UseSpecificCatch") |
17 |
public abstract class AbstractStoresRepository implements StoresRepository { |
18 |
|
19 |
private final String name; |
20 |
private final String label; |
21 |
private final List<StoresRepository> subrepositories; |
22 |
|
23 |
public AbstractStoresRepository(String name) { |
24 |
this(name, null); |
25 |
} |
26 |
|
27 |
public AbstractStoresRepository(String name, String label) { |
28 |
this.name = name;
|
29 |
this.label = label;
|
30 |
this.subrepositories = new ArrayList<>(); |
31 |
} |
32 |
|
33 |
protected abstract DataStoreParameters getMyParameters(String name); |
34 |
|
35 |
protected abstract boolean isEmptyMyRepository(); |
36 |
|
37 |
protected abstract int getMySize(); |
38 |
|
39 |
protected abstract UnmodifiableBasicSet<String> getMyKeySet(); |
40 |
|
41 |
@Override
|
42 |
public void add(String name, DataStoreParameters parameters) { |
43 |
throw new UnsupportedOperationException(); |
44 |
} |
45 |
|
46 |
@Override
|
47 |
public void remove(String name) { |
48 |
throw new UnsupportedOperationException(); |
49 |
} |
50 |
|
51 |
@Override
|
52 |
public String getID() { |
53 |
return this.name; |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public String getLabel() { |
58 |
if( StringUtils.isBlank(this.label) ) { |
59 |
return this.getID(); |
60 |
} |
61 |
return this.label; |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public Collection<StoresRepository> getSubrepositories() { |
66 |
return this.subrepositories; |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public boolean addRepository(StoresRepository repository) { |
71 |
this.removeRepository(repository.getID());
|
72 |
this.subrepositories.add(repository);
|
73 |
return true; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public boolean removeRepository(String name) { |
78 |
for (int i = 0; i < subrepositories.size(); i++) { |
79 |
StoresRepository repo = subrepositories.get(i); |
80 |
if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
|
81 |
this.subrepositories.remove(i);
|
82 |
return true; |
83 |
} |
84 |
} |
85 |
return false; |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public DataStoreParameters get(String name) { |
90 |
DataStoreParameters parameters = this.getMyParameters(name);
|
91 |
if( parameters!=null ) { |
92 |
return parameters;
|
93 |
} |
94 |
for ( StoresRepository theRepository : this.subrepositories) { |
95 |
parameters = theRepository.get(name); |
96 |
if( parameters!=null ) { |
97 |
return parameters;
|
98 |
} |
99 |
} |
100 |
return null; |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public DataStore getStore(String name) { |
105 |
DataStoreParameters parameters = this.get(name);
|
106 |
if( parameters!=null ) { |
107 |
try {
|
108 |
DataStore store = DALLocator.getDataManager().openStore( |
109 |
parameters.getDataStoreName(), |
110 |
parameters |
111 |
); |
112 |
return store;
|
113 |
} catch (Exception ex) { |
114 |
throw new RuntimeException("Can't open store '"+name+"'.", ex); |
115 |
} |
116 |
} |
117 |
return null; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public boolean containsKey(String key) { |
122 |
DataStoreParameters parameters = this.getMyParameters(name);
|
123 |
if( parameters!=null ) { |
124 |
return true; |
125 |
} |
126 |
for ( StoresRepository theRepository : this.subrepositories) { |
127 |
if( theRepository.containsKey(key) ) {
|
128 |
return true; |
129 |
} |
130 |
} |
131 |
return false; |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public UnmodifiableBasicSet<String> keySet() { |
136 |
List<UnmodifiableBasicSet<String>> sets = new ArrayList<>(); |
137 |
sets.add(this.getMyKeySet());
|
138 |
for (StoresRepository theRepository : this.subrepositories) { |
139 |
sets.add(theRepository.keySet()); |
140 |
} |
141 |
if( sets.isEmpty() ) {
|
142 |
return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
143 |
} |
144 |
if( sets.size()==1 ) { |
145 |
return sets.get(0); |
146 |
} |
147 |
return new UnmodifiableBasicSetChained( |
148 |
sets.toArray( |
149 |
new UnmodifiableBasicSet[sets.size()]
|
150 |
) |
151 |
); |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public Map<String, DataStoreParameters> toMap() { |
156 |
throw new UnsupportedOperationException(); |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public boolean isEmpty() { |
161 |
if( !this.isEmptyMyRepository() ) { |
162 |
return false; |
163 |
} |
164 |
for ( StoresRepository theRepository : this.subrepositories) { |
165 |
if( !theRepository.isEmpty() ) {
|
166 |
return false; |
167 |
} |
168 |
} |
169 |
return true; |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public int size() { |
174 |
int sz = 0; |
175 |
if( !this.isEmptyMyRepository() ) { |
176 |
sz = this.getMySize();
|
177 |
} |
178 |
for ( StoresRepository theRepository : this.subrepositories) { |
179 |
sz += theRepository.size(); |
180 |
} |
181 |
return sz;
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public Iterator<DataStoreParameters> iterator() { |
186 |
final Iterator<String> it = this.keySet().iterator(); |
187 |
return new Iterator<DataStoreParameters>() { |
188 |
@Override
|
189 |
public boolean hasNext() { |
190 |
return it.hasNext();
|
191 |
} |
192 |
|
193 |
@Override
|
194 |
public DataStoreParameters next() {
|
195 |
String name = it.next();
|
196 |
return get(name);
|
197 |
} |
198 |
}; |
199 |
} |
200 |
|
201 |
|
202 |
} |