1 13 package com.tonbeller.jpivot.table.span; 14 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.TreeSet ; 23 24 import com.tonbeller.jpivot.olap.model.Member; 25 import com.tonbeller.jpivot.olap.model.MemberPropertyMeta; 26 import com.tonbeller.jpivot.olap.model.Property; 27 import com.tonbeller.jpivot.olap.navi.MemberProperties; 28 29 38 public class ScopedPropertyMetaSet { 39 40 46 class ScopeList { 47 List list = new ArrayList (); 48 Map map = new HashMap (); 49 void add(MemberPropertyMeta mpm) { 50 String name = mpm.getName(); 51 if (!map.containsKey(name)) { 52 map.put(name, mpm); 53 list.add(mpm); 54 } 55 } 56 void remove(MemberPropertyMeta mpm) { 57 mpm = (MemberPropertyMeta) map.get(mpm.getName()); 58 if (mpm == null) 59 return; 60 map.remove(mpm.getName()); 61 list.remove(mpm); 62 } 63 MemberPropertyMeta lookup(String name) { 64 return (MemberPropertyMeta) map.get(name); 65 } 66 boolean contains(String name) { 67 return map.containsKey(name); 68 } 69 Iterator iterator() { 70 return list.iterator(); 71 } 72 } 73 74 private MemberProperties extension; 75 76 public ScopedPropertyMetaSet(MemberProperties extension) { 77 this.extension = extension; 78 } 79 80 private Map scopeMap = new HashMap (); 82 83 public void addAll(Collection memberPropertyMetas) { 84 for (Iterator it = memberPropertyMetas.iterator(); it.hasNext();) { 85 MemberPropertyMeta mpm = (MemberPropertyMeta) it.next(); 86 add(mpm); 87 } 88 } 89 90 public void removeAll(Collection memberPropertyMetas) { 91 for (Iterator it = memberPropertyMetas.iterator(); it.hasNext();) { 92 MemberPropertyMeta mpm = (MemberPropertyMeta) it.next(); 93 remove(mpm); 94 } 95 } 96 97 101 public void addMember(Member member) { 102 if (member == null) 103 return; 104 String scope = extension.getPropertyScope(member); 105 Property[] properties = member.getProperties(); 106 properties = PropertyUtils.normalize(properties); 107 for (int i = 0; i < properties.length; i++) { 108 String label = properties[i].getLabel(); 109 String name = properties[i].getName(); 110 MemberPropertyMeta mpm = new MemberPropertyMeta(label, name, scope); 111 this.add(mpm); 112 } 113 } 114 115 public void add(MemberPropertyMeta mpm) { 116 Object scope = mpm.getScope(); 117 ScopeList scopeList = (ScopeList) scopeMap.get(scope); 118 if (scopeList == null) { 119 scopeList = new ScopeList(); 120 scopeMap.put(scope, scopeList); 121 } 122 scopeList.add(mpm); 123 } 124 125 public void remove(MemberPropertyMeta mpm) { 126 Object scope = mpm.getScope(); 127 ScopeList scopeList = (ScopeList) scopeMap.get(scope); 128 if (scopeList == null) 129 return; 130 scopeList.remove(mpm); 131 } 132 133 138 public boolean contains(MemberPropertyMeta mpm) { 139 return contains(mpm.getScope(), mpm.getName()); 140 } 141 156 public boolean contains(String scope, String name) { 157 ScopeList scopeList = (ScopeList) scopeMap.get(scope); 158 if (scopeList == null) 159 return false; 160 return scopeList.contains(name); 161 } 162 163 MemberPropertyMeta lookup(String scope, String name) { 164 ScopeList scopeList = (ScopeList) scopeMap.get(scope); 165 if (scopeList == null) 166 return null; 167 return scopeList.lookup(name); 168 } 169 170 176 public List intersectList(List metas) { 177 List list = new ArrayList (); 178 for (Iterator it = metas.iterator(); it.hasNext();) { 179 MemberPropertyMeta mpm = (MemberPropertyMeta) it.next(); 180 mpm = lookup(mpm.getScope(), mpm.getName()); 181 if (mpm != null) 182 list.add(mpm); 183 } 184 return list; 185 } 186 187 188 191 public List metaList(MemberPropertyMetaFilter filter) { 192 List list = new ArrayList (); 193 for (Iterator it = scopeMap.values().iterator(); it.hasNext();) { 194 ScopeList scopeList = (ScopeList ) it.next(); 195 for (Iterator vt = scopeList.iterator(); vt.hasNext();) { 196 MemberPropertyMeta meta = (MemberPropertyMeta) vt.next(); 197 if (filter.accept(meta)) 198 list.add(meta); 199 } 200 } 201 return list; 202 } 203 204 207 public ScopedPropertyMetaSet metaSet(MemberPropertyMetaFilter filter) { 208 ScopedPropertyMetaSet set = new ScopedPropertyMetaSet(extension); 209 for (Iterator it = scopeMap.values().iterator(); it.hasNext();) { 210 ScopeList scopeList = (ScopeList) it.next(); 211 for (Iterator vt = scopeList.iterator(); vt.hasNext();) { 212 MemberPropertyMeta meta = (MemberPropertyMeta) vt.next(); 213 if (filter.accept(meta)) 214 set.add(meta); 215 } 216 } 217 return set; 218 } 219 220 223 String [] getAllNames() { 224 Set allNames = new TreeSet (); 225 for (Iterator it = scopeMap.values().iterator(); it.hasNext();) { 226 ScopeList scopeList = (ScopeList) it.next(); 227 for (Iterator vt = scopeList.iterator(); vt.hasNext();) { 228 MemberPropertyMeta meta = (MemberPropertyMeta) vt.next(); 229 allNames.add(meta.getName()); 230 } 231 } 232 return (String []) allNames.toArray(new String [allNames.size()]); 233 } 234 235 238 static class AllFilter implements MemberPropertyMetaFilter { 239 public boolean accept(MemberPropertyMeta meta) { 240 return true; 241 } 242 } 243 public MemberPropertyMetaFilter createAllFilter() { 244 return new AllFilter(); 245 } 246 247 251 class ScopesFilter implements MemberPropertyMetaFilter { 252 Set scopes; 253 public ScopesFilter(Set scopes) { 254 this.scopes = scopes; 255 } 256 public boolean accept(MemberPropertyMeta meta) { 257 return scopes.contains(meta.getScope()); 258 } 259 } 260 public MemberPropertyMetaFilter createScopesFilter(Set scopes) { 261 return new ScopesFilter(scopes); 262 } 263 264 267 static class IgnoreInlineFilter implements MemberPropertyMetaFilter { 268 public boolean accept(MemberPropertyMeta meta) { 269 return !PropertyUtils.isInline(meta.getName()); 270 } 271 } 272 public MemberPropertyMetaFilter createIgnoreInlineFilter() { 273 return new IgnoreInlineFilter(); 274 } 275 276 } 277 | Popular Tags |