Skip to content

Commit

Permalink
Rename and move StreamConstants
Browse files Browse the repository at this point in the history
The class is renamed to `StandardEventNames` and moved to `framework`
package. As it should become part of the framework.  The unused
`SERIALIZED` constant is removed.
  • Loading branch information
cboehme committed Oct 25, 2016
1 parent 7bf435a commit f3c28fd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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,19 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.culturegraph.mf.util;
package org.culturegraph.mf.framework;

/**
* Constants for commonly used literal and entity names.
*
* @author Christoph Böhme
*/
public final class StreamConstants {
public final class StandardEventNames {

public static final String SERIALIZED = "_serialized";
/**
* Name of the literal holding the record id. The literal's value should
* either be the same as the record id in the <i>start-record</i> event or be
* intended to replace the current record id in a subsequent processing step.
*/
public static final String ID = "_id";
private StreamConstants() {
// No instances allowed

private StandardEventNames() {
throw new AssertionError("no instances allowed");
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/culturegraph/mf/morph/Metamorph.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.culturegraph.mf.stream.pipe.StreamFlattener;
import org.culturegraph.mf.types.MultiMap;
import org.culturegraph.mf.util.ResourceUtil;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;
import org.culturegraph.mf.util.xml.Location;
import org.xml.sax.InputSource;

Expand Down Expand Up @@ -220,7 +220,7 @@ public void startRecord(final String identifier) {
final String identifierFinal = identifier;

outputStreamReceiver.startRecord(identifierFinal);
dispatch(StreamConstants.ID, identifierFinal, null);
dispatch(StandardEventNames.ID, identifierFinal, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.culturegraph.mf.framework.DefaultObjectPipe;
import org.culturegraph.mf.framework.StreamReceiver;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;

/**
* Emits a {@link Map} as a record with a literal for each entry in the map.
Expand Down Expand Up @@ -57,12 +57,12 @@
public final class MapToStream extends
DefaultObjectPipe<Map<?, ?>, StreamReceiver> {

private Object idKey = StreamConstants.ID;
private Object idKey = StandardEventNames.ID;

/**
* Sets the key of the map entry that is used for the record id.
* <p>
* The default id key is &quot;{@value StreamConstants#ID}&quot;.
* The default id key is &quot;{@value StandardEventNames#ID}&quot;.
* <p>
* This parameter can be changed anytime during processing. The new value
* becomes effective with the next record being processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.culturegraph.mf.framework.annotations.Out;
import org.culturegraph.mf.types.Triple;
import org.culturegraph.mf.types.Triple.ObjectType;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;

/**
* Emits the literals which are received as triples such
Expand Down Expand Up @@ -163,7 +163,7 @@ public void literal(final String name, final String value) {
assert !isClosed();

if (nestingLevel > encodeLevel) {
if (nestingLevel == 1 && redirect && StreamConstants.ID.equals(name)) {
if (nestingLevel == 1 && redirect && StandardEventNames.ID.equals(name)) {
currentId = value;
} else {
formatter.literal(name, value);
Expand All @@ -186,7 +186,7 @@ private void endEncode() {

private void dispatch(final String name, final String value, final ObjectType type) {
if (redirect) {
if (StreamConstants.ID.equals(name)) {
if (StandardEventNames.ID.equals(name)) {
currentId = value;
} else {
final Matcher matcher = REDIRECT_PATTERN.matcher(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.culturegraph.mf.framework.annotations.In;
import org.culturegraph.mf.framework.annotations.Out;
import org.culturegraph.mf.stream.sink.EntityPathTracker;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;

/**
* Replaces the record id with the value of a literal from the record. The name
Expand Down Expand Up @@ -73,7 +73,7 @@ public final class IdChangePipe extends DefaultStreamPipe<StreamReceiver> {
private final StreamBuffer streamBuffer = new StreamBuffer();
private final EntityPathTracker entityPathTracker = new EntityPathTracker();

private String idLiteral = StreamConstants.ID;
private String idLiteral = StandardEventNames.ID;
private String currentIdentifier;
private String originalIdentifier;
private boolean keepRecordsWithoutIdLiteral = true;
Expand All @@ -88,7 +88,7 @@ public final class IdChangePipe extends DefaultStreamPipe<StreamReceiver> {
* literals named &ldquo;id&rdquo; which are part of an entity named
* &ldquo;metadata&rdquo;.
* <p>
* The default value is &ldquo;{@value StreamConstants#ID}&rdquo;.
* The default value is &ldquo;{@value StandardEventNames#ID}&rdquo;.
* <p>
* This parameter must only be changed between records otherwise the
* behaviour of the module is undefined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.types.Triple;
import org.culturegraph.mf.types.Triple.ObjectType;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void shouldRedirectIfAltIdGiven() {
streamToTriples.setRedirect(true);

streamToTriples.startRecord("id");
streamToTriples.literal(StreamConstants.ID, "altId");
streamToTriples.literal(StandardEventNames.ID, "altId");
streamToTriples.literal("literal", "value");
streamToTriples.endRecord();

Expand Down Expand Up @@ -121,7 +121,7 @@ public void shouldRedirectEvenIfRecordPredicateIsGiven() {
streamToTriples.setRedirect(true);

streamToTriples.startRecord("id");
streamToTriples.literal(StreamConstants.ID, "altId");
streamToTriples.literal(StandardEventNames.ID, "altId");
streamToTriples.literal("literal", "value");
streamToTriples.endRecord();

Expand Down
32 changes: 16 additions & 16 deletions src/test/java/org/culturegraph/mf/stream/pipe/IdChangePipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.culturegraph.mf.framework.StreamReceiver;
import org.culturegraph.mf.util.StreamConstants;
import org.culturegraph.mf.framework.StandardEventNames;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -65,11 +65,11 @@ public void cleanup() {
@Test
public void testShouldChangeIdsOfRecords() {
idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.endRecord();

idChangePipe.startRecord(OLD_RECORD_ID2);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID2);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID2);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
Expand All @@ -86,7 +86,7 @@ public void testShouldKeepRecordsWithoutIdLiteral() {
idChangePipe.literal(LITERAL_NAME, LITERAL_VALUE);
idChangePipe.endRecord();
idChangePipe.startRecord(OLD_RECORD_ID2);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID2);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID2);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
Expand All @@ -105,7 +105,7 @@ public void testShouldRemoveRecordsWithoutIdLiteral() {
idChangePipe.literal(LITERAL_NAME, LITERAL_VALUE);
idChangePipe.endRecord();
idChangePipe.startRecord(OLD_RECORD_ID2);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID2);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID2);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
Expand All @@ -118,25 +118,25 @@ public void testShouldRemoveRecordsWithoutIdLiteral() {
public void testShouldNotUseNestedIdLiteralAsNewId() {
idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.startEntity(ENTITY);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.endEntity();
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord(OLD_RECORD_ID1);
ordered.verify(receiver).startEntity(ENTITY);
ordered.verify(receiver).literal(StreamConstants.ID, NEW_RECORD_ID1);
ordered.verify(receiver).literal(StandardEventNames.ID, NEW_RECORD_ID1);
ordered.verify(receiver).endEntity();
ordered.verify(receiver).endRecord();
}

@Test
public void testShouldAcceptFullPathAsNewId() {
idChangePipe.setIdLiteral(ENTITY + "." + StreamConstants.ID);
idChangePipe.setIdLiteral(ENTITY + "." + StandardEventNames.ID);

idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.startEntity(ENTITY);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.endEntity();
idChangePipe.endRecord();

Expand All @@ -149,10 +149,10 @@ public void testShouldAcceptFullPathAsNewId() {

@Test
public void testShouldNotKeepIdLiteralByDefault() {
idChangePipe.setIdLiteral(StreamConstants.ID);
idChangePipe.setIdLiteral(StandardEventNames.ID);

idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
Expand All @@ -163,24 +163,24 @@ public void testShouldNotKeepIdLiteralByDefault() {

@Test
public void testShouldKeepIdLiteralIfConfigured() {
idChangePipe.setIdLiteral(StreamConstants.ID);
idChangePipe.setIdLiteral(StandardEventNames.ID);
idChangePipe.setKeepIdLiteral(true);

idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord(NEW_RECORD_ID1);
ordered.verify(receiver).literal(StreamConstants.ID, NEW_RECORD_ID1);
ordered.verify(receiver).literal(StandardEventNames.ID, NEW_RECORD_ID1);
ordered.verify(receiver).endRecord();
}

@Test
public void testShouldUseLastIdLiteralAsNewId() {
idChangePipe.startRecord(OLD_RECORD_ID1);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID1);
idChangePipe.literal(StreamConstants.ID, NEW_RECORD_ID2);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID1);
idChangePipe.literal(StandardEventNames.ID, NEW_RECORD_ID2);
idChangePipe.endRecord();

final InOrder ordered = inOrder(receiver);
Expand Down

0 comments on commit f3c28fd

Please sign in to comment.