/** * Create a new CompositeMap which composites all of the Map instances in the * argument. It copies the argument array, it does not use it directly. * * @param composite Maps to be composited * @param mutator MapMutator to be used for mutation operations */ public CompositeMap(Map[] composite, MapMutator mutator) { this.mutator = mutator; this.composite = new Map[0]; for (int i = composite.length - 1; i >= 0; --i) { this.addComposited(composite[i]); } }
/** * Checks if this Map equals another as per the Map specification. * * @param obj the object to compare to * @return true if the maps are equal */ public boolean equals(Object obj) { if (obj instanceof Map) { Map map = (Map) obj; return (this.entrySet().equals(map.entrySet())); } return false; }
public void testRemoveComposited() { CompositeMap map = new CompositeMap(buildOne(), buildTwo()); HashMap three = new HashMap(); three.put("5", "five"); map.addComposited(three); assertTrue(map.containsKey("5")); map.removeComposited(three); assertFalse(map.containsKey("5")); map.removeComposited(buildOne()); assertFalse(map.containsKey("2")); }
public Map makeEmptyMap() { CompositeMap map = new CompositeMap(); map.addComposited(new HashMap()); map.setMutator(new CompositeMap.MapMutator() { public void resolveCollision(CompositeMap composite, Map existing, Map added, Collection intersect) { // Do nothing } public Object put(CompositeMap map, Map[] composited, Object key, Object value) { return composited[0].put(key, value); } public void putAll(CompositeMap map, Map[] composited, Map t) { composited[0].putAll(t); } }); return map; }
public void testGet() { CompositeMap map = new CompositeMap(buildOne(), buildTwo()); Assert.assertEquals("one", map.get("1")); Assert.assertEquals("four", map.get("4")); }
public void testRemoveFromUnderlying() { CompositeMap map = new CompositeMap(buildOne(), buildTwo()); HashMap three = new HashMap(); three.put("5", "five"); map.addComposited(three); assertTrue(map.containsKey("5")); //Now remove "5" three.remove("5"); assertFalse(map.containsKey("5")); }
public void testRemoveFromComposited() { CompositeMap map = new CompositeMap(buildOne(), buildTwo()); HashMap three = new HashMap(); three.put("5", "five"); map.addComposited(three); assertTrue(map.containsKey("5")); //Now remove "5" map.remove("5"); assertFalse(three.containsKey("5")); }
public void testResolveCollision() { CompositeMap map = new CompositeMap(buildOne(), buildTwo(), new CompositeMap.MapMutator() { public void resolveCollision(CompositeMap composite, Map existing, Map added, Collection intersect) { pass = true; } public Object put(CompositeMap map, Map[] composited, Object key, Object value) { throw new UnsupportedOperationException(); } public void putAll(CompositeMap map, Map[] composited, Map t) { throw new UnsupportedOperationException(); } }); map.addComposited(buildOne()); assertTrue(pass); }
if (parentArgs != null && !parentArgs.isEmpty()) { arguments = new CompositeMap(arguments, parentArgs);
public void testPutAll() { CompositeMap map = new CompositeMap(buildOne(), buildTwo(), new CompositeMap.MapMutator() { public void resolveCollision(CompositeMap composite, Map existing, Map added, Collection intersect) { throw new UnsupportedOperationException(); } public Object put(CompositeMap map, Map[] composited, Object key, Object value) { throw new UnsupportedOperationException(); } public void putAll(CompositeMap map, Map[] composited, Map t) { pass = true; } }); map.putAll(null); assertTrue(pass); } }
public void testPut() { CompositeMap map = new CompositeMap(buildOne(), buildTwo(), new CompositeMap.MapMutator() { public void resolveCollision(CompositeMap composite, Map existing, Map added, Collection intersect) { throw new UnsupportedOperationException(); } public Object put(CompositeMap map, Map[] composited, Object key, Object value) { pass = true; return "foo"; } public void putAll(CompositeMap map, Map[] composited, Map t) { throw new UnsupportedOperationException(); } }); map.put("willy", "wonka"); assertTrue(pass); }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) public Object put(CompositeMap map, Map[] composited, Object key, Object value) { Object prevResult = map.get(key); Object result = composited[0].put(key,value); return result == null ? prevResult : result; } };
public void testAddComposited() { CompositeMap map = new CompositeMap(buildOne(), buildTwo()); HashMap three = new HashMap(); three.put("5", "five"); map.addComposited(three); assertTrue(map.containsKey("5")); try { map.addComposited(three); fail("Expecting IllegalArgumentException."); } catch (IllegalArgumentException ex) { // expected } }
/** * Returns configured parameters if <code>{@link #isLanguage(String)} == true</code>. * The returned map contains {@link #getLanguageParams(String) language specific parameters} * merged with {@link #getDefaultParameters()} * @param language the language * @return the parameters or <code>null</code> if none or the parsed language * is not active. */ public Map<String,String> getParameters(String parsedLang){ LangState ls = getLanguageState(parsedLang); if(ls.state){ Map<String,String> params = configuredLanguages.get(ls.lang); if(params != null){ params = new CompositeMap(params,defaultParameters,CONFIGURATION_MERGER); } else { params = defaultParameters; } return params; } else { return null; //to indicate the parsed language is not active } } /**
@Override @SuppressWarnings({"rawtypes", "unchecked"}) public Object put(CompositeMap map, Map[] composited, Object key, Object value) { Object prevResult = map.get(key); Object result = composited[0].put(key,value); return result == null ? prevResult : result; } };
/** * Checks if this Map equals another as per the Map specification. * * @param obj the object to compare to * @return true if the maps are equal */ public boolean equals(Object obj) { if (obj instanceof Map) { Map map = (Map) obj; return (this.entrySet().equals(map.entrySet())); } return false; }
/** * Create a new CompositeMap which composites all of the Map instances in the * argument. It copies the argument array, it does not use it directly. * * @param composite Maps to be composited * @param mutator MapMutator to be used for mutation operations */ public CompositeMap(Map[] composite, MapMutator mutator) { this.mutator = mutator; this.composite = new Map[0]; for (int i = composite.length - 1; i >= 0; --i) { this.addComposited(composite[i]); } }
/** * Returns configured parameters if <code>{@link #isLanguage(String)} == true</code>. * The returned map contains {@link #getLanguageParams(String) language specific parameters} * merged with {@link #getDefaultParameters()} * @param language the language * @return the parameters or <code>null</code> if none or the parsed language * is not active. */ public Map<String,String> getParameters(String parsedLang){ LangState ls = getLanguageState(parsedLang); if(ls.state){ Map<String,String> params = configuredLanguages.get(ls.lang); if(params != null){ params = new CompositeMap(params,defaultParameters,CONFIGURATION_MERGER); } else { params = defaultParameters; } return params; } else { return null; //to indicate the parsed language is not active } } /**
/** * Gets a hash code for the Map as per the Map specification. */ public int hashCode() { int code = 0; for (Iterator i = this.entrySet().iterator(); i.hasNext();) { code += i.next().hashCode(); } return code; }
/** * Create a new CompositeMap which composites all of the Map instances in the * argument. It copies the argument array, it does not use it directly. * * @param composite Maps to be composited * @param mutator MapMutator to be used for mutation operations */ public CompositeMap(Map[] composite, MapMutator mutator) { this.mutator = mutator; this.composite = new Map[0]; for (int i = composite.length - 1; i >= 0; --i) { this.addComposited(composite[i]); } }