svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / DefaultStoresRepository.java @ 44297
History | View | Annotate | Download (4.21 KB)
1 | 44253 | jjdelcerro | package org.gvsig.fmap.dal.impl; |
---|---|---|---|
2 | |||
3 | import java.util.HashMap; |
||
4 | import java.util.Iterator; |
||
5 | import java.util.Map; |
||
6 | import org.gvsig.fmap.dal.DALLocator; |
||
7 | import org.gvsig.fmap.dal.DataStore; |
||
8 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
9 | import org.gvsig.fmap.dal.StoresRepository; |
||
10 | import org.gvsig.tools.util.GetItemByKey; |
||
11 | 44262 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicMap; |
12 | 44259 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicSet; |
13 | import org.gvsig.tools.util.UnmodifiableBasicSetAdapter; |
||
14 | 44262 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicSetChained; |
15 | 44253 | jjdelcerro | |
16 | /**
|
||
17 | *
|
||
18 | * @author jjdelcerro
|
||
19 | */
|
||
20 | @SuppressWarnings("UseSpecificCatch") |
||
21 | public class DefaultStoresRepository implements StoresRepository { |
||
22 | |||
23 | private final Map<String,DataStoreParameters> repository; |
||
24 | 44262 | jjdelcerro | private final Map<String,UnmodifiableBasicMap<String,DataStoreParameters>> delegateds; |
25 | 44253 | jjdelcerro | |
26 | public DefaultStoresRepository() {
|
||
27 | this.repository = new HashMap<>(); |
||
28 | this.delegateds = new HashMap<>(); |
||
29 | } |
||
30 | |||
31 | 44262 | jjdelcerro | public void add(String name, UnmodifiableBasicMap<String,DataStoreParameters> repository) { |
32 | 44253 | jjdelcerro | this.delegateds.put(name, repository);
|
33 | } |
||
34 | |||
35 | @Override
|
||
36 | public void add(String name, DataStoreParameters parameters) { |
||
37 | this.repository.put(name, parameters);
|
||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | public void remove(String name) { |
||
42 | this.repository.remove(name);
|
||
43 | } |
||
44 | |||
45 | @Override
|
||
46 | public DataStore get(String name) { |
||
47 | DataStoreParameters parameters = this.repository.get(name);
|
||
48 | if( parameters==null ) { |
||
49 | for (GetItemByKey<String,DataStoreParameters> theRepository : this.delegateds.values()) { |
||
50 | parameters = theRepository.get(name); |
||
51 | if( parameters!=null ) { |
||
52 | break;
|
||
53 | } |
||
54 | } |
||
55 | if( parameters==null ) { |
||
56 | throw new IllegalArgumentException("Can't exists store '"+name+"' in the repository."); |
||
57 | } |
||
58 | } |
||
59 | DataStore store; |
||
60 | try {
|
||
61 | store = DALLocator.getDataManager().openStore(parameters.getDataStoreName(), parameters); |
||
62 | } catch (Exception ex) { |
||
63 | throw new RuntimeException("Can't open store '"+name+"'.", ex); |
||
64 | } |
||
65 | return store;
|
||
66 | } |
||
67 | |||
68 | @Override
|
||
69 | public boolean isEmpty() { |
||
70 | 44262 | jjdelcerro | if( !this.repository.isEmpty() ) { |
71 | return false; |
||
72 | } |
||
73 | for (UnmodifiableBasicMap<String,DataStoreParameters> theRepository : this.delegateds.values()) { |
||
74 | if( !theRepository.isEmpty() ) {
|
||
75 | return false; |
||
76 | } |
||
77 | } |
||
78 | return true; |
||
79 | 44253 | jjdelcerro | } |
80 | |||
81 | @Override
|
||
82 | public boolean containsKey(String name) { |
||
83 | if( this.repository.containsKey(name) ) { |
||
84 | return true; |
||
85 | } |
||
86 | for (GetItemByKey<String,DataStoreParameters> theRepository : this.delegateds.values()) { |
||
87 | DataStoreParameters parameters = theRepository.get(name); |
||
88 | if( parameters!=null ) { |
||
89 | return true; |
||
90 | } |
||
91 | } |
||
92 | return false; |
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public Map<String, DataStore> toMap() { |
||
97 | throw new UnsupportedOperationException(); |
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | public int size() { |
||
102 | 44262 | jjdelcerro | int sz = this.repository.size(); |
103 | for (UnmodifiableBasicMap<String,DataStoreParameters> theRepository : this.delegateds.values()) { |
||
104 | sz += theRepository.size(); |
||
105 | } |
||
106 | return sz;
|
||
107 | 44253 | jjdelcerro | } |
108 | |||
109 | @Override
|
||
110 | 44259 | jjdelcerro | public Iterator<DataStore> iterator() { |
111 | final Iterator<String> it = this.keySet().iterator(); |
||
112 | return new Iterator<DataStore>() { |
||
113 | @Override
|
||
114 | public boolean hasNext() { |
||
115 | return it.hasNext();
|
||
116 | } |
||
117 | |||
118 | @Override
|
||
119 | public DataStore next() {
|
||
120 | String name = it.next();
|
||
121 | return get(name);
|
||
122 | } |
||
123 | }; |
||
124 | 44253 | jjdelcerro | } |
125 | |||
126 | 44259 | jjdelcerro | @Override
|
127 | public UnmodifiableBasicSet<String> keySet() { |
||
128 | 44262 | jjdelcerro | UnmodifiableBasicSet[] sets = new UnmodifiableBasicSet[this.delegateds.size()+1]; |
129 | int i=0; |
||
130 | sets[i++] = new UnmodifiableBasicSetAdapter(this.repository.keySet()); |
||
131 | for (UnmodifiableBasicMap<String,DataStoreParameters> theRepository : this.delegateds.values()) { |
||
132 | sets[i++] = theRepository.keySet(); |
||
133 | } |
||
134 | return new UnmodifiableBasicSetChained(sets); |
||
135 | 44259 | jjdelcerro | } |
136 | |||
137 | 44253 | jjdelcerro | } |