📄 compoundmapper.java
字号:
package org.placelab.mapper;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.placelab.collections.HashtableSet;import org.placelab.collections.UnsupportedOperationException;import org.placelab.core.Coordinate;import org.placelab.core.PlacelabProperties;/** * A Mapper that draws upon multiple mappers to find Beacons. * The precedence for which Beacon is chosen when multiple * Mappers know of the Beacon is determined by the order in * which the sub-Mappers were added. */public class CompoundMapper extends SimpleMapper { public Vector mappers; public CompoundMapper() { mappers = new Vector(); } /** * Adds a Mapper at lower precedence than all other Mappers * in the CompoundMapper. * @param m */ public void addMapper(Mapper m) { mappers.addElement(m); } /** * Adds a Mapper and gives it the highest precedence * for Beacons where multiple Mappers find the same Beacon. */ public void addMapperAtHead(Mapper m) { mappers.insertElementAt(m, 0); } public Vector findBeacons(String id) { for (Enumeration it = mappers.elements(); it.hasMoreElements(); ) { Mapper m = (Mapper)it.nextElement(); Vector list = m.findBeacons(id); if ((list != null) && (list.size() > 0)) { return list; } } return null; } private class CMIterator implements Enumeration { HashtableSet iters; Enumeration iteriter; Enumeration beaconiter=null; Beacon next = null; Hashtable seen = new Hashtable(); public CMIterator(HashtableSet iters) { this.iters = iters; iteriter = iters.elements(); loadNext(); } public void loadNext() { while (true) { if ((beaconiter != null) && (beaconiter.hasMoreElements())) { next = (Beacon)beaconiter.nextElement(); if (next == null) { return; } if (seen.get(next.getId().toLowerCase()) == null) { seen.put(next.getId().toLowerCase(),"yup"); return; } else { next = null; } } else { beaconiter = null; if (iteriter.hasMoreElements()) { // grab another iterator beaconiter = (Enumeration)iteriter.nextElement(); } else { return; //its all done } } } } public boolean hasMoreElements() { return next != null; } public Object nextElement() { Object rv = next; next = null; loadNext(); return rv; } public void remove() { throw new UnsupportedOperationException("remove() not supported for JDBMMapper iterators"); } } public Enumeration query(Coordinate c1, Coordinate c2) { HashtableSet hs = new HashtableSet(); for (Enumeration it = mappers.elements(); it.hasMoreElements();) { Mapper m = (Mapper)it.nextElement(); hs.addElement(m.query(c1,c2)); } return new CMIterator(hs); } /** * This method returns a Mapper (not necessarily a CompoundMapper) according to the * default set in {@link org.placelab.core.PlacelabProperties}. Set the property * placelab.mapper to JDBM for a JDBMMapper or Hsql for an HsqlMapper. In either * case, the Mappers will load from the default Mapper location for their type, * also specified in PlacelabProperties. See the documentation for the individual * Mapper types for the name of that property. * @see JDBMMapper * @see HsqlMapper */ public static Mapper createDefaultMapper(boolean exitOnError, boolean shouldCache) { Mapper rv = null; String mapperToUse = PlacelabProperties.get("placelab.mapper"); try { if ("JDBM".equalsIgnoreCase(mapperToUse)) { System.out.println("Making a JDBMMapper"); rv = new JDBMMapper(shouldCache); } else if ("Hsql".equalsIgnoreCase(mapperToUse)) { System.out.println("Making an HsqlMapper"); rv = new HsqlMapper(shouldCache); } else if ("wigle".equalsIgnoreCase(mapperToUse)) { System.out.println("Making a WigleMapper"); rv = new WigleMapper(); } else { System.out.println("Making an HSQLMapper by default"); rv = new HsqlMapper(shouldCache); } } catch (Exception ex) { ex.printStackTrace(); if (exitOnError) { System.exit(1); } } return rv; /* CompoundMapper rv = new CompoundMapper(); // first lets add a JDBM mapper try { rv.addMapper(new JDBMMapper(shouldCache)); } catch (IOException ex) { ex.printStackTrace(); System.out.println("Failed to create JDBMMapper"); if (exitOnError) { System.exit(1); } } // add a jdbc mapper if one has been specified String jdbcdriver = System.getProperty("placelab.jdbcmapper.driver"); String jdbcurl = System.getProperty("placelab.jdbcmapper.url"); if ((jdbcdriver != null) && (jdbcurl != null)) { try { rv.addMapper(new JDBCMapper(jdbcurl,jdbcdriver,shouldCache)); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Failed to create JDBCMapper with\n" + "placelab.jdbcmapper.driver = " + jdbcdriver + "\n" + "placelab.jdbcmapper.url = " + jdbcurl + "\n"); if (exitOnError) { System.exit(1); } } } if (rv.mappers.size() == 1) { return (Mapper)rv.mappers.get(0); } else { return rv; } */ } /* (non-Javadoc) * @see org.placelab.mapper.Mapper#overrideOnPut() */ public boolean overrideOnPut() { return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -