Skip to content

Commit

Permalink
Move MultiMap to Metamorph and rename it to Maps
Browse files Browse the repository at this point in the history
The `MultiMap` interface is only used by Metamorph. It is made part of
the morph API to provide access to the maps defined in a Metamorph
script. To reflect this, it is renamed to `Maps`.

The `MultiHashMap` implementation is removed as the `Maps` interface is
directly implemented by the `Metamorph` class.
  • Loading branch information
cboehme committed Nov 21, 2016
1 parent 5bdfb85 commit 3b534e7
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 143 deletions.
27 changes: 16 additions & 11 deletions src/main/java/org/culturegraph/mf/morph/Metamorph.java
Expand Up @@ -23,28 +23,29 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.culturegraph.mf.framework.helpers.DefaultStreamReceiver;
import org.culturegraph.mf.framework.FluxCommand;
import org.culturegraph.mf.framework.StandardEventNames;
import org.culturegraph.mf.framework.StreamPipe;
import org.culturegraph.mf.framework.StreamReceiver;
import org.culturegraph.mf.framework.annotations.Description;
import org.culturegraph.mf.framework.FluxCommand;
import org.culturegraph.mf.framework.annotations.In;
import org.culturegraph.mf.framework.annotations.Out;
import org.culturegraph.mf.framework.helpers.DefaultStreamReceiver;
import org.culturegraph.mf.morph.api.FlushListener;
import org.culturegraph.mf.morph.api.InterceptorFactory;
import org.culturegraph.mf.morph.api.Maps;
import org.culturegraph.mf.morph.api.MorphDefException;
import org.culturegraph.mf.morph.api.MorphErrorHandler;
import org.culturegraph.mf.morph.api.NamedValuePipe;
import org.culturegraph.mf.morph.api.NamedValueReceiver;
import org.culturegraph.mf.morph.api.NamedValueSource;
import org.culturegraph.mf.morph.interceptors.NullInterceptorFactory;
import org.culturegraph.mf.stream.pipe.StreamFlattener;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.util.ResourceUtil;
import org.culturegraph.mf.framework.StandardEventNames;
import org.culturegraph.mf.util.xml.Location;
import org.xml.sax.InputSource;

Expand All @@ -60,7 +61,7 @@
@In(StreamReceiver.class)
@Out(StreamReceiver.class)
@FluxCommand("morph")
public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePipe, MultiMap {
public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePipe, Maps {

public static final String ELSE_KEYWORD = "_else";
public static final char FEEDBACK_CHAR = '@';
Expand All @@ -78,7 +79,7 @@ public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePi
private final Registry<NamedValueReceiver> dataRegistry = MorphCollectionFactory.createRegistry();
private final List<NamedValueReceiver> elseSources = MorphCollectionFactory.createList();

private final MultiMap multiMap = MorphCollectionFactory.createMultiMap();
private final Map<String, Map<String, String>> maps = new HashMap<>();
private final List<Closeable> resources = MorphCollectionFactory.createList();

private final StreamFlattener flattener = new StreamFlattener();
Expand Down Expand Up @@ -358,12 +359,16 @@ public void receive(final String name, final String value, final NamedValueSourc

@Override
public Map<String, String> getMap(final String mapName) {
return multiMap.getMap(mapName);
return maps.getOrDefault(mapName, Collections.emptyMap());
}

@Override
public String getValue(final String mapName, final String key) {
return multiMap.getValue(mapName, key);
final Map<String, String> map = getMap(mapName);
if (map.containsKey(key)) {
return map.get(key);
}
return map.get(Maps.DEFAULT_MAP_KEY);
}

@Override
Expand All @@ -372,17 +377,17 @@ public Map<String, String> putMap(final String mapName, final Map<String, String
final Closeable closable = (Closeable) map;
resources.add(closable);
}
return multiMap.putMap(mapName, map);
return maps.put(mapName, map);
}

@Override
public String putValue(final String mapName, final String key, final String value) {
return multiMap.putValue(mapName, key, value);
return maps.computeIfAbsent(mapName, k -> new HashMap<>()).put(key, value);
}

@Override
public Collection<String> getMapNames() {
return multiMap.getMapNames();
return Collections.unmodifiableSet(maps.keySet());
}

public void registerRecordEndFlush(final FlushListener flushListener) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/culturegraph/mf/morph/MorphBuilder.java
Expand Up @@ -23,12 +23,12 @@
import org.culturegraph.mf.morph.api.Collect;
import org.culturegraph.mf.morph.api.ConditionAware;
import org.culturegraph.mf.morph.api.FlushListener;
import org.culturegraph.mf.morph.api.Maps;
import org.culturegraph.mf.morph.api.MorphDefException;
import org.culturegraph.mf.morph.api.NamedValuePipe;
import org.culturegraph.mf.morph.collectors.Entity;
import org.culturegraph.mf.morph.api.Function;
import org.culturegraph.mf.morph.api.InterceptorFactory;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.util.reflection.ObjectFactory;
import org.culturegraph.mf.util.xml.Location;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -127,7 +127,7 @@ protected void handleInternalMap(final Node mapNode) {
}

if (mapDefault != null) {
metamorph.putValue(mapName, MultiMap.DEFAULT_MAP_KEY, mapDefault);
metamorph.putValue(mapName, Maps.DEFAULT_MAP_KEY, mapDefault);
}
}

Expand Down Expand Up @@ -361,7 +361,7 @@ protected void handleFunction(final Node functionNode) {

function.setSourceLocation((Location) functionNode.getUserData(Location.USER_DATA_ID));

function.setMultiMap(metamorph);
function.setMaps(metamorph);

// add key value entries...
for (Node mapEntryNode = functionNode.getFirstChild(); mapEntryNode != null; mapEntryNode = mapEntryNode
Expand Down
Expand Up @@ -21,8 +21,7 @@
import java.util.LinkedList;
import java.util.List;

import org.culturegraph.mf.types.MultiHashMap;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.morph.api.Maps;


/**
Expand All @@ -38,8 +37,8 @@ private MorphCollectionFactory() {
//no instances
}

public static MultiMap createMultiMap(){
return new MultiHashMap();
public static Maps createMultiMap(){
return new HashMaps();
}

public static <T> List<T> createList(){
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/culturegraph/mf/morph/api/Function.java
Expand Up @@ -16,17 +16,15 @@
package org.culturegraph.mf.morph.api;


import org.culturegraph.mf.types.MultiMap;

/**
* Interface for functions used in Metamorph.
*
* @author Markus Michael Geipel
*
*/
public interface Function extends NamedValuePipe, FlushListener {
public interface Function extends NamedValuePipe, FlushListener {

void putValue(String key, String value);
void setMultiMap(MultiMap multiMapProvider);
void setMaps(Maps maps);

}
@@ -1,5 +1,5 @@
/*
* Copyright 2013, 2014 Deutsche Nationalbibliothek
* Copyright 2016 Christoph Böhme
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
Expand All @@ -13,32 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.culturegraph.mf.types;
package org.culturegraph.mf.morph.api;

import java.util.Collection;
import java.util.Map;

/**
* Simple value store with following a Map&lt;String, Map&lt;String, String&gt;&gt; model. Just keeping the interface simpler.
* Provides access to the maps defined in a Metamorph script.
*
* @author Markus Michael Geipel
*
*/
public interface MultiMap {

public interface Maps {

String DEFAULT_MAP_KEY = "__default";

/**
*
* @param mapName
* @return map corresponding to mapName. Never return <code>null</code>. If there is no corresponding {@link Map}, return empty one.
*/
Collection<String> getMapNames();

Map<String, String> getMap(String mapName);

String getValue(String mapName, String key);

Map<String, String> putMap(String mapName, Map<String, String> map);

String putValue(String mapName, String key, String value);

}
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;

import org.culturegraph.mf.morph.api.Function;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.morph.api.Maps;

/**
* Base class for functions.
Expand All @@ -29,12 +29,12 @@
public abstract class AbstractFunction extends AbstractNamedValuePipe
implements Function {

private MultiMap multiMap;
private Maps maps;
private Map<String, String> localMap;
private String mapName;

protected final String getValue(final String mapName, final String key) {
return multiMap.getValue(mapName, key);
return maps.getValue(mapName, key);
}

protected final String getLocalValue(final String key) {
Expand All @@ -50,7 +50,7 @@ public final String getMapName() {

public final Map<String, String> getMap() {
if (localMap == null) {
return multiMap.getMap(mapName);
return maps.getMap(mapName);
}
return localMap;
}
Expand All @@ -67,9 +67,8 @@ public final void putValue(final String key, final String value) {
localMap.put(key, value);
}

@Override
public final void setMultiMap(final MultiMap multiMapProvider) {
this.multiMap = multiMapProvider;
public final void setMaps(final Maps maps) {
this.maps = maps;
}

@Override
Expand Down
85 changes: 0 additions & 85 deletions src/main/java/org/culturegraph/mf/types/MultiHashMap.java

This file was deleted.

Expand Up @@ -24,9 +24,9 @@
import java.util.Map;

import org.culturegraph.mf.framework.helpers.DefaultStreamReceiver;
import org.culturegraph.mf.morph.api.Maps;
import org.culturegraph.mf.morph.api.NamedValueReceiver;
import org.culturegraph.mf.morph.api.NamedValueSource;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.util.xml.Location;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void shouldReturnValueFromNestedMap() {
@Test
public void shouldReturnDefaultValueIfMapIsKnownButNameIsUnknown() {
final Map<String, String> map = new HashMap<>();
map.put(MultiMap.DEFAULT_MAP_KEY, "defaultValue");
map.put(Maps.DEFAULT_MAP_KEY, "defaultValue");

metamorph.putMap(MAP_NAME, map);

Expand Down

0 comments on commit 3b534e7

Please sign in to comment.